PyGreSQL 6.3.0b1__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pg/__init__.py +192 -0
- pg/_pg.cp315-win_amd64.pyd +0 -0
- pg/_pg.pyi +739 -0
- pg/adapt.py +1033 -0
- pg/attrs.py +29 -0
- pg/cast.py +544 -0
- pg/core.py +180 -0
- pg/db.py +1557 -0
- pg/error.py +59 -0
- pg/helpers.py +283 -0
- pg/notify.py +158 -0
- pg/py.typed +4 -0
- pg/tz.py +21 -0
- pgdb/__init__.py +153 -0
- pgdb/adapt.py +329 -0
- pgdb/cast.py +775 -0
- pgdb/connect.py +73 -0
- pgdb/connection.py +164 -0
- pgdb/constants.py +14 -0
- pgdb/cursor.py +861 -0
- pgdb/py.typed +1 -0
- pgdb/typecode.py +40 -0
- pygresql-6.3.0b1.dist-info/METADATA +81 -0
- pygresql-6.3.0b1.dist-info/RECORD +27 -0
- pygresql-6.3.0b1.dist-info/WHEEL +5 -0
- pygresql-6.3.0b1.dist-info/licenses/LICENSE.txt +30 -0
- pygresql-6.3.0b1.dist-info/top_level.txt +2 -0
pgdb/adapt.py
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"""Type helpers for adaptation of parameters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable, Iterable
|
|
6
|
+
from datetime import date, datetime, time, timedelta, tzinfo
|
|
7
|
+
from json import dumps as jsonencode
|
|
8
|
+
from re import compile as regex
|
|
9
|
+
from time import localtime
|
|
10
|
+
from typing import Any
|
|
11
|
+
from uuid import UUID as Uuid # noqa: N811
|
|
12
|
+
|
|
13
|
+
from pg.core import InterfaceError
|
|
14
|
+
from pg.helpers import is_array_type, join_text
|
|
15
|
+
|
|
16
|
+
from .typecode import TypeCode
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
'ARRAY',
|
|
20
|
+
'BINARY',
|
|
21
|
+
'BOOL',
|
|
22
|
+
'DATE',
|
|
23
|
+
'DATETIME',
|
|
24
|
+
'FLOAT',
|
|
25
|
+
'HSTORE',
|
|
26
|
+
'INTEGER',
|
|
27
|
+
'INTERVAL',
|
|
28
|
+
'JSON',
|
|
29
|
+
'LONG',
|
|
30
|
+
'MONEY',
|
|
31
|
+
'NUMBER',
|
|
32
|
+
'NUMERIC',
|
|
33
|
+
'RECORD',
|
|
34
|
+
'ROWID',
|
|
35
|
+
'SMALLINT',
|
|
36
|
+
'STRING',
|
|
37
|
+
'TIME',
|
|
38
|
+
'TIMESTAMP',
|
|
39
|
+
'UUID',
|
|
40
|
+
'ArrayType',
|
|
41
|
+
'Date',
|
|
42
|
+
'DateFromTicks',
|
|
43
|
+
'DbType',
|
|
44
|
+
'RecordType',
|
|
45
|
+
'Time',
|
|
46
|
+
'TimeFromTicks',
|
|
47
|
+
'Timestamp',
|
|
48
|
+
'TimestampFromTicks'
|
|
49
|
+
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _type_code_with_info(typ: str) -> TypeCode | None:
|
|
54
|
+
"""Get the type code if it carries type information.
|
|
55
|
+
|
|
56
|
+
Returns None for other strings and type codes without information.
|
|
57
|
+
"""
|
|
58
|
+
if not isinstance(typ, TypeCode) or not hasattr(typ, 'oid'):
|
|
59
|
+
return None
|
|
60
|
+
return typ
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _is_array_type_code(typ: TypeCode) -> bool:
|
|
64
|
+
"""Check whether the type code with type information is an array."""
|
|
65
|
+
if typ.elem:
|
|
66
|
+
return is_array_type(typ, typ.len, typ.elem)
|
|
67
|
+
# type code created without the element type, as before version 6.3
|
|
68
|
+
return typ.category == 'A' and typ.startswith('_')
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _type_name(typ: str) -> str | None:
|
|
72
|
+
"""Get the type name for comparing a type with a type object.
|
|
73
|
+
|
|
74
|
+
Arrays are compared like their element types. Type codes with type
|
|
75
|
+
information are compared by their names only if they are base or
|
|
76
|
+
pseudo types, arrays only if their elements are, since other types
|
|
77
|
+
can have the names of built-in types in other schemas.
|
|
78
|
+
"""
|
|
79
|
+
type_code = _type_code_with_info(typ)
|
|
80
|
+
if type_code is None:
|
|
81
|
+
return typ.removeprefix('_')
|
|
82
|
+
if _is_array_type_code(type_code):
|
|
83
|
+
elem_type = type_code._elem_type
|
|
84
|
+
if elem_type and elem_type not in ('b', 'p'):
|
|
85
|
+
return None
|
|
86
|
+
return type_code[1:]
|
|
87
|
+
if type_code.type not in ('b', 'p'):
|
|
88
|
+
return None
|
|
89
|
+
return type_code
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class DbType(frozenset):
|
|
93
|
+
"""Type class for a couple of PostgreSQL data types.
|
|
94
|
+
|
|
95
|
+
PostgreSQL is object-oriented: types are dynamic.
|
|
96
|
+
We must thus use type names as internal type codes.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
def __new__(cls, values: str | Iterable[str]) -> DbType:
|
|
100
|
+
"""Create new type object."""
|
|
101
|
+
if isinstance(values, str):
|
|
102
|
+
values = values.split()
|
|
103
|
+
return super().__new__(cls, values)
|
|
104
|
+
|
|
105
|
+
def __eq__(self, other: Any) -> bool:
|
|
106
|
+
"""Check whether types are considered equal."""
|
|
107
|
+
if isinstance(other, str):
|
|
108
|
+
name = _type_name(other)
|
|
109
|
+
return name is not None and name in self
|
|
110
|
+
return super().__eq__(other)
|
|
111
|
+
|
|
112
|
+
def __ne__(self, other: Any) -> bool:
|
|
113
|
+
"""Check whether types are not considered equal."""
|
|
114
|
+
if isinstance(other, str):
|
|
115
|
+
return not self.__eq__(other)
|
|
116
|
+
return super().__ne__(other)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class ArrayType:
|
|
120
|
+
"""Type class for PostgreSQL array types."""
|
|
121
|
+
|
|
122
|
+
def __eq__(self, other: Any) -> bool:
|
|
123
|
+
"""Check whether arrays are equal."""
|
|
124
|
+
if isinstance(other, str):
|
|
125
|
+
type_code = _type_code_with_info(other)
|
|
126
|
+
if type_code is None:
|
|
127
|
+
return other.startswith('_')
|
|
128
|
+
return _is_array_type_code(type_code)
|
|
129
|
+
return isinstance(other, ArrayType)
|
|
130
|
+
|
|
131
|
+
def __ne__(self, other: Any) -> bool:
|
|
132
|
+
"""Check whether arrays are different."""
|
|
133
|
+
if isinstance(other, str):
|
|
134
|
+
return not self.__eq__(other)
|
|
135
|
+
return not isinstance(other, ArrayType)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class RecordType:
|
|
139
|
+
"""Type class for PostgreSQL record types."""
|
|
140
|
+
|
|
141
|
+
def __eq__(self, other: Any) -> bool:
|
|
142
|
+
"""Check whether records are equal."""
|
|
143
|
+
if isinstance(other, str):
|
|
144
|
+
type_code = _type_code_with_info(other)
|
|
145
|
+
if type_code is None:
|
|
146
|
+
return other == 'record'
|
|
147
|
+
# composite types and the pseudo type for anonymous records
|
|
148
|
+
return type_code.type == 'c' or (
|
|
149
|
+
type_code.type == 'p' and type_code == 'record')
|
|
150
|
+
return isinstance(other, RecordType)
|
|
151
|
+
|
|
152
|
+
def __ne__(self, other: Any) -> bool:
|
|
153
|
+
"""Check whether records are different."""
|
|
154
|
+
if isinstance(other, str):
|
|
155
|
+
return not self.__eq__(other)
|
|
156
|
+
return not isinstance(other, RecordType)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# Mandatory type objects defined by DB-API 2 specs:
|
|
160
|
+
|
|
161
|
+
STRING = DbType('char bpchar name text varchar')
|
|
162
|
+
BINARY = DbType('bytea')
|
|
163
|
+
NUMBER = DbType('int2 int4 serial int8 float4 float8 numeric money')
|
|
164
|
+
DATETIME = DbType('date time timetz timestamp timestamptz interval'
|
|
165
|
+
' abstime reltime') # these are very old
|
|
166
|
+
ROWID = DbType('oid')
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# Additional type objects (more specific):
|
|
170
|
+
|
|
171
|
+
BOOL = DbType('bool')
|
|
172
|
+
SMALLINT = DbType('int2')
|
|
173
|
+
INTEGER = DbType('int2 int4 int8 serial')
|
|
174
|
+
LONG = DbType('int8')
|
|
175
|
+
FLOAT = DbType('float4 float8')
|
|
176
|
+
NUMERIC = DbType('numeric')
|
|
177
|
+
MONEY = DbType('money')
|
|
178
|
+
DATE = DbType('date')
|
|
179
|
+
TIME = DbType('time timetz')
|
|
180
|
+
TIMESTAMP = DbType('timestamp timestamptz')
|
|
181
|
+
INTERVAL = DbType('interval')
|
|
182
|
+
UUID = DbType('uuid')
|
|
183
|
+
HSTORE = DbType('hstore')
|
|
184
|
+
JSON = DbType('json jsonb')
|
|
185
|
+
|
|
186
|
+
# Type object for arrays (also equate to their base types):
|
|
187
|
+
|
|
188
|
+
ARRAY = ArrayType()
|
|
189
|
+
|
|
190
|
+
# Type object for records (encompassing all composite types):
|
|
191
|
+
|
|
192
|
+
RECORD = RecordType()
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
# Mandatory type helpers defined by DB-API 2 specs:
|
|
196
|
+
|
|
197
|
+
def Date(year: int, month: int, day: int) -> date: # noqa: N802
|
|
198
|
+
"""Construct an object holding a date value."""
|
|
199
|
+
return date(year, month, day)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def Time(hour: int, minute: int = 0, # noqa: N802
|
|
203
|
+
second: int = 0, microsecond: int = 0,
|
|
204
|
+
tzinfo: tzinfo | None = None) -> time:
|
|
205
|
+
"""Construct an object holding a time value."""
|
|
206
|
+
return time(hour, minute, second, microsecond, tzinfo)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def Timestamp(year: int, month: int, day: int, # noqa: N802
|
|
210
|
+
hour: int = 0, minute: int = 0,
|
|
211
|
+
second: int = 0, microsecond: int = 0,
|
|
212
|
+
tzinfo: tzinfo | None = None) -> datetime:
|
|
213
|
+
"""Construct an object holding a time stamp value."""
|
|
214
|
+
return datetime(year, month, day, hour, minute,
|
|
215
|
+
second, microsecond, tzinfo)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def DateFromTicks(ticks: float | None) -> date: # noqa: N802
|
|
219
|
+
"""Construct an object holding a date value from the given ticks value."""
|
|
220
|
+
return Date(*localtime(ticks)[:3])
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def TimeFromTicks(ticks: float | None) -> time: # noqa: N802
|
|
224
|
+
"""Construct an object holding a time value from the given ticks value."""
|
|
225
|
+
return Time(*localtime(ticks)[3:6])
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def TimestampFromTicks(ticks: float | None) -> datetime: # noqa: N802
|
|
229
|
+
"""Construct an object holding a time stamp from the given ticks value."""
|
|
230
|
+
return Timestamp(*localtime(ticks)[:6])
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class Binary(bytes):
|
|
234
|
+
"""Construct an object capable of holding a binary (long) string value."""
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
# Additional type helpers for PyGreSQL:
|
|
238
|
+
|
|
239
|
+
def Interval(days: int | float, # noqa: N802
|
|
240
|
+
hours: int | float = 0, minutes: int | float = 0,
|
|
241
|
+
seconds: int | float = 0, microseconds: int | float = 0
|
|
242
|
+
) -> timedelta:
|
|
243
|
+
"""Construct an object holding a time interval value."""
|
|
244
|
+
return timedelta(days, hours=hours, minutes=minutes,
|
|
245
|
+
seconds=seconds, microseconds=microseconds)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
Uuid = Uuid # Construct an object holding a UUID value
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class Hstore(dict):
|
|
252
|
+
"""Wrapper class for marking hstore values."""
|
|
253
|
+
|
|
254
|
+
_re_quote = regex(r'^[Nn][Uu][Ll][Ll]$|[\s,=>]')
|
|
255
|
+
_re_escape = regex(r'(["\\])')
|
|
256
|
+
_re_quote_bytes = regex(rb'^[Nn][Uu][Ll][Ll]$|[\s,=>]')
|
|
257
|
+
_re_escape_bytes = regex(rb'(["\\])')
|
|
258
|
+
|
|
259
|
+
@classmethod
|
|
260
|
+
def _quote(cls, s: Any) -> str | bytes:
|
|
261
|
+
if s is None:
|
|
262
|
+
return 'NULL'
|
|
263
|
+
if isinstance(s, bytes): # raw text in the client encoding
|
|
264
|
+
if not s:
|
|
265
|
+
return b'""'
|
|
266
|
+
quote_bytes = cls._re_quote_bytes.search(s)
|
|
267
|
+
s = cls._re_escape_bytes.sub(rb'\\\1', s)
|
|
268
|
+
return b'"' + s + b'"' if quote_bytes else s
|
|
269
|
+
if not isinstance(s, str):
|
|
270
|
+
if isinstance(s, (bytearray, memoryview)):
|
|
271
|
+
# cannot be adapted yet, like in other parameters
|
|
272
|
+
raise InterfaceError(
|
|
273
|
+
f'Do not know how to adapt type {type(s)}')
|
|
274
|
+
s = str(s)
|
|
275
|
+
if not s:
|
|
276
|
+
return '""'
|
|
277
|
+
quote = cls._re_quote.search(s)
|
|
278
|
+
s = cls._re_escape.sub(r'\\\1', s)
|
|
279
|
+
if quote:
|
|
280
|
+
s = f'"{s}"'
|
|
281
|
+
return s
|
|
282
|
+
|
|
283
|
+
def __str__(self) -> str:
|
|
284
|
+
"""Create a printable representation of the hstore value."""
|
|
285
|
+
value = self._literal(lambda: 'utf-8')
|
|
286
|
+
if isinstance(value, bytes): # show raw bytes with escapes
|
|
287
|
+
value = value.decode('utf-8', 'backslashreplace')
|
|
288
|
+
return value
|
|
289
|
+
|
|
290
|
+
def _literal(self, encoding: Callable[[], str]) -> str | bytes:
|
|
291
|
+
"""Create the hstore literal, as bytes if it contains bytes.
|
|
292
|
+
|
|
293
|
+
Keys and values given as bytes are raw text in the client encoding,
|
|
294
|
+
and the encoding function returns the codec for this encoding.
|
|
295
|
+
"""
|
|
296
|
+
q = self._quote
|
|
297
|
+
return join_text([join_text([q(k), q(v)], '=>', encoding)
|
|
298
|
+
for k, v in self.items()], ',', encoding)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
class Json:
|
|
302
|
+
"""Construct a wrapper for holding an object serializable to JSON."""
|
|
303
|
+
|
|
304
|
+
def __init__(self, obj: Any,
|
|
305
|
+
encode: Callable[[Any], str] | None = None) -> None:
|
|
306
|
+
"""Initialize the JSON object."""
|
|
307
|
+
self.obj = obj
|
|
308
|
+
self.encode = encode or jsonencode
|
|
309
|
+
|
|
310
|
+
def __str__(self) -> str:
|
|
311
|
+
"""Create a printable representation of the JSON object."""
|
|
312
|
+
obj = self.obj
|
|
313
|
+
if isinstance(obj, str):
|
|
314
|
+
return obj
|
|
315
|
+
return self.encode(obj)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
class Literal:
|
|
319
|
+
"""Construct a wrapper for holding a literal SQL string."""
|
|
320
|
+
|
|
321
|
+
def __init__(self, sql: str) -> None:
|
|
322
|
+
"""Initialize literal SQL string."""
|
|
323
|
+
self.sql = sql
|
|
324
|
+
|
|
325
|
+
def __str__(self) -> str:
|
|
326
|
+
"""Return a printable representation of the SQL string."""
|
|
327
|
+
return self.sql
|
|
328
|
+
|
|
329
|
+
__pg_repr__ = __str__
|