Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(codegen): Correct column names in :copyfrom
  • Loading branch information
kyleconroy committed Oct 12, 2023
commit 4786bbcbb171b7e294903e0700f1552716f9a4a3
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
2 changes: 2 additions & 0 deletions internal/compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/sqlc-dev/sqlc/internal/analyzer"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/engine/dolphin"
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
pganalyze "github.com/sqlc-dev/sqlc/internal/engine/postgresql/analyzer"
Expand Down Expand Up @@ -50,6 +51,7 @@ func NewCompiler(conf config.SQL, combo config.CombinedSettings) (*Compiler, err
c.parser = postgresql.NewParser()
c.catalog = postgresql.NewCatalog()
if conf.Database != nil {
debug.Dump(conf)
if conf.Analyzer.Database == nil || *conf.Analyzer.Database {
c.analyzer = pganalyze.New(c.client, *conf.Database)
}
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