Skip to content

Commit 8850ba7

Browse files
authored
1 parent ddfd08a commit 8850ba7

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

internal/opts/options.go

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Options struct {
4343
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
4444
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
4545
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
46+
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`
47+
48+
InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
4649
}
4750

4851
type GlobalOptions struct {
@@ -111,6 +114,16 @@ func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
111114
*options.QueryParameterLimit = 1
112115
}
113116

117+
if options.Initialisms == nil {
118+
options.Initialisms = new([]string)
119+
*options.Initialisms = []string{"id"}
120+
}
121+
122+
options.InitialismsMap = map[string]struct{}{}
123+
for _, initial := range *options.Initialisms {
124+
options.InitialismsMap[initial] = struct{}{}
125+
}
126+
114127
return &options, nil
115128
}
116129

internal/result.go

+3-1
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/struct.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func StructName(name string, options *opts.Options) string {
3232
}, name)
3333

3434
for _, p := range strings.Split(name, "_") {
35-
if p == "id" {
36-
out += "ID"
35+
if _, found := options.InitialismsMap[p]; found {
36+
out += strings.ToUpper(p)
3737
} else {
3838
out += strings.Title(p)
3939
}

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

+3-3
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}}

0 commit comments

Comments
 (0)