Skip to content

Commit fd52526

Browse files
committed
Fix more tests
1 parent 604722c commit fd52526

11 files changed

Lines changed: 54 additions & 16 deletions

File tree

internal/compiler/expand.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package compiler
22

33
import (
44
"fmt"
5+
"regexp"
56
"strings"
67

78
"github.com/sqlc-dev/sqlc/internal/config"
@@ -44,14 +45,25 @@ func (c *Compiler) expand(qc *QueryCatalog, raw *ast.RawStmt) ([]source.Edit, er
4445
return edits, nil
4546
}
4647

48+
var validPostgresIdent = regexp.MustCompile(`^[a-z_][a-z0-9_$]*$`)
49+
4750
func (c *Compiler) quoteIdent(ident string) string {
4851
if c.parser.IsReservedKeyword(ident) {
4952
return c.quote(ident)
5053
}
54+
// SQL identifiers and key words must begin with a letter (a-z, but also
55+
// letters with diacritical marks and non-Latin letters) or an underscore
56+
// (_). Subsequent characters in an identifier or key word can be letters,
57+
// underscores, digits (0-9), or dollar signs ($).
58+
//
59+
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
5160
if c.conf.Engine == config.EnginePostgreSQL {
5261
// camelCase means the column is also camelCase
5362
if strings.ToLower(ident) != ident {
54-
return "\"" + ident + "\""
63+
return c.quote(ident)
64+
}
65+
if !validPostgresIdent.MatchString(strings.ToLower(ident)) {
66+
return c.quote(ident)
5567
}
5668
}
5769
return ident
@@ -154,7 +166,11 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
154166
}
155167
var old []string
156168
for _, p := range parts {
157-
old = append(old, c.quoteIdent(p))
169+
if p == "*" {
170+
old = append(old, p)
171+
} else {
172+
old = append(old, c.quoteIdent(p))
173+
}
158174
}
159175

160176
var oldString string
@@ -189,5 +205,6 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
189205
New: strings.Join(cols, ", "),
190206
})
191207
}
208+
192209
return edits, nil
193210
}

internal/compiler/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
8787
// If the query string was edited, make sure the syntax is valid
8888
if expanded != rawSQL {
8989
if _, err := c.parser.Parse(strings.NewReader(expanded)); err != nil {
90+
fmt.Println(expanded)
9091
return nil, fmt.Errorf("edited query syntax is invalid: %w", err)
9192
}
9293
}

internal/endtoend/testdata/codegen_struct_field_names/stdlib/go/query.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/invalid_group_by_reference/postgresql/stderr.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
query.sql:4:10: column reference "invalid_reference" not found
33
---
44
# package querytest
5-
query.sql:4:11: column "invalid_reference" does not exist
5+
query.sql:4:22: column "invalid_reference" does not exist

internal/endtoend/testdata/invalid_params/pgx/v4/stderr.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
query.sql:1:1: could not determine data type of parameter $1
33
query.sql:5:1: could not determine data type of parameter $2
44
query.sql:8:8: column "foo" does not exist
5-
query.sql:11:1: could not determine data type of parameter $2
5+
query.sql:11:1: could not determine data type of parameter $2
6+
---
7+
# package querytest
8+
query.sql:2:8: column "foo" does not exist
9+
query.sql:5:8: column "foo" does not exist
10+
query.sql:8:8: column "foo" does not exist
11+
query.sql:11:47: column "named" does not exist

internal/endtoend/testdata/invalid_params/pgx/v5/stderr.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
query.sql:1:1: could not determine data type of parameter $1
33
query.sql:5:1: could not determine data type of parameter $2
44
query.sql:8:8: column "foo" does not exist
5-
query.sql:11:1: could not determine data type of parameter $2
5+
query.sql:11:1: could not determine data type of parameter $2
6+
---
7+
# package querytest
8+
query.sql:2:8: column "foo" does not exist
9+
query.sql:5:8: column "foo" does not exist
10+
query.sql:8:8: column "foo" does not exist
11+
query.sql:11:47: column "named" does not exist

internal/endtoend/testdata/invalid_params/stdlib/stderr.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
query.sql:1:1: could not determine data type of parameter $1
33
query.sql:5:1: could not determine data type of parameter $2
44
query.sql:8:8: column "foo" does not exist
5-
query.sql:11:1: could not determine data type of parameter $2
5+
query.sql:11:1: could not determine data type of parameter $2
6+
---
7+
# package querytest
8+
query.sql:2:8: column "foo" does not exist
9+
query.sql:5:8: column "foo" does not exist
10+
query.sql:8:8: column "foo" does not exist
11+
query.sql:11:47: column "named" does not exist

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
-- https://github.com/sqlc-dev/sqlc/issues/437
2-
CREATE TABLE authors (
3-
id BIGSERIAL PRIMARY KEY,
4-
name text NOT NULL,
5-
bio text
6-
);
7-
81
-- name: GetAuthor :one
92
SELECT *
103
FROM authors a
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- https://github.com/sqlc-dev/sqlc/issues/437
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text
6+
);

internal/endtoend/testdata/invalid_table_alias/postgresql/sqlc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"path": "go",
66
"engine": "postgresql",
77
"name": "querytest",
8-
"schema": "query.sql",
8+
"schema": "schema.sql",
99
"queries": "query.sql"
1010
}
1111
]

0 commit comments

Comments
 (0)