Skip to content

Commit

Permalink
Fixes MySQL copyfrom generation with 1 arg stucts (#3446)
Browse files Browse the repository at this point in the history
* Updated MySQL copyfrom template to handle strucs with 1 argument

* Added copyfrom 1 arg struct testcase

* Fixed issue where copyfrom structs were not being emitted

* Added testcase to catch not emitting structs used with copyfrom
  • Loading branch information
terricain authored Jun 15, 2024
1 parent 7ca9f0c commit e623dc1
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
EmitPointer: options.EmitParamsStructPointers,
}

if len(query.Params) <= qpl {
// if query params is 2, and query params limit is 4 AND this is a copyfrom, we still want to emit the query's model
// otherwise we end up with a copyfrom using a struct without the struct definition
if len(query.Params) <= qpl && query.Cmd != ":copyfrom" {
gq.Arg.Emit = false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ func convertRowsFor{{.MethodName}}(w *io.PipeWriter, {{.Arg.SlicePair}}) {
{{- with $arg := .Arg }}
{{- range $arg.CopyFromMySQLFields}}
{{- if eq .Type "string"}}
e.AppendString({{if eq (len $arg.CopyFromMySQLFields) 1}}row{{else}}row.{{.Name}}{{end}})
e.AppendString({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
{{- else if or (eq .Type "[]byte") (eq .Type "json.RawMessage")}}
e.AppendBytes({{if eq (len $arg.CopyFromMySQLFields) 1}}row{{else}}row.{{.Name}}{{end}})
e.AppendBytes({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
{{- else}}
e.AppendValue({{if eq (len $arg.CopyFromMySQLFields) 1}}row{{else}}row.{{.Name}}{{end}})
e.AppendValue({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
{{- end}}
{{- end}}
{{- end}}
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: InsertMultipleValues :copyfrom
INSERT INTO foo (a, b) VALUES (?, ?);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE foo (a text, b text);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": "go",
"sql_package": "database/sql",
"sql_driver": "github.com/go-sql-driver/mysql",
"engine": "mysql",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"query_parameter_limit": 4
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/3443

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,2 @@
-- name: InsertSingleValue :copyfrom
INSERT INTO foo (a) VALUES (?);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE foo (a text);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": "go",
"sql_package": "database/sql",
"sql_driver": "github.com/go-sql-driver/mysql",
"engine": "mysql",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"query_parameter_limit": 0
}
]
}

0 comments on commit e623dc1

Please sign in to comment.