Skip to content
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

feat(sqlite): add typing for sqlite queries #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions 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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
sql:
- schema: schema.sql
queries: query.sql
Expand All @@ -15,4 +15,4 @@ sql:
package: db
emit_sync_querier: true
emit_async_querier: true
emit_pydantic_models: true
emit_pydantic_models: true
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
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: "8b2fef2ff398f9cbe23191ff639aa45ef1d68e49040c5e22eed28390365d6f76"
sql:
- schema: schema.sql
queries: query.sql
Expand Down
2 changes: 2 additions & 0 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func pyInnerType(req *plugin.GenerateRequest, col *plugin.Column) string {
switch req.Settings.Engine {
case "postgresql":
return postgresType(req, col)
case "sqlite":
return sqliteType(req, col)
default:
log.Println("unsupported engine type")
return "Any"
Expand Down
50 changes: 50 additions & 0 deletions internal/sqlite_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package python

import (
"log"
"strings"

"github.com/sqlc-dev/plugin-sdk-go/plugin"
"github.com/sqlc-dev/plugin-sdk-go/sdk"
)

func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
dt := strings.ToLower(sdk.DataType(col.Type))

// see: https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/sqlite_type.go
switch dt {
case "int", "integer", "tinyint", "smallint", "mediumint", "bigint", "unsignedbigint", "int2", "int8":
return "int"
case "blob":
return "bytes"
case "real", "double", "double precision", "float":
return "float"
case "boolean", "bool":
return "bool"
case "date":
return "datetime.date"
case "datetime", "timestamp":
return "datetime.datetime"
case "any":
return "Any"
}

switch {
case strings.HasPrefix(dt, "character"),
strings.HasPrefix(dt, "varchar"),
strings.HasPrefix(dt, "varyingcharacter"),
strings.HasPrefix(dt, "nchar"),
strings.HasPrefix(dt, "nativecharacter"),
strings.HasPrefix(dt, "nvarchar"),
dt == "text",
dt == "clob",
dt == "json":
return "str"
case strings.HasPrefix(dt, "decimal"), dt == "numeric":
return "float"

default:
log.Printf("unknown SQLite type: %s\n", dt)
return "Any"
}
}