Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import: https://github.com/sqlc-dev/sqlc/commit/fe75daef25575b1e7bfc1fd4de60ce4d237901d6 #5

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading