rapidquery 0.1.0a1__cp313-cp313-musllinux_1_2_aarch64.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.
- rapidquery/__init__.py +99 -0
- rapidquery/_lib.cpython-313-aarch64-linux-musl.so +0 -0
- rapidquery/_lib.pyi +3559 -0
- rapidquery-0.1.0a1.dist-info/METADATA +877 -0
- rapidquery-0.1.0a1.dist-info/RECORD +8 -0
- rapidquery-0.1.0a1.dist-info/WHEEL +4 -0
- rapidquery-0.1.0a1.dist-info/licenses/LICENSE +674 -0
- rapidquery.libs/libgcc_s-39080030.so.1 +0 -0
rapidquery/_lib.pyi
ADDED
|
@@ -0,0 +1,3559 @@
|
|
|
1
|
+
from typing_extensions import Self
|
|
2
|
+
import datetime
|
|
3
|
+
import decimal
|
|
4
|
+
import typing
|
|
5
|
+
import uuid
|
|
6
|
+
|
|
7
|
+
_Backends = typing.Literal["sqlite", "mysql", "postgresql", "postgres"]
|
|
8
|
+
|
|
9
|
+
class _AsteriskType:
|
|
10
|
+
"""
|
|
11
|
+
Asterisk `"*"` - very useful for expression creating
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
...
|
|
15
|
+
|
|
16
|
+
ASTERISK: typing.Final[_AsteriskType]
|
|
17
|
+
|
|
18
|
+
class SchemaStatement:
|
|
19
|
+
def to_sql(self, backend: _Backends) -> str:
|
|
20
|
+
"""
|
|
21
|
+
Build a SQL string representation.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
backend: The database backend that determines SQL dialect and formatting
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
A SQL string representation of the expression
|
|
28
|
+
"""
|
|
29
|
+
...
|
|
30
|
+
|
|
31
|
+
class QueryStatement:
|
|
32
|
+
def build(self, backend: _Backends) -> typing.Tuple[str, typing.Tuple[AdaptedValue, ...]]:
|
|
33
|
+
"""
|
|
34
|
+
Build the SQL statement with parameter values.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
backend: The database backend that determines SQL dialect
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
A tuple of (SQL string, parameter values)
|
|
41
|
+
"""
|
|
42
|
+
...
|
|
43
|
+
|
|
44
|
+
def to_sql(self, backend: _Backends) -> str:
|
|
45
|
+
"""
|
|
46
|
+
Build a SQL string representation.
|
|
47
|
+
|
|
48
|
+
**This method is unsafe and can cause SQL injection.** use `.build()` method instead.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
backend: The database backend that determines SQL dialect and formatting
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
A SQL string representation of the expression
|
|
55
|
+
"""
|
|
56
|
+
...
|
|
57
|
+
|
|
58
|
+
T = typing.TypeVar("T")
|
|
59
|
+
|
|
60
|
+
class ColumnTypeMeta(typing.Generic[T]):
|
|
61
|
+
"""
|
|
62
|
+
Base class for all SQL column data types.
|
|
63
|
+
|
|
64
|
+
This abstract base class represents SQL data types that can be used in
|
|
65
|
+
column definitions. Each subclass implements a specific SQL data type
|
|
66
|
+
with its particular characteristics, constraints, and backend-specific
|
|
67
|
+
representations.
|
|
68
|
+
"""
|
|
69
|
+
def __new__(cls) -> Self: ...
|
|
70
|
+
def __eq__(self, other) -> bool: ...
|
|
71
|
+
def __ne__(self, other) -> bool: ...
|
|
72
|
+
def __repr__(self) -> str: ...
|
|
73
|
+
|
|
74
|
+
class _LengthColumnType(ColumnTypeMeta[T]):
|
|
75
|
+
"""
|
|
76
|
+
Base class for column types that have a length parameter.
|
|
77
|
+
|
|
78
|
+
This is an internal base class for column types like CHAR, VARCHAR,
|
|
79
|
+
BINARY, and VARBINARY that specify a maximum length constraint.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
length: typing.Optional[int]
|
|
83
|
+
"""The maximum length constraint for this column type."""
|
|
84
|
+
|
|
85
|
+
def __new__(cls, length: typing.Optional[int] = ...) -> Self: ...
|
|
86
|
+
|
|
87
|
+
class _PrecisionScaleColumnType(ColumnTypeMeta[T]):
|
|
88
|
+
"""
|
|
89
|
+
Base class for numeric column types with precision and scale parameters.
|
|
90
|
+
|
|
91
|
+
This is an internal base class for numeric types like DECIMAL and NUMERIC
|
|
92
|
+
that require both precision (total digits) and scale (decimal places) specification.
|
|
93
|
+
"""
|
|
94
|
+
def __new__(cls, precision_scale: typing.Optional[typing.Tuple[int, int]] = ...) -> Self: ...
|
|
95
|
+
@property
|
|
96
|
+
def precision_scale(self) -> typing.Optional[typing.Tuple[int, int]]:
|
|
97
|
+
"""The total number of significant digits."""
|
|
98
|
+
...
|
|
99
|
+
|
|
100
|
+
class CharType(_LengthColumnType[str]):
|
|
101
|
+
"""
|
|
102
|
+
Fixed-length character string column type (CHAR).
|
|
103
|
+
|
|
104
|
+
Represents a fixed-length character string. Values shorter than the
|
|
105
|
+
specified length are padded with spaces. Suitable for storing data
|
|
106
|
+
with consistent, known lengths like country codes or status flags.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
...
|
|
110
|
+
|
|
111
|
+
class StringType(_LengthColumnType[str]):
|
|
112
|
+
"""
|
|
113
|
+
Variable-length character string column type (VARCHAR).
|
|
114
|
+
|
|
115
|
+
Represents a variable-length character string with a maximum length limit.
|
|
116
|
+
This is the most common string type for storing text data of varying lengths
|
|
117
|
+
like names, descriptions, or user input.
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
...
|
|
121
|
+
|
|
122
|
+
class TextType(ColumnTypeMeta[str]):
|
|
123
|
+
"""
|
|
124
|
+
Large text column type (TEXT).
|
|
125
|
+
|
|
126
|
+
Represents a large text field capable of storing long strings without
|
|
127
|
+
a predefined length limit. Suitable for storing articles, comments,
|
|
128
|
+
descriptions, or any text content that may be very long.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
...
|
|
132
|
+
|
|
133
|
+
class TinyIntegerType(ColumnTypeMeta[int]):
|
|
134
|
+
"""
|
|
135
|
+
Very small integer column type (TINYINT).
|
|
136
|
+
|
|
137
|
+
Typically stores integers in the range -128 to 127 (signed) or 0 to 255
|
|
138
|
+
(unsigned). Useful for flags, small counters, or enumerated values.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
...
|
|
142
|
+
|
|
143
|
+
class SmallIntegerType(ColumnTypeMeta[int]):
|
|
144
|
+
"""
|
|
145
|
+
Small integer column type (SMALLINT).
|
|
146
|
+
|
|
147
|
+
Typically stores integers in the range -32,768 to 32,767 (signed) or
|
|
148
|
+
0 to 65,535 (unsigned). Good for moderate-sized counters or numeric codes.
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
...
|
|
152
|
+
|
|
153
|
+
class IntegerType(ColumnTypeMeta[int]):
|
|
154
|
+
"""
|
|
155
|
+
Standard integer column type (INTEGER/INT).
|
|
156
|
+
|
|
157
|
+
The most common integer type, typically storing 32-bit integers in the
|
|
158
|
+
range -2,147,483,648 to 2,147,483,647 (signed). Suitable for most
|
|
159
|
+
numeric data including IDs, quantities, and counters.
|
|
160
|
+
"""
|
|
161
|
+
|
|
162
|
+
...
|
|
163
|
+
|
|
164
|
+
class BigIntegerType(ColumnTypeMeta[int]):
|
|
165
|
+
"""
|
|
166
|
+
Large integer column type (BIGINT).
|
|
167
|
+
|
|
168
|
+
Stores 64-bit integers for very large numeric values. Essential for
|
|
169
|
+
high-volume systems, timestamps, large counters, or when integer
|
|
170
|
+
overflow is a concern.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
...
|
|
174
|
+
|
|
175
|
+
class TinyUnsignedType(ColumnTypeMeta[int]):
|
|
176
|
+
"""
|
|
177
|
+
Unsigned tiny integer column type.
|
|
178
|
+
|
|
179
|
+
Stores small positive integers only, typically 0 to 255. Useful for
|
|
180
|
+
small counters, percentages, or enumerated values that are always positive.
|
|
181
|
+
"""
|
|
182
|
+
|
|
183
|
+
...
|
|
184
|
+
|
|
185
|
+
class SmallUnsignedType(ColumnTypeMeta[int]):
|
|
186
|
+
"""
|
|
187
|
+
Unsigned small integer column type.
|
|
188
|
+
|
|
189
|
+
Stores moderate positive integers only, typically 0 to 65,535. Good for
|
|
190
|
+
larger counters or numeric codes that are always positive.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
...
|
|
194
|
+
|
|
195
|
+
class UnsignedType(ColumnTypeMeta[int]):
|
|
196
|
+
"""
|
|
197
|
+
Unsigned integer column type.
|
|
198
|
+
|
|
199
|
+
Stores positive integers only, typically 0 to 4,294,967,295. Doubles the
|
|
200
|
+
positive range compared to signed integers, useful for IDs and counters
|
|
201
|
+
that will never be negative.
|
|
202
|
+
"""
|
|
203
|
+
|
|
204
|
+
...
|
|
205
|
+
|
|
206
|
+
class BigUnsignedType(ColumnTypeMeta[int]):
|
|
207
|
+
"""
|
|
208
|
+
Unsigned big integer column type.
|
|
209
|
+
|
|
210
|
+
Stores very large positive integers only. Provides the maximum positive
|
|
211
|
+
integer range for high-volume systems or when very large positive
|
|
212
|
+
values are required.
|
|
213
|
+
"""
|
|
214
|
+
|
|
215
|
+
...
|
|
216
|
+
|
|
217
|
+
class FloatType(ColumnTypeMeta[float]):
|
|
218
|
+
"""
|
|
219
|
+
Single-precision floating point column type (FLOAT).
|
|
220
|
+
|
|
221
|
+
Stores approximate numeric values with single precision. Suitable for
|
|
222
|
+
scientific calculations, measurements, or any numeric data where some
|
|
223
|
+
precision loss is acceptable in exchange for storage efficiency.
|
|
224
|
+
"""
|
|
225
|
+
|
|
226
|
+
...
|
|
227
|
+
|
|
228
|
+
class DoubleType(ColumnTypeMeta[float]):
|
|
229
|
+
"""
|
|
230
|
+
Double-precision floating point column type (DOUBLE).
|
|
231
|
+
|
|
232
|
+
Stores approximate numeric values with double precision. Provides higher
|
|
233
|
+
precision than FLOAT for scientific calculations or when more accuracy
|
|
234
|
+
is required in floating-point operations.
|
|
235
|
+
"""
|
|
236
|
+
|
|
237
|
+
...
|
|
238
|
+
|
|
239
|
+
class DecimalType(_PrecisionScaleColumnType[decimal.Decimal]):
|
|
240
|
+
"""
|
|
241
|
+
Exact numeric decimal column type (DECIMAL/NUMERIC).
|
|
242
|
+
|
|
243
|
+
Stores exact numeric values with fixed precision and scale. Essential for
|
|
244
|
+
financial calculations, currency values, or any situation where exact
|
|
245
|
+
decimal representation is required without floating-point approximation.
|
|
246
|
+
"""
|
|
247
|
+
|
|
248
|
+
...
|
|
249
|
+
|
|
250
|
+
class DateTimeType(ColumnTypeMeta[datetime.datetime]):
|
|
251
|
+
"""
|
|
252
|
+
Date and time column type (DATETIME).
|
|
253
|
+
|
|
254
|
+
Stores both date and time information without timezone awareness.
|
|
255
|
+
Suitable for recording timestamps, event times, or scheduling information
|
|
256
|
+
when timezone handling is managed at the application level.
|
|
257
|
+
"""
|
|
258
|
+
|
|
259
|
+
...
|
|
260
|
+
|
|
261
|
+
class TimestampType(ColumnTypeMeta[datetime.datetime]):
|
|
262
|
+
"""
|
|
263
|
+
Timestamp column type (TIMESTAMP).
|
|
264
|
+
|
|
265
|
+
Stores timestamp values, often with automatic update capabilities.
|
|
266
|
+
Behavior varies by database system - may include timezone handling
|
|
267
|
+
or automatic updates on record changes.
|
|
268
|
+
"""
|
|
269
|
+
|
|
270
|
+
...
|
|
271
|
+
|
|
272
|
+
class TimestampWithTimeZoneType(ColumnTypeMeta[datetime.datetime]):
|
|
273
|
+
"""
|
|
274
|
+
Timestamp with timezone column type (TIMESTAMPTZ).
|
|
275
|
+
|
|
276
|
+
Stores timestamp values with timezone information. Essential for
|
|
277
|
+
applications that need to handle dates and times across different
|
|
278
|
+
timezones accurately.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
...
|
|
282
|
+
|
|
283
|
+
class TimeType(ColumnTypeMeta[datetime.time]):
|
|
284
|
+
"""
|
|
285
|
+
Time-only column type (TIME).
|
|
286
|
+
|
|
287
|
+
Stores time information without date component. Useful for storing
|
|
288
|
+
daily schedules, opening hours, or any time-based data that repeats
|
|
289
|
+
daily regardless of the specific date.
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
...
|
|
293
|
+
|
|
294
|
+
class DateType(ColumnTypeMeta[datetime.date]):
|
|
295
|
+
"""
|
|
296
|
+
Date-only column type (DATE).
|
|
297
|
+
|
|
298
|
+
Stores date information without time component. Ideal for birth dates,
|
|
299
|
+
deadlines, or any date-based data where time precision is not needed.
|
|
300
|
+
"""
|
|
301
|
+
|
|
302
|
+
...
|
|
303
|
+
|
|
304
|
+
class YearType(ColumnTypeMeta[int]):
|
|
305
|
+
"""
|
|
306
|
+
Year-only column type (YEAR).
|
|
307
|
+
|
|
308
|
+
Stores year values efficiently. Useful for storing birth years,
|
|
309
|
+
academic years, or any year-based categorical data where full
|
|
310
|
+
date precision is unnecessary.
|
|
311
|
+
"""
|
|
312
|
+
|
|
313
|
+
...
|
|
314
|
+
|
|
315
|
+
class BlobType(ColumnTypeMeta[bytes]):
|
|
316
|
+
"""
|
|
317
|
+
Binary large object column type (BLOB).
|
|
318
|
+
|
|
319
|
+
Stores large binary data such as images, documents, audio files, or
|
|
320
|
+
any binary content. Size limits vary by database system.
|
|
321
|
+
"""
|
|
322
|
+
|
|
323
|
+
...
|
|
324
|
+
|
|
325
|
+
class BinaryType(_LengthColumnType[bytes]):
|
|
326
|
+
"""
|
|
327
|
+
Fixed-length binary data column type (BINARY).
|
|
328
|
+
|
|
329
|
+
Stores binary data of a fixed length. Values shorter than the specified
|
|
330
|
+
length are padded. Useful for storing hashes, keys, or other binary
|
|
331
|
+
data with consistent length.
|
|
332
|
+
"""
|
|
333
|
+
|
|
334
|
+
...
|
|
335
|
+
|
|
336
|
+
class VarBinaryType(_LengthColumnType[bytes]):
|
|
337
|
+
"""
|
|
338
|
+
Variable-length binary data column type (VARBINARY).
|
|
339
|
+
|
|
340
|
+
Stores binary data of variable length up to a specified maximum.
|
|
341
|
+
More storage-efficient than BINARY for binary data of varying lengths.
|
|
342
|
+
"""
|
|
343
|
+
|
|
344
|
+
...
|
|
345
|
+
|
|
346
|
+
class BitType(_LengthColumnType[bytes]):
|
|
347
|
+
"""
|
|
348
|
+
Fixed-length bit string column type (BIT).
|
|
349
|
+
|
|
350
|
+
Stores a fixed number of bits. Useful for storing boolean flags efficiently
|
|
351
|
+
or binary data where individual bits have meaning.
|
|
352
|
+
"""
|
|
353
|
+
|
|
354
|
+
...
|
|
355
|
+
|
|
356
|
+
class VarBitType(_LengthColumnType[bytes]):
|
|
357
|
+
"""
|
|
358
|
+
Variable-length bit string column type (VARBIT).
|
|
359
|
+
|
|
360
|
+
Stores a variable number of bits up to a specified maximum. More flexible
|
|
361
|
+
than fixed BIT type for bit strings of varying lengths.
|
|
362
|
+
"""
|
|
363
|
+
|
|
364
|
+
...
|
|
365
|
+
|
|
366
|
+
class BooleanType(ColumnTypeMeta[bool]):
|
|
367
|
+
"""
|
|
368
|
+
Boolean column type (BOOLEAN).
|
|
369
|
+
|
|
370
|
+
Stores true/false values. The standard way to store boolean data,
|
|
371
|
+
though implementation varies by database (some use TINYINT(1) or
|
|
372
|
+
similar representations).
|
|
373
|
+
"""
|
|
374
|
+
|
|
375
|
+
...
|
|
376
|
+
|
|
377
|
+
class MoneyType(_PrecisionScaleColumnType[decimal.Decimal]):
|
|
378
|
+
"""
|
|
379
|
+
Money/currency column type (MONEY).
|
|
380
|
+
|
|
381
|
+
Specialized numeric type for storing monetary values with fixed precision.
|
|
382
|
+
Optimized for currency calculations and formatting, though DECIMAL is
|
|
383
|
+
often preferred for financial applications.
|
|
384
|
+
"""
|
|
385
|
+
|
|
386
|
+
...
|
|
387
|
+
|
|
388
|
+
class JsonType(ColumnTypeMeta[typing.Any]):
|
|
389
|
+
"""
|
|
390
|
+
JSON data column type (JSON).
|
|
391
|
+
|
|
392
|
+
Stores JSON documents with validation and indexing capabilities.
|
|
393
|
+
Allows for flexible schema design and complex nested data structures
|
|
394
|
+
while maintaining some query capabilities.
|
|
395
|
+
"""
|
|
396
|
+
|
|
397
|
+
...
|
|
398
|
+
|
|
399
|
+
class JsonBinaryType(ColumnTypeMeta[typing.Any]):
|
|
400
|
+
"""
|
|
401
|
+
Binary JSON column type (JSONB).
|
|
402
|
+
|
|
403
|
+
Stores JSON documents in a binary format for improved performance.
|
|
404
|
+
Provides faster query and manipulation operations compared to text-based
|
|
405
|
+
JSON storage, with additional indexing capabilities.
|
|
406
|
+
"""
|
|
407
|
+
|
|
408
|
+
...
|
|
409
|
+
|
|
410
|
+
class UuidType(ColumnTypeMeta[uuid.UUID]):
|
|
411
|
+
"""
|
|
412
|
+
UUID column type (UUID).
|
|
413
|
+
|
|
414
|
+
Stores universally unique identifiers. Ideal for distributed systems,
|
|
415
|
+
primary keys, or any situation where globally unique identifiers are
|
|
416
|
+
needed without central coordination.
|
|
417
|
+
"""
|
|
418
|
+
|
|
419
|
+
...
|
|
420
|
+
|
|
421
|
+
class VectorType(_LengthColumnType[list]):
|
|
422
|
+
"""
|
|
423
|
+
Vector column type for storing mathematical vectors.
|
|
424
|
+
|
|
425
|
+
Specialized type for storing vector data, often used in machine learning,
|
|
426
|
+
similarity search, or mathematical applications. The length parameter
|
|
427
|
+
typically specifies the vector dimension.
|
|
428
|
+
"""
|
|
429
|
+
|
|
430
|
+
...
|
|
431
|
+
|
|
432
|
+
class CidrType(ColumnTypeMeta[str]):
|
|
433
|
+
"""
|
|
434
|
+
CIDR network address column type (CIDR).
|
|
435
|
+
|
|
436
|
+
Stores IPv4 or IPv6 network addresses in CIDR notation (e.g., 192.168.1.0/24).
|
|
437
|
+
Useful for network configuration, IP address management, and routing tables.
|
|
438
|
+
"""
|
|
439
|
+
|
|
440
|
+
...
|
|
441
|
+
|
|
442
|
+
class InetType(ColumnTypeMeta[str]):
|
|
443
|
+
"""
|
|
444
|
+
Internet address column type (INET).
|
|
445
|
+
|
|
446
|
+
Stores IPv4 or IPv6 addresses, with or without subnet specification.
|
|
447
|
+
More flexible than CIDR type, allowing both host addresses and network ranges.
|
|
448
|
+
"""
|
|
449
|
+
|
|
450
|
+
...
|
|
451
|
+
|
|
452
|
+
class MacAddressType(ColumnTypeMeta[str]):
|
|
453
|
+
"""
|
|
454
|
+
MAC address column type (MACADDR).
|
|
455
|
+
|
|
456
|
+
Stores MAC (Media Access Control) addresses for network devices.
|
|
457
|
+
Provides validation and formatting for 6-byte MAC addresses.
|
|
458
|
+
"""
|
|
459
|
+
|
|
460
|
+
...
|
|
461
|
+
|
|
462
|
+
class LTreeType(ColumnTypeMeta[str]):
|
|
463
|
+
"""
|
|
464
|
+
Label tree column type (LTREE).
|
|
465
|
+
|
|
466
|
+
Stores hierarchical tree-like structures as paths. Useful for representing
|
|
467
|
+
organizational hierarchies, category trees, or any nested data structure
|
|
468
|
+
that needs efficient tree operations.
|
|
469
|
+
"""
|
|
470
|
+
|
|
471
|
+
...
|
|
472
|
+
|
|
473
|
+
INTERVAL_YEAR: typing.Final[int]
|
|
474
|
+
INTERVAL_MONTH: typing.Final[int]
|
|
475
|
+
INTERVAL_DAY: typing.Final[int]
|
|
476
|
+
INTERVAL_HOUR: typing.Final[int]
|
|
477
|
+
INTERVAL_MINUTE: typing.Final[int]
|
|
478
|
+
INTERVAL_SECOND: typing.Final[int]
|
|
479
|
+
INTERVAL_YEAR_TO_MONTH: typing.Final[int]
|
|
480
|
+
INTERVAL_DAY_TO_HOUR: typing.Final[int]
|
|
481
|
+
INTERVAL_DAY_TO_MINUTE: typing.Final[int]
|
|
482
|
+
INTERVAL_DAY_TO_SECOND: typing.Final[int]
|
|
483
|
+
INTERVAL_HOUR_TO_MINUTE: typing.Final[int]
|
|
484
|
+
INTERVAL_HOUR_TO_SECOND: typing.Final[int]
|
|
485
|
+
INTERVAL_MINUTE_TO_SECOND: typing.Final[int]
|
|
486
|
+
|
|
487
|
+
class IntervalType(ColumnTypeMeta[datetime.timedelta]):
|
|
488
|
+
"""
|
|
489
|
+
Time interval column type (INTERVAL).
|
|
490
|
+
|
|
491
|
+
Stores time intervals (durations) with configurable precision and field
|
|
492
|
+
restrictions. Can store periods like "3 days", "2 hours 30 minutes", etc.
|
|
493
|
+
|
|
494
|
+
The fields parameter constrains which time units are stored (using
|
|
495
|
+
PGINTERVAL_* constants), and precision controls fractional seconds.
|
|
496
|
+
"""
|
|
497
|
+
|
|
498
|
+
fields: typing.Optional[int]
|
|
499
|
+
"""Bitmask specifying which interval fields to include (using PGINTERVAL_* constants)."""
|
|
500
|
+
|
|
501
|
+
precision: typing.Optional[int]
|
|
502
|
+
"""Number of fractional digits for seconds."""
|
|
503
|
+
|
|
504
|
+
def __new__(
|
|
505
|
+
cls, fields: typing.Optional[int] = ..., precision: typing.Optional[int] = ...
|
|
506
|
+
) -> Self: ...
|
|
507
|
+
|
|
508
|
+
class EnumType(ColumnTypeMeta[str]):
|
|
509
|
+
"""
|
|
510
|
+
Enumeration column type (ENUM).
|
|
511
|
+
|
|
512
|
+
Stores one value from a predefined set of allowed string values.
|
|
513
|
+
Provides type safety and storage efficiency for categorical data
|
|
514
|
+
with a fixed set of possible values.
|
|
515
|
+
|
|
516
|
+
Examples:
|
|
517
|
+
|
|
518
|
+
Enum("status", ["active", "inactive", "pending"])
|
|
519
|
+
Enum("priority", ["low", "medium", "high", "critical"])
|
|
520
|
+
"""
|
|
521
|
+
|
|
522
|
+
name: str
|
|
523
|
+
"""The name of the enumeration type."""
|
|
524
|
+
|
|
525
|
+
variants: typing.Sequence[str]
|
|
526
|
+
"""The allowed values for this enumeration."""
|
|
527
|
+
|
|
528
|
+
def __new__(cls, name: str, variants: typing.Sequence[str]) -> Self: ...
|
|
529
|
+
|
|
530
|
+
class ArrayType(ColumnTypeMeta[list]):
|
|
531
|
+
"""
|
|
532
|
+
Array column type for storing arrays of elements.
|
|
533
|
+
|
|
534
|
+
Represents a column that stores arrays of a specified element type.
|
|
535
|
+
Useful in databases that support native array types (like PostgreSQL)
|
|
536
|
+
for storing lists of values in a single column.
|
|
537
|
+
"""
|
|
538
|
+
|
|
539
|
+
element: ColumnTypeMeta
|
|
540
|
+
"""The type of elements stored in the array."""
|
|
541
|
+
|
|
542
|
+
def __new__(cls, element: ColumnTypeMeta) -> Self: ...
|
|
543
|
+
|
|
544
|
+
class AdaptedValue(typing.Generic[T]):
|
|
545
|
+
"""
|
|
546
|
+
Bridges Python types, Rust types, and SQL types for seamless data conversion.
|
|
547
|
+
|
|
548
|
+
This class handles validation, adaptation, and conversion between different
|
|
549
|
+
type systems used in the application stack.
|
|
550
|
+
|
|
551
|
+
NOTE: this class is immutable and frozen.
|
|
552
|
+
"""
|
|
553
|
+
|
|
554
|
+
def __new__(cls, val: typing.Any, type: typing.Optional[ColumnTypeMeta] = None) -> Self:
|
|
555
|
+
"""
|
|
556
|
+
Validates and adapts your value for Rust and SQL, then creates a new `AdaptedValue` instance.
|
|
557
|
+
|
|
558
|
+
This method automatically detects the type of your value and selects appropriate Rust and SQL types.
|
|
559
|
+
For example:
|
|
560
|
+
- Python `int` becomes `BIGINT` SQL type (`BigIntegerType`)
|
|
561
|
+
- Python `dict` or `list` becomes `JSON` SQL type (`JsonType`)
|
|
562
|
+
- Python `float` becomes `DOUBLE` SQL type (`DoubleType`)
|
|
563
|
+
|
|
564
|
+
However, for more accurate type selection, it's recommended to use the `type` parameter.
|
|
565
|
+
|
|
566
|
+
Example::
|
|
567
|
+
|
|
568
|
+
# Let the system detect types automatically
|
|
569
|
+
AdaptedValue(1) # -> INTEGER SQL type
|
|
570
|
+
AdaptedValue(1.4) # -> DOUBLE SQL type
|
|
571
|
+
AdaptedValue("127.0.0.1") # -> VARCHAR SQL type
|
|
572
|
+
AdaptedValue({"key": "value"}) # -> JSON SQL type
|
|
573
|
+
|
|
574
|
+
# Explicitly specify the type
|
|
575
|
+
AdaptedValue(1, TinyUnsigned()) # -> TINYINT UNSIGNED SQL type
|
|
576
|
+
AdaptedValue(1.4, Float()) # -> FLOAT SQL type
|
|
577
|
+
AdaptedValue("127.0.0.1", Inet()) # -> INET SQL type (network address)
|
|
578
|
+
AdaptedValue([4.3, 5.6], Vector()) # -> VECTOR SQL type (for AI embeddings)
|
|
579
|
+
|
|
580
|
+
NOTE: this class is immutable and frozen.
|
|
581
|
+
"""
|
|
582
|
+
...
|
|
583
|
+
|
|
584
|
+
@property
|
|
585
|
+
def is_null(self) -> bool:
|
|
586
|
+
"""Returns True if the adapted value is NULL."""
|
|
587
|
+
...
|
|
588
|
+
@property
|
|
589
|
+
def is_integer(self) -> bool:
|
|
590
|
+
"""Returns True if the adapted value is an integer type."""
|
|
591
|
+
...
|
|
592
|
+
@property
|
|
593
|
+
def is_float(self) -> bool:
|
|
594
|
+
"""Returns True if the adapted value is a floating-point type."""
|
|
595
|
+
...
|
|
596
|
+
@property
|
|
597
|
+
def is_boolean(self) -> bool:
|
|
598
|
+
"""Returns True if the adapted value is a boolean type."""
|
|
599
|
+
...
|
|
600
|
+
@property
|
|
601
|
+
def is_string(self) -> bool:
|
|
602
|
+
"""Returns True if the adapted value is a string type."""
|
|
603
|
+
...
|
|
604
|
+
@property
|
|
605
|
+
def is_date(self) -> bool:
|
|
606
|
+
"""Returns True if the adapted value is a date type."""
|
|
607
|
+
...
|
|
608
|
+
@property
|
|
609
|
+
def is_datetime(self) -> bool:
|
|
610
|
+
"""Returns True if the adapted value is a datetime type."""
|
|
611
|
+
...
|
|
612
|
+
@property
|
|
613
|
+
def is_time(self) -> bool:
|
|
614
|
+
"""Returns True if the adapted value is a time type."""
|
|
615
|
+
...
|
|
616
|
+
@property
|
|
617
|
+
def is_uuid(self) -> bool:
|
|
618
|
+
"""Returns True if the adapted value is a UUID type."""
|
|
619
|
+
...
|
|
620
|
+
@property
|
|
621
|
+
def is_bytes(self) -> bool:
|
|
622
|
+
"""Returns True if the adapted value is a bytes/binary type."""
|
|
623
|
+
...
|
|
624
|
+
@property
|
|
625
|
+
def is_json(self) -> bool:
|
|
626
|
+
"""Returns True if the adapted value is a JSON type."""
|
|
627
|
+
...
|
|
628
|
+
@property
|
|
629
|
+
def is_decimal(self) -> bool:
|
|
630
|
+
"""Returns True if the adapted value is a decimal type."""
|
|
631
|
+
...
|
|
632
|
+
@property
|
|
633
|
+
def is_array(self) -> bool:
|
|
634
|
+
"""Returns True if the adapted value is an array type."""
|
|
635
|
+
...
|
|
636
|
+
@property
|
|
637
|
+
def is_vector(self) -> bool:
|
|
638
|
+
"""Returns True if the adapted value is a vector type."""
|
|
639
|
+
...
|
|
640
|
+
|
|
641
|
+
@property
|
|
642
|
+
def value(self) -> T:
|
|
643
|
+
"""
|
|
644
|
+
Converts the adapted value back to a Python type.
|
|
645
|
+
"""
|
|
646
|
+
...
|
|
647
|
+
|
|
648
|
+
# `AdaptedValue` is not a child of SchemaStatement, but we used
|
|
649
|
+
# `to_sql` name for this method to make compatible with others
|
|
650
|
+
def to_sql(self, backend: _Backends) -> str:
|
|
651
|
+
"""
|
|
652
|
+
Converts the adapted value to SQL.
|
|
653
|
+
"""
|
|
654
|
+
...
|
|
655
|
+
|
|
656
|
+
def __hash__(self) -> int: ...
|
|
657
|
+
def __eq__(self, other: Self) -> bool: ...
|
|
658
|
+
def __ne__(self, other: Self) -> bool: ...
|
|
659
|
+
def __repr__(self) -> str: ...
|
|
660
|
+
|
|
661
|
+
class ColumnRef:
|
|
662
|
+
"""
|
|
663
|
+
Represents a reference to a database column with optional table and schema qualification.
|
|
664
|
+
|
|
665
|
+
This class is used to uniquely identify columns in SQL queries, supporting
|
|
666
|
+
schema-qualified and table-qualified column references.
|
|
667
|
+
|
|
668
|
+
Attributes:
|
|
669
|
+
name: The column name
|
|
670
|
+
table: The table name containing the column, if specified
|
|
671
|
+
schema: The schema name containing the table, if specified
|
|
672
|
+
|
|
673
|
+
Example:
|
|
674
|
+
>>> ColumnRef("id")
|
|
675
|
+
>>> ColumnRef("id", table="users")
|
|
676
|
+
>>> ColumnRef("id", table="users", schema="public")
|
|
677
|
+
"""
|
|
678
|
+
|
|
679
|
+
def __new__(
|
|
680
|
+
cls,
|
|
681
|
+
name: str,
|
|
682
|
+
table: typing.Optional[str] = ...,
|
|
683
|
+
schema: typing.Optional[str] = ...,
|
|
684
|
+
) -> Self:
|
|
685
|
+
"""
|
|
686
|
+
Create a new ColumnRef instance.
|
|
687
|
+
|
|
688
|
+
Args:
|
|
689
|
+
name: The name of the column
|
|
690
|
+
table: The table name containing the column
|
|
691
|
+
schema: The schema name containing the table
|
|
692
|
+
|
|
693
|
+
Returns:
|
|
694
|
+
A new ColumnRef instance
|
|
695
|
+
"""
|
|
696
|
+
...
|
|
697
|
+
|
|
698
|
+
@property
|
|
699
|
+
def name(self) -> str: ...
|
|
700
|
+
@property
|
|
701
|
+
def table(self) -> typing.Optional[str]: ...
|
|
702
|
+
@property
|
|
703
|
+
def schema(self) -> typing.Optional[str]: ...
|
|
704
|
+
@classmethod
|
|
705
|
+
def parse(cls, string: str) -> "ColumnRef":
|
|
706
|
+
"""
|
|
707
|
+
Parse a string representation of a column reference.
|
|
708
|
+
|
|
709
|
+
Supports formats like:
|
|
710
|
+
- "column_name"
|
|
711
|
+
- "table.column_name"
|
|
712
|
+
- "schema.table.column_name"
|
|
713
|
+
|
|
714
|
+
Args:
|
|
715
|
+
string: The string to parse
|
|
716
|
+
|
|
717
|
+
Returns:
|
|
718
|
+
A ColumnRef instance representing the parsed reference
|
|
719
|
+
|
|
720
|
+
Raises:
|
|
721
|
+
ValueError: If the string format is invalid
|
|
722
|
+
"""
|
|
723
|
+
...
|
|
724
|
+
|
|
725
|
+
def __eq__(self, other: "ColumnRef") -> bool:
|
|
726
|
+
"""
|
|
727
|
+
Check equality with another ColumnRef.
|
|
728
|
+
|
|
729
|
+
Two ColumnRefs are equal if they have the same name, table, and schema.
|
|
730
|
+
"""
|
|
731
|
+
...
|
|
732
|
+
|
|
733
|
+
def __ne__(self, other: "ColumnRef") -> bool:
|
|
734
|
+
"""
|
|
735
|
+
Check inequality with another ColumnRef.
|
|
736
|
+
"""
|
|
737
|
+
...
|
|
738
|
+
|
|
739
|
+
def copy(self) -> Self: ...
|
|
740
|
+
def __copy__(self) -> Self: ...
|
|
741
|
+
def copy_with(
|
|
742
|
+
self,
|
|
743
|
+
*,
|
|
744
|
+
name: typing.Optional[str] = ...,
|
|
745
|
+
table: typing.Optional[str] = ...,
|
|
746
|
+
schema: typing.Optional[str] = ...,
|
|
747
|
+
) -> Self: ...
|
|
748
|
+
def __repr__(self) -> str: ...
|
|
749
|
+
|
|
750
|
+
_ExprValue = typing.Union[
|
|
751
|
+
Expr,
|
|
752
|
+
AdaptedValue,
|
|
753
|
+
ColumnRef,
|
|
754
|
+
Column,
|
|
755
|
+
tuple,
|
|
756
|
+
_AsteriskType,
|
|
757
|
+
typing.Any,
|
|
758
|
+
Select,
|
|
759
|
+
]
|
|
760
|
+
|
|
761
|
+
class Expr:
|
|
762
|
+
"""
|
|
763
|
+
Represents a SQL expression that can be built into SQL code.
|
|
764
|
+
|
|
765
|
+
This class provides a fluent interface for constructing complex SQL expressions
|
|
766
|
+
in a database-agnostic way. It supports arithmetic operations, comparisons,
|
|
767
|
+
logical operations, and database-specific functions.
|
|
768
|
+
|
|
769
|
+
The class automatically handles SQL injection protection and proper quoting
|
|
770
|
+
when building the final SQL statement.
|
|
771
|
+
|
|
772
|
+
Example::
|
|
773
|
+
# Basic comparison
|
|
774
|
+
e = Expr(1) > 2
|
|
775
|
+
e.to_sql("mysql")
|
|
776
|
+
# Result: 1 > 2
|
|
777
|
+
|
|
778
|
+
# IN clause with tuple
|
|
779
|
+
e = Expr.col("id").in_((1, 2, 3))
|
|
780
|
+
e.to_sql("mysql")
|
|
781
|
+
# Result: "id" IN (1, 2, 3)
|
|
782
|
+
|
|
783
|
+
# Complex expression with functions
|
|
784
|
+
e = FunctionCall.upper(Expr.col("name")).to_expr() == "JOHN"
|
|
785
|
+
e.to_sql("postgres")
|
|
786
|
+
# Result: UPPER("name") = 'JOHN'
|
|
787
|
+
"""
|
|
788
|
+
|
|
789
|
+
def __new__(cls, value: _ExprValue, /) -> Self:
|
|
790
|
+
"""
|
|
791
|
+
Create a new expression from a value.
|
|
792
|
+
|
|
793
|
+
Args:
|
|
794
|
+
value: The value to convert to an expression. Can be a primitive
|
|
795
|
+
value, ColumnRef, AdaptedValue, or another Expr.
|
|
796
|
+
|
|
797
|
+
Returns:
|
|
798
|
+
A new Expr instance representing the value
|
|
799
|
+
"""
|
|
800
|
+
...
|
|
801
|
+
|
|
802
|
+
@classmethod
|
|
803
|
+
def val(cls, value: AdaptedValue) -> Self:
|
|
804
|
+
"""
|
|
805
|
+
Create an expression from an AdaptedValue.
|
|
806
|
+
|
|
807
|
+
AdaptedValues are values that have been adapted for safe use in SQL,
|
|
808
|
+
such as properly escaped strings or formatted dates.
|
|
809
|
+
|
|
810
|
+
Args:
|
|
811
|
+
value: The adapted value to convert to an expression
|
|
812
|
+
|
|
813
|
+
Returns:
|
|
814
|
+
An Expr representing the adapted value
|
|
815
|
+
"""
|
|
816
|
+
...
|
|
817
|
+
|
|
818
|
+
@classmethod
|
|
819
|
+
def func(cls, value: FunctionCall) -> Self:
|
|
820
|
+
"""
|
|
821
|
+
Create an expression from a FunctionCall.
|
|
822
|
+
|
|
823
|
+
Args:
|
|
824
|
+
value: The function call to convert to an expression
|
|
825
|
+
|
|
826
|
+
Returns:
|
|
827
|
+
An Expr representing the function call
|
|
828
|
+
"""
|
|
829
|
+
...
|
|
830
|
+
|
|
831
|
+
@classmethod
|
|
832
|
+
def col(cls, name: typing.Union[str, ColumnRef]) -> Self:
|
|
833
|
+
"""
|
|
834
|
+
Create a column reference expression.
|
|
835
|
+
|
|
836
|
+
Args:
|
|
837
|
+
name: The column name or ColumnRef to reference
|
|
838
|
+
|
|
839
|
+
Returns:
|
|
840
|
+
An Expr representing the column reference
|
|
841
|
+
"""
|
|
842
|
+
...
|
|
843
|
+
|
|
844
|
+
@classmethod
|
|
845
|
+
def exists(cls, stmt: Select) -> Self: ...
|
|
846
|
+
@classmethod
|
|
847
|
+
def any(cls, stmt: Select) -> Self: ...
|
|
848
|
+
@classmethod
|
|
849
|
+
def some(cls, stmt: Select) -> Self: ...
|
|
850
|
+
@classmethod
|
|
851
|
+
def all(cls, stmt: Select) -> Self: ...
|
|
852
|
+
@classmethod
|
|
853
|
+
def tuple(
|
|
854
|
+
cls,
|
|
855
|
+
values: typing.Union[typing.Set[Self], typing.List[Self], typing.Tuple[Self]],
|
|
856
|
+
) -> Self:
|
|
857
|
+
"""
|
|
858
|
+
Create a tuple expression for tuple comparisons.
|
|
859
|
+
|
|
860
|
+
Args:
|
|
861
|
+
values: A collection of expressions to include in the tuple
|
|
862
|
+
|
|
863
|
+
Returns:
|
|
864
|
+
An Expr representing a SQL tuple
|
|
865
|
+
|
|
866
|
+
Example:
|
|
867
|
+
>>> Expr.tuple(["id", "name"])
|
|
868
|
+
# Can be used in: WHERE (id, name) IN ((1, 'a'), (2, 'b'))
|
|
869
|
+
"""
|
|
870
|
+
...
|
|
871
|
+
|
|
872
|
+
@classmethod
|
|
873
|
+
def asterisk(cls) -> Self:
|
|
874
|
+
"""
|
|
875
|
+
Create a wildcard expression for SELECT * queries.
|
|
876
|
+
|
|
877
|
+
Returns:
|
|
878
|
+
An Expr representing the asterisk wildcard
|
|
879
|
+
"""
|
|
880
|
+
...
|
|
881
|
+
|
|
882
|
+
@classmethod
|
|
883
|
+
def custom(cls, value: str) -> Self:
|
|
884
|
+
"""
|
|
885
|
+
Create an expression from a custom SQL string.
|
|
886
|
+
|
|
887
|
+
Warning: This method does not escape the input, so it should only
|
|
888
|
+
be used with trusted strings to avoid SQL injection vulnerabilities.
|
|
889
|
+
|
|
890
|
+
Args:
|
|
891
|
+
value: The raw SQL string to use as an expression
|
|
892
|
+
|
|
893
|
+
Returns:
|
|
894
|
+
An Expr representing the custom SQL
|
|
895
|
+
"""
|
|
896
|
+
...
|
|
897
|
+
|
|
898
|
+
@classmethod
|
|
899
|
+
def current_date(cls) -> Self:
|
|
900
|
+
"""
|
|
901
|
+
Create an expression for the CURRENT_DATE SQL function.
|
|
902
|
+
|
|
903
|
+
Returns:
|
|
904
|
+
An Expr representing CURRENT_DATE
|
|
905
|
+
"""
|
|
906
|
+
...
|
|
907
|
+
|
|
908
|
+
@classmethod
|
|
909
|
+
def current_time(cls) -> Self:
|
|
910
|
+
"""
|
|
911
|
+
Create an expression for the CURRENT_TIME SQL function.
|
|
912
|
+
|
|
913
|
+
Returns:
|
|
914
|
+
An Expr representing CURRENT_TIME
|
|
915
|
+
"""
|
|
916
|
+
...
|
|
917
|
+
|
|
918
|
+
@classmethod
|
|
919
|
+
def current_timestamp(cls) -> Self:
|
|
920
|
+
"""
|
|
921
|
+
Create an expression for the CURRENT_TIMESTAMP SQL function.
|
|
922
|
+
|
|
923
|
+
Returns:
|
|
924
|
+
An Expr representing CURRENT_TIMESTAMP
|
|
925
|
+
"""
|
|
926
|
+
...
|
|
927
|
+
|
|
928
|
+
@classmethod
|
|
929
|
+
def null(cls) -> Self:
|
|
930
|
+
"""
|
|
931
|
+
Create an expression representing the NULL value.
|
|
932
|
+
|
|
933
|
+
Returns:
|
|
934
|
+
An Expr representing NULL
|
|
935
|
+
"""
|
|
936
|
+
...
|
|
937
|
+
|
|
938
|
+
def cast_as(self, value: str) -> Self:
|
|
939
|
+
"""
|
|
940
|
+
Create a CAST expression to convert to a specific SQL type.
|
|
941
|
+
|
|
942
|
+
Args:
|
|
943
|
+
value: The target SQL type name (e.g., 'INTEGER', 'VARCHAR(255)')
|
|
944
|
+
|
|
945
|
+
Returns:
|
|
946
|
+
A new Expr representing the cast operation
|
|
947
|
+
"""
|
|
948
|
+
...
|
|
949
|
+
|
|
950
|
+
def like(self, pattern: str, escape: typing.Optional[str] = ...) -> Self:
|
|
951
|
+
"""
|
|
952
|
+
Create a LIKE pattern matching expression.
|
|
953
|
+
|
|
954
|
+
Args:
|
|
955
|
+
pattern: The pattern to match against
|
|
956
|
+
escape: Optional escape character for wildcards in the pattern
|
|
957
|
+
|
|
958
|
+
Returns:
|
|
959
|
+
A new Expr representing the LIKE operation
|
|
960
|
+
"""
|
|
961
|
+
...
|
|
962
|
+
|
|
963
|
+
def not_like(self, pattern: str, escape: typing.Optional[str] = ...) -> Self:
|
|
964
|
+
"""
|
|
965
|
+
Create a NOT LIKE pattern matching expression.
|
|
966
|
+
|
|
967
|
+
Args:
|
|
968
|
+
pattern: The pattern that should not match
|
|
969
|
+
escape: Optional escape character for wildcards in the pattern
|
|
970
|
+
|
|
971
|
+
Returns:
|
|
972
|
+
A new Expr representing the NOT LIKE operation
|
|
973
|
+
"""
|
|
974
|
+
...
|
|
975
|
+
|
|
976
|
+
def __eq__(self, other: _ExprValue) -> Self:
|
|
977
|
+
"""
|
|
978
|
+
Create an equality comparison expression.
|
|
979
|
+
"""
|
|
980
|
+
...
|
|
981
|
+
|
|
982
|
+
def __ne__(self, other: _ExprValue) -> Self:
|
|
983
|
+
"""
|
|
984
|
+
Create an inequality comparison expression.
|
|
985
|
+
"""
|
|
986
|
+
...
|
|
987
|
+
|
|
988
|
+
def __gt__(self, other: _ExprValue) -> Self:
|
|
989
|
+
"""
|
|
990
|
+
Create a greater-than comparison expression.
|
|
991
|
+
"""
|
|
992
|
+
...
|
|
993
|
+
|
|
994
|
+
def __ge__(self, other: _ExprValue) -> Self:
|
|
995
|
+
"""
|
|
996
|
+
Create a greater-than-or-equal comparison expression.
|
|
997
|
+
"""
|
|
998
|
+
...
|
|
999
|
+
|
|
1000
|
+
def __lt__(self, other: _ExprValue) -> Self:
|
|
1001
|
+
"""
|
|
1002
|
+
Create a less-than comparison expression.
|
|
1003
|
+
"""
|
|
1004
|
+
...
|
|
1005
|
+
|
|
1006
|
+
def __le__(self, other: _ExprValue) -> Self:
|
|
1007
|
+
"""
|
|
1008
|
+
Create a less-than-or-equal comparison expression.
|
|
1009
|
+
"""
|
|
1010
|
+
...
|
|
1011
|
+
|
|
1012
|
+
def __add__(self, other: _ExprValue) -> Self:
|
|
1013
|
+
"""
|
|
1014
|
+
Create an addition expression.
|
|
1015
|
+
"""
|
|
1016
|
+
...
|
|
1017
|
+
|
|
1018
|
+
def __sub__(self, other: _ExprValue) -> Self:
|
|
1019
|
+
"""
|
|
1020
|
+
Create a subtraction expression.
|
|
1021
|
+
"""
|
|
1022
|
+
...
|
|
1023
|
+
|
|
1024
|
+
def __and__(self, other: _ExprValue) -> Self:
|
|
1025
|
+
"""
|
|
1026
|
+
Create a logical AND expression.
|
|
1027
|
+
"""
|
|
1028
|
+
...
|
|
1029
|
+
|
|
1030
|
+
def __or__(self, other: _ExprValue) -> Self:
|
|
1031
|
+
"""
|
|
1032
|
+
Create a logical OR expression.
|
|
1033
|
+
"""
|
|
1034
|
+
...
|
|
1035
|
+
|
|
1036
|
+
def bit_and(self, other: _ExprValue) -> Self: ...
|
|
1037
|
+
def bit_or(self, other: _ExprValue) -> Self: ...
|
|
1038
|
+
def __truediv__(self, other: _ExprValue) -> Self:
|
|
1039
|
+
"""
|
|
1040
|
+
Create a division expression.
|
|
1041
|
+
"""
|
|
1042
|
+
...
|
|
1043
|
+
|
|
1044
|
+
def is_(self, other: _ExprValue) -> Self:
|
|
1045
|
+
"""
|
|
1046
|
+
Create an IS comparison expression (for NULL comparisons).
|
|
1047
|
+
|
|
1048
|
+
Typically used with NULL: column.is_(Expr.null())
|
|
1049
|
+
|
|
1050
|
+
Args:
|
|
1051
|
+
other: The expression to compare with
|
|
1052
|
+
|
|
1053
|
+
Returns:
|
|
1054
|
+
A new Expr representing the IS comparison
|
|
1055
|
+
"""
|
|
1056
|
+
...
|
|
1057
|
+
|
|
1058
|
+
def sqlite_matches(self, other: _ExprValue) -> Self:
|
|
1059
|
+
"""
|
|
1060
|
+
Create a SQLite MATCH expression for full-text search.
|
|
1061
|
+
|
|
1062
|
+
Args:
|
|
1063
|
+
other: The expression to match against
|
|
1064
|
+
|
|
1065
|
+
Returns:
|
|
1066
|
+
A new Expr representing the MATCH operation
|
|
1067
|
+
"""
|
|
1068
|
+
...
|
|
1069
|
+
|
|
1070
|
+
def sqlite_glob(self, other: _ExprValue) -> Self:
|
|
1071
|
+
"""
|
|
1072
|
+
Create a SQLite GLOB expression for pattern matching.
|
|
1073
|
+
|
|
1074
|
+
Args:
|
|
1075
|
+
other: The glob pattern to match against
|
|
1076
|
+
|
|
1077
|
+
Returns:
|
|
1078
|
+
A new Expr representing the GLOB operation
|
|
1079
|
+
"""
|
|
1080
|
+
...
|
|
1081
|
+
|
|
1082
|
+
def pg_concat(self, other: _ExprValue) -> Self:
|
|
1083
|
+
"""
|
|
1084
|
+
Create a PostgreSQL concatenation expression using || operator.
|
|
1085
|
+
|
|
1086
|
+
Args:
|
|
1087
|
+
other: The expression to concatenate with
|
|
1088
|
+
|
|
1089
|
+
Returns:
|
|
1090
|
+
A new Expr representing the concatenation
|
|
1091
|
+
"""
|
|
1092
|
+
...
|
|
1093
|
+
|
|
1094
|
+
def pg_contained(self, other: _ExprValue) -> Self:
|
|
1095
|
+
"""
|
|
1096
|
+
Create a PostgreSQL contained expression using <@ operator.
|
|
1097
|
+
|
|
1098
|
+
Used for array and range containment checks.
|
|
1099
|
+
|
|
1100
|
+
Args:
|
|
1101
|
+
other: The expression to check containment against
|
|
1102
|
+
|
|
1103
|
+
Returns:
|
|
1104
|
+
A new Expr representing the contained operation
|
|
1105
|
+
"""
|
|
1106
|
+
...
|
|
1107
|
+
|
|
1108
|
+
def sqlite_cast_json_field(self, other: _ExprValue) -> Self:
|
|
1109
|
+
"""
|
|
1110
|
+
Extract and cast a JSON field to appropriate SQL type using ->> operator.
|
|
1111
|
+
|
|
1112
|
+
This operator returns the JSON field as text and can be cast to other types.
|
|
1113
|
+
|
|
1114
|
+
Args:
|
|
1115
|
+
other: The JSON field path/name to extract
|
|
1116
|
+
|
|
1117
|
+
Returns:
|
|
1118
|
+
A new Expr representing the JSON field extraction and casting
|
|
1119
|
+
"""
|
|
1120
|
+
...
|
|
1121
|
+
|
|
1122
|
+
def sqlite_get_json_field(self, other: _ExprValue) -> Self:
|
|
1123
|
+
"""
|
|
1124
|
+
Extract a JSON field using -> operator (returns JSON type).
|
|
1125
|
+
|
|
1126
|
+
Args:
|
|
1127
|
+
other: The JSON field path/name to extract
|
|
1128
|
+
|
|
1129
|
+
Returns:
|
|
1130
|
+
A new Expr representing the JSON field extraction
|
|
1131
|
+
"""
|
|
1132
|
+
...
|
|
1133
|
+
|
|
1134
|
+
def pg_cast_json_field(self, other: _ExprValue) -> Self:
|
|
1135
|
+
"""
|
|
1136
|
+
Extract and cast a JSON field to appropriate SQL type using ->> operator.
|
|
1137
|
+
|
|
1138
|
+
This operator returns the JSON field as text and can be cast to other types.
|
|
1139
|
+
|
|
1140
|
+
Args:
|
|
1141
|
+
other: The JSON field path/name to extract
|
|
1142
|
+
|
|
1143
|
+
Returns:
|
|
1144
|
+
A new Expr representing the JSON field extraction and casting
|
|
1145
|
+
"""
|
|
1146
|
+
...
|
|
1147
|
+
|
|
1148
|
+
def pg_get_json_field(self, other: _ExprValue) -> Self:
|
|
1149
|
+
"""
|
|
1150
|
+
Extract a JSON field using -> operator (returns JSON type).
|
|
1151
|
+
|
|
1152
|
+
Args:
|
|
1153
|
+
other: The JSON field path/name to extract
|
|
1154
|
+
|
|
1155
|
+
Returns:
|
|
1156
|
+
A new Expr representing the JSON field extraction
|
|
1157
|
+
"""
|
|
1158
|
+
...
|
|
1159
|
+
|
|
1160
|
+
def pg_contains(self, other: _ExprValue) -> Self:
|
|
1161
|
+
"""
|
|
1162
|
+
Create a PostgreSQL contains expression using @> operator.
|
|
1163
|
+
|
|
1164
|
+
Used for array and range containment checks.
|
|
1165
|
+
|
|
1166
|
+
Args:
|
|
1167
|
+
other: The expression to check if it is contained
|
|
1168
|
+
|
|
1169
|
+
Returns:
|
|
1170
|
+
A new Expr representing the contains operation
|
|
1171
|
+
"""
|
|
1172
|
+
...
|
|
1173
|
+
|
|
1174
|
+
def pg_matches(self, other: _ExprValue) -> Self:
|
|
1175
|
+
"""
|
|
1176
|
+
Create a PostgreSQL full-text search matches expression using @@ operator.
|
|
1177
|
+
|
|
1178
|
+
Args:
|
|
1179
|
+
other: The full-text search query
|
|
1180
|
+
|
|
1181
|
+
Returns:
|
|
1182
|
+
A new Expr representing the full-text match operation
|
|
1183
|
+
"""
|
|
1184
|
+
...
|
|
1185
|
+
|
|
1186
|
+
def pg_ilike(self, other: _ExprValue) -> Self:
|
|
1187
|
+
"""
|
|
1188
|
+
Create a PostgreSQL case-insensitive LIKE expression.
|
|
1189
|
+
|
|
1190
|
+
Args:
|
|
1191
|
+
other: The pattern to match against
|
|
1192
|
+
|
|
1193
|
+
Returns:
|
|
1194
|
+
A new Expr representing the ILIKE operation
|
|
1195
|
+
"""
|
|
1196
|
+
...
|
|
1197
|
+
|
|
1198
|
+
def pg_not_ilike(self, other: _ExprValue) -> Self:
|
|
1199
|
+
"""
|
|
1200
|
+
Create a PostgreSQL case-insensitive NOT LIKE expression.
|
|
1201
|
+
|
|
1202
|
+
Args:
|
|
1203
|
+
other: The pattern that should not match
|
|
1204
|
+
|
|
1205
|
+
Returns:
|
|
1206
|
+
A new Expr representing the NOT ILIKE operation
|
|
1207
|
+
"""
|
|
1208
|
+
...
|
|
1209
|
+
|
|
1210
|
+
def is_not(self, other: _ExprValue) -> Self:
|
|
1211
|
+
"""
|
|
1212
|
+
Create an IS NOT comparison expression.
|
|
1213
|
+
|
|
1214
|
+
Args:
|
|
1215
|
+
other: The expression to compare with
|
|
1216
|
+
|
|
1217
|
+
Returns:
|
|
1218
|
+
A new Expr representing the IS NOT comparison
|
|
1219
|
+
"""
|
|
1220
|
+
...
|
|
1221
|
+
|
|
1222
|
+
def is_null(self) -> Self:
|
|
1223
|
+
"""
|
|
1224
|
+
Create an IS NULL expression.
|
|
1225
|
+
|
|
1226
|
+
Returns:
|
|
1227
|
+
A new Expr representing the IS NULL check
|
|
1228
|
+
"""
|
|
1229
|
+
...
|
|
1230
|
+
|
|
1231
|
+
def is_not_null(self) -> Self:
|
|
1232
|
+
"""
|
|
1233
|
+
Create an IS NOT NULL expression.
|
|
1234
|
+
|
|
1235
|
+
Returns:
|
|
1236
|
+
A new Expr representing the IS NOT NULL check
|
|
1237
|
+
"""
|
|
1238
|
+
...
|
|
1239
|
+
|
|
1240
|
+
def __lshift__(self, other: _ExprValue) -> Self:
|
|
1241
|
+
"""
|
|
1242
|
+
Create a bitwise left shift expression.
|
|
1243
|
+
"""
|
|
1244
|
+
...
|
|
1245
|
+
|
|
1246
|
+
def __rshift__(self, other: _ExprValue) -> Self:
|
|
1247
|
+
"""
|
|
1248
|
+
Create a bitwise right shift expression.
|
|
1249
|
+
"""
|
|
1250
|
+
...
|
|
1251
|
+
|
|
1252
|
+
def __mod__(self, other: _ExprValue) -> Self:
|
|
1253
|
+
"""
|
|
1254
|
+
Create a modulo expression.
|
|
1255
|
+
"""
|
|
1256
|
+
...
|
|
1257
|
+
|
|
1258
|
+
def __mul__(self, other: _ExprValue) -> Self:
|
|
1259
|
+
"""
|
|
1260
|
+
Create a multiplication expression.
|
|
1261
|
+
"""
|
|
1262
|
+
...
|
|
1263
|
+
|
|
1264
|
+
def between(self, a: _ExprValue, b: _ExprValue) -> Self:
|
|
1265
|
+
"""
|
|
1266
|
+
Create a BETWEEN range comparison expression.
|
|
1267
|
+
|
|
1268
|
+
Args:
|
|
1269
|
+
a: The lower bound of the range
|
|
1270
|
+
b: The upper bound of the range
|
|
1271
|
+
|
|
1272
|
+
Returns:
|
|
1273
|
+
A new Expr representing the BETWEEN operation
|
|
1274
|
+
"""
|
|
1275
|
+
...
|
|
1276
|
+
|
|
1277
|
+
def not_between(self, a: _ExprValue, b: _ExprValue) -> Self:
|
|
1278
|
+
"""
|
|
1279
|
+
Create a NOT BETWEEN range comparison expression.
|
|
1280
|
+
|
|
1281
|
+
Args:
|
|
1282
|
+
a: The lower bound of the range
|
|
1283
|
+
b: The upper bound of the range
|
|
1284
|
+
|
|
1285
|
+
Returns:
|
|
1286
|
+
A new Expr representing the NOT BETWEEN operation
|
|
1287
|
+
"""
|
|
1288
|
+
...
|
|
1289
|
+
|
|
1290
|
+
def in_subquery(self, stmt: Select) -> Self: ...
|
|
1291
|
+
def not_in_subquery(self, stmt: Select) -> Self: ...
|
|
1292
|
+
def in_(self, other: typing.Sequence[_ExprValue]) -> Self:
|
|
1293
|
+
"""
|
|
1294
|
+
Create an IN membership expression.
|
|
1295
|
+
|
|
1296
|
+
Args:
|
|
1297
|
+
other: A sequence of expressions to check membership against
|
|
1298
|
+
|
|
1299
|
+
Returns:
|
|
1300
|
+
A new Expr representing the IN operation
|
|
1301
|
+
"""
|
|
1302
|
+
...
|
|
1303
|
+
|
|
1304
|
+
def not_in(self, other: typing.Sequence[_ExprValue]) -> Self:
|
|
1305
|
+
"""
|
|
1306
|
+
Create a NOT IN membership expression.
|
|
1307
|
+
|
|
1308
|
+
Args:
|
|
1309
|
+
other: A sequence of expressions to check non-membership against
|
|
1310
|
+
|
|
1311
|
+
Returns:
|
|
1312
|
+
A new Expr representing the NOT IN operation
|
|
1313
|
+
"""
|
|
1314
|
+
...
|
|
1315
|
+
|
|
1316
|
+
# `Expr` is not a child of SchemaStatement, but we used
|
|
1317
|
+
# `to_sql` name for this method to make compatible with others
|
|
1318
|
+
def to_sql(self, backend: _Backends) -> str:
|
|
1319
|
+
"""
|
|
1320
|
+
Converts the adapted value to SQL.
|
|
1321
|
+
"""
|
|
1322
|
+
...
|
|
1323
|
+
|
|
1324
|
+
def __repr__(self) -> str:
|
|
1325
|
+
"""
|
|
1326
|
+
Return a developer-friendly string representation.
|
|
1327
|
+
|
|
1328
|
+
Returns:
|
|
1329
|
+
A string that could be used to recreate this expression
|
|
1330
|
+
"""
|
|
1331
|
+
...
|
|
1332
|
+
|
|
1333
|
+
class FunctionCall:
|
|
1334
|
+
"""
|
|
1335
|
+
Represents a SQL function call that can be used in expressions.
|
|
1336
|
+
|
|
1337
|
+
This class provides a type-safe way to construct SQL function calls
|
|
1338
|
+
with proper argument handling and database dialect support.
|
|
1339
|
+
"""
|
|
1340
|
+
|
|
1341
|
+
def __new__(cls, name: str) -> Self:
|
|
1342
|
+
"""
|
|
1343
|
+
Create a new function call with the given name.
|
|
1344
|
+
|
|
1345
|
+
Args:
|
|
1346
|
+
name: The name of the SQL function
|
|
1347
|
+
|
|
1348
|
+
Returns:
|
|
1349
|
+
A new FunctionCall instance
|
|
1350
|
+
"""
|
|
1351
|
+
...
|
|
1352
|
+
|
|
1353
|
+
def arg(self, arg: _ExprValue) -> Self:
|
|
1354
|
+
"""
|
|
1355
|
+
Add an argument to the function call.
|
|
1356
|
+
|
|
1357
|
+
Args:
|
|
1358
|
+
arg: The expression to add as an argument
|
|
1359
|
+
|
|
1360
|
+
Returns:
|
|
1361
|
+
Self for method chaining
|
|
1362
|
+
"""
|
|
1363
|
+
...
|
|
1364
|
+
|
|
1365
|
+
def to_expr(self) -> Expr:
|
|
1366
|
+
"""
|
|
1367
|
+
Convert this function call to an expression.
|
|
1368
|
+
|
|
1369
|
+
Shorthand for `Expr(self)`
|
|
1370
|
+
|
|
1371
|
+
Returns:
|
|
1372
|
+
An Expr representing this column
|
|
1373
|
+
"""
|
|
1374
|
+
...
|
|
1375
|
+
|
|
1376
|
+
@classmethod
|
|
1377
|
+
def sum(cls, expr: _ExprValue) -> Self: ...
|
|
1378
|
+
@classmethod
|
|
1379
|
+
def now(cls) -> Self: ...
|
|
1380
|
+
@classmethod
|
|
1381
|
+
def min(cls, expr: _ExprValue) -> Self:
|
|
1382
|
+
"""
|
|
1383
|
+
Create a MIN aggregate function call.
|
|
1384
|
+
|
|
1385
|
+
Args:
|
|
1386
|
+
expr: The expression to find the minimum of
|
|
1387
|
+
|
|
1388
|
+
Returns:
|
|
1389
|
+
A FunctionCall representing MIN(expr)
|
|
1390
|
+
"""
|
|
1391
|
+
...
|
|
1392
|
+
|
|
1393
|
+
@classmethod
|
|
1394
|
+
def max(cls, expr: _ExprValue) -> Self:
|
|
1395
|
+
"""
|
|
1396
|
+
Create a MAX aggregate function call.
|
|
1397
|
+
|
|
1398
|
+
Args:
|
|
1399
|
+
expr: The expression to find the maximum of
|
|
1400
|
+
|
|
1401
|
+
Returns:
|
|
1402
|
+
A FunctionCall representing MAX(expr)
|
|
1403
|
+
"""
|
|
1404
|
+
...
|
|
1405
|
+
|
|
1406
|
+
@classmethod
|
|
1407
|
+
def abs(cls, expr: _ExprValue) -> Self:
|
|
1408
|
+
"""
|
|
1409
|
+
Create an ABS absolute value function call.
|
|
1410
|
+
|
|
1411
|
+
Args:
|
|
1412
|
+
expr: The expression to get the absolute value of
|
|
1413
|
+
|
|
1414
|
+
Returns:
|
|
1415
|
+
A FunctionCall representing ABS(expr)
|
|
1416
|
+
"""
|
|
1417
|
+
...
|
|
1418
|
+
|
|
1419
|
+
@classmethod
|
|
1420
|
+
def avg(cls, expr: _ExprValue) -> Self:
|
|
1421
|
+
"""
|
|
1422
|
+
Create an AVG average function call.
|
|
1423
|
+
|
|
1424
|
+
Args:
|
|
1425
|
+
expr: The expression to calculate the average of
|
|
1426
|
+
|
|
1427
|
+
Returns:
|
|
1428
|
+
A FunctionCall representing AVG(expr)
|
|
1429
|
+
"""
|
|
1430
|
+
...
|
|
1431
|
+
|
|
1432
|
+
@classmethod
|
|
1433
|
+
def count(cls, expr: _ExprValue) -> Self:
|
|
1434
|
+
"""
|
|
1435
|
+
Create a COUNT aggregate function call.
|
|
1436
|
+
|
|
1437
|
+
Args:
|
|
1438
|
+
expr: The expression to count
|
|
1439
|
+
|
|
1440
|
+
Returns:
|
|
1441
|
+
A FunctionCall representing COUNT(expr)
|
|
1442
|
+
"""
|
|
1443
|
+
...
|
|
1444
|
+
|
|
1445
|
+
@classmethod
|
|
1446
|
+
def count_distinct(cls, expr: _ExprValue) -> Self:
|
|
1447
|
+
"""
|
|
1448
|
+
Create a COUNT(DISTINCT ...) aggregate function call.
|
|
1449
|
+
|
|
1450
|
+
Args:
|
|
1451
|
+
expr: The expression to count distinct values of
|
|
1452
|
+
|
|
1453
|
+
Returns:
|
|
1454
|
+
A FunctionCall representing COUNT(DISTINCT expr)
|
|
1455
|
+
"""
|
|
1456
|
+
...
|
|
1457
|
+
|
|
1458
|
+
@classmethod
|
|
1459
|
+
def if_null(cls, expr: _ExprValue) -> Self:
|
|
1460
|
+
"""
|
|
1461
|
+
Create an IFNULL/COALESCE function call (database-dependent).
|
|
1462
|
+
|
|
1463
|
+
Args:
|
|
1464
|
+
expr: The expression to check for NULL
|
|
1465
|
+
|
|
1466
|
+
Returns:
|
|
1467
|
+
A FunctionCall representing the NULL-checking function
|
|
1468
|
+
"""
|
|
1469
|
+
...
|
|
1470
|
+
|
|
1471
|
+
@classmethod
|
|
1472
|
+
def greatest(cls, *exprs: _ExprValue) -> Self:
|
|
1473
|
+
"""
|
|
1474
|
+
Create a GREATEST function call returning the largest value.
|
|
1475
|
+
|
|
1476
|
+
Args:
|
|
1477
|
+
exprs: Sequence of expressions to compare
|
|
1478
|
+
|
|
1479
|
+
Returns:
|
|
1480
|
+
A FunctionCall representing GREATEST(expr1, expr2, ...)
|
|
1481
|
+
"""
|
|
1482
|
+
...
|
|
1483
|
+
|
|
1484
|
+
@classmethod
|
|
1485
|
+
def least(cls, *exprs: _ExprValue) -> Self:
|
|
1486
|
+
"""
|
|
1487
|
+
Create a LEAST function call returning the smallest value.
|
|
1488
|
+
|
|
1489
|
+
Args:
|
|
1490
|
+
exprs: Sequence of expressions to compare
|
|
1491
|
+
|
|
1492
|
+
Returns:
|
|
1493
|
+
A FunctionCall representing LEAST(expr1, expr2, ...)
|
|
1494
|
+
"""
|
|
1495
|
+
...
|
|
1496
|
+
|
|
1497
|
+
@classmethod
|
|
1498
|
+
def char_length(cls, expr: _ExprValue) -> Self:
|
|
1499
|
+
"""
|
|
1500
|
+
Create a CHAR_LENGTH/LENGTH function call.
|
|
1501
|
+
|
|
1502
|
+
Args:
|
|
1503
|
+
expr: The string expression to measure
|
|
1504
|
+
|
|
1505
|
+
Returns:
|
|
1506
|
+
A FunctionCall representing CHAR_LENGTH(expr)
|
|
1507
|
+
"""
|
|
1508
|
+
...
|
|
1509
|
+
|
|
1510
|
+
@classmethod
|
|
1511
|
+
def coalesce(cls, *exprs: _ExprValue) -> Self:
|
|
1512
|
+
"""
|
|
1513
|
+
Create a COALESCE function call returning first non-NULL value.
|
|
1514
|
+
|
|
1515
|
+
Args:
|
|
1516
|
+
exprs: Sequence of expressions to check
|
|
1517
|
+
|
|
1518
|
+
Returns:
|
|
1519
|
+
A FunctionCall representing COALESCE(expr1, expr2, ...)
|
|
1520
|
+
"""
|
|
1521
|
+
...
|
|
1522
|
+
|
|
1523
|
+
@classmethod
|
|
1524
|
+
def lower(cls, expr: _ExprValue) -> Self:
|
|
1525
|
+
"""
|
|
1526
|
+
Create a LOWER case conversion function call.
|
|
1527
|
+
|
|
1528
|
+
Args:
|
|
1529
|
+
expr: The string expression to convert to lowercase
|
|
1530
|
+
|
|
1531
|
+
Returns:
|
|
1532
|
+
A FunctionCall representing LOWER(expr)
|
|
1533
|
+
"""
|
|
1534
|
+
...
|
|
1535
|
+
|
|
1536
|
+
@classmethod
|
|
1537
|
+
def upper(cls, expr: _ExprValue) -> Self:
|
|
1538
|
+
"""
|
|
1539
|
+
Create an UPPER case conversion function call.
|
|
1540
|
+
|
|
1541
|
+
Args:
|
|
1542
|
+
expr: The string expression to convert to uppercase
|
|
1543
|
+
|
|
1544
|
+
Returns:
|
|
1545
|
+
A FunctionCall representing UPPER(expr)
|
|
1546
|
+
"""
|
|
1547
|
+
...
|
|
1548
|
+
|
|
1549
|
+
@classmethod
|
|
1550
|
+
def bit_and(cls, expr: _ExprValue) -> Self:
|
|
1551
|
+
"""
|
|
1552
|
+
Create a BIT_AND aggregate function call.
|
|
1553
|
+
|
|
1554
|
+
Args:
|
|
1555
|
+
expr: The expression for bitwise AND operation
|
|
1556
|
+
|
|
1557
|
+
Returns:
|
|
1558
|
+
A FunctionCall representing BIT_AND(expr)
|
|
1559
|
+
"""
|
|
1560
|
+
...
|
|
1561
|
+
|
|
1562
|
+
@classmethod
|
|
1563
|
+
def bit_or(cls, expr: _ExprValue) -> Self:
|
|
1564
|
+
"""
|
|
1565
|
+
Create a BIT_OR aggregate function call.
|
|
1566
|
+
|
|
1567
|
+
Args:
|
|
1568
|
+
expr: The expression for bitwise OR operation
|
|
1569
|
+
|
|
1570
|
+
Returns:
|
|
1571
|
+
A FunctionCall representing BIT_OR(expr)
|
|
1572
|
+
"""
|
|
1573
|
+
...
|
|
1574
|
+
|
|
1575
|
+
@classmethod
|
|
1576
|
+
def random(cls) -> Self:
|
|
1577
|
+
"""
|
|
1578
|
+
Create a RANDOM/RAND function call.
|
|
1579
|
+
|
|
1580
|
+
Returns:
|
|
1581
|
+
A FunctionCall representing the random number function
|
|
1582
|
+
"""
|
|
1583
|
+
...
|
|
1584
|
+
|
|
1585
|
+
@classmethod
|
|
1586
|
+
def round(cls, expr: _ExprValue) -> Self:
|
|
1587
|
+
"""
|
|
1588
|
+
Create a ROUND function call.
|
|
1589
|
+
|
|
1590
|
+
Args:
|
|
1591
|
+
expr: The numeric expression to round
|
|
1592
|
+
|
|
1593
|
+
Returns:
|
|
1594
|
+
A FunctionCall representing ROUND(expr)
|
|
1595
|
+
"""
|
|
1596
|
+
...
|
|
1597
|
+
|
|
1598
|
+
@classmethod
|
|
1599
|
+
def round_with_precision(cls, a: _ExprValue, b: _ExprValue) -> Self:
|
|
1600
|
+
"""
|
|
1601
|
+
Call ROUND function with the precision.
|
|
1602
|
+
|
|
1603
|
+
Args:
|
|
1604
|
+
a: The numeric expression to round
|
|
1605
|
+
b: The numeric expression to round
|
|
1606
|
+
|
|
1607
|
+
Returns:
|
|
1608
|
+
A FunctionCall representing ROUND(a, b)
|
|
1609
|
+
"""
|
|
1610
|
+
...
|
|
1611
|
+
|
|
1612
|
+
@classmethod
|
|
1613
|
+
def md5(cls, expr: _ExprValue) -> Self:
|
|
1614
|
+
"""
|
|
1615
|
+
Create an MD5 hash function call.
|
|
1616
|
+
|
|
1617
|
+
Args:
|
|
1618
|
+
expr: The expression to hash
|
|
1619
|
+
|
|
1620
|
+
Returns:
|
|
1621
|
+
A FunctionCall representing MD5(expr)
|
|
1622
|
+
"""
|
|
1623
|
+
...
|
|
1624
|
+
|
|
1625
|
+
# `FunctionCall` is not a child of SchemaStatement, but we used
|
|
1626
|
+
# `to_sql` name for this method to make compatible with others
|
|
1627
|
+
def to_sql(self, backend: _Backends) -> str:
|
|
1628
|
+
"""
|
|
1629
|
+
Converts the adapted value to SQL.
|
|
1630
|
+
"""
|
|
1631
|
+
...
|
|
1632
|
+
|
|
1633
|
+
def __repr__(self) -> str:
|
|
1634
|
+
"""
|
|
1635
|
+
Return a developer-friendly string representation.
|
|
1636
|
+
|
|
1637
|
+
Returns:
|
|
1638
|
+
A string that could be used to recreate this function call
|
|
1639
|
+
"""
|
|
1640
|
+
...
|
|
1641
|
+
|
|
1642
|
+
def all(arg1: Expr, *args: Expr) -> Expr:
|
|
1643
|
+
"""
|
|
1644
|
+
Create a logical AND condition that is true only if all conditions are true.
|
|
1645
|
+
|
|
1646
|
+
This is equivalent to SQL's AND operator applied to multiple expressions.
|
|
1647
|
+
|
|
1648
|
+
Args:
|
|
1649
|
+
arg1: The first condition
|
|
1650
|
+
*args: Additional conditions to combine
|
|
1651
|
+
|
|
1652
|
+
Returns:
|
|
1653
|
+
An Expr representing the logical AND of all input expressions
|
|
1654
|
+
|
|
1655
|
+
Example:
|
|
1656
|
+
>>> all(Expr.col("age") > 18, Expr.col("status") == "active")
|
|
1657
|
+
# Equivalent to: age > 18 AND status = 'active'
|
|
1658
|
+
"""
|
|
1659
|
+
...
|
|
1660
|
+
|
|
1661
|
+
def any(arg1: Expr, *args: Expr) -> Expr:
|
|
1662
|
+
"""
|
|
1663
|
+
Create a logical OR condition that is true if any condition is true.
|
|
1664
|
+
|
|
1665
|
+
This is equivalent to SQL's OR operator applied to multiple expressions.
|
|
1666
|
+
|
|
1667
|
+
Args:
|
|
1668
|
+
arg1: The first condition
|
|
1669
|
+
*args: Additional conditions to combine
|
|
1670
|
+
|
|
1671
|
+
Returns:
|
|
1672
|
+
An Expr representing the logical OR of all input expressions
|
|
1673
|
+
|
|
1674
|
+
Example:
|
|
1675
|
+
>>> any(Expr.col("status") == "pending", Expr.col("status") == "approved")
|
|
1676
|
+
# Equivalent to: status = 'pending' OR status = 'approved'
|
|
1677
|
+
"""
|
|
1678
|
+
...
|
|
1679
|
+
|
|
1680
|
+
def not_(arg1: Expr) -> Expr:
|
|
1681
|
+
"""
|
|
1682
|
+
Create a logical NOT.
|
|
1683
|
+
|
|
1684
|
+
Example:
|
|
1685
|
+
>>> not_(Expr.col("status") == "pending", Expr.col("status"))
|
|
1686
|
+
# Equivalent to: NOT status = 'pending'
|
|
1687
|
+
"""
|
|
1688
|
+
...
|
|
1689
|
+
|
|
1690
|
+
class Column(typing.Generic[T]):
|
|
1691
|
+
"""
|
|
1692
|
+
Defines a table column with its properties and constraints.
|
|
1693
|
+
|
|
1694
|
+
Represents a complete column definition including:
|
|
1695
|
+
- Column name and data type
|
|
1696
|
+
- Constraints (primary key, unique, nullable)
|
|
1697
|
+
- Auto-increment behavior
|
|
1698
|
+
- Default values and generated columns
|
|
1699
|
+
- Comments and extra specifications
|
|
1700
|
+
|
|
1701
|
+
This class is used within Table to specify the structure
|
|
1702
|
+
of table columns. It encapsulates all the properties that define how
|
|
1703
|
+
a column behaves and what data it can store.
|
|
1704
|
+
|
|
1705
|
+
Example:
|
|
1706
|
+
>>> Column("id", Integer(), primary_key=True, auto_increment=True)
|
|
1707
|
+
>>> Column("name", String(255), nullable=False, default="unknown")
|
|
1708
|
+
>>> Column("created_at", Timestamp(), default=Expr.current_timestamp())
|
|
1709
|
+
"""
|
|
1710
|
+
|
|
1711
|
+
name: str
|
|
1712
|
+
"""The name of the column."""
|
|
1713
|
+
|
|
1714
|
+
type: ColumnTypeMeta[T]
|
|
1715
|
+
"""The data type of the column."""
|
|
1716
|
+
|
|
1717
|
+
primary_key: bool
|
|
1718
|
+
"""Whether this column is part of the primary key."""
|
|
1719
|
+
|
|
1720
|
+
nullable: bool
|
|
1721
|
+
"""Whether this column can contain NULL values."""
|
|
1722
|
+
|
|
1723
|
+
unique: bool
|
|
1724
|
+
"""Whether this column must contain unique values."""
|
|
1725
|
+
|
|
1726
|
+
auto_increment: bool
|
|
1727
|
+
"""Whether this column should auto-increment."""
|
|
1728
|
+
|
|
1729
|
+
extra: typing.Optional[str]
|
|
1730
|
+
"""Extra SQL specifications for this column."""
|
|
1731
|
+
|
|
1732
|
+
default: typing.Optional[Expr]
|
|
1733
|
+
"""Default value for this column."""
|
|
1734
|
+
|
|
1735
|
+
generated: typing.Optional[Expr]
|
|
1736
|
+
"""Expression for generated column values."""
|
|
1737
|
+
|
|
1738
|
+
stored_generated: bool
|
|
1739
|
+
"""Whether the generated column is STORED (vs VIRTUAL)."""
|
|
1740
|
+
|
|
1741
|
+
comment: typing.Optional[str]
|
|
1742
|
+
"""Comment describing this column."""
|
|
1743
|
+
|
|
1744
|
+
def __new__(
|
|
1745
|
+
cls,
|
|
1746
|
+
name: str,
|
|
1747
|
+
type: ColumnTypeMeta[T],
|
|
1748
|
+
primary_key: bool = ...,
|
|
1749
|
+
unique: bool = ...,
|
|
1750
|
+
nullable: bool = ...,
|
|
1751
|
+
auto_increment: bool = ...,
|
|
1752
|
+
extra: typing.Optional[str] = ...,
|
|
1753
|
+
comment: typing.Optional[str] = ...,
|
|
1754
|
+
default: _ExprValue = ...,
|
|
1755
|
+
generated: _ExprValue = ...,
|
|
1756
|
+
stored_generated: bool = ...,
|
|
1757
|
+
) -> Self:
|
|
1758
|
+
"""
|
|
1759
|
+
Create a new Column definition.
|
|
1760
|
+
|
|
1761
|
+
Args:
|
|
1762
|
+
name: The column name
|
|
1763
|
+
type: The column data type
|
|
1764
|
+
primary_key: Whether this is a primary key column
|
|
1765
|
+
unique: Whether this column has a unique constraint
|
|
1766
|
+
nullable: Whether NULL values are allowed
|
|
1767
|
+
auto_increment: Whether the column auto-increments
|
|
1768
|
+
extra: Additional column specifications
|
|
1769
|
+
comment: Column description comment
|
|
1770
|
+
default: Default value expression
|
|
1771
|
+
generated: Generation expression for computed columns
|
|
1772
|
+
stored_generated: Whether computed column is stored physically
|
|
1773
|
+
|
|
1774
|
+
Returns:
|
|
1775
|
+
A new Column instance
|
|
1776
|
+
"""
|
|
1777
|
+
...
|
|
1778
|
+
|
|
1779
|
+
def to_column_ref(self) -> ColumnRef:
|
|
1780
|
+
"""
|
|
1781
|
+
Convert this column definition to a ColumnRef.
|
|
1782
|
+
|
|
1783
|
+
Returns:
|
|
1784
|
+
A ColumnRef referencing this column
|
|
1785
|
+
"""
|
|
1786
|
+
...
|
|
1787
|
+
|
|
1788
|
+
def to_expr(self) -> Expr:
|
|
1789
|
+
"""
|
|
1790
|
+
Convert this column to an expression.
|
|
1791
|
+
|
|
1792
|
+
Shorthand for `Expr(self.to_column_ref())`
|
|
1793
|
+
|
|
1794
|
+
Returns:
|
|
1795
|
+
An Expr representing this column
|
|
1796
|
+
"""
|
|
1797
|
+
...
|
|
1798
|
+
|
|
1799
|
+
def adapt(self, value: T) -> AdaptedValue[T]:
|
|
1800
|
+
"""
|
|
1801
|
+
Shorthand for `AdaptedValue(value, type=self.type)`
|
|
1802
|
+
"""
|
|
1803
|
+
...
|
|
1804
|
+
|
|
1805
|
+
def __copy__(self) -> Self:
|
|
1806
|
+
"""
|
|
1807
|
+
Create a shallow copy of this Column.
|
|
1808
|
+
"""
|
|
1809
|
+
...
|
|
1810
|
+
|
|
1811
|
+
def copy(self) -> Self:
|
|
1812
|
+
"""
|
|
1813
|
+
Create a copy of this Column.
|
|
1814
|
+
|
|
1815
|
+
Returns:
|
|
1816
|
+
A new Column instance with the same values
|
|
1817
|
+
"""
|
|
1818
|
+
...
|
|
1819
|
+
|
|
1820
|
+
def __repr__(self) -> str:
|
|
1821
|
+
"""
|
|
1822
|
+
Return a developer-friendly string representation.
|
|
1823
|
+
|
|
1824
|
+
Returns:
|
|
1825
|
+
A string showing the column definition
|
|
1826
|
+
"""
|
|
1827
|
+
...
|
|
1828
|
+
|
|
1829
|
+
class TableName:
|
|
1830
|
+
"""
|
|
1831
|
+
Represents a table name reference with optional schema, database, and alias.
|
|
1832
|
+
|
|
1833
|
+
This class encapsulates a table name that can include:
|
|
1834
|
+
- The base table name
|
|
1835
|
+
- Optional schema/namespace qualification
|
|
1836
|
+
- Optional database qualification (for systems that support it)
|
|
1837
|
+
|
|
1838
|
+
The class provides parsing capabilities for string representations
|
|
1839
|
+
and supports comparison operations.
|
|
1840
|
+
|
|
1841
|
+
Examples:
|
|
1842
|
+
>>> TableName("users") # Simple table name
|
|
1843
|
+
>>> TableName("users", schema="public") # Schema-qualified table
|
|
1844
|
+
>>> TableName("users", schema="hr", database="company") # Fully qualified
|
|
1845
|
+
"""
|
|
1846
|
+
|
|
1847
|
+
def __new__(
|
|
1848
|
+
cls,
|
|
1849
|
+
name: str,
|
|
1850
|
+
schema: typing.Optional[str] = ...,
|
|
1851
|
+
database: typing.Optional[str] = ...,
|
|
1852
|
+
alias: typing.Optional[str] = ...,
|
|
1853
|
+
) -> Self:
|
|
1854
|
+
"""
|
|
1855
|
+
Create a new TableName instance.
|
|
1856
|
+
"""
|
|
1857
|
+
...
|
|
1858
|
+
|
|
1859
|
+
@property
|
|
1860
|
+
def name(self) -> str: ...
|
|
1861
|
+
@property
|
|
1862
|
+
def schema(self) -> typing.Optional[str]: ...
|
|
1863
|
+
@property
|
|
1864
|
+
def database(self) -> typing.Optional[str]: ...
|
|
1865
|
+
@property
|
|
1866
|
+
def alias(self) -> typing.Optional[str]: ...
|
|
1867
|
+
@classmethod
|
|
1868
|
+
def parse(cls, string: str) -> Self:
|
|
1869
|
+
"""
|
|
1870
|
+
Parse a string representation of a table name.
|
|
1871
|
+
|
|
1872
|
+
Supports formats like:
|
|
1873
|
+
- "table_name"
|
|
1874
|
+
- "schema.table_name"
|
|
1875
|
+
- "database.schema.table_name"
|
|
1876
|
+
|
|
1877
|
+
Args:
|
|
1878
|
+
string: The string to parse
|
|
1879
|
+
|
|
1880
|
+
Returns:
|
|
1881
|
+
A TableName instance representing the parsed name
|
|
1882
|
+
|
|
1883
|
+
Raises:
|
|
1884
|
+
ValueError: If the string format is invalid
|
|
1885
|
+
"""
|
|
1886
|
+
...
|
|
1887
|
+
|
|
1888
|
+
def __eq__(self, other: Self) -> bool:
|
|
1889
|
+
"""
|
|
1890
|
+
Check equality with another TableName.
|
|
1891
|
+
"""
|
|
1892
|
+
...
|
|
1893
|
+
|
|
1894
|
+
def __ne__(self, other: Self) -> bool:
|
|
1895
|
+
"""
|
|
1896
|
+
Check inequality with another TableName.
|
|
1897
|
+
"""
|
|
1898
|
+
...
|
|
1899
|
+
|
|
1900
|
+
def copy_with(
|
|
1901
|
+
self,
|
|
1902
|
+
*,
|
|
1903
|
+
name: typing.Optional[str] = ...,
|
|
1904
|
+
schema: typing.Optional[str] = ...,
|
|
1905
|
+
database: typing.Optional[str] = ...,
|
|
1906
|
+
alias: typing.Optional[str] = ...,
|
|
1907
|
+
) -> Self: ...
|
|
1908
|
+
def __copy__(self) -> Self:
|
|
1909
|
+
"""
|
|
1910
|
+
Create a shallow copy of this TableName.
|
|
1911
|
+
"""
|
|
1912
|
+
...
|
|
1913
|
+
|
|
1914
|
+
def copy(self) -> Self:
|
|
1915
|
+
"""
|
|
1916
|
+
Create a copy of this TableName.
|
|
1917
|
+
|
|
1918
|
+
Returns:
|
|
1919
|
+
A new TableName instance with the same values
|
|
1920
|
+
"""
|
|
1921
|
+
...
|
|
1922
|
+
|
|
1923
|
+
def __repr__(self) -> str:
|
|
1924
|
+
"""
|
|
1925
|
+
Return a string representation of the TableName.
|
|
1926
|
+
"""
|
|
1927
|
+
...
|
|
1928
|
+
|
|
1929
|
+
_ForeignKeyActions = typing.Literal["CASCADE", "NO ACTION", "RESTRICT", "SET DEFAULT", "SET NULL"]
|
|
1930
|
+
|
|
1931
|
+
class ForeignKey:
|
|
1932
|
+
"""
|
|
1933
|
+
Specifies a foreign key relationship between tables.
|
|
1934
|
+
|
|
1935
|
+
Defines referential integrity constraints including:
|
|
1936
|
+
- Source columns (in the child table)
|
|
1937
|
+
- Target columns (in the parent table)
|
|
1938
|
+
- Actions for updates and deletes (CASCADE, RESTRICT, SET NULL, etc.)
|
|
1939
|
+
- Optional naming for the constraint
|
|
1940
|
+
|
|
1941
|
+
Foreign keys ensure data consistency by requiring that values in the
|
|
1942
|
+
child table's columns match existing values in the parent table's columns.
|
|
1943
|
+
|
|
1944
|
+
Example:
|
|
1945
|
+
>>> ForeignKey(
|
|
1946
|
+
... from_columns=["user_id"],
|
|
1947
|
+
... to_columns=["id"],
|
|
1948
|
+
... to_table="users",
|
|
1949
|
+
... on_delete="CASCADE",
|
|
1950
|
+
... on_update="RESTRICT"
|
|
1951
|
+
... )
|
|
1952
|
+
"""
|
|
1953
|
+
|
|
1954
|
+
from_columns: typing.List[str]
|
|
1955
|
+
"""
|
|
1956
|
+
The column names in the child table that reference the parent.
|
|
1957
|
+
|
|
1958
|
+
Note: This attribute is immutable. To modify it, create a new list:
|
|
1959
|
+
|
|
1960
|
+
fk.from_columns.append("file_id") # Wrong ❌
|
|
1961
|
+
fk.from_columns = ["id", "name"] # Correct ✅
|
|
1962
|
+
"""
|
|
1963
|
+
|
|
1964
|
+
to_columns: typing.List[str]
|
|
1965
|
+
"""
|
|
1966
|
+
The column names in the parent table being referenced.
|
|
1967
|
+
|
|
1968
|
+
Note: This attribute is immutable. To modify it, create a new list:
|
|
1969
|
+
|
|
1970
|
+
fk.to_columns.append("file_id") # Wrong ❌
|
|
1971
|
+
fk.to_columns = ["id", "name"] # Correct ✅
|
|
1972
|
+
"""
|
|
1973
|
+
|
|
1974
|
+
to_table: TableName
|
|
1975
|
+
"""The parent table being referenced."""
|
|
1976
|
+
|
|
1977
|
+
from_table: typing.Optional[TableName]
|
|
1978
|
+
"""The child table containing the foreign key (optional if inferred)."""
|
|
1979
|
+
|
|
1980
|
+
name: typing.Optional[str]
|
|
1981
|
+
"""The name of the foreign key constraint."""
|
|
1982
|
+
|
|
1983
|
+
on_delete: typing.Optional[_ForeignKeyActions]
|
|
1984
|
+
"""Action to take when referenced row is deleted."""
|
|
1985
|
+
|
|
1986
|
+
on_update: typing.Optional[_ForeignKeyActions]
|
|
1987
|
+
"""Action to take when referenced row is updated."""
|
|
1988
|
+
|
|
1989
|
+
def __new__(
|
|
1990
|
+
cls,
|
|
1991
|
+
from_columns: typing.Sequence[str],
|
|
1992
|
+
to_columns: typing.Sequence[str],
|
|
1993
|
+
to_table: typing.Union[TableName, str],
|
|
1994
|
+
from_table: typing.Union[TableName, str, None] = ...,
|
|
1995
|
+
name: typing.Optional[str] = ...,
|
|
1996
|
+
on_delete: typing.Optional[_ForeignKeyActions] = ...,
|
|
1997
|
+
on_update: typing.Optional[_ForeignKeyActions] = ...,
|
|
1998
|
+
) -> None:
|
|
1999
|
+
"""
|
|
2000
|
+
Create a new ForeignKey.
|
|
2001
|
+
|
|
2002
|
+
Args:
|
|
2003
|
+
from_columns: Columns in the child/referencing table
|
|
2004
|
+
to_columns: Columns in the parent/referenced table
|
|
2005
|
+
to_table: The parent table being referenced
|
|
2006
|
+
from_table: The child table (optional, often inferred from context)
|
|
2007
|
+
name: Constraint name (optional)
|
|
2008
|
+
on_delete: Action on parent row deletion
|
|
2009
|
+
on_update: Action on parent row update
|
|
2010
|
+
|
|
2011
|
+
Returns:
|
|
2012
|
+
A new ForeignKey instance
|
|
2013
|
+
"""
|
|
2014
|
+
...
|
|
2015
|
+
|
|
2016
|
+
def __copy__(self) -> Self:
|
|
2017
|
+
"""
|
|
2018
|
+
Create a shallow copy of this ForeignKey.
|
|
2019
|
+
"""
|
|
2020
|
+
...
|
|
2021
|
+
|
|
2022
|
+
def copy(self) -> Self:
|
|
2023
|
+
"""
|
|
2024
|
+
Create a copy of this ForeignKey.
|
|
2025
|
+
|
|
2026
|
+
Returns:
|
|
2027
|
+
A new ForeignKey instance with the same values
|
|
2028
|
+
"""
|
|
2029
|
+
...
|
|
2030
|
+
|
|
2031
|
+
def __repr__(self) -> str:
|
|
2032
|
+
"""
|
|
2033
|
+
Return a string representation of the ForeignKey.
|
|
2034
|
+
"""
|
|
2035
|
+
...
|
|
2036
|
+
|
|
2037
|
+
class IndexColumn:
|
|
2038
|
+
"""
|
|
2039
|
+
Defines a column within an index specification.
|
|
2040
|
+
|
|
2041
|
+
Represents a single column's participation in an index, including:
|
|
2042
|
+
- The column name
|
|
2043
|
+
- Optional prefix length (for partial indexing)
|
|
2044
|
+
- Sort order (ascending or descending)
|
|
2045
|
+
|
|
2046
|
+
Used within Index to specify which columns are indexed
|
|
2047
|
+
and how they should be ordered.
|
|
2048
|
+
|
|
2049
|
+
Example:
|
|
2050
|
+
|
|
2051
|
+
>>> IndexColumn("name") # Simple column
|
|
2052
|
+
>>> IndexColumn("email", order="desc") # Descending order
|
|
2053
|
+
>>> IndexColumn("content", prefix=100) # Prefix indexing for long text
|
|
2054
|
+
"""
|
|
2055
|
+
|
|
2056
|
+
name: str
|
|
2057
|
+
"""The name of the column to include in the index."""
|
|
2058
|
+
|
|
2059
|
+
prefix: typing.Optional[int]
|
|
2060
|
+
"""Number of characters to index for string columns (prefix indexing)."""
|
|
2061
|
+
|
|
2062
|
+
order: typing.Optional[typing.Literal["asc", "desc"]]
|
|
2063
|
+
"""Sort order for this column ("asc" or "desc")."""
|
|
2064
|
+
|
|
2065
|
+
def __new__(
|
|
2066
|
+
cls,
|
|
2067
|
+
name: str,
|
|
2068
|
+
prefix: typing.Optional[int] = ...,
|
|
2069
|
+
order: typing.Optional[typing.Literal["asc", "desc"]] = ...,
|
|
2070
|
+
) -> Self:
|
|
2071
|
+
"""
|
|
2072
|
+
Create a new IndexColumn.
|
|
2073
|
+
|
|
2074
|
+
Args:
|
|
2075
|
+
name: The column name
|
|
2076
|
+
prefix: Prefix length for string columns
|
|
2077
|
+
order: Sort order ("asc" or "desc")
|
|
2078
|
+
|
|
2079
|
+
Returns:
|
|
2080
|
+
A new IndexColumn instance
|
|
2081
|
+
"""
|
|
2082
|
+
...
|
|
2083
|
+
|
|
2084
|
+
def __copy__(self) -> Self:
|
|
2085
|
+
"""
|
|
2086
|
+
Create a shallow copy of this IndexColumn.
|
|
2087
|
+
"""
|
|
2088
|
+
...
|
|
2089
|
+
|
|
2090
|
+
def copy(self) -> Self:
|
|
2091
|
+
"""
|
|
2092
|
+
Create a copy of this IndexColumn.
|
|
2093
|
+
|
|
2094
|
+
Returns:
|
|
2095
|
+
A new IndexColumn instance with the same values
|
|
2096
|
+
"""
|
|
2097
|
+
...
|
|
2098
|
+
|
|
2099
|
+
def __repr__(self) -> str:
|
|
2100
|
+
"""
|
|
2101
|
+
Return a string representation of the IndexColumn.
|
|
2102
|
+
"""
|
|
2103
|
+
...
|
|
2104
|
+
|
|
2105
|
+
_IndexType = typing.Literal["BTREE", "FULL TEXT", "HASH"]
|
|
2106
|
+
|
|
2107
|
+
class Index(SchemaStatement):
|
|
2108
|
+
"""
|
|
2109
|
+
Represents a database index specification.
|
|
2110
|
+
|
|
2111
|
+
This class defines the structure and properties of a database index,
|
|
2112
|
+
including column definitions, uniqueness constraints, index type,
|
|
2113
|
+
and partial indexing conditions.
|
|
2114
|
+
|
|
2115
|
+
You can use it to generate `CREATE INDEX` SQL expressions.
|
|
2116
|
+
|
|
2117
|
+
Example:
|
|
2118
|
+
|
|
2119
|
+
>>> Index(
|
|
2120
|
+
... columns=["id", IndexColumn("name", order="desc")],
|
|
2121
|
+
... name="idx_user_name",
|
|
2122
|
+
... unique=True
|
|
2123
|
+
... )
|
|
2124
|
+
"""
|
|
2125
|
+
|
|
2126
|
+
name: str
|
|
2127
|
+
"""The name of the index."""
|
|
2128
|
+
|
|
2129
|
+
table: typing.Optional[TableName]
|
|
2130
|
+
"""The table on which to create the index."""
|
|
2131
|
+
|
|
2132
|
+
if_not_exists: bool
|
|
2133
|
+
"""Whether to use IF NOT EXISTS clause."""
|
|
2134
|
+
|
|
2135
|
+
primary: bool
|
|
2136
|
+
"""Whether this is a primary key constraint."""
|
|
2137
|
+
|
|
2138
|
+
unique: bool
|
|
2139
|
+
"""Whether this is a unique constraint."""
|
|
2140
|
+
|
|
2141
|
+
nulls_not_distinct: bool
|
|
2142
|
+
"""Whether NULL values should be considered equal for uniqueness."""
|
|
2143
|
+
|
|
2144
|
+
include: typing.Sequence[str]
|
|
2145
|
+
"""Additional columns to include in the index for covering queries."""
|
|
2146
|
+
|
|
2147
|
+
columns: typing.Sequence[typing.Union[IndexColumn, str]]
|
|
2148
|
+
"""The columns that make up this index."""
|
|
2149
|
+
|
|
2150
|
+
index_type: typing.Optional[typing.Union[str, _IndexType]]
|
|
2151
|
+
"""The type/algorithm for this index."""
|
|
2152
|
+
|
|
2153
|
+
where: typing.Optional[Expr]
|
|
2154
|
+
"""Condition for partial indexing."""
|
|
2155
|
+
|
|
2156
|
+
def __new__(
|
|
2157
|
+
cls,
|
|
2158
|
+
columns: typing.Sequence[typing.Union[IndexColumn, str]],
|
|
2159
|
+
name: typing.Optional[str] = ...,
|
|
2160
|
+
table: typing.Union[str, TableName] = ...,
|
|
2161
|
+
if_not_exists: bool = ...,
|
|
2162
|
+
primary: bool = ...,
|
|
2163
|
+
unique: bool = ...,
|
|
2164
|
+
nulls_not_distinct: bool = ...,
|
|
2165
|
+
include: typing.Sequence[str] = ...,
|
|
2166
|
+
index_type: typing.Union[str, _IndexType] = ...,
|
|
2167
|
+
where: typing.Optional[Expr] = ...,
|
|
2168
|
+
) -> Self:
|
|
2169
|
+
"""
|
|
2170
|
+
Create a new Index specification.
|
|
2171
|
+
|
|
2172
|
+
Args:
|
|
2173
|
+
columns: The columns to include in the index
|
|
2174
|
+
name: The index name (optional)
|
|
2175
|
+
table: The table to index (optional)
|
|
2176
|
+
if_not_exists: Whether to use IF NOT EXISTS
|
|
2177
|
+
primary: Whether this is a primary key
|
|
2178
|
+
unique: Whether to enforce uniqueness
|
|
2179
|
+
nulls_not_distinct: Whether NULLs are distinct for uniqueness
|
|
2180
|
+
include: Additional included columns
|
|
2181
|
+
index_type: The index algorithm type
|
|
2182
|
+
where: Condition for partial indexing
|
|
2183
|
+
|
|
2184
|
+
Returns:
|
|
2185
|
+
A new Index instance
|
|
2186
|
+
"""
|
|
2187
|
+
...
|
|
2188
|
+
|
|
2189
|
+
def __copy__(self) -> Self:
|
|
2190
|
+
"""
|
|
2191
|
+
Create a shallow copy of this Index.
|
|
2192
|
+
"""
|
|
2193
|
+
...
|
|
2194
|
+
|
|
2195
|
+
def copy(self) -> Self:
|
|
2196
|
+
"""
|
|
2197
|
+
Create a copy of this Index.
|
|
2198
|
+
|
|
2199
|
+
Returns:
|
|
2200
|
+
A new Index instance with the same values
|
|
2201
|
+
"""
|
|
2202
|
+
...
|
|
2203
|
+
|
|
2204
|
+
def __repr__(self) -> str:
|
|
2205
|
+
"""
|
|
2206
|
+
Return a string representation of the Index.
|
|
2207
|
+
"""
|
|
2208
|
+
...
|
|
2209
|
+
|
|
2210
|
+
class DropIndex(SchemaStatement):
|
|
2211
|
+
"""
|
|
2212
|
+
Represents a DROP INDEX SQL statement.
|
|
2213
|
+
|
|
2214
|
+
Builds index deletion statements with support for:
|
|
2215
|
+
- Conditional deletion (IF EXISTS)
|
|
2216
|
+
- Table-specific index dropping (for databases that require it)
|
|
2217
|
+
- Proper error handling for non-existent indexes
|
|
2218
|
+
|
|
2219
|
+
Example:
|
|
2220
|
+
|
|
2221
|
+
>>> DropIndex(
|
|
2222
|
+
... name="idx_user_name",
|
|
2223
|
+
... if_exists=True
|
|
2224
|
+
... )
|
|
2225
|
+
"""
|
|
2226
|
+
|
|
2227
|
+
name: str
|
|
2228
|
+
"""The name of the index to drop."""
|
|
2229
|
+
|
|
2230
|
+
table: typing.Optional[TableName]
|
|
2231
|
+
"""The table from which to drop the index."""
|
|
2232
|
+
|
|
2233
|
+
if_exists: bool
|
|
2234
|
+
"""Whether to use IF EXISTS clause to avoid errors."""
|
|
2235
|
+
|
|
2236
|
+
def __new__(
|
|
2237
|
+
self,
|
|
2238
|
+
name: str = ...,
|
|
2239
|
+
table: typing.Optional[TableName] = ...,
|
|
2240
|
+
if_exists: bool = ...,
|
|
2241
|
+
) -> Self:
|
|
2242
|
+
"""
|
|
2243
|
+
Create a new DropIndex.
|
|
2244
|
+
|
|
2245
|
+
Args:
|
|
2246
|
+
name: The index name
|
|
2247
|
+
table: The table from which to drop the index (optional)
|
|
2248
|
+
if_exists: Whether to use IF EXISTS
|
|
2249
|
+
|
|
2250
|
+
Returns:
|
|
2251
|
+
A new Index instance
|
|
2252
|
+
"""
|
|
2253
|
+
...
|
|
2254
|
+
|
|
2255
|
+
def __copy__(self) -> Self:
|
|
2256
|
+
"""
|
|
2257
|
+
Create a shallow copy of this DropIndex.
|
|
2258
|
+
"""
|
|
2259
|
+
...
|
|
2260
|
+
|
|
2261
|
+
def copy(self) -> Self:
|
|
2262
|
+
"""
|
|
2263
|
+
Create a copy of this DropIndex.
|
|
2264
|
+
|
|
2265
|
+
Returns:
|
|
2266
|
+
A new DropIndex instance with the same values
|
|
2267
|
+
"""
|
|
2268
|
+
...
|
|
2269
|
+
|
|
2270
|
+
def __repr__(self) -> str:
|
|
2271
|
+
"""
|
|
2272
|
+
Return a string representation of the DropIndex.
|
|
2273
|
+
"""
|
|
2274
|
+
...
|
|
2275
|
+
|
|
2276
|
+
class _TableColumnsSequence:
|
|
2277
|
+
def __getattr__(self, name: str) -> Column: ...
|
|
2278
|
+
def get(self, name: str) -> Column: ...
|
|
2279
|
+
def append(self, col: Column) -> None: ...
|
|
2280
|
+
def remove(self, name: str) -> None: ...
|
|
2281
|
+
def to_list(self) -> typing.Sequence[Column]: ...
|
|
2282
|
+
def clear(self) -> None: ...
|
|
2283
|
+
def __len__(self) -> int: ...
|
|
2284
|
+
|
|
2285
|
+
class Table(SchemaStatement):
|
|
2286
|
+
"""
|
|
2287
|
+
Represents a complete database table definition.
|
|
2288
|
+
|
|
2289
|
+
This class encapsulates all aspects of a table structure including:
|
|
2290
|
+
- Column definitions with their types and constraints
|
|
2291
|
+
- Indexes for query optimization
|
|
2292
|
+
- Foreign key relationships for referential integrity
|
|
2293
|
+
- Check constraints for data validation
|
|
2294
|
+
- Table-level options like engine, collation, and character set
|
|
2295
|
+
|
|
2296
|
+
Used to generate CREATE TABLE SQL statements with full schema specifications.
|
|
2297
|
+
|
|
2298
|
+
Example:
|
|
2299
|
+
>>> Table(
|
|
2300
|
+
... "users",
|
|
2301
|
+
... columns=[
|
|
2302
|
+
... Column("id", Integer, primary_key=True, auto_increment=True),
|
|
2303
|
+
... Column("email", String(255), unique=True, nullable=False)
|
|
2304
|
+
... ],
|
|
2305
|
+
... indexes=[Index(["email"])],
|
|
2306
|
+
... if_not_exists=True
|
|
2307
|
+
... )
|
|
2308
|
+
"""
|
|
2309
|
+
|
|
2310
|
+
indexes: typing.Sequence[Index]
|
|
2311
|
+
"""Indexes defined on this table for query optimization."""
|
|
2312
|
+
|
|
2313
|
+
foreign_keys: typing.Sequence[ForeignKey]
|
|
2314
|
+
"""Foreign key constraints defining relationships with other tables."""
|
|
2315
|
+
|
|
2316
|
+
checks: typing.Sequence[Expr]
|
|
2317
|
+
"""Check constraints for validating data integrity."""
|
|
2318
|
+
|
|
2319
|
+
if_not_exists: bool
|
|
2320
|
+
"""Whether to use IF NOT EXISTS clause to avoid errors if table exists."""
|
|
2321
|
+
|
|
2322
|
+
temporary: bool
|
|
2323
|
+
"""Whether this is a temporary table that exists only for the session."""
|
|
2324
|
+
|
|
2325
|
+
comment: typing.Optional[str]
|
|
2326
|
+
"""Comment describing the purpose of this table."""
|
|
2327
|
+
|
|
2328
|
+
engine: typing.Optional[str]
|
|
2329
|
+
"""Storage engine for the table (e.g., InnoDB, MyISAM for MySQL)."""
|
|
2330
|
+
|
|
2331
|
+
collate: typing.Optional[str]
|
|
2332
|
+
"""Collation for string comparisons and sorting in this table."""
|
|
2333
|
+
|
|
2334
|
+
character_set: typing.Optional[str]
|
|
2335
|
+
"""Character set encoding for text data in this table."""
|
|
2336
|
+
|
|
2337
|
+
extra: typing.Optional[str]
|
|
2338
|
+
"""Additional table-specific options for the CREATE TABLE statement."""
|
|
2339
|
+
|
|
2340
|
+
def __new__(
|
|
2341
|
+
cls,
|
|
2342
|
+
name: typing.Union[str, TableName],
|
|
2343
|
+
columns: typing.Sequence[Column],
|
|
2344
|
+
indexes: typing.Sequence[Index] = ...,
|
|
2345
|
+
foreign_keys: typing.Sequence[ForeignKey] = ...,
|
|
2346
|
+
checks: typing.Sequence[Expr] = ...,
|
|
2347
|
+
if_not_exists: bool = ...,
|
|
2348
|
+
temporary: bool = ...,
|
|
2349
|
+
comment: typing.Optional[str] = ...,
|
|
2350
|
+
engine: typing.Optional[str] = ...,
|
|
2351
|
+
collate: typing.Optional[str] = ...,
|
|
2352
|
+
character_set: typing.Optional[str] = ...,
|
|
2353
|
+
extra: typing.Optional[str] = ...,
|
|
2354
|
+
) -> Self:
|
|
2355
|
+
"""
|
|
2356
|
+
Create a new Table definition.
|
|
2357
|
+
|
|
2358
|
+
Args:
|
|
2359
|
+
name: The table name, optionally schema-qualified
|
|
2360
|
+
columns: List of column definitions
|
|
2361
|
+
indexes: List of index definitions
|
|
2362
|
+
foreign_keys: List of foreign key constraints
|
|
2363
|
+
checks: List of check constraint expressions
|
|
2364
|
+
if_not_exists: Whether to use IF NOT EXISTS clause
|
|
2365
|
+
temporary: Whether to create a temporary table
|
|
2366
|
+
comment: Table description comment
|
|
2367
|
+
engine: Storage engine specification
|
|
2368
|
+
collate: Collation specification
|
|
2369
|
+
character_set: Character set specification
|
|
2370
|
+
extra: Additional SQL specifications
|
|
2371
|
+
|
|
2372
|
+
Returns:
|
|
2373
|
+
A new Table instance
|
|
2374
|
+
"""
|
|
2375
|
+
...
|
|
2376
|
+
|
|
2377
|
+
@property
|
|
2378
|
+
def name(self) -> TableName:
|
|
2379
|
+
"""The name of this table."""
|
|
2380
|
+
...
|
|
2381
|
+
|
|
2382
|
+
@property
|
|
2383
|
+
def columns(self) -> _TableColumnsSequence:
|
|
2384
|
+
"""Returns columns as `_TableColumnsSequence`"""
|
|
2385
|
+
...
|
|
2386
|
+
|
|
2387
|
+
@property
|
|
2388
|
+
def c(self) -> _TableColumnsSequence:
|
|
2389
|
+
"""Returns columns as `_TableColumnsSequence`. It is an alias for `self.columns`"""
|
|
2390
|
+
...
|
|
2391
|
+
|
|
2392
|
+
def __repr__(self) -> str: ...
|
|
2393
|
+
|
|
2394
|
+
class _AliasedTableColumnsSequence:
|
|
2395
|
+
def __getattr__(self, name: str) -> ColumnRef: ...
|
|
2396
|
+
def get(self, name: str) -> ColumnRef: ...
|
|
2397
|
+
def __len__(self) -> int: ...
|
|
2398
|
+
|
|
2399
|
+
class AliasedTable:
|
|
2400
|
+
def __new__(cls, table: typing.Union[Table, Self], alias: str) -> Self: ...
|
|
2401
|
+
@property
|
|
2402
|
+
def name(self) -> TableName:
|
|
2403
|
+
"""The name of aliased table."""
|
|
2404
|
+
...
|
|
2405
|
+
|
|
2406
|
+
@property
|
|
2407
|
+
def columns(self) -> _AliasedTableColumnsSequence:
|
|
2408
|
+
"""Returns columns as `_AliasedTableColumnsSequence`"""
|
|
2409
|
+
...
|
|
2410
|
+
|
|
2411
|
+
@property
|
|
2412
|
+
def c(self) -> _AliasedTableColumnsSequence:
|
|
2413
|
+
"""Returns columns as `_AliasedTableColumnsSequence`. It is an alias for `self.columns`"""
|
|
2414
|
+
...
|
|
2415
|
+
|
|
2416
|
+
def __repr__(self) -> str: ...
|
|
2417
|
+
|
|
2418
|
+
class DropTable(SchemaStatement):
|
|
2419
|
+
"""
|
|
2420
|
+
Represents a DROP TABLE SQL statement.
|
|
2421
|
+
|
|
2422
|
+
Builds table deletion statements with support for:
|
|
2423
|
+
- Conditional deletion (IF EXISTS) to avoid errors
|
|
2424
|
+
- CASCADE to drop dependent objects
|
|
2425
|
+
- RESTRICT to prevent deletion if dependencies exist
|
|
2426
|
+
|
|
2427
|
+
Example:
|
|
2428
|
+
>>> DropTable("old_users", if_exists=True, cascade=True)
|
|
2429
|
+
"""
|
|
2430
|
+
|
|
2431
|
+
name: TableName
|
|
2432
|
+
"""The name of the table to drop."""
|
|
2433
|
+
|
|
2434
|
+
if_exists: bool
|
|
2435
|
+
"""Whether to use IF EXISTS clause to avoid errors if table doesn't exist."""
|
|
2436
|
+
|
|
2437
|
+
restrict: bool
|
|
2438
|
+
"""Whether to use RESTRICT to prevent dropping if dependencies exist."""
|
|
2439
|
+
|
|
2440
|
+
cascade: bool
|
|
2441
|
+
"""Whether to use CASCADE to drop dependent objects automatically."""
|
|
2442
|
+
|
|
2443
|
+
def __new__(
|
|
2444
|
+
cls,
|
|
2445
|
+
name: typing.Union[str, TableName],
|
|
2446
|
+
if_exists: bool = ...,
|
|
2447
|
+
restrict: bool = ...,
|
|
2448
|
+
cascade: bool = ...,
|
|
2449
|
+
) -> Self:
|
|
2450
|
+
"""
|
|
2451
|
+
Create a new DropTable statement.
|
|
2452
|
+
|
|
2453
|
+
Args:
|
|
2454
|
+
name: The table name to drop
|
|
2455
|
+
if_exists: Whether to use IF EXISTS clause
|
|
2456
|
+
restrict: Whether to use RESTRICT mode
|
|
2457
|
+
cascade: Whether to use CASCADE mode
|
|
2458
|
+
|
|
2459
|
+
Returns:
|
|
2460
|
+
A new DropTable instance
|
|
2461
|
+
"""
|
|
2462
|
+
...
|
|
2463
|
+
|
|
2464
|
+
def __copy__(self) -> Self:
|
|
2465
|
+
"""
|
|
2466
|
+
Create a shallow copy of this DropTable.
|
|
2467
|
+
"""
|
|
2468
|
+
...
|
|
2469
|
+
|
|
2470
|
+
def copy(self) -> Self:
|
|
2471
|
+
"""
|
|
2472
|
+
Create a copy of this DropTable.
|
|
2473
|
+
|
|
2474
|
+
Returns:
|
|
2475
|
+
A new DropTable instance with the same values
|
|
2476
|
+
"""
|
|
2477
|
+
...
|
|
2478
|
+
|
|
2479
|
+
def __repr__(self) -> str:
|
|
2480
|
+
"""
|
|
2481
|
+
Return a developer-friendly string representation.
|
|
2482
|
+
|
|
2483
|
+
Returns:
|
|
2484
|
+
A string showing the drop table statement
|
|
2485
|
+
"""
|
|
2486
|
+
...
|
|
2487
|
+
|
|
2488
|
+
class RenameTable(SchemaStatement):
|
|
2489
|
+
"""
|
|
2490
|
+
Represents a RENAME TABLE SQL statement.
|
|
2491
|
+
|
|
2492
|
+
Changes the name of an existing table to a new name. Both names can be
|
|
2493
|
+
schema-qualified if needed.
|
|
2494
|
+
|
|
2495
|
+
Example:
|
|
2496
|
+
>>> RenameTable("old_users", "users")
|
|
2497
|
+
>>> RenameTable("public.old_users", "archive.users")
|
|
2498
|
+
"""
|
|
2499
|
+
|
|
2500
|
+
from_name: TableName
|
|
2501
|
+
"""The current name of the table."""
|
|
2502
|
+
|
|
2503
|
+
to_name: TableName
|
|
2504
|
+
"""The new name for the table."""
|
|
2505
|
+
|
|
2506
|
+
def __new__(
|
|
2507
|
+
cls,
|
|
2508
|
+
from_name: typing.Union[str, TableName],
|
|
2509
|
+
to_name: typing.Union[str, TableName],
|
|
2510
|
+
) -> Self:
|
|
2511
|
+
"""
|
|
2512
|
+
Create a new RenameTable statement.
|
|
2513
|
+
|
|
2514
|
+
Args:
|
|
2515
|
+
from_name: The current table name
|
|
2516
|
+
to_name: The new table name
|
|
2517
|
+
|
|
2518
|
+
Returns:
|
|
2519
|
+
A new RenameTable instance
|
|
2520
|
+
"""
|
|
2521
|
+
...
|
|
2522
|
+
|
|
2523
|
+
def __copy__(self) -> Self:
|
|
2524
|
+
"""
|
|
2525
|
+
Create a shallow copy of this RenameTable.
|
|
2526
|
+
"""
|
|
2527
|
+
...
|
|
2528
|
+
|
|
2529
|
+
def copy(self) -> Self:
|
|
2530
|
+
"""
|
|
2531
|
+
Create a copy of this RenameTable.
|
|
2532
|
+
|
|
2533
|
+
Returns:
|
|
2534
|
+
A new RenameTable instance with the same values
|
|
2535
|
+
"""
|
|
2536
|
+
...
|
|
2537
|
+
|
|
2538
|
+
def __repr__(self) -> str:
|
|
2539
|
+
"""
|
|
2540
|
+
Return a developer-friendly string representation.
|
|
2541
|
+
|
|
2542
|
+
Returns:
|
|
2543
|
+
A string showing the rename table statement
|
|
2544
|
+
"""
|
|
2545
|
+
...
|
|
2546
|
+
|
|
2547
|
+
class TruncateTable(SchemaStatement):
|
|
2548
|
+
"""
|
|
2549
|
+
Represents a TRUNCATE TABLE SQL statement.
|
|
2550
|
+
|
|
2551
|
+
Quickly removes all rows from a table, typically faster than DELETE
|
|
2552
|
+
and with different transaction and trigger behavior depending on the
|
|
2553
|
+
database system.
|
|
2554
|
+
|
|
2555
|
+
Example:
|
|
2556
|
+
>>> TruncateTable("temp_data")
|
|
2557
|
+
"""
|
|
2558
|
+
|
|
2559
|
+
name: TableName
|
|
2560
|
+
"""The name of the table to truncate."""
|
|
2561
|
+
|
|
2562
|
+
def __new__(
|
|
2563
|
+
cls,
|
|
2564
|
+
name: typing.Union[str, TableName],
|
|
2565
|
+
) -> Self:
|
|
2566
|
+
"""
|
|
2567
|
+
Create a new TruncateTable statement.
|
|
2568
|
+
|
|
2569
|
+
Args:
|
|
2570
|
+
name: The table name to truncate
|
|
2571
|
+
|
|
2572
|
+
Returns:
|
|
2573
|
+
A new TruncateTable instance
|
|
2574
|
+
"""
|
|
2575
|
+
...
|
|
2576
|
+
|
|
2577
|
+
def __copy__(self) -> Self:
|
|
2578
|
+
"""
|
|
2579
|
+
Create a shallow copy of this TruncateTable.
|
|
2580
|
+
"""
|
|
2581
|
+
...
|
|
2582
|
+
|
|
2583
|
+
def copy(self) -> Self:
|
|
2584
|
+
"""
|
|
2585
|
+
Create a copy of this TruncateTable.
|
|
2586
|
+
|
|
2587
|
+
Returns:
|
|
2588
|
+
A new TruncateTable instance with the same values
|
|
2589
|
+
"""
|
|
2590
|
+
...
|
|
2591
|
+
|
|
2592
|
+
def __repr__(self) -> str:
|
|
2593
|
+
"""
|
|
2594
|
+
Return a developer-friendly string representation.
|
|
2595
|
+
|
|
2596
|
+
Returns:
|
|
2597
|
+
A string showing the truncate table statement
|
|
2598
|
+
"""
|
|
2599
|
+
...
|
|
2600
|
+
|
|
2601
|
+
class AlterTableOptionMeta:
|
|
2602
|
+
"""
|
|
2603
|
+
Base class for all ALTER TABLE operation types.
|
|
2604
|
+
|
|
2605
|
+
This abstract base class represents the different types of modifications
|
|
2606
|
+
that can be made to an existing table structure, such as adding/dropping
|
|
2607
|
+
columns, modifying column definitions, or managing foreign keys.
|
|
2608
|
+
"""
|
|
2609
|
+
|
|
2610
|
+
def __new__(cls): ...
|
|
2611
|
+
def __repr__(self) -> str:
|
|
2612
|
+
"""
|
|
2613
|
+
Return a developer-friendly string representation.
|
|
2614
|
+
"""
|
|
2615
|
+
...
|
|
2616
|
+
|
|
2617
|
+
class AlterTableAddColumnOption(AlterTableOptionMeta):
|
|
2618
|
+
"""
|
|
2619
|
+
ALTER TABLE operation to add a new column.
|
|
2620
|
+
|
|
2621
|
+
Adds a column to an existing table with optional IF NOT EXISTS clause
|
|
2622
|
+
to prevent errors if the column already exists.
|
|
2623
|
+
|
|
2624
|
+
Example:
|
|
2625
|
+
>>> AlterTableAddColumnOption(
|
|
2626
|
+
... Column("created_at", Timestamp, nullable=False),
|
|
2627
|
+
... if_not_exists=True
|
|
2628
|
+
... )
|
|
2629
|
+
"""
|
|
2630
|
+
|
|
2631
|
+
def __new__(cls, column: Column, if_not_exists: bool) -> Self: ...
|
|
2632
|
+
@property
|
|
2633
|
+
def column(self) -> Column: ...
|
|
2634
|
+
@property
|
|
2635
|
+
def if_not_exists(self) -> bool: ...
|
|
2636
|
+
|
|
2637
|
+
class AlterTableAddForeignKeyOption(AlterTableOptionMeta):
|
|
2638
|
+
"""
|
|
2639
|
+
ALTER TABLE operation to add a foreign key constraint.
|
|
2640
|
+
|
|
2641
|
+
Adds referential integrity between tables by creating a foreign key
|
|
2642
|
+
relationship on an existing table.
|
|
2643
|
+
|
|
2644
|
+
Example:
|
|
2645
|
+
>>> AlterTableAddForeignKeyOption(
|
|
2646
|
+
... ForeignKey(
|
|
2647
|
+
... from_columns=["user_id"],
|
|
2648
|
+
... to_columns=["id"],
|
|
2649
|
+
... to_table="users",
|
|
2650
|
+
... on_delete="CASCADE"
|
|
2651
|
+
... )
|
|
2652
|
+
... )
|
|
2653
|
+
"""
|
|
2654
|
+
|
|
2655
|
+
def __new__(cls, foreign_key: ForeignKey) -> Self: ...
|
|
2656
|
+
@property
|
|
2657
|
+
def foreign_key(self) -> ForeignKey: ...
|
|
2658
|
+
def __repr__(self) -> str: ...
|
|
2659
|
+
|
|
2660
|
+
class AlterTableDropColumnOption(AlterTableOptionMeta):
|
|
2661
|
+
"""
|
|
2662
|
+
ALTER TABLE operation to drop an existing column.
|
|
2663
|
+
|
|
2664
|
+
Removes a column from the table. This operation may fail if the column
|
|
2665
|
+
is referenced by other database objects.
|
|
2666
|
+
|
|
2667
|
+
Example:
|
|
2668
|
+
>>> AlterTableDropColumnOption("deprecated_field")
|
|
2669
|
+
"""
|
|
2670
|
+
|
|
2671
|
+
def __new__(cls, name: str) -> Self: ...
|
|
2672
|
+
@property
|
|
2673
|
+
def name(self) -> str: ...
|
|
2674
|
+
def __repr__(self) -> str: ...
|
|
2675
|
+
|
|
2676
|
+
class AlterTableDropForeignKeyOption(AlterTableOptionMeta):
|
|
2677
|
+
"""
|
|
2678
|
+
ALTER TABLE operation to drop a foreign key constraint.
|
|
2679
|
+
|
|
2680
|
+
Removes a foreign key relationship by its constraint name.
|
|
2681
|
+
|
|
2682
|
+
Example:
|
|
2683
|
+
>>> AlterTableDropForeignKeyOption("fk_user_posts")
|
|
2684
|
+
"""
|
|
2685
|
+
|
|
2686
|
+
def __new__(cls, name: str) -> Self: ...
|
|
2687
|
+
@property
|
|
2688
|
+
def name(self) -> str: ...
|
|
2689
|
+
def __repr__(self) -> str: ...
|
|
2690
|
+
|
|
2691
|
+
class AlterTableModifyColumnOption(AlterTableOptionMeta):
|
|
2692
|
+
"""
|
|
2693
|
+
ALTER TABLE operation to modify a column definition.
|
|
2694
|
+
|
|
2695
|
+
Changes properties of an existing column such as type, nullability,
|
|
2696
|
+
default value, or other constraints.
|
|
2697
|
+
|
|
2698
|
+
Example:
|
|
2699
|
+
>>> AlterTableModifyColumnOption(
|
|
2700
|
+
... Column("email", String(512), nullable=False)
|
|
2701
|
+
... )
|
|
2702
|
+
"""
|
|
2703
|
+
|
|
2704
|
+
def __new__(cls, column: Column) -> Self: ...
|
|
2705
|
+
@property
|
|
2706
|
+
def column(self) -> Column: ...
|
|
2707
|
+
def __repr__(self) -> str: ...
|
|
2708
|
+
|
|
2709
|
+
class AlterTableRenameColumnOption(AlterTableOptionMeta):
|
|
2710
|
+
"""
|
|
2711
|
+
ALTER TABLE operation to rename a column.
|
|
2712
|
+
|
|
2713
|
+
Changes the name of an existing column without modifying its type
|
|
2714
|
+
or constraints.
|
|
2715
|
+
|
|
2716
|
+
Example:
|
|
2717
|
+
>>> AlterTableRenameColumnOption("old_name", "new_name")
|
|
2718
|
+
"""
|
|
2719
|
+
|
|
2720
|
+
def __new__(cls, from_name: str, to_name: str) -> Self: ...
|
|
2721
|
+
@property
|
|
2722
|
+
def from_name(self) -> str: ...
|
|
2723
|
+
@property
|
|
2724
|
+
def to_name(self) -> str: ...
|
|
2725
|
+
def __repr__(self) -> str: ...
|
|
2726
|
+
|
|
2727
|
+
class AlterTable(SchemaStatement):
|
|
2728
|
+
"""
|
|
2729
|
+
Represents an ALTER TABLE SQL statement.
|
|
2730
|
+
|
|
2731
|
+
Provides a flexible way to modify existing table structures by applying
|
|
2732
|
+
one or more alteration operations such as adding/dropping columns,
|
|
2733
|
+
modifying column definitions, or managing constraints.
|
|
2734
|
+
|
|
2735
|
+
Multiple operations can be batched together in a single ALTER TABLE
|
|
2736
|
+
statement for efficiency.
|
|
2737
|
+
|
|
2738
|
+
Example:
|
|
2739
|
+
>>> AlterTable(
|
|
2740
|
+
... "users",
|
|
2741
|
+
... options=[
|
|
2742
|
+
... AlterTableAddColumnOption(Column("status", String(20))),
|
|
2743
|
+
... AlterTableModifyColumnOption(Column("email", String(512)))
|
|
2744
|
+
... ]
|
|
2745
|
+
... )
|
|
2746
|
+
"""
|
|
2747
|
+
|
|
2748
|
+
name: TableName
|
|
2749
|
+
"""The name of the table to alter."""
|
|
2750
|
+
|
|
2751
|
+
options: typing.Sequence[AlterTableOptionMeta]
|
|
2752
|
+
"""The list of alteration operations to apply."""
|
|
2753
|
+
|
|
2754
|
+
def __new__(
|
|
2755
|
+
cls, name: typing.Union[str, TableName], options: typing.Sequence[AlterTableOptionMeta]
|
|
2756
|
+
) -> Self:
|
|
2757
|
+
"""
|
|
2758
|
+
Create a new AlterTable statement.
|
|
2759
|
+
|
|
2760
|
+
Args:
|
|
2761
|
+
name: The table name to alter
|
|
2762
|
+
options: List of alteration operations
|
|
2763
|
+
|
|
2764
|
+
Returns:
|
|
2765
|
+
A new AlterTable instance
|
|
2766
|
+
"""
|
|
2767
|
+
...
|
|
2768
|
+
|
|
2769
|
+
def add_option(self, option: AlterTableOptionMeta) -> None:
|
|
2770
|
+
"""
|
|
2771
|
+
Add an alteration operation to this ALTER TABLE statement.
|
|
2772
|
+
|
|
2773
|
+
Args:
|
|
2774
|
+
option: The alteration operation to add
|
|
2775
|
+
"""
|
|
2776
|
+
...
|
|
2777
|
+
|
|
2778
|
+
def __copy__(self) -> Self:
|
|
2779
|
+
"""
|
|
2780
|
+
Create a shallow copy of this AlterTable.
|
|
2781
|
+
"""
|
|
2782
|
+
...
|
|
2783
|
+
|
|
2784
|
+
def copy(self) -> Self:
|
|
2785
|
+
"""
|
|
2786
|
+
Create a copy of this AlterTable.
|
|
2787
|
+
|
|
2788
|
+
Returns:
|
|
2789
|
+
A new AlterTable instance with the same values
|
|
2790
|
+
"""
|
|
2791
|
+
...
|
|
2792
|
+
|
|
2793
|
+
def __repr__(self) -> str: ...
|
|
2794
|
+
|
|
2795
|
+
class OnConflict:
|
|
2796
|
+
"""
|
|
2797
|
+
Specifies conflict resolution behavior for INSERT statements.
|
|
2798
|
+
|
|
2799
|
+
Handles situations where an INSERT would violate a unique constraint
|
|
2800
|
+
or primary key. Supports various strategies:
|
|
2801
|
+
- DO NOTHING: Skip the conflicting row
|
|
2802
|
+
- DO UPDATE: Update the existing row with new values
|
|
2803
|
+
|
|
2804
|
+
This corresponds to INSERT ... ON CONFLICT in PostgreSQL and
|
|
2805
|
+
INSERT ... ON DUPLICATE KEY UPDATE in MySQL.
|
|
2806
|
+
|
|
2807
|
+
Example:
|
|
2808
|
+
>>> OnConflict("email").do_update("name")
|
|
2809
|
+
>>> OnConflict("id", "version").do_nothing()
|
|
2810
|
+
"""
|
|
2811
|
+
|
|
2812
|
+
def __new__(cls, *targets: typing.Union[str, Column]) -> Self:
|
|
2813
|
+
"""
|
|
2814
|
+
Create a new conflict resolution specification.
|
|
2815
|
+
|
|
2816
|
+
Args:
|
|
2817
|
+
*targets: Column names or Column objects that define the conflict constraint
|
|
2818
|
+
|
|
2819
|
+
Returns:
|
|
2820
|
+
A new OnConflict instance
|
|
2821
|
+
"""
|
|
2822
|
+
...
|
|
2823
|
+
|
|
2824
|
+
def do_nothing(self, *keys: typing.Union[str, Column]) -> Self:
|
|
2825
|
+
"""
|
|
2826
|
+
Specify DO NOTHING action for conflicts.
|
|
2827
|
+
|
|
2828
|
+
When a conflict occurs, the conflicting row will be skipped.
|
|
2829
|
+
|
|
2830
|
+
Args:
|
|
2831
|
+
*keys: Provide primary keys if you are using MySQL, for MySQL specific polyfill.
|
|
2832
|
+
|
|
2833
|
+
Returns:
|
|
2834
|
+
Self for method chaining
|
|
2835
|
+
"""
|
|
2836
|
+
...
|
|
2837
|
+
|
|
2838
|
+
@typing.overload
|
|
2839
|
+
def do_update(self, *keys: typing.Union[str, Column]) -> Self:
|
|
2840
|
+
"""
|
|
2841
|
+
Specify DO UPDATE action for conflicts using column names.
|
|
2842
|
+
|
|
2843
|
+
Args:
|
|
2844
|
+
*keys: Columns to update on conflict
|
|
2845
|
+
|
|
2846
|
+
Returns:
|
|
2847
|
+
Self for method chaining
|
|
2848
|
+
"""
|
|
2849
|
+
...
|
|
2850
|
+
|
|
2851
|
+
@typing.overload
|
|
2852
|
+
def do_update(self, **kwds: _ExprValue) -> Self:
|
|
2853
|
+
"""
|
|
2854
|
+
Specify DO UPDATE action for conflicts with explicit values.
|
|
2855
|
+
|
|
2856
|
+
Args:
|
|
2857
|
+
**kwds: Column names and their new values
|
|
2858
|
+
|
|
2859
|
+
Returns:
|
|
2860
|
+
Self for method chaining
|
|
2861
|
+
"""
|
|
2862
|
+
...
|
|
2863
|
+
|
|
2864
|
+
def target_where(self, condition: Expr) -> Self:
|
|
2865
|
+
"""
|
|
2866
|
+
Add a WHERE clause to the conflict target (partial unique index).
|
|
2867
|
+
|
|
2868
|
+
Args:
|
|
2869
|
+
condition: The condition that must match for the conflict to apply
|
|
2870
|
+
|
|
2871
|
+
Returns:
|
|
2872
|
+
Self for method chaining
|
|
2873
|
+
"""
|
|
2874
|
+
...
|
|
2875
|
+
|
|
2876
|
+
def action_where(self, condition: Expr) -> Self:
|
|
2877
|
+
"""
|
|
2878
|
+
Add a WHERE clause to the conflict action (conditional update).
|
|
2879
|
+
|
|
2880
|
+
Args:
|
|
2881
|
+
condition: The condition that must be true for the update to occur
|
|
2882
|
+
|
|
2883
|
+
Returns:
|
|
2884
|
+
Self for method chaining
|
|
2885
|
+
"""
|
|
2886
|
+
...
|
|
2887
|
+
|
|
2888
|
+
def __repr__(self) -> str: ...
|
|
2889
|
+
|
|
2890
|
+
class Insert(QueryStatement):
|
|
2891
|
+
"""
|
|
2892
|
+
Builds INSERT SQL statements with a fluent interface.
|
|
2893
|
+
|
|
2894
|
+
Provides a chainable API for constructing INSERT queries with support for:
|
|
2895
|
+
- Single or multiple row insertion
|
|
2896
|
+
- Conflict resolution (UPSERT)
|
|
2897
|
+
- RETURNING clauses
|
|
2898
|
+
- REPLACE functionality
|
|
2899
|
+
- Default values
|
|
2900
|
+
|
|
2901
|
+
Example:
|
|
2902
|
+
>>> Insert().into("users").columns("name", "email").values(
|
|
2903
|
+
... "John", "john@example.com"
|
|
2904
|
+
... ).returning_all()
|
|
2905
|
+
>>> Insert().into("users").values(name="Jane", email="jane@example.com")
|
|
2906
|
+
"""
|
|
2907
|
+
|
|
2908
|
+
def __new__(cls) -> Self:
|
|
2909
|
+
"""
|
|
2910
|
+
Create a new INSERT statement builder.
|
|
2911
|
+
|
|
2912
|
+
Returns:
|
|
2913
|
+
A new Insert instance
|
|
2914
|
+
"""
|
|
2915
|
+
...
|
|
2916
|
+
|
|
2917
|
+
def replace(self) -> Self:
|
|
2918
|
+
"""
|
|
2919
|
+
Convert this INSERT to a REPLACE statement.
|
|
2920
|
+
|
|
2921
|
+
REPLACE will delete existing rows that conflict with the new row
|
|
2922
|
+
before inserting.
|
|
2923
|
+
|
|
2924
|
+
Returns:
|
|
2925
|
+
Self for method chaining
|
|
2926
|
+
"""
|
|
2927
|
+
...
|
|
2928
|
+
|
|
2929
|
+
def into(self, table: typing.Union[str, Table, TableName]) -> Self:
|
|
2930
|
+
"""
|
|
2931
|
+
Specify the target table for insertion.
|
|
2932
|
+
|
|
2933
|
+
Args:
|
|
2934
|
+
table: The table name, Table object, or TableName to insert into
|
|
2935
|
+
|
|
2936
|
+
Returns:
|
|
2937
|
+
Self for method chaining
|
|
2938
|
+
"""
|
|
2939
|
+
...
|
|
2940
|
+
|
|
2941
|
+
def columns(self, *args: typing.Union[Column, str]) -> Self:
|
|
2942
|
+
"""
|
|
2943
|
+
Specify the columns for insertion.
|
|
2944
|
+
|
|
2945
|
+
Args:
|
|
2946
|
+
*args: Column names or Column objects
|
|
2947
|
+
|
|
2948
|
+
Returns:
|
|
2949
|
+
Self for method chaining
|
|
2950
|
+
"""
|
|
2951
|
+
...
|
|
2952
|
+
|
|
2953
|
+
@typing.overload
|
|
2954
|
+
def values(self, **kwds: _ExprValue) -> Self:
|
|
2955
|
+
"""
|
|
2956
|
+
Specify values to insert using keyword arguments.
|
|
2957
|
+
|
|
2958
|
+
Args:
|
|
2959
|
+
**kwds: Column names and their values
|
|
2960
|
+
|
|
2961
|
+
Returns:
|
|
2962
|
+
Self for method chaining
|
|
2963
|
+
"""
|
|
2964
|
+
...
|
|
2965
|
+
|
|
2966
|
+
@typing.overload
|
|
2967
|
+
def values(self, *args: _ExprValue) -> Self:
|
|
2968
|
+
"""
|
|
2969
|
+
Specify values to insert using positional arguments.
|
|
2970
|
+
|
|
2971
|
+
Args:
|
|
2972
|
+
*args: Values in the same order as columns
|
|
2973
|
+
|
|
2974
|
+
Returns:
|
|
2975
|
+
Self for method chaining
|
|
2976
|
+
"""
|
|
2977
|
+
...
|
|
2978
|
+
|
|
2979
|
+
def or_default_values(self, rows: int = ...) -> Self:
|
|
2980
|
+
"""
|
|
2981
|
+
Use DEFAULT VALUES if no values were specified.
|
|
2982
|
+
|
|
2983
|
+
Args:
|
|
2984
|
+
rows: Number of rows to insert with default values
|
|
2985
|
+
|
|
2986
|
+
Returns:
|
|
2987
|
+
Self for method chaining
|
|
2988
|
+
"""
|
|
2989
|
+
...
|
|
2990
|
+
|
|
2991
|
+
def on_conflict(self, action: OnConflict) -> Self:
|
|
2992
|
+
"""
|
|
2993
|
+
Specify conflict resolution behavior (UPSERT).
|
|
2994
|
+
|
|
2995
|
+
Args:
|
|
2996
|
+
action: The OnConflict specification
|
|
2997
|
+
|
|
2998
|
+
Returns:
|
|
2999
|
+
Self for method chaining
|
|
3000
|
+
"""
|
|
3001
|
+
...
|
|
3002
|
+
|
|
3003
|
+
def returning(self, *args: typing.Union[Column, str]) -> Self:
|
|
3004
|
+
"""
|
|
3005
|
+
Specify columns to return from the inserted rows.
|
|
3006
|
+
|
|
3007
|
+
Args:
|
|
3008
|
+
*args: Column names or Column objects to return
|
|
3009
|
+
|
|
3010
|
+
Returns:
|
|
3011
|
+
Self for method chaining
|
|
3012
|
+
"""
|
|
3013
|
+
...
|
|
3014
|
+
|
|
3015
|
+
def returning_all(self) -> Self:
|
|
3016
|
+
"""
|
|
3017
|
+
Return all columns from the inserted rows.
|
|
3018
|
+
|
|
3019
|
+
Returns:
|
|
3020
|
+
Self for method chaining
|
|
3021
|
+
"""
|
|
3022
|
+
...
|
|
3023
|
+
|
|
3024
|
+
def __repr__(self) -> str: ...
|
|
3025
|
+
|
|
3026
|
+
class Delete(QueryStatement):
|
|
3027
|
+
"""
|
|
3028
|
+
Builds DELETE SQL statements with a fluent interface.
|
|
3029
|
+
|
|
3030
|
+
Provides a chainable API for constructing DELETE queries with support for:
|
|
3031
|
+
- WHERE conditions for filtering
|
|
3032
|
+
- LIMIT for restricting deletion count
|
|
3033
|
+
- ORDER BY for determining deletion order
|
|
3034
|
+
- RETURNING clauses for getting deleted data
|
|
3035
|
+
"""
|
|
3036
|
+
|
|
3037
|
+
def __new__(cls) -> Self:
|
|
3038
|
+
"""
|
|
3039
|
+
Create a new DELETE statement builder.
|
|
3040
|
+
|
|
3041
|
+
Returns:
|
|
3042
|
+
A new Delete instance
|
|
3043
|
+
"""
|
|
3044
|
+
...
|
|
3045
|
+
|
|
3046
|
+
def from_table(self, table: typing.Union[str, Table, TableName]) -> Self:
|
|
3047
|
+
"""
|
|
3048
|
+
Specify the table to delete from.
|
|
3049
|
+
|
|
3050
|
+
Args:
|
|
3051
|
+
table: The table name, Table object, or TableName
|
|
3052
|
+
|
|
3053
|
+
Returns:
|
|
3054
|
+
Self for method chaining
|
|
3055
|
+
"""
|
|
3056
|
+
...
|
|
3057
|
+
|
|
3058
|
+
def limit(self, n: int) -> Self:
|
|
3059
|
+
"""
|
|
3060
|
+
Limit the number of rows to delete.
|
|
3061
|
+
|
|
3062
|
+
Args:
|
|
3063
|
+
n: Maximum number of rows to delete
|
|
3064
|
+
|
|
3065
|
+
Returns:
|
|
3066
|
+
Self for method chaining
|
|
3067
|
+
"""
|
|
3068
|
+
...
|
|
3069
|
+
|
|
3070
|
+
def returning(self, *args: typing.Union[Column, str]) -> Self:
|
|
3071
|
+
"""
|
|
3072
|
+
Specify columns to return from the deleted rows.
|
|
3073
|
+
|
|
3074
|
+
Args:
|
|
3075
|
+
*args: Column names or Column objects to return
|
|
3076
|
+
|
|
3077
|
+
Returns:
|
|
3078
|
+
Self for method chaining
|
|
3079
|
+
"""
|
|
3080
|
+
...
|
|
3081
|
+
|
|
3082
|
+
def returning_all(self) -> Self:
|
|
3083
|
+
"""
|
|
3084
|
+
Return all columns from the deleted rows.
|
|
3085
|
+
|
|
3086
|
+
Returns:
|
|
3087
|
+
Self for method chaining
|
|
3088
|
+
"""
|
|
3089
|
+
...
|
|
3090
|
+
|
|
3091
|
+
def where(self, condition: _ExprValue) -> Self:
|
|
3092
|
+
"""
|
|
3093
|
+
Add a WHERE condition to filter rows to delete.
|
|
3094
|
+
|
|
3095
|
+
Args:
|
|
3096
|
+
condition: The filter condition expression
|
|
3097
|
+
|
|
3098
|
+
Returns:
|
|
3099
|
+
Self for method chaining
|
|
3100
|
+
"""
|
|
3101
|
+
...
|
|
3102
|
+
|
|
3103
|
+
def order_by(
|
|
3104
|
+
self,
|
|
3105
|
+
target: _ExprValue,
|
|
3106
|
+
order: typing.Literal["asc", "desc"],
|
|
3107
|
+
null_order: typing.Optional[typing.Literal["first", "last"]] = ...,
|
|
3108
|
+
) -> Self:
|
|
3109
|
+
"""
|
|
3110
|
+
Specify the order in which to delete rows.
|
|
3111
|
+
|
|
3112
|
+
Typically used with LIMIT to delete specific rows.
|
|
3113
|
+
|
|
3114
|
+
Returns:
|
|
3115
|
+
Self for method chaining
|
|
3116
|
+
"""
|
|
3117
|
+
...
|
|
3118
|
+
|
|
3119
|
+
def __repr__(self) -> str: ...
|
|
3120
|
+
|
|
3121
|
+
class Update(QueryStatement):
|
|
3122
|
+
"""
|
|
3123
|
+
Builds UPDATE SQL statements with a fluent interface.
|
|
3124
|
+
|
|
3125
|
+
Provides a chainable API for constructing UPDATE queries with support for:
|
|
3126
|
+
- Setting column values
|
|
3127
|
+
- WHERE conditions for filtering
|
|
3128
|
+
- LIMIT for restricting update count
|
|
3129
|
+
- ORDER BY for determining update order
|
|
3130
|
+
- RETURNING clauses for getting updated data
|
|
3131
|
+
|
|
3132
|
+
Example:
|
|
3133
|
+
>>> Update().table("users").values(
|
|
3134
|
+
... status="active", last_updated=datetime.now()
|
|
3135
|
+
... ).where(Expr.col("id") == 123).returning_all()
|
|
3136
|
+
>>> Update().table("users").values(budget=Expr.col("budget") + 10) \\
|
|
3137
|
+
... .where(Expr.col("name").like(r"%ali%")) \\
|
|
3138
|
+
... .order_by(Order(Expr.col("id"), ORDER_ASC)) \\
|
|
3139
|
+
... .returning("age")
|
|
3140
|
+
"""
|
|
3141
|
+
|
|
3142
|
+
def __new__(cls) -> Self:
|
|
3143
|
+
"""
|
|
3144
|
+
Create a new UPDATE statement builder.
|
|
3145
|
+
|
|
3146
|
+
Returns:
|
|
3147
|
+
A new Update instance
|
|
3148
|
+
"""
|
|
3149
|
+
...
|
|
3150
|
+
|
|
3151
|
+
def table(self, table: typing.Union[str, Table, TableName]) -> Self:
|
|
3152
|
+
"""
|
|
3153
|
+
Specify the table to update.
|
|
3154
|
+
|
|
3155
|
+
Args:
|
|
3156
|
+
table: The table name, Table object, or TableName
|
|
3157
|
+
|
|
3158
|
+
Returns:
|
|
3159
|
+
Self for method chaining
|
|
3160
|
+
"""
|
|
3161
|
+
...
|
|
3162
|
+
|
|
3163
|
+
def from_table(self, table: typing.Union[str, Table, TableName]) -> Self:
|
|
3164
|
+
"""
|
|
3165
|
+
Update using data from another table (`UPDATE .. FROM ..`).
|
|
3166
|
+
|
|
3167
|
+
**Notes** \\
|
|
3168
|
+
MySQL doesn't support the UPDATE FROM syntax. And the current implementation attempt to tranform it to the UPDATE JOIN syntax, which only works for one join target.
|
|
3169
|
+
|
|
3170
|
+
Args:
|
|
3171
|
+
table: The table name, Table object, or TableName
|
|
3172
|
+
|
|
3173
|
+
Returns:
|
|
3174
|
+
Self for method chaining
|
|
3175
|
+
"""
|
|
3176
|
+
...
|
|
3177
|
+
|
|
3178
|
+
def values(self, **kwds: _ExprValue) -> Self:
|
|
3179
|
+
"""
|
|
3180
|
+
Specify columns and their new values.
|
|
3181
|
+
|
|
3182
|
+
Args:
|
|
3183
|
+
**kwds: Column names and their new values as keyword arguments
|
|
3184
|
+
|
|
3185
|
+
Returns:
|
|
3186
|
+
Self for method chaining
|
|
3187
|
+
"""
|
|
3188
|
+
...
|
|
3189
|
+
|
|
3190
|
+
def where(self, condition: _ExprValue) -> Self:
|
|
3191
|
+
"""
|
|
3192
|
+
Add a WHERE condition to filter rows to update.
|
|
3193
|
+
|
|
3194
|
+
Args:
|
|
3195
|
+
condition: The filter condition expression
|
|
3196
|
+
|
|
3197
|
+
Returns:
|
|
3198
|
+
Self for method chaining
|
|
3199
|
+
"""
|
|
3200
|
+
...
|
|
3201
|
+
|
|
3202
|
+
def order_by(
|
|
3203
|
+
self,
|
|
3204
|
+
target: _ExprValue,
|
|
3205
|
+
order: typing.Literal["asc", "desc"],
|
|
3206
|
+
null_order: typing.Optional[typing.Literal["first", "last"]] = ...,
|
|
3207
|
+
) -> Self:
|
|
3208
|
+
"""
|
|
3209
|
+
Specify the order in which to update rows.
|
|
3210
|
+
|
|
3211
|
+
Typically used with LIMIT to update specific rows.
|
|
3212
|
+
|
|
3213
|
+
Returns:
|
|
3214
|
+
Self for method chaining
|
|
3215
|
+
"""
|
|
3216
|
+
...
|
|
3217
|
+
|
|
3218
|
+
def returning(self, *args: typing.Union[Column, str]) -> Self:
|
|
3219
|
+
"""
|
|
3220
|
+
Specify columns to return from the updated rows.
|
|
3221
|
+
|
|
3222
|
+
Args:
|
|
3223
|
+
*args: Column names or Column objects to return
|
|
3224
|
+
|
|
3225
|
+
Returns:
|
|
3226
|
+
Self for method chaining
|
|
3227
|
+
"""
|
|
3228
|
+
...
|
|
3229
|
+
|
|
3230
|
+
def returning_all(self) -> Self:
|
|
3231
|
+
"""
|
|
3232
|
+
Return all columns from the updated rows.
|
|
3233
|
+
|
|
3234
|
+
Returns:
|
|
3235
|
+
Self for method chaining
|
|
3236
|
+
"""
|
|
3237
|
+
...
|
|
3238
|
+
|
|
3239
|
+
def limit(self, n: int) -> Self:
|
|
3240
|
+
"""
|
|
3241
|
+
Limit the number of rows to update.
|
|
3242
|
+
|
|
3243
|
+
Args:
|
|
3244
|
+
n: Maximum number of rows to update
|
|
3245
|
+
|
|
3246
|
+
Returns:
|
|
3247
|
+
Self for method chaining
|
|
3248
|
+
"""
|
|
3249
|
+
...
|
|
3250
|
+
|
|
3251
|
+
def __repr__(self) -> str: ...
|
|
3252
|
+
|
|
3253
|
+
class SelectExpr:
|
|
3254
|
+
"""
|
|
3255
|
+
Represents a column expression with an optional alias in a SELECT clause.
|
|
3256
|
+
|
|
3257
|
+
Used to specify both the expression to select and an optional alias name
|
|
3258
|
+
for the result column.
|
|
3259
|
+
|
|
3260
|
+
Example:
|
|
3261
|
+
>>> SelectExpr(Expr.col("price") * 1.1, "price_with_tax")
|
|
3262
|
+
>>> SelectExpr(Expr.count(), "total_count")
|
|
3263
|
+
"""
|
|
3264
|
+
|
|
3265
|
+
def __new__(cls, expr: _ExprValue, alias: typing.Optional[str] = ...):
|
|
3266
|
+
"""
|
|
3267
|
+
Create a new SelectExpr.
|
|
3268
|
+
|
|
3269
|
+
Args:
|
|
3270
|
+
expr: The expression to select
|
|
3271
|
+
alias: Optional alias name for the result column
|
|
3272
|
+
|
|
3273
|
+
Returns:
|
|
3274
|
+
A new SelectExpr instance
|
|
3275
|
+
"""
|
|
3276
|
+
...
|
|
3277
|
+
|
|
3278
|
+
@property
|
|
3279
|
+
def expr(self) -> Expr:
|
|
3280
|
+
"""The expression to be selected."""
|
|
3281
|
+
...
|
|
3282
|
+
|
|
3283
|
+
@property
|
|
3284
|
+
def alias(self) -> typing.Optional[str]:
|
|
3285
|
+
"""The alias name for the result column, if any."""
|
|
3286
|
+
...
|
|
3287
|
+
|
|
3288
|
+
def __repr__(self) -> str: ...
|
|
3289
|
+
|
|
3290
|
+
class Select(QueryStatement):
|
|
3291
|
+
"""
|
|
3292
|
+
Builds SELECT SQL statements with a fluent interface.
|
|
3293
|
+
|
|
3294
|
+
Provides a chainable API for constructing SELECT queries with support for:
|
|
3295
|
+
- Column selection with expressions and aliases
|
|
3296
|
+
- Table and subquery sources
|
|
3297
|
+
- Filtering with WHERE and HAVING
|
|
3298
|
+
- Joins (inner, left, right, full, cross, lateral)
|
|
3299
|
+
- Grouping and aggregation
|
|
3300
|
+
- Ordering and pagination
|
|
3301
|
+
- Set operations (UNION, EXCEPT, INTERSECT)
|
|
3302
|
+
- Row locking for transactions
|
|
3303
|
+
- DISTINCT queries
|
|
3304
|
+
|
|
3305
|
+
Example:
|
|
3306
|
+
>>> Select(Expr.col("name"), Expr.col("email")).from_table("users") \\
|
|
3307
|
+
... .where(Expr.col("active") == True) \\
|
|
3308
|
+
... .order_by(Order(Expr.col("created_at"), ORDER_DESC)) \\
|
|
3309
|
+
... .limit(10)
|
|
3310
|
+
>>> Select().columns("id", "title").from_table("posts") \\
|
|
3311
|
+
... .join("users", Expr.col("posts.user_id") == Expr.col("users.id")) \\
|
|
3312
|
+
... .where(Expr.col("published") == True)
|
|
3313
|
+
"""
|
|
3314
|
+
|
|
3315
|
+
def __new__(cls, *cols: typing.Union[SelectExpr, _ExprValue]) -> Self:
|
|
3316
|
+
"""
|
|
3317
|
+
Create a new SELECT statement builder.
|
|
3318
|
+
|
|
3319
|
+
Args:
|
|
3320
|
+
*cols: Optional initial columns to select (expressions or SelectExpr objects)
|
|
3321
|
+
|
|
3322
|
+
Returns:
|
|
3323
|
+
A new Select instance
|
|
3324
|
+
"""
|
|
3325
|
+
...
|
|
3326
|
+
|
|
3327
|
+
def distinct(self, *on: typing.Union[Column, ColumnRef, str]) -> Self:
|
|
3328
|
+
"""
|
|
3329
|
+
Make this a DISTINCT query to eliminate duplicate rows.
|
|
3330
|
+
|
|
3331
|
+
Args:
|
|
3332
|
+
*on: Optional columns for DISTINCT ON (PostgreSQL-specific)
|
|
3333
|
+
|
|
3334
|
+
Returns:
|
|
3335
|
+
Self for method chaining
|
|
3336
|
+
"""
|
|
3337
|
+
...
|
|
3338
|
+
|
|
3339
|
+
def columns(self, *cols: typing.Union[SelectExpr, _ExprValue]) -> Self:
|
|
3340
|
+
"""
|
|
3341
|
+
Specify or add columns to select.
|
|
3342
|
+
|
|
3343
|
+
Args:
|
|
3344
|
+
*cols: Column names, expressions, or SelectExpr objects to select
|
|
3345
|
+
|
|
3346
|
+
Returns:
|
|
3347
|
+
Self for method chaining
|
|
3348
|
+
"""
|
|
3349
|
+
...
|
|
3350
|
+
|
|
3351
|
+
def from_table(self, table: typing.Union[Table, TableName, str]) -> Self:
|
|
3352
|
+
"""
|
|
3353
|
+
Specify the source table for the query.
|
|
3354
|
+
|
|
3355
|
+
Args:
|
|
3356
|
+
table: The table name, Table object, or TableName to select from
|
|
3357
|
+
|
|
3358
|
+
Returns:
|
|
3359
|
+
Self for method chaining
|
|
3360
|
+
"""
|
|
3361
|
+
...
|
|
3362
|
+
|
|
3363
|
+
def from_subquery(self, subquery: Select, alias: str) -> Self:
|
|
3364
|
+
"""
|
|
3365
|
+
Use a subquery as the data source.
|
|
3366
|
+
|
|
3367
|
+
Args:
|
|
3368
|
+
subquery: The SELECT query to use as a subquery
|
|
3369
|
+
alias: Alias name for the subquery
|
|
3370
|
+
|
|
3371
|
+
Returns:
|
|
3372
|
+
Self for method chaining
|
|
3373
|
+
"""
|
|
3374
|
+
...
|
|
3375
|
+
|
|
3376
|
+
def from_function(self, function: FunctionCall, alias: str) -> Self:
|
|
3377
|
+
"""
|
|
3378
|
+
Use a table-returning function as the data source.
|
|
3379
|
+
|
|
3380
|
+
Args:
|
|
3381
|
+
function: The function call that returns table data
|
|
3382
|
+
alias: Alias name for the function result
|
|
3383
|
+
|
|
3384
|
+
Returns:
|
|
3385
|
+
Self for method chaining
|
|
3386
|
+
"""
|
|
3387
|
+
...
|
|
3388
|
+
|
|
3389
|
+
def limit(self, n: int) -> Self:
|
|
3390
|
+
"""
|
|
3391
|
+
Limit the number of rows returned.
|
|
3392
|
+
|
|
3393
|
+
Args:
|
|
3394
|
+
n: Maximum number of rows to return
|
|
3395
|
+
|
|
3396
|
+
Returns:
|
|
3397
|
+
Self for method chaining
|
|
3398
|
+
"""
|
|
3399
|
+
...
|
|
3400
|
+
|
|
3401
|
+
def offset(self, n: int) -> Self:
|
|
3402
|
+
"""
|
|
3403
|
+
Skip a number of rows before returning results.
|
|
3404
|
+
|
|
3405
|
+
Typically used with LIMIT for pagination.
|
|
3406
|
+
|
|
3407
|
+
Args:
|
|
3408
|
+
n: Number of rows to skip
|
|
3409
|
+
|
|
3410
|
+
Returns:
|
|
3411
|
+
Self for method chaining
|
|
3412
|
+
"""
|
|
3413
|
+
...
|
|
3414
|
+
|
|
3415
|
+
def where(self, condition: _ExprValue) -> Self:
|
|
3416
|
+
"""
|
|
3417
|
+
Add a WHERE condition to filter rows.
|
|
3418
|
+
|
|
3419
|
+
Args:
|
|
3420
|
+
condition: The filter condition expression
|
|
3421
|
+
|
|
3422
|
+
Returns:
|
|
3423
|
+
Self for method chaining
|
|
3424
|
+
"""
|
|
3425
|
+
...
|
|
3426
|
+
|
|
3427
|
+
def having(self, condition: _ExprValue) -> Self:
|
|
3428
|
+
"""
|
|
3429
|
+
Add a HAVING condition to filter grouped results.
|
|
3430
|
+
|
|
3431
|
+
Used with GROUP BY to filter aggregated data.
|
|
3432
|
+
|
|
3433
|
+
Args:
|
|
3434
|
+
condition: The filter condition expression
|
|
3435
|
+
|
|
3436
|
+
Returns:
|
|
3437
|
+
Self for method chaining
|
|
3438
|
+
"""
|
|
3439
|
+
...
|
|
3440
|
+
|
|
3441
|
+
def order_by(
|
|
3442
|
+
self,
|
|
3443
|
+
target: _ExprValue,
|
|
3444
|
+
order: typing.Literal["asc", "desc"],
|
|
3445
|
+
null_order: typing.Optional[typing.Literal["first", "last"]] = ...,
|
|
3446
|
+
) -> Self:
|
|
3447
|
+
"""
|
|
3448
|
+
Specify the order of results.
|
|
3449
|
+
|
|
3450
|
+
Returns:
|
|
3451
|
+
Self for method chaining
|
|
3452
|
+
"""
|
|
3453
|
+
...
|
|
3454
|
+
|
|
3455
|
+
def lock(
|
|
3456
|
+
self,
|
|
3457
|
+
type: typing.Literal["exclusive", "shared"] = ...,
|
|
3458
|
+
behavior: typing.Optional[typing.Literal["nowait", "skip"]] = ...,
|
|
3459
|
+
tables: typing.Sequence[typing.Union[str, TableName, Table]] = ...,
|
|
3460
|
+
) -> Self:
|
|
3461
|
+
"""
|
|
3462
|
+
Add row locking for transactional queries (FOR UPDATE/FOR SHARE).
|
|
3463
|
+
|
|
3464
|
+
Args:
|
|
3465
|
+
type: Lock type - "exclusive" (FOR UPDATE) or "shared" (FOR SHARE)
|
|
3466
|
+
behavior: Optional lock behavior - "nowait" or "skip" (SKIP LOCKED)
|
|
3467
|
+
tables: Optional specific tables to lock (for multi-table queries)
|
|
3468
|
+
|
|
3469
|
+
Returns:
|
|
3470
|
+
Self for method chaining
|
|
3471
|
+
"""
|
|
3472
|
+
...
|
|
3473
|
+
|
|
3474
|
+
def group_by(
|
|
3475
|
+
self,
|
|
3476
|
+
*cols: _ExprValue,
|
|
3477
|
+
) -> Self:
|
|
3478
|
+
"""
|
|
3479
|
+
Group results by specified columns for aggregation.
|
|
3480
|
+
|
|
3481
|
+
Args:
|
|
3482
|
+
*cols: Column names or expressions to group by
|
|
3483
|
+
|
|
3484
|
+
Returns:
|
|
3485
|
+
Self for method chaining
|
|
3486
|
+
"""
|
|
3487
|
+
...
|
|
3488
|
+
|
|
3489
|
+
def union(
|
|
3490
|
+
self,
|
|
3491
|
+
statement: Self,
|
|
3492
|
+
type: typing.Literal["all", "except", "intersect", "distinct"] = ...,
|
|
3493
|
+
) -> Self:
|
|
3494
|
+
"""
|
|
3495
|
+
Combine this query with another using set operations.
|
|
3496
|
+
|
|
3497
|
+
Args:
|
|
3498
|
+
statement: The SELECT query to combine with
|
|
3499
|
+
type: Set operation type:
|
|
3500
|
+
- "distinct": UNION (default, removes duplicates)
|
|
3501
|
+
- "all": UNION ALL (keeps duplicates)
|
|
3502
|
+
- "except": EXCEPT (rows in first but not second)
|
|
3503
|
+
- "intersect": INTERSECT (rows in both queries)
|
|
3504
|
+
|
|
3505
|
+
Returns:
|
|
3506
|
+
Self for method chaining
|
|
3507
|
+
"""
|
|
3508
|
+
...
|
|
3509
|
+
|
|
3510
|
+
def join(
|
|
3511
|
+
self,
|
|
3512
|
+
table: typing.Union[str, TableName, Table, AliasedTable],
|
|
3513
|
+
on: _ExprValue,
|
|
3514
|
+
type: typing.Literal["", "cross", "full", "inner", "right", "left"] = ...,
|
|
3515
|
+
) -> Self:
|
|
3516
|
+
"""
|
|
3517
|
+
Join another table to the query.
|
|
3518
|
+
|
|
3519
|
+
Args:
|
|
3520
|
+
table: The table name, Table object, or TableName to join
|
|
3521
|
+
on: The join condition expression
|
|
3522
|
+
type: Join type:
|
|
3523
|
+
- "": Default join (typically INNER)
|
|
3524
|
+
- "inner": INNER JOIN
|
|
3525
|
+
- "left": LEFT JOIN (LEFT OUTER JOIN)
|
|
3526
|
+
- "right": RIGHT JOIN (RIGHT OUTER JOIN)
|
|
3527
|
+
- "full": FULL JOIN (FULL OUTER JOIN)
|
|
3528
|
+
- "cross": CROSS JOIN
|
|
3529
|
+
|
|
3530
|
+
Returns:
|
|
3531
|
+
Self for method chaining
|
|
3532
|
+
"""
|
|
3533
|
+
...
|
|
3534
|
+
|
|
3535
|
+
def join_lateral(
|
|
3536
|
+
self,
|
|
3537
|
+
query: Self,
|
|
3538
|
+
alias: str,
|
|
3539
|
+
on: _ExprValue,
|
|
3540
|
+
type: typing.Literal["", "cross", "full", "inner", "right", "left"] = ...,
|
|
3541
|
+
) -> Self:
|
|
3542
|
+
"""
|
|
3543
|
+
Join a lateral subquery (subquery that can reference prior FROM items).
|
|
3544
|
+
|
|
3545
|
+
LATERAL allows the subquery to reference columns from preceding tables
|
|
3546
|
+
in the FROM clause. Useful for correlated subqueries in joins.
|
|
3547
|
+
|
|
3548
|
+
Args:
|
|
3549
|
+
query: The SELECT query to join laterally
|
|
3550
|
+
alias: Alias name for the lateral subquery
|
|
3551
|
+
on: The join condition expression
|
|
3552
|
+
type: Join type (see join() for options)
|
|
3553
|
+
|
|
3554
|
+
Returns:
|
|
3555
|
+
Self for method chaining
|
|
3556
|
+
"""
|
|
3557
|
+
...
|
|
3558
|
+
|
|
3559
|
+
def __repr__(self) -> str: ...
|