-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathquery.py
More file actions
80 lines (56 loc) · 2.17 KB
/
query.py
File metadata and controls
80 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Code generated by sqlc. DO NOT EDIT.
# versions:
# 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
"""
@dataclasses.dataclass()
class DeleteBarByIDParams:
id: int
DELETE_BAR_BY_ID_AND_NAME = """-- name: delete_bar_by_id_and_name \\:execrows
DELETE FROM bar WHERE id = :p1 AND name = :p2
"""
@dataclasses.dataclass()
class DeleteBarByIDAndNameParams:
id: int
name: str
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
def delete_bar_by_id_and_name(self, arg: DeleteBarByIDAndNameParams) -> int:
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {"p1": arg.id, "p2": arg.name})
return result.rowcount
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
async def delete_bar_by_id_and_name(self, arg: DeleteBarByIDAndNameParams) -> int:
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID_AND_NAME), {"p1": arg.id, "p2": arg.name})
return result.rowcount