package main
import (
"fmt"
"log"
"github.com/marianogappa/sqlparser"
)
func main() {
query, err := sqlparser.Parse("SELECT a, b, c FROM 'd' WHERE e = '1' AND f > '2'")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+#v", query)
}
query, err := sqlparser.Parse(`SELECT a FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a]
}
query, err := sqlparser.Parse(`select a fRoM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a = ''`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Eq,
Operand2: ,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a < '1'`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Lt,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a <= '1'`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Lte,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a > '1'`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Gt,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a >= '1'`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Gte,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a != '1'`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Ne,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a != b`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Ne,
Operand2: b,
Operand2IsField: true,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`SELECT * FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [*]
}
query, err := sqlparser.Parse(`SELECT a, * FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a *]
}
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a != '1' AND b = '2'`)
query.Query {
Type: Select
TableName: b
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Ne,
Operand2: 1,
Operand2IsField: false,
}
{
Operand1: b,
Operand1IsField: true,
Operator: Eq,
Operand2: 2,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: [a c d]
}
query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello' WHERE a = '1'`)
query.Query {
Type: Update
TableName: a
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Eq,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[b:hello]
Inserts: []
Fields: []
}
query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello\'world' WHERE a = '1'`)
query.Query {
Type: Update
TableName: a
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Eq,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[b:hello\'world]
Inserts: []
Fields: []
}
query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1'`)
query.Query {
Type: Update
TableName: a
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Eq,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[b:hello c:bye]
Inserts: []
Fields: []
}
query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1' AND b = '789'`)
query.Query {
Type: Update
TableName: a
Conditions: [
{
Operand1: a,
Operand1IsField: true,
Operator: Eq,
Operand2: 1,
Operand2IsField: false,
}
{
Operand1: b,
Operand1IsField: true,
Operator: Eq,
Operand2: 789,
Operand2IsField: false,
}]
Updates: map[b:hello c:bye]
Inserts: []
Fields: []
}
query, err := sqlparser.Parse(`DELETE FROM 'a' WHERE b = '1'`)
query.Query {
Type: Delete
TableName: a
Conditions: [
{
Operand1: b,
Operand1IsField: true,
Operator: Eq,
Operand2: 1,
Operand2IsField: false,
}]
Updates: map[]
Inserts: []
Fields: []
}
query, err := sqlparser.Parse(`INSERT INTO 'a' (b) VALUES ('1')`)
query.Query {
Type: Insert
TableName: a
Conditions: []
Updates: map[]
Inserts: [[1]]
Fields: [b]
}
query, err := sqlparser.Parse(`INSERT INTO 'a' (b,c, d) VALUES ('1','2' , '3' )`)
query.Query {
Type: Insert
TableName: a
Conditions: []
Updates: map[]
Inserts: [[1 2 3]]
Fields: [b c d]
}
query, err := sqlparser.Parse(`INSERT INTO 'a' (b,c, d) VALUES ('1','2' , '3' ),('4','5' ,'6' )`)
query.Query {
Type: Insert
TableName: a
Conditions: []
Updates: map[]
Inserts: [[1 2 3] [4 5 6]]
Fields: [b c d]
}
query, err := sqlparser.Parse(``)
query type cannot be empty
query, err := sqlparser.Parse(`SELECT`)
table name cannot be empty
query, err := sqlparser.Parse(`SELECT FROM 'a'`)
at SELECT: expected field to SELECT
query, err := sqlparser.Parse(`SELECT b, FROM 'a'`)
at SELECT: expected field to SELECT
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE`)
at WHERE: empty WHERE clause
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a`)
at WHERE: condition without operator
query, err := sqlparser.Parse(`UPDATE`)
table name cannot be empty
query, err := sqlparser.Parse(`UPDATE 'a'`)
at WHERE: WHERE clause is mandatory for UPDATE & DELETE
query, err := sqlparser.Parse(`UPDATE 'a' SET`)
at WHERE: WHERE clause is mandatory for UPDATE & DELETE
query, err := sqlparser.Parse(`UPDATE 'a' SET b WHERE`)
at UPDATE: expected '='
query, err := sqlparser.Parse(`UPDATE 'a' SET b = WHERE`)
at UPDATE: expected quoted value
query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello' WHERE`)
at WHERE: empty WHERE clause
query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello' WHERE a`)
at WHERE: condition without operator
query, err := sqlparser.Parse(`DELETE FROM`)
table name cannot be empty
query, err := sqlparser.Parse(`DELETE FROM 'a'`)
at WHERE: WHERE clause is mandatory for UPDATE & DELETE
query, err := sqlparser.Parse(`DELETE FROM 'a' WHERE`)
at WHERE: empty WHERE clause
query, err := sqlparser.Parse(`DELETE FROM 'a' WHERE b`)
at WHERE: condition without operator
query, err := sqlparser.Parse(`INSERT INTO`)
table name cannot be empty
query, err := sqlparser.Parse(`INSERT INTO 'a'`)
at INSERT INTO: need at least one row to insert
query, err := sqlparser.Parse(`INSERT INTO 'a' (`)
at INSERT INTO: need at least one row to insert
query, err := sqlparser.Parse(`INSERT INTO 'a' (b`)
at INSERT INTO: need at least one row to insert
query, err := sqlparser.Parse(`INSERT INTO 'a' (b)`)
at INSERT INTO: need at least one row to insert
query, err := sqlparser.Parse(`INSERT INTO 'a' (b) VALUES`)
at INSERT INTO: need at least one row to insert
query, err := sqlparser.Parse(`INSERT INTO 'a' (b) VALUES (`)
at INSERT INTO: value count doesn't match field count
query, err := sqlparser.Parse(`INSERT INTO 'a' (*) VALUES ('1')`)
at INSERT INTO: expected at least one field to insert