Skip to content

feat(Go):Add query_parameter_limit conf to codegen #1558

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

Merged
merged 6 commits into from
Apr 7, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
__pycache__
.DS_Store
6 changes: 5 additions & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ The `gen` mapping supports the following keys:
- Customize the name of the querier file. Defaults to `querier.go`.
- `output_files_suffix`:
- If specified the suffix will be added to the name of the generated files.
- `rename`:
- `query_parameter_limit`:
- Positional arguments that will be generated in Go functions (>= `1` or `-1`). To always emit a parameter struct, you would need to set it to `-1`. `0` is invalid. Defaults to `1`.
`rename`:
- Customize the name of generated struct fields. Explained in detail on the `Renaming fields` section.
- `overrides`:
- It is a collection of definitions that dictates which types are used to map a database types. Explained in detail on the `Type overriding` section.
Expand Down Expand Up @@ -403,6 +405,8 @@ Each mapping in the `packages` collection has the following keys:
- Customize the name of the querier file. Defaults to `querier.go`.
- `output_files_suffix`:
- If specified the suffix will be added to the name of the generated files.
- `query_parameter_limit`:
- Positional arguments that will be generated in Go functions (`>= 0`). To always emit a parameter struct, you would need to set it to `0`. Defaults to `1`.

### overrides

Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func pluginCodegen(s config.Codegen) *plugin.Codegen {
}

func pluginGoCode(s config.SQLGo) *plugin.GoCode {
if s.QueryParameterLimit == nil {
s.QueryParameterLimit = new(int32)
*s.QueryParameterLimit = 1
}

return &plugin.GoCode{
EmitInterface: s.EmitInterface,
EmitJsonTags: s.EmitJSONTags,
Expand All @@ -98,6 +103,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
OutputQuerierFileName: s.OutputQuerierFileName,
OutputFilesSuffix: s.OutputFilesSuffix,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
QueryParameterLimit: s.QueryParameterLimit,
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ func toCamelInitCase(name string, initUpper bool) string {
}
return out
}

func toLowerCase(str string) string {
if str == "" {
return ""
}

return strings.ToLower(str[:1]) + str[1:]
}
12 changes: 12 additions & 0 deletions internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func (v QueryValue) Pair() string {
if v.isEmpty() {
return ""
}

var out []string
if !v.EmitStruct() && v.IsStruct() {
for _, f := range v.Struct.Fields {
out = append(out, toLowerCase(f.Name)+" "+f.Type)
}

return strings.Join(out, ",")
}

return v.Name + " " + v.DefineType()
}

Expand Down Expand Up @@ -107,6 +117,8 @@ func (v QueryValue) Params() string {
for _, f := range v.Struct.Fields {
if !f.HasSqlcSlice() && strings.HasPrefix(f.Type, "[]") && f.Type != "[]byte" && !v.SQLDriver.IsPGX() {
out = append(out, "pq.Array("+v.Name+"."+f.Name+")")
} else if !v.EmitStruct() && v.IsStruct() {
out = append(out, toLowerCase(f.Name))
} else {
out = append(out, v.Name+"."+f.Name)
}
Expand Down
10 changes: 8 additions & 2 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
}
sqlpkg := parseDriver(req.Settings.Go.SqlPackage)

if len(query.Params) == 1 {
qpl := int(*req.Settings.Go.QueryParameterLimit)

if len(query.Params) == 1 && qpl != 0 {
p := query.Params[0]
gq.Arg = QueryValue{
Name: paramName(p),
Expand All @@ -211,7 +213,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
SQLDriver: sqlpkg,
Column: p.Column,
}
} else if len(query.Params) > 1 {
} else if len(query.Params) >= 1 {
var cols []goColumn
for _, p := range query.Params {
cols = append(cols, goColumn{
Expand All @@ -230,6 +232,10 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
SQLDriver: sqlpkg,
EmitPointer: req.Settings.Go.EmitParamsStructPointers,
}

if len(query.Params) <= qpl {
gq.Arg.Emit = false
}
}

if len(query.Columns) == 1 && query.Columns[0].EmbedTable == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type SQLGo struct {
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
}

type SQLJSON struct {
Expand All @@ -150,6 +151,7 @@ var ErrNoPackages = errors.New("no packages")
var ErrNoQuerierType = errors.New("no querier emit type enabled")
var ErrUnknownEngine = errors.New("invalid engine")
var ErrUnknownVersion = errors.New("invalid version number")
var ErrInvalidQueryParameterLimit = errors.New("invalid query parameter limit")

var ErrPluginBuiltin = errors.New("a built-in plugin with that name already exists")
var ErrPluginNoName = errors.New("missing plugin name")
Expand All @@ -159,8 +161,6 @@ var ErrPluginNoType = errors.New("plugin: field `process` or `wasm` required")
var ErrPluginBothTypes = errors.New("plugin: both `process` and `wasm` cannot both be defined")
var ErrPluginProcessNoCmd = errors.New("plugin: missing process command")

var ErrInvalidQueryParameterLimit = errors.New("invalid query parameter limit")

func ParseConfig(rd io.Reader) (Config, error) {
var buf bytes.Buffer
var config Config
Expand Down
12 changes: 12 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type v1PackageSettings struct {
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
}

func v1ParseConfig(rd io.Reader) (Config, error) {
Expand Down Expand Up @@ -74,6 +75,16 @@ func v1ParseConfig(rd io.Reader) (Config, error) {
if settings.Packages[j].Path == "" {
return config, ErrNoPackagePath
}

if settings.Packages[j].QueryParameterLimit != nil && (*settings.Packages[j].QueryParameterLimit < 0) {
return config, ErrInvalidQueryParameterLimit
}

if settings.Packages[j].QueryParameterLimit == nil {
settings.Packages[j].QueryParameterLimit = new(int32)
*settings.Packages[j].QueryParameterLimit = 1
}

for i := range settings.Packages[j].Overrides {
if err := settings.Packages[j].Overrides[i].Parse(); err != nil {
return config, err
Expand Down Expand Up @@ -143,6 +154,7 @@ func (c *V1GenerateSettings) Translate() Config {
OutputModelsFileName: pkg.OutputModelsFileName,
OutputQuerierFileName: pkg.OutputQuerierFileName,
OutputFilesSuffix: pkg.OutputFilesSuffix,
QueryParameterLimit: pkg.QueryParameterLimit,
},
},
StrictFunctionChecks: pkg.StrictFunctionChecks,
Expand Down
10 changes: 10 additions & 0 deletions internal/config/v_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
if conf.SQL[j].Gen.Go.Package == "" {
conf.SQL[j].Gen.Go.Package = filepath.Base(conf.SQL[j].Gen.Go.Out)
}

if conf.SQL[j].Gen.Go.QueryParameterLimit != nil && (*conf.SQL[j].Gen.Go.QueryParameterLimit < 0) {
return conf, ErrInvalidQueryParameterLimit
}

if conf.SQL[j].Gen.Go.QueryParameterLimit == nil {
conf.SQL[j].Gen.Go.QueryParameterLimit = new(int32)
*conf.SQL[j].Gen.Go.QueryParameterLimit = 1
}

for i := range conf.SQL[j].Gen.Go.Overrides {
if err := conf.SQL[j].Gen.Go.Overrides[i].Parse(); err != nil {
return conf, err
Expand Down
3 changes: 2 additions & 1 deletion internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"emit_enum_valid_method": false,
"emit_all_enum_values": false,
"inflection_exclude_table_names": [],
"emit_pointers_for_null_types": false
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1
},
"json": {
"out": "gen",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"emit_enum_valid_method": false,
"emit_all_enum_values": false,
"inflection_exclude_table_names": [],
"emit_pointers_for_null_types": false
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1
},
"json": {
"out": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"emit_enum_valid_method": false,
"emit_all_enum_values": false,
"inflection_exclude_table_names": [],
"emit_pointers_for_null_types": false
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1
},
"json": {
"out": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
country_code CHAR(2) NOT NULL
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE name = $1 AND country_code = $2 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio, country_code
) VALUES (
$1, $2, $3
)
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"query_parameter_limit": -1
}
]
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error parsing sqlc.json: invalid query parameter limit

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

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

Loading