Skip to content

Commit a38b45d

Browse files
authored
Feat/omit kwargs (#124)
* work * work * fragment * fragment * feat(docs): Update README.md with information about configuration options and type overrides (feat/omit-kwargs) * update sqlc
1 parent 63a138a commit a38b45d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+133
-110
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Added
2+
body: Added config option `omit_kwargs_limit`.
3+
time: 2025-09-03T02:16:38.516778+02:00
4+
custom:
5+
Author: rayakame
6+
PR: "124"

README.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# sqlc-gen-better-python
2+
23
[![Codecov](https://codecov.io/gh/rayakame/sqlc-gen-better-python/graph/badge.svg?token=LROCMXW6MC)](https://codecov.io/gh/rayakame/sqlc-gen-better-python)
34
![Python Version from PEP 621 TOML](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Frayakame%2Fsqlc-gen-better-python%2Fmain%2Fpyproject.toml)
45
![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)
@@ -39,6 +40,7 @@ sql:
3940
More options at the [`sqlc` config reference](https://docs.sqlc.dev/en/stable/reference/config.html)
4041

4142
## Configuration Options
43+
4244
| Name | Type | Required | Description |
4345
|----------------------------------|----------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
4446
| `package` | string | yes | The name of the package where the generated files will be located |
@@ -54,34 +56,39 @@ More options at the [`sqlc` config reference](https://docs.sqlc.dev/en/stable/re
5456
| `docstrings` | string | no | If set, there will be docstrings generated in the selected format. This can be one of `google`, `numpy`, `pep257` and `none`. `none` will not generate any docstrings. |
5557
| `docstrings_emit_sql` | bool | no | If set to `false` the SQL code for each query wont be included in the docstrings. This defaults to `true` but is not used when `docstrings` is not set or set to `none` |
5658
| `query_parameter_limit` | integer | no | Not yet implemented. |
59+
| `omit_kwargs_limit` | integer | no | This can be used to set a limit where any query with less or equal amounts of parameters will not require kwargs for the parameters. This defaults to `0` which makes every query require kwargs for their parameters. |
5760
| `speedups` | bool | no | If set to `true` the plugin will use other librarys for type conversion. Needs extra dependecys to be installed. This option currently only affects `sqlite3` & `aiosqlite` and uses the library `ciso8601` |
5861
| `overrides` | list[Override] | no | A list of [type overrides](#type-overrides). |
5962
| `debug` | bool | no | If set to `true`, there will be debug logs generated into a `log.txt` file when executing `sqlc generate`. Defaults to `false` |
6063

6164
### Type Overrides
62-
Similar to `sqlc-gen-go` this plugin supports overriding types with your own. You can either override the type of every column that has a specific sql type, or you can overwrite the type of specific columns.
65+
66+
Similar to `sqlc-gen-go` this plugin supports overriding types with your own. You can either override the type of every
67+
column that has a specific sql type, or you can overwrite the type of specific columns.
6368

6469
```yaml
6570
# filename: sqlc.yaml
6671
# ...
67-
options:
68-
# ...
69-
overrides:
70-
- db_type: text
71-
py_type:
72-
import: collections
73-
package: UserString
74-
type: UserString
75-
- column: table_name.text_column
76-
py_type:
77-
import: collections
78-
type: collections.UserString
72+
options:
73+
# ...
74+
overrides:
75+
- db_type: text
76+
py_type:
77+
import: collections
78+
package: UserString
79+
type: UserString
80+
- column: table_name.text_column
81+
py_type:
82+
import: collections
83+
type: collections.UserString
7984

8085
```
8186

8287
## Feature Support
88+
8389
Every [sqlc macro](https://docs.sqlc.dev/en/latest/reference/macros.html) is supported.
84-
The supported [query commands](https://docs.sqlc.dev/en/latest/reference/query-annotations.html) depend on the SQL driver you are using, supported commands are listed below.
90+
The supported [query commands](https://docs.sqlc.dev/en/latest/reference/query-annotations.html) depend on the SQL
91+
driver you are using, supported commands are listed below.
8592
> Every `:batch*` command is not supported by this plugin and probably will never be.
8693
8794
> Prepared Queries are not planned for the near future, but will be implemented sooner or later
@@ -95,15 +102,21 @@ The supported [query commands](https://docs.sqlc.dev/en/latest/reference/query-a
95102
| mysql | no | no | no | no | no | no | no |
96103

97104
## Development
105+
98106
A roadmap of what is planned & worked on can be found [here](https://github.com/users/rayakame/projects/1/).
99107

100-
Contributions are very welcome, for more information and help please read the [contribution guidelines](https://github.com/rayakame/sqlc-gen-better-python/blob/main/CONTRIBUTING.md).
108+
Contributions are very welcome, for more information and help please read
109+
the [contribution guidelines](https://github.com/rayakame/sqlc-gen-better-python/blob/main/CONTRIBUTING.md).
110+
101111
### Changelog
112+
102113
Can be found [here](https://github.com/rayakame/sqlc-gen-better-python/blob/main/CHANGELOG.md)
103114

104115
## Credits
105-
Because of missing documentation about creating these plugins, this work is heavily
116+
117+
Because of missing documentation about creating these plugins, this work is heavily
106118
inspired by:
119+
107120
- [sqlc-gen-go](https://github.com/sqlc-dev/sqlc-gen-go)
108121
- [sqlc-gen-java](https://github.com/tandemdude/sqlc-gen-java)
109122

internal/codegen/builders/string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package builders
22

33
import (
4+
"fmt"
45
"github.com/rayakame/sqlc-gen-better-python/internal/core"
56
"os"
67
"strings"
@@ -20,6 +21,15 @@ func NewIndentStringBuilder(indentChar string, charsPerIndentLevel int) *IndentS
2021
}
2122
}
2223

24+
func (b *IndentStringBuilder) WriteQueryFunctionArgs(args []core.FunctionArg, conf *core.Config) {
25+
for i, arg := range args {
26+
if i == 0 && len(args) > int(*conf.OmitKwargsLimit) {
27+
b.WriteString(", *")
28+
}
29+
b.WriteString(fmt.Sprintf(", %s", arg.FunctionFormat))
30+
}
31+
}
32+
2333
func (b *IndentStringBuilder) WriteIndentedString(level int, txt string) int {
2434
count, _ := b.WriteString(strings.Repeat(b.indentChar, level*b.charsPerIndentLevel) + txt)
2535
return count

internal/codegen/drivers/aiosqlite.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,7 @@ func AioSQLiteBuildPyQueryFunc(query *core.Query, body *builders.IndentStringBui
175175
asyncFunc = ""
176176
}
177177
body.WriteIndentedString(indentLevel, fmt.Sprintf("%sdef %s(%s", asyncFunc, query.FuncName, params))
178-
for i, arg := range args {
179-
if i == 0 {
180-
body.WriteString(", *")
181-
}
182-
body.WriteString(fmt.Sprintf(", %s", arg.FunctionFormat))
183-
}
178+
body.WriteQueryFunctionArgs(args, conf)
184179
if query.Cmd == metadata.CmdExec {
185180
body.WriteLine(fmt.Sprintf(") -> %s:", retType.Type))
186181
body.WriteQueryFunctionDocstring(indentLevel+1, query, docstringConnType, args, retType)

internal/codegen/drivers/asyncpg.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ func AsyncpgBuildPyQueryFunc(query *core.Query, body *builders.IndentStringBuild
6363
asyncFunc = ""
6464
}
6565
body.WriteIndentedString(indentLevel, fmt.Sprintf("%sdef %s(%s", asyncFunc, query.FuncName, params))
66-
for i, arg := range args {
67-
if i == 0 {
68-
body.WriteString(", *")
69-
}
70-
body.WriteString(fmt.Sprintf(", %s", arg.FunctionFormat))
71-
}
66+
body.WriteQueryFunctionArgs(args, conf)
7267
if query.Cmd == metadata.CmdExec {
7368
body.WriteLine(fmt.Sprintf(") -> %s:", retType.Type))
7469
body.WriteQueryFunctionDocstring(indentLevel+1, query, docstringConnType, args, retType)

internal/codegen/drivers/sqlite3.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,7 @@ func SQLite3BuildPyQueryFunc(query *core.Query, body *builders.IndentStringBuild
172172
docstringConnType = ""
173173
}
174174
body.WriteIndentedString(indentLevel, fmt.Sprintf("def %s(%s", query.FuncName, params))
175-
for i, arg := range args {
176-
if i == 0 {
177-
body.WriteString(", *")
178-
}
179-
body.WriteString(fmt.Sprintf(", %s", arg.FunctionFormat))
180-
}
175+
body.WriteQueryFunctionArgs(args, conf)
181176
if query.Cmd == metadata.CmdExec {
182177
body.WriteLine(fmt.Sprintf(") -> %s:", retType.Type))
183178
body.WriteQueryFunctionDocstring(indentLevel+1, query, docstringConnType, args, retType)

internal/core/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
OmitUnusedModels bool `json:"omit_unused_models" yaml:"omit_unused_models"`
2020
OmitTypecheckingBlock bool `json:"omit_typechecking_block" yaml:"omit_typechecking_block"`
2121
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
22+
OmitKwargsLimit *int32 `json:"omit_kwargs_limit,omitempty" yaml:"omit_kwargs_limit"`
2223
EmitInitFile *bool `json:"emit_init_file" yaml:"emit_init_file"`
2324
EmitDocstrings *string `json:"docstrings" yaml:"docstrings"`
2425
EmitDocstringsSQL *bool `json:"docstrings_emit_sql" yaml:"docstrings_emit_sql"`
@@ -64,6 +65,10 @@ func ParseConfig(req *plugin.GenerateRequest) (*Config, error) {
6465
config.QueryParameterLimit = new(int32)
6566
*config.QueryParameterLimit = 1
6667
}
68+
if config.OmitKwargsLimit == nil {
69+
config.OmitKwargsLimit = new(int32)
70+
*config.OmitKwargsLimit = 0
71+
}
6772
if config.Initialisms == nil {
6873
config.Initialisms = new([]string)
6974
*config.Initialisms = []string{"id"}
@@ -93,6 +98,9 @@ func ValidateConf(conf *Config, engine string) error {
9398
if *conf.QueryParameterLimit < 0 {
9499
return fmt.Errorf("invalid options: query parameter limit must not be negative")
95100
}
101+
if *conf.OmitKwargsLimit < 0 {
102+
return fmt.Errorf("invalid options: omit kwarg limit must not be negative")
103+
}
96104

97105
if conf.EmitInitFile == nil {
98106
return fmt.Errorf("invalid options: you need to specify emit_init_file")
@@ -107,7 +115,6 @@ func ValidateConf(conf *Config, engine string) error {
107115
}
108116

109117
if err := isModelTypeValid(conf.ModelType); err != nil {
110-
111118
return fmt.Errorf("invalid options: %s", err)
112119
}
113120

sqlc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins:
33
- name: python
44
wasm:
55
url: file://sqlc-gen-better-python.wasm
6-
sha256: 750bbaa7a45acdce18c4c06ea4384c4df7e5f3e0e51ed6c8badcaea2dbe6d14d
6+
sha256: ee7bd0c07b784b80ea8c5853d9a6a04c51a7abbfd2663f470e9a5d2f623b967e
77
sql:
88
- schema: test/schema.sql
99
queries: test/queries.sql
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Code generated by sqlc. DO NOT EDIT.
22
# versions:
33
# sqlc v1.30.0
4-
# sqlc-gen-better-python v0.4.3
4+
# sqlc-gen-better-python v0.4.4
55
"""Package containing queries and models automatically generated using sqlc-gen-better-python."""

test/driver_aiosqlite/attrs/classes/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
22
# versions:
33
# sqlc v1.30.0
4-
# sqlc-gen-better-python v0.4.3
4+
# sqlc-gen-better-python v0.4.4
55
"""Module containing models."""
66
from __future__ import annotations
77

0 commit comments

Comments
 (0)