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)
}
Example: SELECT works
query, err := sqlparser.Parse(`SELECT a FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a]
}
Example: SELECT works with lowercase
query, err := sqlparser.Parse(`select a fRoM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a]
}
Example: SELECT many fields works
query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a c d]
}
Example: SELECT with WHERE with = works
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]
}
Example: SELECT with WHERE with < works
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]
}
Example: SELECT with WHERE with <= works
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]
}
Example: SELECT with WHERE with > works
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]
}
Example: SELECT with WHERE with >= works
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]
}
Example: SELECT with WHERE with != works
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]
}