-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathquery.py
More file actions
68 lines (54 loc) · 1.87 KB
/
query.py
File metadata and controls
68 lines (54 loc) · 1.87 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
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.30.0
# source: query.sql
import numpy
from numpy.typing import NDArray
from typing import Optional
import sqlalchemy
import sqlalchemy.ext.asyncio
from python import models
CREATE_ITEM = """-- name: create_item \\:one
INSERT INTO items (embedding) VALUES (:p1) RETURNING id, embedding
"""
GET_ITEM = """-- name: get_item \\:one
SELECT id, embedding FROM items WHERE id = :p1
"""
class Querier:
def __init__(self, conn: sqlalchemy.engine.Connection):
self._conn = conn
def create_item(self, *, embedding: Optional[NDArray[numpy.float32]]) -> Optional[models.Item]:
row = self._conn.execute(sqlalchemy.text(CREATE_ITEM), {"p1": embedding}).first()
if row is None:
return None
return models.Item(
id=row[0],
embedding=row[1],
)
def get_item(self, *, id: int) -> Optional[models.Item]:
row = self._conn.execute(sqlalchemy.text(GET_ITEM), {"p1": id}).first()
if row is None:
return None
return models.Item(
id=row[0],
embedding=row[1],
)
class AsyncQuerier:
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
self._conn = conn
async def create_item(self, *, embedding: Optional[NDArray[numpy.float32]]) -> Optional[models.Item]:
row = (await self._conn.execute(sqlalchemy.text(CREATE_ITEM), {"p1": embedding})).first()
if row is None:
return None
return models.Item(
id=row[0],
embedding=row[1],
)
async def get_item(self, *, id: int) -> Optional[models.Item]:
row = (await self._conn.execute(sqlalchemy.text(GET_ITEM), {"p1": id})).first()
if row is None:
return None
return models.Item(
id=row[0],
embedding=row[1],
)