Skip to content

feat: emit_module, emit_generators and emit_async flags #15

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: go
on:
push:
tags:
- v*
branches:
- main
pull_request:
Expand All @@ -21,3 +23,31 @@ jobs:
- run: make test
- run: sqlc diff
working-directory: examples

- name: Generate checksum
id: checksum
run: |-
echo "sha256=$(sha256sum bin/sqlc-gen-python.wasm | awk '{ print $1 }')" >> $GITHUB_OUTPUT

- uses: actions/upload-artifact@v4
with:
name: sqlc-gen-python
path: bin/sqlc-gen-python.wasm

- name: Release the build on tag
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
body: |
# Configuration
```yaml
version: '2'
plugins:
- name: py
wasm:
url: ${{ github.server_url }}/${{ github.repository}}/releases/download/${{ github.ref_name }}/sqlc-gen-python.wasm
sha256: ${{ steps.checksum.outputs.sha256 }}
```
generate_release_notes: true
files: |
bin/sqlc-gen-python.wasm
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# README
## Usage

```yaml
Expand All @@ -16,6 +17,17 @@ sql:
plugin: py
options:
package: authors
emit_sync_querier: true
emit_async_querier: true
emit_module: false
emit_generators: true
emit_async: false
```

## Multiple packages
If you have have a mono-repository setup you may want to split the output of queries and models. You can achieve this by using the `output_models_file_name`
and `output_querier_file` fields. If `output_models_file_name` is set to `null` for it will not output the `models.py` file. Setting `output_querier_file` to false will prevent outputting any query files. Combining these you can set one codegen to only output models while the other codegen outputs only queries. Make sure the `package` configuration is set equally so the query files import correctly the models.

SQLC needs at least one query, so you may need to add a unused query like the following in your schema and reuse the `schema` as `queries`.
```sql
-- name: Skip :one
SELECT 1;
```
375 changes: 193 additions & 182 deletions internal/ast/ast.pb.go

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package python

// TODO: Where are these properly documented?
type Config struct {
EmitAsync bool `json:"emit_async"` // Emits async code instead of sync
EmitExactTableNames bool `json:"emit_exact_table_names"`
EmitSyncQuerier bool `json:"emit_sync_querier"`
EmitAsyncQuerier bool `json:"emit_async_querier"`
Package string `json:"package"`
Out string `json:"out"`
EmitGenerators bool `json:"emit_generators"` // Will we use generators or lists, defaults to false
EmitModule bool `json:"emit_module"` // If true emits functions in module, else wraps in a class.
EmitPydanticModels bool `json:"emit_pydantic_models"`
QueryParameterLimit *int32 `json:"query_parameter_limit"`
EmitSyncQuerier bool `json:"emit_sync_querier"` // DEPRECATED ALIAS FOR: emit_type = 'class', emit_generators = True
EmitAsyncQuerier bool `json:"emit_async_querier"` // DEPRECATED ALIAS FOR: emit_type = 'class', emit_generators = True
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names"`
Out string `json:"out"`
OutputModelsFileName *string `json:"output_models_file_name,omitempty"` // Can be string or null to exclude generating models file
OutputQuerierFile bool `json:"output_querier_file,omitempty"` // Skips outputting queries
Package string `json:"package"`
QueryParameterLimit *int32 `json:"query_parameter_limit"`
}
3 changes: 2 additions & 1 deletion internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ func TestGenerate(t *testing.T) {
cmd := exec.Command(sqlc, "diff")
cmd.Dir = dir
got, err := cmd.CombinedOutput()
// TODO: We are diffing patches! Does this make sense and what should we provide to the end user?
if diff := cmp.Diff(string(want), string(got)); diff != "" {
t.Errorf("sqlc diff mismatch (-want +got):\n%s", diff)
t.Errorf("sqlc diff mismatch (-want +got):\n%s", string(got))
}
if len(want) == 0 && err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/emit_pydantic_models/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/exec_result/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/exec_rows/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins:
- name: py
wasm:
url: file://../../../../bin/sqlc-gen-python.wasm
sha256: "a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
sha256: "c97fad53818679a948c68f3ffe84530d7ca4999f636d3f3d89202c6c08ee224d"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
Loading