Skip to content

codegen: Correctly generate CopyFrom columns for single-column copyfroms #2185

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
merged 1 commit into from
Apr 3, 2023
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
3 changes: 2 additions & 1 deletion internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type QueryValue struct {
Emit bool
EmitPointer bool
Name string
DBName string // The name of the field in the database. Only set if Struct==nil.
Struct *Struct
Typ string
SQLDriver SQLDriver
Expand Down Expand Up @@ -116,7 +117,7 @@ func (v QueryValue) Params() string {

func (v QueryValue) ColumnNames() string {
if v.Struct == nil {
return fmt.Sprintf("[]string{%q}", v.Name)
return fmt.Sprintf("[]string{%q}", v.DBName)
}
escapedNames := make([]string, len(v.Struct.Fields))
for i, f := range v.Struct.Fields {
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
p := query.Params[0]
gq.Arg = QueryValue{
Name: paramName(p),
DBName: p.Column.GetName(),
Typ: goType(req, p.Column),
SQLDriver: sqlpkg,
}
Expand Down Expand Up @@ -198,6 +199,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
}
gq.Ret = QueryValue{
Name: name,
DBName: name,
Typ: goType(req, c),
SQLDriver: sqlpkg,
}
Expand Down

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.

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 @@
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY
);

-- name: CreateAuthors :copyfrom
INSERT INTO authors (author_id) VALUES ($1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"emit_interface": true
}
]
}

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.

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 @@
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY
);

-- name: CreateAuthors :copyfrom
INSERT INTO authors (author_id) VALUES ($1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v5",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"emit_interface": true
}
]
}