-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathhalfvec.py
More file actions
27 lines (18 loc) · 887 Bytes
/
Copy pathhalfvec.py
File metadata and controls
27 lines (18 loc) · 887 Bytes
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
from psycopg2.extensions import adapt, connection, cursor, new_array_type, new_type, register_adapter, register_type
from .. import HalfVector
class HalfvecAdapter:
def __init__(self, value: HalfVector) -> None:
self._value = value
def getquoted(self) -> bytes:
return adapt(self._value.to_text()).getquoted()
def cast_halfvec(value: str | None, cur: cursor) -> HalfVector | None:
if value is None:
return None
return HalfVector.from_text(value)
def register_halfvec_info(oid: int, array_oid: int | None, scope: connection | cursor | None) -> None:
halfvec = new_type((oid,), 'HALFVEC', cast_halfvec)
register_type(halfvec, scope)
if array_oid is not None:
halfvecarray = new_array_type((array_oid,), 'HALFVECARRAY', halfvec)
register_type(halfvecarray, scope)
register_adapter(HalfVector, HalfvecAdapter)