Skip to content

Commit 44d7340

Browse files
committed
fix(mysql): encode copyfrom enum values as strings
Generated non-null enums are named string types, which mysqltsv does not accept through AppendValue. Recognize generated enum names, including external model qualification, and emit AppendString with an explicit string conversion. Keep nullable driver.Valuer wrappers, overrides and ordinary types on their existing paths. Add CLI golden cases for scalar arguments, parameter-struct pointers and split models, with nullable, overridden and text controls. Verified the tagged CI suite with all required process plugins, six CGO-free build targets, and live MySQL round trips. REPLACE duplicate-key semantics remain separate from this encoding fix. Fixes #4324
1 parent 3c2546a commit 44d7340

17 files changed

Lines changed: 1598 additions & 0 deletions

File tree

internal/codegen/golang/gen.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
212212
return nil, errors.New(":batch* commands are only supported by pgx")
213213
}
214214

215+
var mysqlEnumTypes map[string]bool
216+
if tctx.SQLDriver == opts.SQLDriverGoSQLDriverMySQL {
217+
mysqlEnumTypes = make(map[string]bool, len(enums))
218+
for _, enum := range enums {
219+
mysqlEnumTypes[options.ModelsTypeQualifier()+enum.Name] = true
220+
}
221+
}
222+
215223
funcMap := template.FuncMap{
216224
"lowerTitle": sdk.LowerTitle,
217225
"comment": sdk.DoubleSlashComment,
@@ -222,6 +230,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
222230

223231
// These methods are Go specific, they do not belong in the codegen package
224232
// (as that is language independent)
233+
"isMySQLEnum": func(t string) bool { return mysqlEnumTypes[t] },
225234
"dbarg": tctx.codegenDbarg,
226235
"emitPreparedQueries": tctx.codegenEmitPreparedQueries,
227236
"queryMethod": tctx.codegenQueryMethod,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ func convertRowsFor{{.MethodName}}(w *io.PipeWriter, {{.Arg.SlicePair}}) {
1010
{{- range $arg.CopyFromMySQLFields}}
1111
{{- if eq .Type "string"}}
1212
e.AppendString({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
13+
{{- else if isMySQLEnum .Type}}
14+
e.AppendString(string({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}}))
1315
{{- else if or (eq .Type "[]byte") (eq .Type "json.RawMessage")}}
1416
e.AppendBytes({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
1517
{{- else}}

internal/endtoend/testdata/copyfrom_enum/mysql/db/copyfrom.go

Lines changed: 228 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_enum/mysql/db/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_enum/mysql/db/query.sql.go

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

0 commit comments

Comments
 (0)