Skip to content

fix(codegen): Correct column names in :copyfrom #2838

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
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
working-directory: internal/endtoend/testdata

- name: report
if: ${{ github.ref == 'refs/heads/main' }}
if: false
run: ./scripts/report.sh
env:
BUILDKITE_ANALYTICS_TOKEN: ${{ secrets.BUILDKITE_ANALYTICS_TOKEN }}
Expand All @@ -87,4 +87,4 @@ jobs:
with:
go-version: '1.21.3'
- run: go install golang.org/x/vuln/cmd/govulncheck@latest
- run: govulncheck ./...
- run: govulncheck ./...
5 changes: 4 additions & 1 deletion examples/batch/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"database": {
"managed": true
},
"analyzer": {
"database": false
},
"rules": [
"sqlc/db-prepare"
],
Expand All @@ -22,4 +25,4 @@
"emit_interface": true
}
]
}
}
6 changes: 5 additions & 1 deletion internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func (v QueryValue) ColumnNamesAsGoSlice() string {
}
escapedNames := make([]string, len(v.Struct.Fields))
for i, f := range v.Struct.Fields {
escapedNames[i] = fmt.Sprintf("%q", f.DBName)
if f.Column != nil && f.Column.OriginalName != "" {
escapedNames[i] = fmt.Sprintf("%q", f.Column.OriginalName)
} else {
escapedNames[i] = fmt.Sprintf("%q", f.DBName)
}
}
return "[]string{" + strings.Join(escapedNames, ", ") + "}"
}
Expand Down
3 changes: 2 additions & 1 deletion internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type v1PackageSettings struct {
Name string `json:"name" yaml:"name"`
Engine Engine `json:"engine,omitempty" yaml:"engine"`
Database *Database `json:"database,omitempty" yaml:"database"`
Analyzer Analyzer `json:"analyzer" yaml:"analyzer"`
Analyzer Analyzer `json:"analyzer" yaml:"analyzer"`
Path string `json:"path" yaml:"path"`
Schema Paths `json:"schema" yaml:"schema"`
Queries Paths `json:"queries" yaml:"queries"`
Expand Down Expand Up @@ -148,6 +148,7 @@ func (c *V1GenerateSettings) Translate() Config {
Schema: pkg.Schema,
Queries: pkg.Queries,
Rules: pkg.Rules,
Analyzer: pkg.Analyzer,
Gen: SQLGen{
Go: &SQLGo{
EmitInterface: pkg.EmitInterface,
Expand Down
4 changes: 2 additions & 2 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestExamples(t *testing.T) {
// t.Parallel()
t.Parallel()
ctx := context.Background()

examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
Expand All @@ -40,7 +40,7 @@ func TestExamples(t *testing.T) {
}
tc := replay.Name()
t.Run(tc, func(t *testing.T) {
// t.Parallel()
t.Parallel()
path := filepath.Join(examples, tc)
var stderr bytes.Buffer
opts := &cmd.Options{
Expand Down
1 change: 1 addition & 0 deletions internal/endtoend/testdata/copyfrom_named_params/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/2833

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- name: StageUserData :copyfrom
insert into "user_data" ("id", "user")
values (
sqlc.arg('id_param'),
sqlc.arg('user_param')
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table "user_data" (
"id" varchar not null,
"user" varchar not null,
primary key ("id")
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
5 changes: 4 additions & 1 deletion internal/sql/validate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func validateCopyfrom(n ast.Node) error {
return nil
}
for _, v := range sublist.Items {
if _, ok := v.(*ast.ParamRef); !ok {
_, ok := v.(*ast.ParamRef)
ok = ok || named.IsParamFunc(v)
ok = ok || named.IsParamSign(v)
if !ok {
return errors.New(":copyfrom doesn't support non-parameter values")
}
}
Expand Down