Skip to content

dinosql/internal: Add lower and upper functions #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/dinosql/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 2 additions & 0 deletions internal/dinosql/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions internal/dinosql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
Expand Down
17 changes: 17 additions & 0 deletions internal/dinosql/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
`
Expand Down
30 changes: 30 additions & 0 deletions internal/pg/functions_string.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
},
}
}
5 changes: 1 addition & 4 deletions internal/pg/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -77,6 +73,7 @@ func pgCatalog() Schema {
},
}

fs = append(fs, stringFunctions()...)
fs = append(fs, advisoryLockFunctions()...)

s.Funcs = make(map[string][]Function, len(fs))
Expand Down