plain.postgres 0.84.0__py3-none-any.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.
- plain/postgres/CHANGELOG.md +1028 -0
- plain/postgres/README.md +925 -0
- plain/postgres/__init__.py +120 -0
- plain/postgres/agents/.claude/rules/plain-postgres.md +78 -0
- plain/postgres/aggregates.py +236 -0
- plain/postgres/backups/__init__.py +0 -0
- plain/postgres/backups/cli.py +148 -0
- plain/postgres/backups/clients.py +94 -0
- plain/postgres/backups/core.py +172 -0
- plain/postgres/base.py +1415 -0
- plain/postgres/cli/__init__.py +3 -0
- plain/postgres/cli/db.py +142 -0
- plain/postgres/cli/migrations.py +1085 -0
- plain/postgres/config.py +18 -0
- plain/postgres/connection.py +1331 -0
- plain/postgres/connections.py +77 -0
- plain/postgres/constants.py +13 -0
- plain/postgres/constraints.py +495 -0
- plain/postgres/database_url.py +94 -0
- plain/postgres/db.py +59 -0
- plain/postgres/default_settings.py +38 -0
- plain/postgres/deletion.py +475 -0
- plain/postgres/dialect.py +640 -0
- plain/postgres/entrypoints.py +4 -0
- plain/postgres/enums.py +103 -0
- plain/postgres/exceptions.py +217 -0
- plain/postgres/expressions.py +1912 -0
- plain/postgres/fields/__init__.py +2118 -0
- plain/postgres/fields/encrypted.py +354 -0
- plain/postgres/fields/json.py +413 -0
- plain/postgres/fields/mixins.py +30 -0
- plain/postgres/fields/related.py +1192 -0
- plain/postgres/fields/related_descriptors.py +290 -0
- plain/postgres/fields/related_lookups.py +223 -0
- plain/postgres/fields/related_managers.py +661 -0
- plain/postgres/fields/reverse_descriptors.py +229 -0
- plain/postgres/fields/reverse_related.py +328 -0
- plain/postgres/fields/timezones.py +143 -0
- plain/postgres/forms.py +773 -0
- plain/postgres/functions/__init__.py +189 -0
- plain/postgres/functions/comparison.py +127 -0
- plain/postgres/functions/datetime.py +454 -0
- plain/postgres/functions/math.py +140 -0
- plain/postgres/functions/mixins.py +59 -0
- plain/postgres/functions/text.py +282 -0
- plain/postgres/functions/window.py +125 -0
- plain/postgres/indexes.py +286 -0
- plain/postgres/lookups.py +758 -0
- plain/postgres/meta.py +584 -0
- plain/postgres/migrations/__init__.py +53 -0
- plain/postgres/migrations/autodetector.py +1379 -0
- plain/postgres/migrations/exceptions.py +54 -0
- plain/postgres/migrations/executor.py +188 -0
- plain/postgres/migrations/graph.py +364 -0
- plain/postgres/migrations/loader.py +377 -0
- plain/postgres/migrations/migration.py +180 -0
- plain/postgres/migrations/operations/__init__.py +34 -0
- plain/postgres/migrations/operations/base.py +139 -0
- plain/postgres/migrations/operations/fields.py +373 -0
- plain/postgres/migrations/operations/models.py +798 -0
- plain/postgres/migrations/operations/special.py +184 -0
- plain/postgres/migrations/optimizer.py +74 -0
- plain/postgres/migrations/questioner.py +340 -0
- plain/postgres/migrations/recorder.py +119 -0
- plain/postgres/migrations/serializer.py +378 -0
- plain/postgres/migrations/state.py +882 -0
- plain/postgres/migrations/utils.py +147 -0
- plain/postgres/migrations/writer.py +302 -0
- plain/postgres/options.py +207 -0
- plain/postgres/otel.py +231 -0
- plain/postgres/preflight.py +336 -0
- plain/postgres/query.py +2242 -0
- plain/postgres/query_utils.py +456 -0
- plain/postgres/registry.py +217 -0
- plain/postgres/schema.py +1885 -0
- plain/postgres/sql/__init__.py +40 -0
- plain/postgres/sql/compiler.py +1869 -0
- plain/postgres/sql/constants.py +22 -0
- plain/postgres/sql/datastructures.py +222 -0
- plain/postgres/sql/query.py +2947 -0
- plain/postgres/sql/where.py +374 -0
- plain/postgres/test/__init__.py +0 -0
- plain/postgres/test/pytest.py +117 -0
- plain/postgres/test/utils.py +18 -0
- plain/postgres/transaction.py +222 -0
- plain/postgres/types.py +92 -0
- plain/postgres/types.pyi +751 -0
- plain/postgres/utils.py +345 -0
- plain_postgres-0.84.0.dist-info/METADATA +937 -0
- plain_postgres-0.84.0.dist-info/RECORD +93 -0
- plain_postgres-0.84.0.dist-info/WHEEL +4 -0
- plain_postgres-0.84.0.dist-info/entry_points.txt +5 -0
- plain_postgres-0.84.0.dist-info/licenses/LICENSE +61 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
from .comparison import Cast, Coalesce, Greatest, JSONObject, Least, NullIf
|
|
2
|
+
from .datetime import (
|
|
3
|
+
Extract,
|
|
4
|
+
ExtractDay,
|
|
5
|
+
ExtractHour,
|
|
6
|
+
ExtractIsoWeekDay,
|
|
7
|
+
ExtractIsoYear,
|
|
8
|
+
ExtractMinute,
|
|
9
|
+
ExtractMonth,
|
|
10
|
+
ExtractQuarter,
|
|
11
|
+
ExtractSecond,
|
|
12
|
+
ExtractWeek,
|
|
13
|
+
ExtractWeekDay,
|
|
14
|
+
ExtractYear,
|
|
15
|
+
Now,
|
|
16
|
+
Trunc,
|
|
17
|
+
TruncDate,
|
|
18
|
+
TruncDay,
|
|
19
|
+
TruncHour,
|
|
20
|
+
TruncMinute,
|
|
21
|
+
TruncMonth,
|
|
22
|
+
TruncQuarter,
|
|
23
|
+
TruncSecond,
|
|
24
|
+
TruncTime,
|
|
25
|
+
TruncWeek,
|
|
26
|
+
TruncYear,
|
|
27
|
+
)
|
|
28
|
+
from .math import (
|
|
29
|
+
Abs,
|
|
30
|
+
ACos,
|
|
31
|
+
ASin,
|
|
32
|
+
ATan,
|
|
33
|
+
ATan2,
|
|
34
|
+
Ceil,
|
|
35
|
+
Cos,
|
|
36
|
+
Cot,
|
|
37
|
+
Degrees,
|
|
38
|
+
Exp,
|
|
39
|
+
Floor,
|
|
40
|
+
Ln,
|
|
41
|
+
Log,
|
|
42
|
+
Mod,
|
|
43
|
+
Pi,
|
|
44
|
+
Power,
|
|
45
|
+
Radians,
|
|
46
|
+
Random,
|
|
47
|
+
Round,
|
|
48
|
+
Sign,
|
|
49
|
+
Sin,
|
|
50
|
+
Sqrt,
|
|
51
|
+
Tan,
|
|
52
|
+
)
|
|
53
|
+
from .text import (
|
|
54
|
+
MD5,
|
|
55
|
+
SHA1,
|
|
56
|
+
SHA224,
|
|
57
|
+
SHA256,
|
|
58
|
+
SHA384,
|
|
59
|
+
SHA512,
|
|
60
|
+
Chr,
|
|
61
|
+
Concat,
|
|
62
|
+
ConcatPair,
|
|
63
|
+
Left,
|
|
64
|
+
Length,
|
|
65
|
+
Lower,
|
|
66
|
+
LPad,
|
|
67
|
+
LTrim,
|
|
68
|
+
Ord,
|
|
69
|
+
Repeat,
|
|
70
|
+
Replace,
|
|
71
|
+
Reverse,
|
|
72
|
+
Right,
|
|
73
|
+
RPad,
|
|
74
|
+
RTrim,
|
|
75
|
+
StrIndex,
|
|
76
|
+
Substr,
|
|
77
|
+
Trim,
|
|
78
|
+
Upper,
|
|
79
|
+
)
|
|
80
|
+
from .window import (
|
|
81
|
+
CumeDist,
|
|
82
|
+
DenseRank,
|
|
83
|
+
FirstValue,
|
|
84
|
+
Lag,
|
|
85
|
+
LastValue,
|
|
86
|
+
Lead,
|
|
87
|
+
NthValue,
|
|
88
|
+
Ntile,
|
|
89
|
+
PercentRank,
|
|
90
|
+
Rank,
|
|
91
|
+
RowNumber,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
__all__ = [
|
|
95
|
+
# comparison and conversion
|
|
96
|
+
"Cast",
|
|
97
|
+
"Coalesce",
|
|
98
|
+
"Greatest",
|
|
99
|
+
"JSONObject",
|
|
100
|
+
"Least",
|
|
101
|
+
"NullIf",
|
|
102
|
+
# datetime
|
|
103
|
+
"Extract",
|
|
104
|
+
"ExtractDay",
|
|
105
|
+
"ExtractHour",
|
|
106
|
+
"ExtractMinute",
|
|
107
|
+
"ExtractMonth",
|
|
108
|
+
"ExtractQuarter",
|
|
109
|
+
"ExtractSecond",
|
|
110
|
+
"ExtractWeek",
|
|
111
|
+
"ExtractIsoWeekDay",
|
|
112
|
+
"ExtractWeekDay",
|
|
113
|
+
"ExtractIsoYear",
|
|
114
|
+
"ExtractYear",
|
|
115
|
+
"Now",
|
|
116
|
+
"Trunc",
|
|
117
|
+
"TruncDate",
|
|
118
|
+
"TruncDay",
|
|
119
|
+
"TruncHour",
|
|
120
|
+
"TruncMinute",
|
|
121
|
+
"TruncMonth",
|
|
122
|
+
"TruncQuarter",
|
|
123
|
+
"TruncSecond",
|
|
124
|
+
"TruncTime",
|
|
125
|
+
"TruncWeek",
|
|
126
|
+
"TruncYear",
|
|
127
|
+
# math
|
|
128
|
+
"Abs",
|
|
129
|
+
"ACos",
|
|
130
|
+
"ASin",
|
|
131
|
+
"ATan",
|
|
132
|
+
"ATan2",
|
|
133
|
+
"Ceil",
|
|
134
|
+
"Cos",
|
|
135
|
+
"Cot",
|
|
136
|
+
"Degrees",
|
|
137
|
+
"Exp",
|
|
138
|
+
"Floor",
|
|
139
|
+
"Ln",
|
|
140
|
+
"Log",
|
|
141
|
+
"Mod",
|
|
142
|
+
"Pi",
|
|
143
|
+
"Power",
|
|
144
|
+
"Radians",
|
|
145
|
+
"Random",
|
|
146
|
+
"Round",
|
|
147
|
+
"Sign",
|
|
148
|
+
"Sin",
|
|
149
|
+
"Sqrt",
|
|
150
|
+
"Tan",
|
|
151
|
+
# text
|
|
152
|
+
"MD5",
|
|
153
|
+
"SHA1",
|
|
154
|
+
"SHA224",
|
|
155
|
+
"SHA256",
|
|
156
|
+
"SHA384",
|
|
157
|
+
"SHA512",
|
|
158
|
+
"Chr",
|
|
159
|
+
"Concat",
|
|
160
|
+
"ConcatPair",
|
|
161
|
+
"Left",
|
|
162
|
+
"Length",
|
|
163
|
+
"Lower",
|
|
164
|
+
"LPad",
|
|
165
|
+
"LTrim",
|
|
166
|
+
"Ord",
|
|
167
|
+
"Repeat",
|
|
168
|
+
"Replace",
|
|
169
|
+
"Reverse",
|
|
170
|
+
"Right",
|
|
171
|
+
"RPad",
|
|
172
|
+
"RTrim",
|
|
173
|
+
"StrIndex",
|
|
174
|
+
"Substr",
|
|
175
|
+
"Trim",
|
|
176
|
+
"Upper",
|
|
177
|
+
# window
|
|
178
|
+
"CumeDist",
|
|
179
|
+
"DenseRank",
|
|
180
|
+
"FirstValue",
|
|
181
|
+
"Lag",
|
|
182
|
+
"LastValue",
|
|
183
|
+
"Lead",
|
|
184
|
+
"NthValue",
|
|
185
|
+
"Ntile",
|
|
186
|
+
"PercentRank",
|
|
187
|
+
"Rank",
|
|
188
|
+
"RowNumber",
|
|
189
|
+
]
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""Database functions that do comparisons or type conversions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
|
|
7
|
+
from plain.postgres.expressions import Func, Value
|
|
8
|
+
from plain.postgres.fields import Field, TextField
|
|
9
|
+
from plain.postgres.fields.json import JSONField
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from plain.postgres.connection import DatabaseConnection
|
|
13
|
+
from plain.postgres.sql.compiler import SQLCompiler
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Cast(Func):
|
|
17
|
+
"""Coerce an expression to a new field type."""
|
|
18
|
+
|
|
19
|
+
function = "CAST"
|
|
20
|
+
# PostgreSQL :: shortcut syntax is more readable than standard CAST().
|
|
21
|
+
template = "(%(expressions)s)::%(db_type)s"
|
|
22
|
+
|
|
23
|
+
def __init__(self, expression: Any, output_field: Field) -> None:
|
|
24
|
+
super().__init__(expression, output_field=output_field)
|
|
25
|
+
|
|
26
|
+
def as_sql(
|
|
27
|
+
self,
|
|
28
|
+
compiler: SQLCompiler,
|
|
29
|
+
connection: DatabaseConnection,
|
|
30
|
+
function: str | None = None,
|
|
31
|
+
template: str | None = None,
|
|
32
|
+
arg_joiner: str | None = None,
|
|
33
|
+
**extra_context: Any,
|
|
34
|
+
) -> tuple[str, list[Any]]:
|
|
35
|
+
extra_context["db_type"] = self.output_field.cast_db_type()
|
|
36
|
+
return super().as_sql(
|
|
37
|
+
compiler, connection, function, template, arg_joiner, **extra_context
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Coalesce(Func):
|
|
42
|
+
"""Return, from left to right, the first non-null expression."""
|
|
43
|
+
|
|
44
|
+
function = "COALESCE"
|
|
45
|
+
|
|
46
|
+
def __init__(self, *expressions: Any, **extra: Any) -> None:
|
|
47
|
+
if len(expressions) < 2:
|
|
48
|
+
raise ValueError("Coalesce must take at least two expressions")
|
|
49
|
+
super().__init__(*expressions, **extra)
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def empty_result_set_value(self) -> Any:
|
|
53
|
+
for expression in self.get_source_expressions():
|
|
54
|
+
result = expression.empty_result_set_value
|
|
55
|
+
if result is NotImplemented or result is not None:
|
|
56
|
+
return result
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class Greatest(Func):
|
|
61
|
+
"""
|
|
62
|
+
Return the maximum expression.
|
|
63
|
+
|
|
64
|
+
If any expression is null the return value is database-specific:
|
|
65
|
+
On PostgreSQL, the maximum not-null expression is returned.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
function = "GREATEST"
|
|
69
|
+
|
|
70
|
+
def __init__(self, *expressions: Any, **extra: Any) -> None:
|
|
71
|
+
if len(expressions) < 2:
|
|
72
|
+
raise ValueError("Greatest must take at least two expressions")
|
|
73
|
+
super().__init__(*expressions, **extra)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class JSONObject(Func):
|
|
77
|
+
# PostgreSQL uses JSONB_BUILD_OBJECT for JSON object construction.
|
|
78
|
+
function = "JSONB_BUILD_OBJECT"
|
|
79
|
+
output_field = JSONField()
|
|
80
|
+
|
|
81
|
+
def __init__(self, **fields: Any) -> None:
|
|
82
|
+
expressions = []
|
|
83
|
+
for key, value in fields.items():
|
|
84
|
+
expressions.extend((Value(key), value))
|
|
85
|
+
super().__init__(*expressions)
|
|
86
|
+
|
|
87
|
+
def as_sql(
|
|
88
|
+
self,
|
|
89
|
+
compiler: SQLCompiler,
|
|
90
|
+
connection: DatabaseConnection,
|
|
91
|
+
function: str | None = None,
|
|
92
|
+
template: str | None = None,
|
|
93
|
+
arg_joiner: str | None = None,
|
|
94
|
+
**extra_context: Any,
|
|
95
|
+
) -> tuple[str, list[Any]]:
|
|
96
|
+
# PostgreSQL requires keys to be cast to text.
|
|
97
|
+
copy = self.copy()
|
|
98
|
+
copy.set_source_expressions(
|
|
99
|
+
[
|
|
100
|
+
Cast(expression, TextField()) if index % 2 == 0 else expression
|
|
101
|
+
for index, expression in enumerate(copy.get_source_expressions())
|
|
102
|
+
]
|
|
103
|
+
)
|
|
104
|
+
return super(JSONObject, copy).as_sql(
|
|
105
|
+
compiler, connection, function, template, arg_joiner, **extra_context
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class Least(Func):
|
|
110
|
+
"""
|
|
111
|
+
Return the minimum expression.
|
|
112
|
+
|
|
113
|
+
If any expression is null the return value is database-specific:
|
|
114
|
+
On PostgreSQL, return the minimum not-null expression.
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
function = "LEAST"
|
|
118
|
+
|
|
119
|
+
def __init__(self, *expressions: Any, **extra: Any) -> None:
|
|
120
|
+
if len(expressions) < 2:
|
|
121
|
+
raise ValueError("Least must take at least two expressions")
|
|
122
|
+
super().__init__(*expressions, **extra)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class NullIf(Func):
|
|
126
|
+
function = "NULLIF"
|
|
127
|
+
arity = 2
|