From 54e6a4d78aff5e8f3c6f019d4a8a41a38e17c3a5 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 25 Dec 2019 09:07:57 -0800 Subject: [PATCH] dinosql/internal: Add lower and upper functions --- internal/dinosql/gen.go | 2 +- internal/dinosql/gen_test.go | 2 ++ internal/dinosql/parser.go | 10 ++++++++-- internal/dinosql/query_test.go | 17 +++++++++++++++++ internal/pg/functions_string.go | 30 ++++++++++++++++++++++++++++++ internal/pg/pg_catalog.go | 5 +---- 6 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 internal/pg/functions_string.go diff --git a/internal/dinosql/gen.go b/internal/dinosql/gen.go index 30ebc908fc..9da593b974 100644 --- a/internal/dinosql/gen.go +++ b/internal/dinosql/gen.go @@ -577,7 +577,7 @@ func (r Result) goInnerType(col core.Column) string { } return "sql.NullTime" - case "text", "pg_catalog.varchar", "pg_catalog.bpchar": + case "text", "pg_catalog.varchar", "pg_catalog.bpchar", "string": if notNull { return "string" } diff --git a/internal/dinosql/gen_test.go b/internal/dinosql/gen_test.go index 4cfa0481a0..3920ee8441 100644 --- a/internal/dinosql/gen_test.go +++ b/internal/dinosql/gen_test.go @@ -96,6 +96,7 @@ func TestInnerType(t *testing.T) { "integer": "int32", "int": "int32", "pg_catalog.int4": "int32", + "string": "string", // Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html "date": "time.Time", "pg_catalog.time": "time.Time", @@ -122,6 +123,7 @@ func TestNullInnerType(t *testing.T) { "integer": "sql.NullInt32", "int": "sql.NullInt32", "pg_catalog.int4": "sql.NullInt32", + "string": "sql.NullString", // Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html "date": "sql.NullTime", "pg_catalog.time": "sql.NullTime", diff --git a/internal/dinosql/parser.go b/internal/dinosql/parser.go index 1b979de45c..55535efbc6 100644 --- a/internal/dinosql/parser.go +++ b/internal/dinosql/parser.go @@ -1246,15 +1246,21 @@ func resolveCatalogRefs(c core.Catalog, rvs []nodes.RangeVar, args []paramRef) ( DataType: "any", }, }) + continue } if i >= len(fun.Arguments) { return nil, fmt.Errorf("incorrect number of arguments to %s", fun.Name) } + arg := fun.Arguments[i] + name := arg.Name + if name == "" { + name = fun.Name + } a = append(a, Parameter{ Number: ref.ref.Number, Column: core.Column{ - Name: fun.Arguments[i].Name, - DataType: fun.Arguments[i].DataType, + Name: name, + DataType: arg.DataType, NotNull: true, }, }) diff --git a/internal/dinosql/query_test.go b/internal/dinosql/query_test.go index 9a3aa8fea6..ad2c23c537 100644 --- a/internal/dinosql/query_test.go +++ b/internal/dinosql/query_test.go @@ -674,6 +674,23 @@ func TestQueries(t *testing.T) { }, }, }, + { + "lower-switched-order", + ` + CREATE TABLE foo (bar text not null, bat text not null); + SELECT bar FROM foo WHERE bar = $1 AND bat = LOWER($2); + `, + Query{ + Columns: []core.Column{ + {Table: public("foo"), Name: "bar", DataType: "text", NotNull: true}, + }, + Params: []Parameter{ + {1, core.Column{Table: public("foo"), Name: "bar", DataType: "text", NotNull: true}}, + {2, core.Column{Name: "lower", DataType: "string", NotNull: true}}, + }, + }, + }, + { "identical-tables", ` diff --git a/internal/pg/functions_string.go b/internal/pg/functions_string.go new file mode 100644 index 0000000000..ccf1a98e1b --- /dev/null +++ b/internal/pg/functions_string.go @@ -0,0 +1,30 @@ +package pg + +// String Functions and Operators +// +// https://www.postgresql.org/docs/current/functions-string.html +// +// Table 9.9. SQL String Functions and Operators +func stringFunctions() []Function { + return []Function{ + argN("position", 2), + { + Name: "lower", + ReturnType: "text", + Arguments: []Argument{ + { + DataType: "string", + }, + }, + }, + { + Name: "upper", + ReturnType: "text", + Arguments: []Argument{ + { + DataType: "string", + }, + }, + }, + } +} diff --git a/internal/pg/pg_catalog.go b/internal/pg/pg_catalog.go index 5c36f3e587..752458c205 100644 --- a/internal/pg/pg_catalog.go +++ b/internal/pg/pg_catalog.go @@ -44,10 +44,6 @@ func pgCatalog() Schema { // https://www.postgresql.org/docs/current/functions-math.html#FUNCTIONS-MATH-RANDOM-TABLE argN("random", 0), - // Table 9.8. SQL String Functions and Operators - // https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-SQL - argN("position", 2), - // Table 9.52. General-Purpose Aggregate Functions // https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE { @@ -77,6 +73,7 @@ func pgCatalog() Schema { }, } + fs = append(fs, stringFunctions()...) fs = append(fs, advisoryLockFunctions()...) s.Funcs = make(map[string][]Function, len(fs))