Skip to content

Commit 5548ce1

Browse files
authored
Merge branch 'main' into feat/NullableEnums
2 parents 2054b50 + dfe4386 commit 5548ce1

File tree

62 files changed

+1325
-440
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1325
-440
lines changed

docs/reference/config.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ packages:
2020
emit_result_struct_pointers: false
2121
emit_params_struct_pointers: false
2222
emit_methods_with_db_argument: false
23+
emit_enum_valid_method: false
24+
emit_all_enum_values: false
2325
json_tags_case_style: "camel"
2426
output_db_file_name: "db.go"
2527
output_models_file_name: "models.go"
@@ -60,6 +62,12 @@ Each package document has the following keys:
6062
- If true, parameters are passed as pointers to structs. Defaults to `false`.
6163
- `emit_methods_with_db_argument`:
6264
- If true, generated methods will accept a DBTX argument instead of storing a DBTX on the `*Queries` struct. Defaults to `false`.
65+
- `emit_enum_valid_method`:
66+
- If true, generate a Valid method on enum types,
67+
indicating whether a string is a valid enum value.
68+
- `emit_all_enum_values`:
69+
- If true, emit a function per enum type
70+
that returns all valid enum values.
6371
- `json_tags_case_style`:
6472
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
6573
- `output_db_file_name`:
@@ -106,7 +114,7 @@ packages: [...]
106114
overrides:
107115
- db_type: "uuid"
108116
go_type:
109-
- import: "a/b/v2"
117+
import: "a/b/v2"
110118
package: "b"
111119
type: "MyType"
112120
pointer: false # or true

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/jackc/pgconn v1.12.1
1111
github.com/jackc/pgx/v4 v4.16.1
1212
github.com/jinzhu/inflection v1.0.0
13-
github.com/lib/pq v1.10.5
13+
github.com/lib/pq v1.10.6
1414
github.com/pganalyze/pg_query_go/v2 v2.1.0
1515
github.com/pingcap/parser v0.0.0-20210914110036-002913dd28ec
1616
github.com/spf13/cobra v1.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
112112
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
113113
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
114114
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
115-
github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ=
116-
github.com/lib/pq v1.10.5/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
115+
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
116+
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
117117
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
118118
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
119119
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=

internal/cmd/shim.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
8686
EmitResultStructPointers: s.EmitResultStructPointers,
8787
EmitParamsStructPointers: s.EmitParamsStructPointers,
8888
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
89+
EmitEnumValidMethod: s.EmitEnumValidMethod,
90+
EmitAllEnumValues: s.EmitAllEnumValues,
8991
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
9092
Package: s.Package,
9193
Out: s.Out,

internal/codegen/golang/gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type tmplCtx struct {
3232
EmitInterface bool
3333
EmitEmptySlices bool
3434
EmitMethodsWithDBArgument bool
35+
EmitEnumValidMethod bool
36+
EmitAllEnumValues bool
3537
UsesCopyFrom bool
3638
UsesBatch bool
3739
}
@@ -84,6 +86,8 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
8486
EmitPreparedQueries: golang.EmitPreparedQueries,
8587
EmitEmptySlices: golang.EmitEmptySlices,
8688
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
89+
EmitEnumValidMethod: golang.EmitEnumValidMethod,
90+
EmitAllEnumValues: golang.EmitAllEnumValues,
8791
UsesCopyFrom: usesCopyFrom(queries),
8892
UsesBatch: usesBatch(queries),
8993
SQLPackage: SQLPackageFromString(golang.SqlPackage),

internal/codegen/golang/imports.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,14 @@ func (i *importer) queryImports(filename string) fileImports {
376376
}
377377

378378
func (i *importer) copyfromImports() fileImports {
379-
std, pkg := buildImports(i.Settings, i.Queries, func(name string) bool {
380-
for _, q := range i.Queries {
381-
if q.Cmd != metadata.CmdCopyFrom {
382-
continue
383-
}
379+
copyFromQueries := make([]Query, 0, len(i.Queries))
380+
for _, q := range i.Queries {
381+
if q.Cmd == metadata.CmdCopyFrom {
382+
copyFromQueries = append(copyFromQueries, q)
383+
}
384+
}
385+
std, pkg := buildImports(i.Settings, copyFromQueries, func(name string) bool {
386+
for _, q := range copyFromQueries {
384387
if q.hasRetType() {
385388
if strings.HasPrefix(q.Ret.Type(), name) {
386389
return true

internal/codegen/golang/templates/template.tmpl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ func (e *{{.Name}}) Scan(src interface{}) error {
8787
return nil
8888
}
8989

90+
{{ if $.EmitEnumValidMethod }}
91+
func (e {{.Name}}) Valid() bool {
92+
switch e {
93+
case {{ range $idx, $name := .Constants }}{{ if ne $idx 0 }},{{ "\n" }}{{ end }}{{ .Name }}{{ end }}:
94+
return true
95+
}
96+
return false
97+
}
98+
{{ end }}
99+
100+
{{ if $.EmitAllEnumValues }}
101+
func All{{ .Name }}Values() []{{ .Name }} {
102+
return []{{ .Name }}{ {{ range .Constants}}{{ "\n" }}{{ .Name }},{{ end }}
103+
}
104+
}
105+
{{ end }}
106+
90107
type Null{{.Name}} struct {
91108
{{.Name}} {{.Name}}
92109
Valid bool // Valid is true if String is not NULL
@@ -109,7 +126,6 @@ func (ns Null{{.Name}}) Value() (driver.Value, error) {
109126
}
110127
return ns.{{.Name}}, nil
111128
}
112-
113129
{{end}}
114130

115131
{{range .Structs}}

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ type SQLGo struct {
128128
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
129129
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
130130
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
131+
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
132+
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
131133
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
132134
Package string `json:"package" yaml:"package"`
133135
Out string `json:"out" yaml:"out"`

internal/config/v_one.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type v1PackageSettings struct {
3232
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
3333
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
3434
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument" yaml:"emit_methods_with_db_argument"`
35+
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
36+
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
3537
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
3638
SQLPackage string `json:"sql_package" yaml:"sql_package"`
3739
Overrides []Override `json:"overrides" yaml:"overrides"`
@@ -126,6 +128,8 @@ func (c *V1GenerateSettings) Translate() Config {
126128
EmitResultStructPointers: pkg.EmitResultStructPointers,
127129
EmitParamsStructPointers: pkg.EmitParamsStructPointers,
128130
EmitMethodsWithDBArgument: pkg.EmitMethodsWithDBArgument,
131+
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
132+
EmitAllEnumValues: pkg.EmitAllEnumValues,
129133
Package: pkg.Name,
130134
Out: pkg.Path,
131135
SQLPackage: pkg.SQLPackage,

internal/endtoend/testdata/codegen_json/gen/codegen_request.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"output_db_file_name": "",
4242
"output_models_file_name": "",
4343
"output_querier_file_name": "",
44-
"output_files_suffix": ""
44+
"output_files_suffix": "",
45+
"emit_enum_valid_method": false,
46+
"emit_all_enum_values": false
4547
},
4648
"json": {
4749
"out": "gen",

internal/endtoend/testdata/copyfrom_imports/postgresql/pgx/go/query.sql.go

Lines changed: 11 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_imports/postgresql/pgx/query.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ INSERT INTO myschema.foo (a, b) VALUES ($1, $2);
66

77
-- name: InsertSingleValue :exec
88
INSERT INTO myschema.foo (a) VALUES ($1);
9+
10+
-- name: DeleteValues :execresult
11+
DELETE
12+
FROM myschema.foo;

internal/endtoend/testdata/ddl_alter_table_add_column/mysql/go/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/ddl_alter_table_add_column/mysql/go/models.go

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

internal/endtoend/testdata/ddl_alter_table_add_column/mysql/go/query.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* name: Placeholder :exec */
2+
SELECT 1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE foo (bar text NOT NULL);
2+
ALTER TABLE foo ADD COLUMN baz integer;
3+
ALTER TABLE foo ADD bio integer;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/ddl_alter_table_add_column/postgresql/pgx/go/db.go

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

internal/endtoend/testdata/ddl_alter_table_add_column/postgresql/pgx/go/models.go

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

internal/endtoend/testdata/ddl_alter_table_add_column/postgresql/pgx/go/query.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Placeholder :exec
2+
SELECT 1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE foo (bar text NOT NULL);
2+
ALTER TABLE foo ADD COLUMN baz int;
3+
ALTER TABLE foo ADD bio int;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"sql_package": "pgx/v4",
8+
"name": "querytest",
9+
"schema": "schema.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)