Skip to content

Python query param limit #1530

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
Aug 29, 2022
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
12 changes: 8 additions & 4 deletions examples/python/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"out": "src/authors",
"package": "authors",
"emit_sync_querier": true,
"emit_async_querier": true
"emit_async_querier": true,
"query_parameter_limit": 5
}
}
},
Expand All @@ -22,7 +23,8 @@
"python": {
"out": "src/booktest",
"package": "booktest",
"emit_async_querier": true
"emit_async_querier": true,
"query_parameter_limit": 5
}
}
},
Expand All @@ -34,7 +36,8 @@
"python": {
"out": "src/jets",
"package": "jets",
"emit_async_querier": true
"emit_async_querier": true,
"query_parameter_limit": 5
}
}
},
Expand All @@ -46,7 +49,8 @@
"python": {
"out": "src/ondeck",
"package": "ondeck",
"emit_async_querier": true
"emit_async_querier": true,
"query_parameter_limit": 5
}
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
EmitSyncQuerier: s.EmitSyncQuerier,
EmitAsyncQuerier: s.EmitAsyncQuerier,
EmitPydanticModels: s.EmitPydanticModels,
QueryParameterLimit: s.QueryParameterLimit,
}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/codegen/python/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,11 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
SourceName: query.Filename,
}

if len(query.Params) > 4 {
qpl := 4
if req.Settings.Python.QueryParameterLimit != nil {
qpl = int(*req.Settings.Python.QueryParameterLimit)
}
if len(query.Params) > qpl || qpl == 0 {
var cols []pyColumn
for _, p := range query.Params {
cols = append(cols, pyColumn{
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ type SQLPython struct {
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
}

type SQLJSON struct {
Expand All @@ -195,6 +196,8 @@ 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
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func v1ParseConfig(rd io.Reader) (Config, error) {
if settings.Packages[j].Engine == "" {
settings.Packages[j].Engine = EnginePostgreSQL
}

}

return settings.Translate(), nil
}

Expand Down
13 changes: 9 additions & 4 deletions internal/config/v_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
}
// TODO: Store built-in plugins somewhere else
builtins := map[string]struct{}{
"go": struct{}{},
"json": struct{}{},
"kotlin": struct{}{},
"python": struct{}{},
"go": {},
"json": {},
"kotlin": {},
"python": {},
}
plugins := map[string]struct{}{}
for i := range conf.Plugins {
Expand Down Expand Up @@ -91,6 +91,11 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
}
}
if conf.SQL[j].Gen.Python != nil {
if conf.SQL[j].Gen.Python.QueryParameterLimit != nil {
if *conf.SQL[j].Gen.Python.QueryParameterLimit < 0 {
return conf, ErrInvalidQueryParameterLimit
}
}
if conf.SQL[j].Gen.Python.Out == "" {
return conf, ErrNoOutPath
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE bar (id serial not null, name text not null, primary key (id));

-- name: DeleteBarByID :execrows
DELETE FROM bar WHERE id = $1;

-- name: DeleteBarByIDAndName :execrows
DELETE FROM bar WHERE id = $1 AND name = $2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "2",
"sql": [
{
"schema": "query.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"python": {
"out": "python",
"package": "querytest",
"emit_sync_querier": true,
"emit_async_querier": true,
"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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
import dataclasses


@dataclasses.dataclass()
class Bar:
id: int
name: str
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
# source: query.sql
import dataclasses

import sqlalchemy
import sqlalchemy.ext.asyncio

from querytest import models


DELETE_BAR_BY_ID = """-- name: delete_bar_by_id \\:execrows
DELETE FROM bar WHERE id = :p1
"""


@dataclasses.dataclass()
class DeleteBarByIDParams:
id: int


DELETE_BAR_BY_ID_AND_NAME = """-- name: delete_bar_by_id_and_name \\:execrows
DELETE FROM bar WHERE id = :p1 AND name = :p2
"""


@dataclasses.dataclass()
class DeleteBarByIDAndNameParams:
id: int
name: str


class Querier:
def __init__(self, conn: sqlalchemy.engine.Connection):
self._conn = conn

def delete_bar_by_id(self, arg: DeleteBarByIDParams) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": arg.id})
return result.rowcount

def delete_bar_by_id_and_name(self, arg: DeleteBarByIDAndNameParams) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {"p1": arg.id, "p2": arg.name})
return result.rowcount


class AsyncQuerier:
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
self._conn = conn

async def delete_bar_by_id(self, arg: DeleteBarByIDParams) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": arg.id})
return result.rowcount

async def delete_bar_by_id_and_name(self, arg: DeleteBarByIDAndNameParams) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {"p1": arg.id, "p2": arg.name})
return result.rowcount
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE bar (id serial not null, name text not null, primary key (id));

-- name: DeleteBarByID :execrows
DELETE FROM bar WHERE id = $1;

-- name: DeleteBarByIDAndName :execrows
DELETE FROM bar WHERE id = $1 AND name = $2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "2",
"sql": [
{
"schema": "query.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"python": {
"out": "python",
"package": "querytest",
"emit_sync_querier": true,
"emit_async_querier": true,
"query_parameter_limit": 0
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
import dataclasses


@dataclasses.dataclass()
class Bar:
id: int
name: str
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
# source: query.sql
import sqlalchemy
import sqlalchemy.ext.asyncio

from querytest import models


DELETE_BAR_BY_ID = """-- name: delete_bar_by_id \\:execrows
DELETE FROM bar WHERE id = :p1
"""


DELETE_BAR_BY_ID_AND_NAME = """-- name: delete_bar_by_id_and_name \\:execrows
DELETE FROM bar WHERE id = :p1 AND name = :p2
"""


class Querier:
def __init__(self, conn: sqlalchemy.engine.Connection):
self._conn = conn

def delete_bar_by_id(self, *, id: int) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": id})
return result.rowcount

def delete_bar_by_id_and_name(self, *, id: int, name: str) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {"p1": id, "p2": name})
return result.rowcount


class AsyncQuerier:
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
self._conn = conn

async def delete_bar_by_id(self, *, id: int) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": id})
return result.rowcount

async def delete_bar_by_id_and_name(self, *, id: int, name: str) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {"p1": id, "p2": name})
return result.rowcount
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE bar (id serial not null, name text not null, primary key (id));

-- name: DeleteBarByID :execrows
DELETE FROM bar WHERE id = $1;

-- name: DeleteBarByIDAndName :execrows
DELETE FROM bar WHERE id = $1 AND name = $2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "2",
"sql": [
{
"schema": "query.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"python": {
"out": "python",
"package": "querytest",
"emit_sync_querier": true,
"emit_async_querier": true,
"query_parameter_limit": 2
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
import dataclasses


@dataclasses.dataclass()
class Bar:
id: int
name1: str
name2: str
name3: str
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
# source: query.sql
import sqlalchemy
import sqlalchemy.ext.asyncio

from querytest import models


DELETE_BAR_BY_ID = """-- name: delete_bar_by_id \\:execrows
DELETE FROM bar WHERE id = :p1
"""


DELETE_BAR_BY_ID_AND_NAME = """-- name: delete_bar_by_id_and_name \\:execrows
DELETE FROM bar
WHERE id = :p1
AND name1 = :p2
AND name2 = :p3
AND name3 = :p4
"""


class Querier:
def __init__(self, conn: sqlalchemy.engine.Connection):
self._conn = conn

def delete_bar_by_id(self, *, id: int) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": id})
return result.rowcount

def delete_bar_by_id_and_name(self, *, id: int, name1: str, name2: str, name3: str) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {
"p1": id,
"p2": name1,
"p3": name2,
"p4": name3,
})
return result.rowcount


class AsyncQuerier:
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
self._conn = conn

async def delete_bar_by_id(self, *, id: int) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": id})
return result.rowcount

async def delete_bar_by_id_and_name(self, *, id: int, name1: str, name2: str, name3: str) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {
"p1": id,
"p2": name1,
"p3": name2,
"p4": name3,
})
return result.rowcount
Loading