Skip to content

Commit 1e40dc8

Browse files
authored
Python query param limit (sqlc-dev#1530)
* python query param limit support * refactor DefaultQueryParameterLimit update pluginPythonCode unexported default limit * refactor to optional query_parameter_limit proto * 0 has no limit, <0 invalid * revert to default python qpl of 4
1 parent 7331b9d commit 1e40dc8

File tree

25 files changed

+555
-188
lines changed

25 files changed

+555
-188
lines changed

examples/python/sqlc.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"out": "src/authors",
1111
"package": "authors",
1212
"emit_sync_querier": true,
13-
"emit_async_querier": true
13+
"emit_async_querier": true,
14+
"query_parameter_limit": 5
1415
}
1516
}
1617
},
@@ -22,7 +23,8 @@
2223
"python": {
2324
"out": "src/booktest",
2425
"package": "booktest",
25-
"emit_async_querier": true
26+
"emit_async_querier": true,
27+
"query_parameter_limit": 5
2628
}
2729
}
2830
},
@@ -34,7 +36,8 @@
3436
"python": {
3537
"out": "src/jets",
3638
"package": "jets",
37-
"emit_async_querier": true
39+
"emit_async_querier": true,
40+
"query_parameter_limit": 5
3841
}
3942
}
4043
},
@@ -46,7 +49,8 @@
4649
"python": {
4750
"out": "src/ondeck",
4851
"package": "ondeck",
49-
"emit_async_querier": true
52+
"emit_async_querier": true,
53+
"query_parameter_limit": 5
5054
}
5155
}
5256
}

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
8585
EmitSyncQuerier: s.EmitSyncQuerier,
8686
EmitAsyncQuerier: s.EmitAsyncQuerier,
8787
EmitPydanticModels: s.EmitPydanticModels,
88+
QueryParameterLimit: s.QueryParameterLimit,
8889
}
8990
}
9091

internal/codegen/python/gen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,11 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
400400
SourceName: query.Filename,
401401
}
402402

403-
if len(query.Params) > 4 {
403+
qpl := 4
404+
if req.Settings.Python.QueryParameterLimit != nil {
405+
qpl = int(*req.Settings.Python.QueryParameterLimit)
406+
}
407+
if len(query.Params) > qpl || qpl == 0 {
404408
var cols []pyColumn
405409
for _, p := range query.Params {
406410
cols = append(cols, pyColumn{

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ type SQLPython struct {
169169
Out string `json:"out" yaml:"out"`
170170
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
171171
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
172+
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
172173
}
173174

174175
type SQLJSON struct {
@@ -195,6 +196,8 @@ var ErrPluginNoType = errors.New("plugin: field `process` or `wasm` required")
195196
var ErrPluginBothTypes = errors.New("plugin: both `process` and `wasm` cannot both be defined")
196197
var ErrPluginProcessNoCmd = errors.New("plugin: missing process command")
197198

199+
var ErrInvalidQueryParameterLimit = errors.New("invalid query parameter limit")
200+
198201
func ParseConfig(rd io.Reader) (Config, error) {
199202
var buf bytes.Buffer
200203
var config Config

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func v1ParseConfig(rd io.Reader) (Config, error) {
8484
if settings.Packages[j].Engine == "" {
8585
settings.Packages[j].Engine = EnginePostgreSQL
8686
}
87+
8788
}
89+
8890
return settings.Translate(), nil
8991
}
9092

internal/config/v_two.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
3636
}
3737
// TODO: Store built-in plugins somewhere else
3838
builtins := map[string]struct{}{
39-
"go": struct{}{},
40-
"json": struct{}{},
41-
"kotlin": struct{}{},
42-
"python": struct{}{},
39+
"go": {},
40+
"json": {},
41+
"kotlin": {},
42+
"python": {},
4343
}
4444
plugins := map[string]struct{}{}
4545
for i := range conf.Plugins {
@@ -91,6 +91,11 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
9191
}
9292
}
9393
if conf.SQL[j].Gen.Python != nil {
94+
if conf.SQL[j].Gen.Python.QueryParameterLimit != nil {
95+
if *conf.SQL[j].Gen.Python.QueryParameterLimit < 0 {
96+
return conf, ErrInvalidQueryParameterLimit
97+
}
98+
}
9499
if conf.SQL[j].Gen.Python.Out == "" {
95100
return conf, ErrNoOutPath
96101
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE bar (id serial not null, name text not null, primary key (id));
2+
3+
-- name: DeleteBarByID :execrows
4+
DELETE FROM bar WHERE id = $1;
5+
6+
-- name: DeleteBarByIDAndName :execrows
7+
DELETE FROM bar WHERE id = $1 AND name = $2;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "query.sql",
6+
"queries": "query.sql",
7+
"engine": "postgresql",
8+
"gen": {
9+
"python": {
10+
"out": "python",
11+
"package": "querytest",
12+
"emit_sync_querier": true,
13+
"emit_async_querier": true,
14+
"query_parameter_limit": -1
15+
}
16+
}
17+
}
18+
]
19+
}
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
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.15.0
4+
import dataclasses
5+
6+
7+
@dataclasses.dataclass()
8+
class Bar:
9+
id: int
10+
name: str

0 commit comments

Comments
 (0)