Skip to content

Commit e623dc1

Browse files
authored
Fixes MySQL copyfrom generation with 1 arg stucts (#3446)
* 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
1 parent 7ca9f0c commit e623dc1

File tree

17 files changed

+270
-4
lines changed

17 files changed

+270
-4
lines changed

internal/codegen/golang/result.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
259259
EmitPointer: options.EmitParamsStructPointers,
260260
}
261261

262-
if len(query.Params) <= qpl {
262+
// 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
263+
// otherwise we end up with a copyfrom using a struct without the struct definition
264+
if len(query.Params) <= qpl && query.Cmd != ":copyfrom" {
263265
gq.Arg.Emit = false
264266
}
265267
}

internal/codegen/golang/templates/go-sql-driver-mysql/copyfromCopy.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ func convertRowsFor{{.MethodName}}(w *io.PipeWriter, {{.Arg.SlicePair}}) {
99
{{- with $arg := .Arg }}
1010
{{- range $arg.CopyFromMySQLFields}}
1111
{{- if eq .Type "string"}}
12-
e.AppendString({{if eq (len $arg.CopyFromMySQLFields) 1}}row{{else}}row.{{.Name}}{{end}})
12+
e.AppendString({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
1313
{{- else if or (eq .Type "[]byte") (eq .Type "json.RawMessage")}}
14-
e.AppendBytes({{if eq (len $arg.CopyFromMySQLFields) 1}}row{{else}}row.{{.Name}}{{end}})
14+
e.AppendBytes({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
1515
{{- else}}
16-
e.AppendValue({{if eq (len $arg.CopyFromMySQLFields) 1}}row{{else}}row.{{.Name}}{{end}})
16+
e.AppendValue({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
1717
{{- end}}
1818
{{- end}}
1919
{{- end}}

internal/endtoend/testdata/copyfrom_multicolumn_parameter_limit/mysql/go/copyfrom.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_multicolumn_parameter_limit/mysql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_multicolumn_parameter_limit/mysql/go/models.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_multicolumn_parameter_limit/mysql/go/query.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: InsertMultipleValues :copyfrom
2+
INSERT INTO foo (a, b) VALUES (?, ?);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE foo (a text, b text);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"sql_package": "database/sql",
7+
"sql_driver": "github.com/go-sql-driver/mysql",
8+
"engine": "mysql",
9+
"name": "querytest",
10+
"schema": "schema.sql",
11+
"queries": "query.sql",
12+
"query_parameter_limit": 4
13+
}
14+
]
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/3443

internal/endtoend/testdata/copyfrom_singlecolumn_struct_only/mysql/go/copyfrom.go

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_singlecolumn_struct_only/mysql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_singlecolumn_struct_only/mysql/go/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_singlecolumn_struct_only/mysql/go/query.sql.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: InsertSingleValue :copyfrom
2+
INSERT INTO foo (a) VALUES (?);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE foo (a text);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"sql_package": "database/sql",
7+
"sql_driver": "github.com/go-sql-driver/mysql",
8+
"engine": "mysql",
9+
"name": "querytest",
10+
"schema": "schema.sql",
11+
"queries": "query.sql",
12+
"query_parameter_limit": 0
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)