Skip to content

Commit 6cb8def

Browse files
authored
dinosql/internal: Add lower and upper functions (#215)
1 parent 3a876eb commit 6cb8def

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

internal/dinosql/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func (r Result) goInnerType(col core.Column) string {
577577
}
578578
return "sql.NullTime"
579579

580-
case "text", "pg_catalog.varchar", "pg_catalog.bpchar":
580+
case "text", "pg_catalog.varchar", "pg_catalog.bpchar", "string":
581581
if notNull {
582582
return "string"
583583
}

internal/dinosql/gen_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func TestInnerType(t *testing.T) {
9696
"integer": "int32",
9797
"int": "int32",
9898
"pg_catalog.int4": "int32",
99+
"string": "string",
99100
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
100101
"date": "time.Time",
101102
"pg_catalog.time": "time.Time",
@@ -122,6 +123,7 @@ func TestNullInnerType(t *testing.T) {
122123
"integer": "sql.NullInt32",
123124
"int": "sql.NullInt32",
124125
"pg_catalog.int4": "sql.NullInt32",
126+
"string": "sql.NullString",
125127
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
126128
"date": "sql.NullTime",
127129
"pg_catalog.time": "sql.NullTime",

internal/dinosql/parser.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,15 +1246,21 @@ func resolveCatalogRefs(c core.Catalog, rvs []nodes.RangeVar, args []paramRef) (
12461246
DataType: "any",
12471247
},
12481248
})
1249+
continue
12491250
}
12501251
if i >= len(fun.Arguments) {
12511252
return nil, fmt.Errorf("incorrect number of arguments to %s", fun.Name)
12521253
}
1254+
arg := fun.Arguments[i]
1255+
name := arg.Name
1256+
if name == "" {
1257+
name = fun.Name
1258+
}
12531259
a = append(a, Parameter{
12541260
Number: ref.ref.Number,
12551261
Column: core.Column{
1256-
Name: fun.Arguments[i].Name,
1257-
DataType: fun.Arguments[i].DataType,
1262+
Name: name,
1263+
DataType: arg.DataType,
12581264
NotNull: true,
12591265
},
12601266
})

internal/dinosql/query_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,23 @@ func TestQueries(t *testing.T) {
674674
},
675675
},
676676
},
677+
{
678+
"lower-switched-order",
679+
`
680+
CREATE TABLE foo (bar text not null, bat text not null);
681+
SELECT bar FROM foo WHERE bar = $1 AND bat = LOWER($2);
682+
`,
683+
Query{
684+
Columns: []core.Column{
685+
{Table: public("foo"), Name: "bar", DataType: "text", NotNull: true},
686+
},
687+
Params: []Parameter{
688+
{1, core.Column{Table: public("foo"), Name: "bar", DataType: "text", NotNull: true}},
689+
{2, core.Column{Name: "lower", DataType: "string", NotNull: true}},
690+
},
691+
},
692+
},
693+
677694
{
678695
"identical-tables",
679696
`

internal/pg/functions_string.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pg
2+
3+
// String Functions and Operators
4+
//
5+
// https://www.postgresql.org/docs/current/functions-string.html
6+
//
7+
// Table 9.9. SQL String Functions and Operators
8+
func stringFunctions() []Function {
9+
return []Function{
10+
argN("position", 2),
11+
{
12+
Name: "lower",
13+
ReturnType: "text",
14+
Arguments: []Argument{
15+
{
16+
DataType: "string",
17+
},
18+
},
19+
},
20+
{
21+
Name: "upper",
22+
ReturnType: "text",
23+
Arguments: []Argument{
24+
{
25+
DataType: "string",
26+
},
27+
},
28+
},
29+
}
30+
}

internal/pg/pg_catalog.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ func pgCatalog() Schema {
4444
// https://www.postgresql.org/docs/current/functions-math.html#FUNCTIONS-MATH-RANDOM-TABLE
4545
argN("random", 0),
4646

47-
// Table 9.8. SQL String Functions and Operators
48-
// https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-SQL
49-
argN("position", 2),
50-
5147
// Table 9.52. General-Purpose Aggregate Functions
5248
// https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
5349
{
@@ -77,6 +73,7 @@ func pgCatalog() Schema {
7773
},
7874
}
7975

76+
fs = append(fs, stringFunctions()...)
8077
fs = append(fs, advisoryLockFunctions()...)
8178

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

0 commit comments

Comments
 (0)