Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Aug 5, 2024
1 parent ddfd08a commit 4261fcb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 13 additions & 0 deletions internal/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type Options struct {
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`

InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
}

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

if options.Initialisms == nil {
options.Initialisms = new([]string)
*options.Initialisms = []string{"id"}
}

options.InitialismsMap = map[string]struct{}{}
for _, initial := range *options.Initialisms {
options.InitialismsMap[initial] = struct{}{}
}

return &options, nil
}

Expand Down
4 changes: 3 additions & 1 deletion internal/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
4 changes: 2 additions & 2 deletions internal/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func StructName(name string, options *opts.Options) string {
}, name)

for _, p := range strings.Split(name, "_") {
if p == "id" {
out += "ID"
if _, found := options.InitialismsMap[p]; found {
out += strings.ToUpper(p)
} else {
out += strings.Title(p)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/templates/go-sql-driver-mysql/copyfromCopy.tmpl
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

0 comments on commit 4261fcb

Please sign in to comment.