Skip to content

Commit a71c584

Browse files
authored
engine/postgres: Upgrade to pg_query_go/v4 (#2114)
* Add support for pg.Boolean * Return ast.Null * Fix cast stmt bool * Add support for FunctionParameterMode_FUNC_PARAM_DEFAULT
1 parent 27ffced commit a71c584

File tree

24 files changed

+92
-89
lines changed

24 files changed

+92
-89
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/jinzhu/inflection v1.0.0
1515
github.com/lib/pq v1.10.7
1616
github.com/mattn/go-sqlite3 v1.14.16
17-
github.com/pganalyze/pg_query_go/v2 v2.2.0
17+
github.com/pganalyze/pg_query_go/v4 v4.2.0
1818
github.com/spf13/cobra v1.6.1
1919
github.com/spf13/pflag v1.0.5
2020
golang.org/x/sync v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
118118
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
119119
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
120120
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
121-
github.com/pganalyze/pg_query_go/v2 v2.2.0 h1:OW+reH+ZY7jdEuPyuLGlf1m7dLbE+fDudKXhLs0Ttpk=
122-
github.com/pganalyze/pg_query_go/v2 v2.2.0/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=
121+
github.com/pganalyze/pg_query_go/v4 v4.2.0 h1:67hSBZXYfABNYisEu/Xfu6R2gupnQwaoRhQicy0HSnQ=
122+
github.com/pganalyze/pg_query_go/v4 v4.2.0/go.mod h1:aEkDNOXNM5j0YGzaAapwJ7LB3dLNj+bvbWcLv1hOVqA=
123123
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
124124
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 h1:+FZIDR/D97YOPik4N4lPDaUcLDF/EQPogxtlHB2ZZRM=
125125
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=

internal/compiler/compat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type Relation struct {
3333
func parseRelation(node ast.Node) (*Relation, error) {
3434
switch n := node.(type) {
3535

36+
case *ast.Boolean:
37+
return &Relation{
38+
Name: "bool",
39+
}, nil
40+
3641
case *ast.List:
3742
parts := stringSlice(n)
3843
switch len(parts) {

internal/compiler/output_columns.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
156156
col := toColumn(tc.TypeName)
157157
col.Name = name
158158
cols = append(cols, col)
159+
} else if aconst, ok := n.Defresult.(*ast.A_Const); ok {
160+
tn, err := ParseTypeName(aconst.Val)
161+
if err != nil {
162+
return nil, err
163+
}
164+
cols = append(cols, &Column{Name: name, DataType: dataType(tn), NotNull: true})
159165
} else {
160166
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
161167
}

internal/endtoend/testdata/emit_result_and_params_struct_pointers/postgresql/pgx/v4/go/query.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/emit_result_and_params_struct_pointers/postgresql/pgx/v4/query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ ON CONFLICT DO NOTHING
77
RETURNING *;
88

99
-- name: GetOne :one
10-
SELECT * FROM foo WHERE a = ? AND b = ? LIMIT 1;
10+
SELECT * FROM foo WHERE a = $1 AND b = $2 LIMIT 1;
1111

1212
-- name: GetAll :many
1313
SELECT * FROM foo;
1414

1515
-- name: GetAllAByB :many
16-
SELECT a FROM foo WHERE b = ?;
16+
SELECT a FROM foo WHERE b = $1;

internal/endtoend/testdata/emit_result_and_params_struct_pointers/postgresql/pgx/v5/go/query.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/emit_result_and_params_struct_pointers/postgresql/pgx/v5/query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ ON CONFLICT DO NOTHING
77
RETURNING *;
88

99
-- name: GetOne :one
10-
SELECT * FROM foo WHERE a = ? AND b = ? LIMIT 1;
10+
SELECT * FROM foo WHERE a = $1 AND b = $2 LIMIT 1;
1111

1212
-- name: GetAll :many
1313
SELECT * FROM foo;
1414

1515
-- name: GetAllAByB :many
16-
SELECT a FROM foo WHERE b = ?;
16+
SELECT a FROM foo WHERE b = $1;
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
SELECT random(1);
2-
SELECT position();
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# package querytest
22
query.sql:1:8: function random(unknown) does not exist
3-
query.sql:2:8: function position() does not exist
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
SELECT random(1);
2-
SELECT position();
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# package querytest
22
query.sql:1:8: function random(unknown) does not exist
3-
query.sql:2:8: function position() does not exist
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
SELECT random(1);
2-
SELECT position();
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# package querytest
22
query.sql:1:8: function random(unknown) does not exist
3-
query.sql:2:8: function position() does not exist

internal/endtoend/testdata/mix_param_types/postgresql/go/test.sql.go

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/mix_param_types/postgresql/test.sql

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,3 @@ SELECT count(1) FROM bar WHERE id = $1 AND name <> sqlc.arg(name);
1212

1313
-- name: CountThree :one
1414
SELECT count(1) FROM bar WHERE id > $2 AND phone <> sqlc.arg(phone) AND name <> $1;
15-
16-
-- name: CountFour :one
17-
SELECT count(1) FROM bar WHERE id > ? AND phone <> sqlc.arg(phone) AND name <> ?;

internal/endtoend/testdata/params_placeholder_in_left_expr/postgresql/go/query.sql.go

Lines changed: 4 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/params_placeholder_in_left_expr/postgresql/query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CREATE TABLE users (
44
);
55

66
-- name: FindByID :many
7-
SELECT * FROM users WHERE ? = id;
7+
SELECT * FROM users WHERE $1 = id;
88

99
-- name: FindByIDAndName :many
10-
SELECT * FROM users WHERE ? = id AND ? = name;
10+
SELECT * FROM users WHERE $1 = id AND $1 = name;

0 commit comments

Comments
 (0)