Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
# sqlc v1.28.0
# source: query.sql
import dataclasses
from typing import Optional

import sqlalchemy
import sqlalchemy.ext.asyncio

from querytest import models


COUNT_BARS = """-- name: count_bars \\:one
SELECT count(*) FROM bar
"""


@dataclasses.dataclass()
class CountBarsParams:
pass


DELETE_BAR_BY_ID = """-- name: delete_bar_by_id \\:execrows
DELETE FROM bar WHERE id = :p1
"""
Expand All @@ -35,6 +46,12 @@ class Querier:
def __init__(self, conn: sqlalchemy.engine.Connection):
self._conn = conn

def count_bars(self, arg: CountBarsParams) -> Optional[int]:
row = self._conn.execute(sqlalchemy.text(COUNT_BARS), {}).first()
if row is None:
return None
return row[0]

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
Expand All @@ -48,6 +65,12 @@ class AsyncQuerier:
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
self._conn = conn

async def count_bars(self, arg: CountBarsParams) -> Optional[int]:
row = (await self._conn.execute(sqlalchemy.text(COUNT_BARS), {})).first()
if row is None:
return None
return row[0]

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ DELETE FROM bar WHERE id = $1;

-- name: DeleteBarByIDAndName :execrows
DELETE FROM bar WHERE id = $1 AND name = $2;

-- name: CountBars :one
SELECT count(*) FROM bar;
5 changes: 5 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) {
w.print(")")
}
w.print(":\n")
if len(cd.Body) == 0 {
w.printIndent(indent + 1)
w.print("pass\n")
return
}
for i, node := range cd.Body {
if i != 0 {
if _, ok := node.Node.(*ast.Node_FunctionDef); ok {
Expand Down
28 changes: 27 additions & 1 deletion internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func TestPrinter(t *testing.T) {
},
},
},
Expected: `class Foo(str, enum.Enum):`,
Expected: `
class Foo(str, enum.Enum):
pass
`,
},
"dataclass": {
Node: &ast.Node{
Expand Down Expand Up @@ -125,6 +128,29 @@ func TestPrinter(t *testing.T) {
class Foo:
bar: int
bat: Optional[int]
`,
},
"empty-dataclass": {
Node: &ast.Node{
Node: &ast.Node_ClassDef{
ClassDef: &ast.ClassDef{
Name: "Foo",
DecoratorList: []*ast.Node{
{
Node: &ast.Node_Name{
Name: &ast.Name{
Id: "dataclass",
},
},
},
},
},
},
},
Expected: `
@dataclass
class Foo:
pass
`,
},
"call": {
Expand Down