- Notifications
You must be signed in to change notification settings - Fork39
feat: mysql support#25
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
zztkm wants to merge14 commits intosqlc-dev:mainChoose a base branch fromzztkm:support-mysql-type
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+66,743 −19
Open
Changes fromall commits
Commits
Show all changes
14 commits Select commitHold shift + click to select a range
3ae1682 deps: go mod tidy
zztkmfe21db7 wip: generate examples
zztkm4b62b13 test: add generation test
zztkm42d649c feat: wip generate orm.py
zztkm48821ef feat: with Mapped class annotation
zztkmdd6d5f7 fix import path
zztkm33bc14c ci: update makefile
zztkm9604f32 Merge pull request #1 from zztkm/feat-sqlalchemy-model
zztkmf035944 fix makefile
zztkma5f76e3 docs: update license
zztkm2249174 feat: mysql support #24
zztkmd3d1c06 Add support for MySQL placeholders in
zztkm0431a69 Update README.md
zztkmea2451d Merge branch 'main' into support-mysql-type
zztkmFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| dist |
23 changes: 23 additions & 0 deletionsLICENSE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
46 changes: 40 additions & 6 deletionsMakefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,42 @@ | ||
| VERSION := $$(make -s show-version) | ||
| CURRENT_REVISION := $(shell git rev-parse --short HEAD) | ||
| BUILD_LDFLAGS := "-s -w -X main.revision=$(CURRENT_REVISION)" | ||
| GOBIN ?= $(shell go env GOPATH)/bin | ||
| .PHONY: show-version | ||
| show-version: $(GOBIN)/gobump | ||
| @gobump show -r cmd/sqlc-gen-python-orm | ||
| $(GOBIN)/gobump: | ||
| @go install github.com/x-motemen/gobump/cmd/gobump@latest | ||
| .PHONY: compile | ||
| compile: | ||
| sqlc compile | ||
| .PHONY: generate | ||
| generate: sqlc.yaml | ||
| sqlc generate | ||
| .PHONY: release | ||
| release: dist/sqlc-gen-python-orm.wasm dist/sqlc-gen-python-orm.wasm.sha256 | ||
| gh release create "v${VERSION}" dist/sqlc-gen-python-orm.wasm dist/sqlc-gen-python-orm.wasm.sha256 | ||
| .PHONY: release | ||
| release-overwrite: dist/sqlc-gen-python-orm.wasm dist/sqlc-gen-python-orm.wasm.sha256 | ||
| gh release delete -y --cleanup-tag "v${VERSION}" | ||
| gh release create "v${VERSION}" dist/sqlc-gen-python-orm.wasm dist/sqlc-gen-python-orm.wasm.sha256 | ||
| .PHONY: clean | ||
| clean: | ||
| rm -rf ./_examples/gen | ||
| sqlc.yaml: dist/sqlc-gen-python-orm.wasm.sha256 _sqlc.yaml | ||
| cat _sqlc.yaml | WASM_SHA256=$$(cat $<) envsubst > $@ | ||
| dist/sqlc-gen-python-orm.wasm.sha256: dist/sqlc-gen-python-orm.wasm | ||
| openssl sha256 $< | awk '{print $$2}' > $@ | ||
| dist/sqlc-gen-python-orm.wasm: internal/* | ||
| GOOS=wasip1 GOARCH=wasm go build -o $@ ./cmd/sqlc-gen-python-orm/main.go |
23 changes: 20 additions & 3 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions_examples/gen/sqlc/models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.20.0 | ||
| import pydantic | ||
| from typing import Optional | ||
| class Author(pydantic.BaseModel): | ||
| id: int | ||
| name: str | ||
| age: int | ||
| bio: Optional[str] | ||
| is_active: bool |
18 changes: 18 additions & 0 deletions_examples/gen/sqlc/orm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.20.0 | ||
| from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | ||
| from typing import Optional | ||
| class Base(DeclarativeBase): | ||
| pass | ||
| class Author(Base): | ||
| __tablename__ = "authors" | ||
| id: Mapped[int] | ||
| name: Mapped[str] | ||
| age: Mapped[int] | ||
| bio: Mapped[Optional[str]] | ||
| is_active: Mapped[bool] |
198 changes: 198 additions & 0 deletions_examples/gen/sqlc/query.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.20.0 | ||
| # source: query.sql | ||
| from typing import AsyncIterator, Iterator, Optional | ||
| import sqlalchemy | ||
| import sqlalchemy.ext.asyncio | ||
| from . import models | ||
| CREATE_AUTHOR = """-- name: create_author \\:one | ||
| insert into authors ( | ||
| name, bio, age | ||
| ) values ( | ||
| :p1, :p2, :p3 | ||
| ) | ||
| returning id, name, age, bio, is_active | ||
| """ | ||
| DELETE_AUTHOR = """-- name: delete_author \\:exec | ||
| delete from authors | ||
| where id = :p1 | ||
| """ | ||
| GET_AUTHOR = """-- name: get_author \\:one | ||
| select id, name, age, bio, is_active from authors | ||
| where id = :p1 LIMIT 1 | ||
| """ | ||
| LIST_AUTHORS = """-- name: list_authors \\:many | ||
| select id, name, age, bio, is_active from authors | ||
| order by name | ||
| """ | ||
| LOCK_AUTHOR = """-- name: lock_author \\:one | ||
| select id, name, age, bio, is_active from authors | ||
| where id = :p1 LIMIT 1 FOR UPDATE NOWAIT | ||
| """ | ||
| UPDATE_AUTHOR = """-- name: update_author \\:one | ||
| update authors | ||
| set name = :p2, | ||
| bio = :p3, | ||
| age = :p4 | ||
| where id = :p1 | ||
| returning id, name, age, bio, is_active | ||
| """ | ||
| class Querier: | ||
| def __init__(self, conn: sqlalchemy.engine.Connection): | ||
| self._conn = conn | ||
| def create_author(self, *, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: | ||
| row = self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio, "p3": age}).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| def delete_author(self, *, id: int) -> None: | ||
| self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) | ||
| def get_author(self, *, id: int) -> Optional[models.Author]: | ||
| row = self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id}).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| def list_authors(self) -> Iterator[models.Author]: | ||
| result = self._conn.execute(sqlalchemy.text(LIST_AUTHORS)) | ||
| for row in result: | ||
| yield models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| def lock_author(self, *, id: int) -> Optional[models.Author]: | ||
| row = self._conn.execute(sqlalchemy.text(LOCK_AUTHOR), {"p1": id}).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| def update_author(self, *, id: int, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: | ||
| row = self._conn.execute(sqlalchemy.text(UPDATE_AUTHOR), { | ||
| "p1": id, | ||
| "p2": name, | ||
| "p3": bio, | ||
| "p4": age, | ||
| }).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| class AsyncQuerier: | ||
| def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): | ||
| self._conn = conn | ||
| async def create_author(self, *, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: | ||
| row = (await self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio, "p3": age})).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| async def delete_author(self, *, id: int) -> None: | ||
| await self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) | ||
| async def get_author(self, *, id: int) -> Optional[models.Author]: | ||
| row = (await self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id})).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| async def list_authors(self) -> AsyncIterator[models.Author]: | ||
| result = await self._conn.stream(sqlalchemy.text(LIST_AUTHORS)) | ||
| async for row in result: | ||
| yield models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| async def lock_author(self, *, id: int) -> Optional[models.Author]: | ||
| row = (await self._conn.execute(sqlalchemy.text(LOCK_AUTHOR), {"p1": id})).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) | ||
| async def update_author(self, *, id: int, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: | ||
| row = (await self._conn.execute(sqlalchemy.text(UPDATE_AUTHOR), { | ||
| "p1": id, | ||
| "p2": name, | ||
| "p3": bio, | ||
| "p4": age, | ||
| })).first() | ||
| if row is None: | ||
| return None | ||
| return models.Author( | ||
| id=row[0], | ||
| name=row[1], | ||
| age=row[2], | ||
| bio=row[3], | ||
| is_active=row[4], | ||
| ) |
31 changes: 31 additions & 0 deletions_examples/query.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| -- name: GetAuthor :one | ||
| select * from authors | ||
| where id = $1 LIMIT 1; | ||
| -- name: LockAuthor :one | ||
| select * from authors | ||
| where id = $1 LIMIT 1 FOR UPDATE NOWAIT; | ||
| -- name: ListAuthors :many | ||
| select * from authors | ||
| order by name; | ||
| -- name: CreateAuthor :one | ||
| insert into authors ( | ||
| name, bio, age | ||
| ) values ( | ||
| $1, $2, $3 | ||
| ) | ||
| returning *; | ||
| -- name: UpdateAuthor :one | ||
| update authors | ||
| set name = $2, | ||
| bio = $3, | ||
| age = $4 | ||
| where id = $1 | ||
| returning *; | ||
| -- name: DeleteAuthor :exec | ||
| delete from authors | ||
| where id = $1; |
7 changes: 7 additions & 0 deletions_examples/schema.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| create table authors ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| name text not null, | ||
| age int not null default 0, | ||
| bio text, | ||
| is_active boolean not null default true | ||
| ); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.