Skip to content

Commit 04ab48f

Browse files
author
go-mez
committed
feat(Go):Add query_parameter_limit conf to codegen
1 parent 6e45a9f commit 04ab48f

File tree

24 files changed

+627
-107
lines changed

24 files changed

+627
-107
lines changed

docs/reference/config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ packages:
2424
output_db_file_name: "db.go"
2525
output_models_file_name: "models.go"
2626
output_querier_file_name: "querier.go"
27+
query_parameter_limit: 2
2728
```
2829
2930
Each package document has the following keys:
@@ -70,6 +71,8 @@ Each package document has the following keys:
7071
- Customize the name of the querier file. Defaults to `querier.go`.
7172
- `output_files_suffix`:
7273
- If specified the suffix will be added to the name of the generated files.
74+
- `query_parameter_limit`:
75+
- 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`.
7376

7477
## Type Overrides
7578

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
9292
OutputModelsFileName: s.OutputModelsFileName,
9393
OutputQuerierFileName: s.OutputQuerierFileName,
9494
OutputFilesSuffix: s.OutputFilesSuffix,
95+
QueryParameterLimit: *s.QueryParameterLimit,
9596
}
9697
}
9798

internal/codegen/golang/field.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ func toCamelInitCase(name string, initUpper bool) string {
7777
}
7878
return out
7979
}
80+
81+
func toLowerCase(str string) string {
82+
var b strings.Builder
83+
84+
b.WriteString(strings.ToLower(string(str[0])))
85+
b.WriteString(str[1:])
86+
87+
return b.String()
88+
}

internal/codegen/golang/query.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func (v QueryValue) Pair() string {
3737
if v.isEmpty() {
3838
return ""
3939
}
40+
41+
var out []string
42+
if !v.EmitStruct() && v.IsStruct() {
43+
for _, f := range v.Struct.Fields {
44+
out = append(out, toLowerCase(f.Name)+" "+f.Type)
45+
}
46+
47+
return strings.Join(out, ",")
48+
}
49+
4050
return v.Name + " " + v.DefineType()
4151
}
4252

@@ -102,6 +112,8 @@ func (v QueryValue) Params() string {
102112
for _, f := range v.Struct.Fields {
103113
if strings.HasPrefix(f.Type, "[]") && f.Type != "[]byte" && v.SQLPackage != SQLPackagePGX {
104114
out = append(out, "pq.Array("+v.Name+"."+f.Name+")")
115+
} else if !v.EmitStruct() && v.IsStruct() {
116+
out = append(out, toLowerCase(f.Name))
105117
} else {
106118
out = append(out, v.Name+"."+f.Name)
107119
}

internal/codegen/golang/result.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,16 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
157157
}
158158
sqlpkg := SQLPackageFromString(req.Settings.Go.SqlPackage)
159159

160-
if len(query.Params) == 1 {
160+
qpl := int(req.Settings.Go.QueryParameterLimit)
161+
162+
if len(query.Params) == 1 && qpl != -1 {
161163
p := query.Params[0]
162164
gq.Arg = QueryValue{
163165
Name: paramName(p),
164166
Typ: goType(req, p.Column),
165167
SQLPackage: sqlpkg,
166168
}
167-
} else if len(query.Params) > 1 {
169+
} else if len(query.Params) >= 1 && (qpl >= 1 || qpl == -1) {
168170
var cols []goColumn
169171
for _, p := range query.Params {
170172
cols = append(cols, goColumn{
@@ -183,6 +185,10 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
183185
SQLPackage: sqlpkg,
184186
EmitPointer: req.Settings.Go.EmitParamsStructPointers,
185187
}
188+
189+
if len(query.Params) <= qpl {
190+
gq.Arg.Emit = false
191+
}
186192
}
187193

188194
if len(query.Columns) == 1 {

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ type SQLGo struct {
137137
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
138138
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
139139
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
140+
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
140141
}
141142

142143
type SQLKotlin struct {
@@ -308,6 +309,7 @@ var ErrNoPackageName = errors.New("missing package name")
308309
var ErrNoPackagePath = errors.New("missing package path")
309310
var ErrNoOutPath = errors.New("no output path")
310311
var ErrNoQuerierType = errors.New("no querier emit type enabled")
312+
var ErrInvalidQueryParameterLimit = errors.New("invalid query parameter limit")
311313

312314
func ParseConfig(rd io.Reader) (Config, error) {
313315
var buf bytes.Buffer

internal/config/v_one.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type v1PackageSettings struct {
4040
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
4141
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
4242
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
43+
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
4344
}
4445

4546
func v1ParseConfig(rd io.Reader) (Config, error) {
@@ -71,6 +72,17 @@ func v1ParseConfig(rd io.Reader) (Config, error) {
7172
if settings.Packages[j].Path == "" {
7273
return config, ErrNoPackagePath
7374
}
75+
76+
if settings.Packages[j].QueryParameterLimit != nil && (*settings.Packages[j].QueryParameterLimit < -1 ||
77+
*settings.Packages[j].QueryParameterLimit == 0) {
78+
return config, ErrInvalidQueryParameterLimit
79+
}
80+
81+
if settings.Packages[j].QueryParameterLimit == nil {
82+
settings.Packages[j].QueryParameterLimit = new(int32)
83+
*settings.Packages[j].QueryParameterLimit = 1
84+
}
85+
7486
for i := range settings.Packages[j].Overrides {
7587
if err := settings.Packages[j].Overrides[i].Parse(); err != nil {
7688
return config, err
@@ -135,6 +147,7 @@ func (c *V1GenerateSettings) Translate() Config {
135147
OutputModelsFileName: pkg.OutputModelsFileName,
136148
OutputQuerierFileName: pkg.OutputQuerierFileName,
137149
OutputFilesSuffix: pkg.OutputFilesSuffix,
150+
QueryParameterLimit: pkg.QueryParameterLimit,
138151
},
139152
},
140153
StrictFunctionChecks: pkg.StrictFunctionChecks,

internal/config/v_two.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
4545
if conf.SQL[j].Gen.Go.Package == "" {
4646
conf.SQL[j].Gen.Go.Package = filepath.Base(conf.SQL[j].Gen.Go.Out)
4747
}
48+
49+
if conf.SQL[j].Gen.Go.QueryParameterLimit != nil && (*conf.SQL[j].Gen.Go.QueryParameterLimit < -1 ||
50+
*conf.SQL[j].Gen.Go.QueryParameterLimit == 0) {
51+
return conf, ErrInvalidQueryParameterLimit
52+
}
53+
54+
if conf.SQL[j].Gen.Go.QueryParameterLimit == nil {
55+
*conf.SQL[j].Gen.Go.QueryParameterLimit = 1
56+
}
57+
4858
for i := range conf.SQL[j].Gen.Go.Overrides {
4959
if err := conf.SQL[j].Gen.Go.Overrides[i].Parse(); err != nil {
5060
return conf, err
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text,
6+
country_code CHAR(2) NOT NULL
7+
);
8+
9+
-- name: GetAuthor :one
10+
SELECT * FROM authors
11+
WHERE name = $1 AND country_code = $2 LIMIT 1;
12+
13+
-- name: ListAuthors :many
14+
SELECT * FROM authors
15+
ORDER BY name;
16+
17+
-- name: CreateAuthor :one
18+
INSERT INTO authors (
19+
name, bio, country_code
20+
) VALUES (
21+
$1, $2, $3
22+
)
23+
RETURNING *;
24+
25+
-- name: DeleteAuthor :exec
26+
DELETE FROM authors
27+
WHERE id = $1;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql",
10+
"query_parameter_limit": 0
11+
}
12+
]
13+
}
14+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error parsing sqlc.json: invalid query parameter limit

internal/endtoend/testdata/query_parameter_limit_minus_one/postgresql/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/query_parameter_limit_minus_one/postgresql/go/models.go

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

internal/endtoend/testdata/query_parameter_limit_minus_one/postgresql/go/query.sql.go

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text,
6+
country_code CHAR(2) NOT NULL
7+
);
8+
9+
-- name: GetAuthor :one
10+
SELECT * FROM authors
11+
WHERE name = $1 AND country_code = $2 LIMIT 1;
12+
13+
-- name: ListAuthors :many
14+
SELECT * FROM authors
15+
ORDER BY name;
16+
17+
-- name: CreateAuthor :one
18+
INSERT INTO authors (
19+
name, bio, country_code
20+
) VALUES (
21+
$1, $2, $3
22+
)
23+
RETURNING *;
24+
25+
-- name: DeleteAuthor :exec
26+
DELETE FROM authors
27+
WHERE id = $1;

0 commit comments

Comments
 (0)