Documentation ¶
Overview ¶
Package ql contains functions for adapting generic JSON database queries to different backend databases
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CQL ¶
CQL returns a CQL query and arguments.
Example ¶
package main import ( "fmt" "github.com/jmank88/ql" ) func main() { q := &ql.Query{ Fields: []string{"a", "b", "c", "d"}, Target: "target", Filter: ql.And( ql.Compare("a", "=", "test"), ql.Or( ql.Compare("b", "<", 2), ql.Compare("c", "!=", 5), ), ql.Compare("d", ">", 0), ), Sort: []ql.Sort{ { Field: "a", Descending: true, }, }, Max: newInt(20), } query, args := ql.MYSQL(q) // session.Query(query, args...) fmt.Println(query) fmt.Println(args) } func newInt(i int) *int { return &i }
Output: SELECT a, b, c, d FROM target WHERE (a = ? AND (b < ? OR c != ?) AND d > ?) ORDER BY a DESC LIMIT 20 [test 2 5 0]
func MYSQL ¶
MYSQL returns a MYSQL compatible SQL query and arguments.
Example ¶
package main import ( "fmt" "github.com/jmank88/ql" ) func main() { q := &ql.Query{ Fields: []string{"a", "b", "c", "d"}, Target: "target", Filter: ql.And( ql.Compare("a", "=", "test"), ql.Or( ql.Compare("b", "<", 2), ql.Compare("c", "!=", 5), ), ql.Compare("d", ">", 0), ), Sort: []ql.Sort{ { Field: "a", Descending: true, }, }, Max: newInt(100), } query, args := ql.MYSQL(q) // db.Query(query, args...) fmt.Println(query) fmt.Println(args) } func newInt(i int) *int { return &i }
Output: SELECT a, b, c, d FROM target WHERE (a = ? AND (b < ? OR c != ?) AND d > ?) ORDER BY a DESC LIMIT 100 [test 2 5 0]
func POSTGRESQL ¶
POSTGRESQL returns a POSTGRESQL compatible SQL query and arguments.
Example ¶
package main import ( "fmt" "github.com/jmank88/ql" ) func main() { q := &ql.Query{ Fields: []string{"a", "b", "c", "d"}, Target: "target", Filter: ql.And( ql.Compare("a", "=", "test"), ql.Or( ql.Compare("b", "<", 2), ql.Compare("c", "!=", 5), ), ql.Compare("d", ">", 0), ), Sort: []ql.Sort{ { Field: "a", Descending: true, }, }, Max: newInt(100), } query, args := ql.POSTGRESQL(q) // db.Query(query, args...) fmt.Println(query) fmt.Println(args) } func newInt(i int) *int { return &i }
Output: SELECT a, b, c, d FROM target WHERE (a = $1 AND (b < $2 OR c != $3) AND d > $4) ORDER BY a DESC LIMIT 100 [test 2 5 0]
Types ¶
type Comparison ¶
type Comparison struct { // One of: =, !=, <, >, <=, >=. Comparator string `json:"comparator,omitempty"` Field string `json:"field,omitempty"` Value interface{} `json:"value,omitempty"` }
A Comparison filter compares a field to a value.
type Composite ¶
type Composite struct { // One of (case-insensitive): AND, OR. Operator string `json:"operator,omitempty"` Filters []*Filter `json:"filters,omitempty"` }
An Composite filter logically combines two or more Filters.
type Filter ¶
type Filter struct { Comparison Composite }
Filter is a union of filter types. Only one embedded struct should have set fields.
type Query ¶
type Query struct { // Target of the query. Target string `json:"target"` // Optional names of fields to fetch. Fields []string `json:"fields,omitempty"` // Optional filter. Filter *Filter `json:"filter,omitempty"` // Optional sort order. Sort []Sort `json:"sort,omitempty"` // Optional maximum records to fetch. Max *int `json:"max,omitempty"` }
Query is a generic, JSON-serializable query.
Example ¶
package main import ( "encoding/json" "os" "github.com/jmank88/ql" ) func main() { e := json.NewEncoder(os.Stdout) e.SetIndent("", "\t") _ = e.Encode(&ql.Query{ Fields: []string{"a", "b", "c", "d"}, Target: "target", Filter: ql.And( ql.Compare("a", "=", "test"), ql.Or( ql.Compare("b", "<", json.Number("2")), ql.Compare("c", "!=", json.Number("5")), ), ql.Compare("d", ">", json.Number("0")), ), Sort: []ql.Sort{ { Field: "a", Descending: true, }, }, }) }
Output: { "target": "target", "fields": [ "a", "b", "c", "d" ], "filter": { "operator": "AND", "filters": [ { "comparator": "=", "field": "a", "value": "test" }, { "operator": "OR", "filters": [ { "comparator": "\u003c", "field": "b", "value": 2 }, { "comparator": "!=", "field": "c", "value": 5 } ] }, { "comparator": "\u003e", "field": "d", "value": 0 } ] }, "sort": [ { "field": "a", "descending": true } ] }
Click to show internal directories.
Click to hide internal directories.