-
Notifications
You must be signed in to change notification settings - Fork 726
/
query.py
211 lines (176 loc) · 5.35 KB
/
query.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.12.0
# source: query.sql
import dataclasses
import datetime
from typing import AsyncIterator, List, Optional
import sqlalchemy
import sqlalchemy.ext.asyncio
from booktest import models
BOOKS_BY_TAGS = """-- name: books_by_tags \\:many
SELECT
book_id,
title,
name,
isbn,
tags
FROM books
LEFT JOIN authors ON books.author_id = authors.author_id
WHERE tags && :p1\\:\\:varchar[]
"""
@dataclasses.dataclass()
class BooksByTagsRow:
book_id: int
title: str
name: Optional[str]
isbn: str
tags: List[str]
BOOKS_BY_TITLE_YEAR = """-- name: books_by_title_year \\:many
SELECT book_id, author_id, isbn, book_type, title, year, available, tags FROM books
WHERE title = :p1 AND year = :p2
"""
CREATE_AUTHOR = """-- name: create_author \\:one
INSERT INTO authors (name) VALUES (:p1)
RETURNING author_id, name
"""
CREATE_BOOK = """-- name: create_book \\:one
INSERT INTO books (
author_id,
isbn,
book_type,
title,
year,
available,
tags
) VALUES (
:p1,
:p2,
:p3,
:p4,
:p5,
:p6,
:p7
)
RETURNING book_id, author_id, isbn, book_type, title, year, available, tags
"""
@dataclasses.dataclass()
class CreateBookParams:
author_id: int
isbn: str
book_type: models.BookType
title: str
year: int
available: datetime.datetime
tags: List[str]
DELETE_BOOK = """-- name: delete_book \\:exec
DELETE FROM books
WHERE book_id = :p1
"""
GET_AUTHOR = """-- name: get_author \\:one
SELECT author_id, name FROM authors
WHERE author_id = :p1
"""
GET_BOOK = """-- name: get_book \\:one
SELECT book_id, author_id, isbn, book_type, title, year, available, tags FROM books
WHERE book_id = :p1
"""
UPDATE_BOOK = """-- name: update_book \\:exec
UPDATE books
SET title = :p1, tags = :p2
WHERE book_id = :p3
"""
UPDATE_BOOK_ISBN = """-- name: update_book_isbn \\:exec
UPDATE books
SET title = :p1, tags = :p2, isbn = :p4
WHERE book_id = :p3
"""
class AsyncQuerier:
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
self._conn = conn
async def books_by_tags(self, *, dollar_1: List[str]) -> AsyncIterator[BooksByTagsRow]:
result = await self._conn.stream(sqlalchemy.text(BOOKS_BY_TAGS), {"p1": dollar_1})
async for row in result:
yield BooksByTagsRow(
book_id=row[0],
title=row[1],
name=row[2],
isbn=row[3],
tags=row[4],
)
async def books_by_title_year(self, *, title: str, year: int) -> AsyncIterator[models.Book]:
result = await self._conn.stream(sqlalchemy.text(BOOKS_BY_TITLE_YEAR), {"p1": title, "p2": year})
async for row in result:
yield models.Book(
book_id=row[0],
author_id=row[1],
isbn=row[2],
book_type=row[3],
title=row[4],
year=row[5],
available=row[6],
tags=row[7],
)
async def create_author(self, *, name: str) -> Optional[models.Author]:
row = (await self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name})).first()
if row is None:
return None
return models.Author(
author_id=row[0],
name=row[1],
)
async def create_book(self, arg: CreateBookParams) -> Optional[models.Book]:
row = (await self._conn.execute(sqlalchemy.text(CREATE_BOOK), {
"p1": arg.author_id,
"p2": arg.isbn,
"p3": arg.book_type,
"p4": arg.title,
"p5": arg.year,
"p6": arg.available,
"p7": arg.tags,
})).first()
if row is None:
return None
return models.Book(
book_id=row[0],
author_id=row[1],
isbn=row[2],
book_type=row[3],
title=row[4],
year=row[5],
available=row[6],
tags=row[7],
)
async def delete_book(self, *, book_id: int) -> None:
await self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": book_id})
async def get_author(self, *, author_id: int) -> Optional[models.Author]:
row = (await self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": author_id})).first()
if row is None:
return None
return models.Author(
author_id=row[0],
name=row[1],
)
async def get_book(self, *, book_id: int) -> Optional[models.Book]:
row = (await self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": book_id})).first()
if row is None:
return None
return models.Book(
book_id=row[0],
author_id=row[1],
isbn=row[2],
book_type=row[3],
title=row[4],
year=row[5],
available=row[6],
tags=row[7],
)
async def update_book(self, *, title: str, tags: List[str], book_id: int) -> None:
await self._conn.execute(sqlalchemy.text(UPDATE_BOOK), {"p1": title, "p2": tags, "p3": book_id})
async def update_book_isbn(self, *, title: str, tags: List[str], book_id: int, isbn: str) -> None:
await self._conn.execute(sqlalchemy.text(UPDATE_BOOK_ISBN), {
"p1": title,
"p2": tags,
"p3": book_id,
"p4": isbn,
})