Add support for GraphQL variables

This commit is contained in:
Vikram Rangnekar
2019-04-19 01:55:03 -04:00
parent 0755ecf6bd
commit 652b31ce38
10 changed files with 223 additions and 152 deletions

View File

@ -539,8 +539,7 @@ func (v *selectBlock) renderWhere(w io.Writer) error {
if len(v.ti.PrimaryCol) == 0 {
return fmt.Errorf("no primary key column defined for %s", v.sel.Table)
}
fmt.Fprintf(w, `(("%s") = ('%s'))`, v.ti.PrimaryCol, val.Val)
valExists = false
fmt.Fprintf(w, `(("%s") =`, v.ti.PrimaryCol)
case qcode.OpTsQuery:
if len(v.ti.TSVCol) == 0 {
return fmt.Errorf("no tsv column defined for %s", v.sel.Table)
@ -632,7 +631,11 @@ func renderVal(w io.Writer, ex *qcode.Exp, vars map[string]string) {
io.WriteString(w, ` (`)
switch ex.Type {
case qcode.ValBool, qcode.ValInt, qcode.ValFloat:
io.WriteString(w, ex.Val)
if len(ex.Val) != 0 {
fmt.Fprintf(w, `%s`, ex.Val)
} else {
io.WriteString(w, `''`)
}
case qcode.ValStr:
fmt.Fprintf(w, `'%s'`, ex.Val)
case qcode.ValVar:

View File

@ -353,7 +353,7 @@ func fetchByID(t *testing.T) {
}
}`
sql := `SELECT json_object_agg('product', products) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "products" FROM (SELECT "products"."id", "products"."name" FROM "products" WHERE ((("products"."price") > (0)) AND (("products"."price") < (8)) AND (("id") = ('15'))) LIMIT ('1') :: integer) AS "products_0" LIMIT ('1') :: integer) AS "done_1337";`
sql := `SELECT json_object_agg('product', products) FROM (SELECT row_to_json((SELECT "sel_0" FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name") AS "sel_0")) AS "products" FROM (SELECT "products"."id", "products"."name" FROM "products" WHERE ((("products"."price") > (0)) AND (("products"."price") < (8)) AND (("id") = (15))) LIMIT ('1') :: integer) AS "products_0" LIMIT ('1') :: integer) AS "done_1337";`
resSQL, err := compileGQLToPSQL(gql)
if err != nil {