Skip to content
Closed
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
5 changes: 5 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ The `gen` mapping supports the following keys:
- `emit_all_enum_values`:
- If true, emit a function per enum type
that returns all valid enum values.
- `emit_getters`:
- If true, structs for models and queries will have getters for each field. (Go only) Defaults to `false`.
- `json_tags_id_uppercase`:
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
- `json_tags_case_style`:
Expand Down Expand Up @@ -455,6 +457,7 @@ packages:
emit_pointers_for_null_types: false
emit_enum_valid_method: false
emit_all_enum_values: false
emit_getters: false
json_tags_case_style: "camel"
output_batch_file_name: "batch.go"
output_db_file_name: "db.go"
Expand Down Expand Up @@ -506,6 +509,8 @@ Each mapping in the `packages` collection has the following keys:
- `emit_all_enum_values`:
- If true, emit a function per enum type
that returns all valid enum values.
- `emit_getters`:
- If true, structs for models and queries will have getters for each field. (Go only) Defaults to `false`.
- `json_tags_case_style`:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
- `output_batch_file_name`:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
EmitPointersForNullTypes: s.EmitPointersForNullTypes,
EmitEnumValidMethod: s.EmitEnumValidMethod,
EmitAllEnumValues: s.EmitAllEnumValues,
EmitGetters: s.EmitGetters,
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
Package: s.Package,
Out: s.Out,
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type tmplCtx struct {
EmitAllEnumValues bool
UsesCopyFrom bool
UsesBatch bool
EmitGetters bool
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -135,6 +136,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
EmitEnumValidMethod: golang.EmitEnumValidMethod,
EmitAllEnumValues: golang.EmitAllEnumValues,
EmitGetters: golang.EmitGetters,
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(golang.SqlPackage),
Expand Down
5 changes: 5 additions & 0 deletions internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type QueryValue struct {
Emit bool
EmitGetters bool
EmitPointer bool
Name string
DBName string // The name of the field in the database. Only set if Struct==nil.
Expand All @@ -26,6 +27,10 @@ func (v QueryValue) EmitStruct() bool {
return v.Emit
}

func (v QueryValue) EmitStructGetters() bool {
return v.EmitGetters
}

func (v QueryValue) IsStruct() bool {
return v.Struct != nil
}
Expand Down
18 changes: 18 additions & 0 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@ const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
{{$.Q}}

{{if .Arg.EmitStruct}}
{{ $structName := .Arg.Type }}
type {{.Arg.Type}} struct { {{- range .Arg.UniqueFields}}
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
{{- end}}
}

{{if $.EmitGetters}}{{range .Arg.UniqueFields}}
{{if .Name}}func (m {{$structName}}) Get{{.Name}}() {{.Type}} {
return m.{{.Name}}
}{{end}}

{{- end}}
{{end}}
{{end}}

{{if .Ret.EmitStruct}}
{{ $structName := .Ret.Type }}
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
{{- end}}
}

{{if $.EmitGetters}}{{range .Ret.Struct.Fields}}
{{if .Name}}func (m {{$structName}}) Get{{.Name}}() {{.Type}} {
return m.{{.Name}}
}{{end}}

{{- end}}
{{end}}
{{end}}

{{if eq .Cmd ":one"}}
Expand Down
11 changes: 10 additions & 1 deletion internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,23 @@ func All{{ .Name }}Values() []{{ .Name }} {
{{end}}

{{range .Structs}}
{{ $structName := .Name }}
{{if .Comment}}{{comment .Comment}}{{end}}
type {{.Name}} struct { {{- range .Fields}}
type {{$structName}} struct { {{- range .Fields}}
{{- if .Comment}}
{{comment .Comment}}{{else}}
{{- end}}
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
{{- end}}
}

{{if $.EmitGetters}}{{range .Fields}}
{{if .Name}}func (m {{$structName}}) Get{{.Name}}() {{.Type}} {
return m.{{.Name}}
}{{end}}

{{- end}}
{{end}}
{{end}}
{{end}}

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type SQLGo struct {
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
EmitGetters bool `json:"emit_getters,omitempty" yaml:"emit_getters"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type v1PackageSettings struct {
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
EmitGetters bool `json:"emit_getters,omitempty" yaml:"emit_getters"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
Expand Down Expand Up @@ -163,6 +164,7 @@ func (c *V1GenerateSettings) Translate() Config {
EmitPointersForNullTypes: pkg.EmitPointersForNullTypes,
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
EmitAllEnumValues: pkg.EmitAllEnumValues,
EmitGetters: pkg.EmitGetters,
Package: pkg.Name,
Out: pkg.Path,
SQLPackage: pkg.SQLPackage,
Expand Down
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/emit_getters/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions internal/endtoend/testdata/emit_getters/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions internal/endtoend/testdata/emit_getters/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/emit_getters/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TYPE ip_protocol AS enum ('tcp', 'ip', 'icmp');

CREATE TABLE bar_old (id_old serial not null, ip_old ip_protocol NOT NULL);

-- name: ListFoo :many
SELECT id_old as foo_old, id_old as baz_old
FROM bar_old
WHERE ip_old = $1 AND id_old = $2;

-- name: ListBar :many
SELECT * FROM bar_old;

22 changes: 22 additions & 0 deletions internal/endtoend/testdata/emit_getters/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"emit_enum_valid_method": true,
"emit_all_enum_values": true
}
],
"rename": {
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol_tcp": "IPProtocolTCP"
}
}
1 change: 1 addition & 0 deletions internal/plugin/codegen.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.