Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea/
/.idea/
.vscode
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
5 changes: 5 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ func pluginSettings(cs config.CombinedSettings) *plugin.Settings {
}

func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
var qpl int32
if s.QueryParameterLimit != nil {
qpl = *s.QueryParameterLimit
}
return &plugin.PythonCode{
Out: s.Out,
Package: s.Package,
EmitExactTableNames: s.EmitExactTableNames,
EmitSyncQuerier: s.EmitSyncQuerier,
EmitAsyncQuerier: s.EmitAsyncQuerier,
QueryParameterLimit: qpl,
}
}

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

if len(query.Params) > 4 {
if len(query.Params) > int(req.Settings.Python.QueryParameterLimit) || req.Settings.Python.QueryParameterLimit == -1 {
var cols []pyColumn
for _, p := range query.Params {
cols = append(cols, pyColumn{
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const (
EngineXLemon Engine = "_lemon"
)

var defaultQueryParameterLimit int32 = 1

type Config struct {
Version string `json:"version" yaml:"version"`
Project Project `json:"project" yaml:"project"`
Expand Down Expand Up @@ -152,6 +154,7 @@ type SQLPython struct {
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
}

type Override struct {
Expand Down Expand Up @@ -308,6 +311,7 @@ var ErrNoPackageName = errors.New("missing package name")
var ErrNoPackagePath = errors.New("missing package path")
var ErrNoOutPath = errors.New("no output path")
var ErrNoQuerierType = errors.New("no querier emit type enabled")
var ErrInvalidQueryParameterLimit = errors.New("invalid query parameter limit")

func ParseConfig(rd io.Reader) (Config, error) {
var buf bytes.Buffer
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 @@ -82,7 +82,9 @@ func v1ParseConfig(rd io.Reader) (Config, error) {
if settings.Packages[j].Engine == "" {
settings.Packages[j].Engine = EnginePostgreSQL
}

}

return settings.Translate(), nil
}

Expand Down
42 changes: 25 additions & 17 deletions internal/config/v_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,54 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
}
}
}
for j := range conf.SQL {
if conf.SQL[j].Engine == "" {
for _, sql := range conf.SQL {
if sql.Engine == "" {
return conf, ErrMissingEngine
}
if conf.SQL[j].Gen.Go != nil {
if conf.SQL[j].Gen.Go.Out == "" {
if sql.Gen.Go != nil {
if sql.Gen.Go.Out == "" {
return conf, ErrNoPackagePath
}
if conf.SQL[j].Gen.Go.Package == "" {
conf.SQL[j].Gen.Go.Package = filepath.Base(conf.SQL[j].Gen.Go.Out)
if sql.Gen.Go.Package == "" {
sql.Gen.Go.Package = filepath.Base(sql.Gen.Go.Out)
}
for i := range conf.SQL[j].Gen.Go.Overrides {
if err := conf.SQL[j].Gen.Go.Overrides[i].Parse(); err != nil {
for i := range sql.Gen.Go.Overrides {
if err := sql.Gen.Go.Overrides[i].Parse(); err != nil {
return conf, err
}
}
}
if conf.SQL[j].Gen.Kotlin != nil {
if conf.SQL[j].Gen.Kotlin.Out == "" {
if sql.Gen.Kotlin != nil {
if sql.Gen.Kotlin.Out == "" {
return conf, ErrNoOutPath
}
if conf.SQL[j].Gen.Kotlin.Package == "" {
if sql.Gen.Kotlin.Package == "" {
return conf, ErrNoPackageName
}
}
if conf.SQL[j].Gen.Python != nil {
if conf.SQL[j].Gen.Python.Out == "" {
if sql.Gen.Python != nil {
if sql.Gen.Python.QueryParameterLimit != nil {
if *sql.Gen.Python.QueryParameterLimit == 0 || *sql.Gen.Python.QueryParameterLimit < -1 {
return conf, ErrInvalidQueryParameterLimit
}
} else {
sql.Gen.Python.QueryParameterLimit = &defaultQueryParameterLimit
}
if sql.Gen.Python.Out == "" {
return conf, ErrNoOutPath
}
if conf.SQL[j].Gen.Python.Package == "" {
if sql.Gen.Python.Package == "" {
return conf, ErrNoPackageName
}
if !conf.SQL[j].Gen.Python.EmitSyncQuerier && !conf.SQL[j].Gen.Python.EmitAsyncQuerier {
if !sql.Gen.Python.EmitSyncQuerier && !sql.Gen.Python.EmitAsyncQuerier {
return conf, ErrNoQuerierType
}
for i := range conf.SQL[j].Gen.Python.Overrides {
if err := conf.SQL[j].Gen.Python.Overrides[i].Parse(); err != nil {
for i := range sql.Gen.Python.Overrides {
if err := sql.Gen.Python.Overrides[i].Parse(); err != nil {
return conf, err
}
}

}
}
return conf, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.13.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.13.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": -1
}
}
}
]
}
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 @@
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.13.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.13.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
}
}
}
]
}
Loading