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]
Aliases: map[]
}
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]
Aliases: map[]
}
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]
Aliases: map[]
}
Example: SELECT with alias works
query, err := sqlparser.Parse(`SELECT a as z, b as y, c FROM 'b'`)
query.Query {
Type: Select
TableName: b
Conditions: []
Updates: map[]
Inserts: []
Fields: [a b c]
Aliases: map[a:z b:y]
}
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]
Aliases: map[]
}
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]
Aliases: map[]
}
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]
Aliases: map[]
}
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]
Aliases: map[]
}
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]
Aliases: map[]
}
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]
Aliases: map[]
}
Example: SELECT with WHERE with != works (comparing field against another field)
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]
Aliases: map[]
}