sqlspec 0.25.0__py3-none-any.whl → 0.27.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.
Potentially problematic release.
This version of sqlspec might be problematic. Click here for more details.
- sqlspec/__init__.py +7 -15
- sqlspec/_serialization.py +256 -24
- sqlspec/_typing.py +71 -52
- sqlspec/adapters/adbc/_types.py +1 -1
- sqlspec/adapters/adbc/adk/__init__.py +5 -0
- sqlspec/adapters/adbc/adk/store.py +870 -0
- sqlspec/adapters/adbc/config.py +69 -12
- sqlspec/adapters/adbc/data_dictionary.py +340 -0
- sqlspec/adapters/adbc/driver.py +266 -58
- sqlspec/adapters/adbc/litestar/__init__.py +5 -0
- sqlspec/adapters/adbc/litestar/store.py +504 -0
- sqlspec/adapters/adbc/type_converter.py +153 -0
- sqlspec/adapters/aiosqlite/_types.py +1 -1
- sqlspec/adapters/aiosqlite/adk/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/adk/store.py +527 -0
- sqlspec/adapters/aiosqlite/config.py +88 -15
- sqlspec/adapters/aiosqlite/data_dictionary.py +149 -0
- sqlspec/adapters/aiosqlite/driver.py +143 -40
- sqlspec/adapters/aiosqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/litestar/store.py +281 -0
- sqlspec/adapters/aiosqlite/pool.py +7 -7
- sqlspec/adapters/asyncmy/__init__.py +7 -1
- sqlspec/adapters/asyncmy/_types.py +2 -2
- sqlspec/adapters/asyncmy/adk/__init__.py +5 -0
- sqlspec/adapters/asyncmy/adk/store.py +493 -0
- sqlspec/adapters/asyncmy/config.py +68 -23
- sqlspec/adapters/asyncmy/data_dictionary.py +161 -0
- sqlspec/adapters/asyncmy/driver.py +313 -58
- sqlspec/adapters/asyncmy/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncmy/litestar/store.py +296 -0
- sqlspec/adapters/asyncpg/__init__.py +2 -1
- sqlspec/adapters/asyncpg/_type_handlers.py +71 -0
- sqlspec/adapters/asyncpg/_types.py +11 -7
- sqlspec/adapters/asyncpg/adk/__init__.py +5 -0
- sqlspec/adapters/asyncpg/adk/store.py +450 -0
- sqlspec/adapters/asyncpg/config.py +59 -35
- sqlspec/adapters/asyncpg/data_dictionary.py +173 -0
- sqlspec/adapters/asyncpg/driver.py +170 -25
- sqlspec/adapters/asyncpg/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncpg/litestar/store.py +253 -0
- sqlspec/adapters/bigquery/_types.py +1 -1
- sqlspec/adapters/bigquery/adk/__init__.py +5 -0
- sqlspec/adapters/bigquery/adk/store.py +576 -0
- sqlspec/adapters/bigquery/config.py +27 -10
- sqlspec/adapters/bigquery/data_dictionary.py +149 -0
- sqlspec/adapters/bigquery/driver.py +368 -142
- sqlspec/adapters/bigquery/litestar/__init__.py +5 -0
- sqlspec/adapters/bigquery/litestar/store.py +327 -0
- sqlspec/adapters/bigquery/type_converter.py +125 -0
- sqlspec/adapters/duckdb/_types.py +1 -1
- sqlspec/adapters/duckdb/adk/__init__.py +14 -0
- sqlspec/adapters/duckdb/adk/store.py +553 -0
- sqlspec/adapters/duckdb/config.py +80 -20
- sqlspec/adapters/duckdb/data_dictionary.py +163 -0
- sqlspec/adapters/duckdb/driver.py +167 -45
- sqlspec/adapters/duckdb/litestar/__init__.py +5 -0
- sqlspec/adapters/duckdb/litestar/store.py +332 -0
- sqlspec/adapters/duckdb/pool.py +4 -4
- sqlspec/adapters/duckdb/type_converter.py +133 -0
- sqlspec/adapters/oracledb/_numpy_handlers.py +133 -0
- sqlspec/adapters/oracledb/_types.py +20 -2
- sqlspec/adapters/oracledb/adk/__init__.py +5 -0
- sqlspec/adapters/oracledb/adk/store.py +1745 -0
- sqlspec/adapters/oracledb/config.py +122 -32
- sqlspec/adapters/oracledb/data_dictionary.py +509 -0
- sqlspec/adapters/oracledb/driver.py +353 -91
- sqlspec/adapters/oracledb/litestar/__init__.py +5 -0
- sqlspec/adapters/oracledb/litestar/store.py +767 -0
- sqlspec/adapters/oracledb/migrations.py +348 -73
- sqlspec/adapters/oracledb/type_converter.py +207 -0
- sqlspec/adapters/psqlpy/_type_handlers.py +44 -0
- sqlspec/adapters/psqlpy/_types.py +2 -1
- sqlspec/adapters/psqlpy/adk/__init__.py +5 -0
- sqlspec/adapters/psqlpy/adk/store.py +482 -0
- sqlspec/adapters/psqlpy/config.py +46 -17
- sqlspec/adapters/psqlpy/data_dictionary.py +172 -0
- sqlspec/adapters/psqlpy/driver.py +123 -209
- sqlspec/adapters/psqlpy/litestar/__init__.py +5 -0
- sqlspec/adapters/psqlpy/litestar/store.py +272 -0
- sqlspec/adapters/psqlpy/type_converter.py +102 -0
- sqlspec/adapters/psycopg/_type_handlers.py +80 -0
- sqlspec/adapters/psycopg/_types.py +2 -1
- sqlspec/adapters/psycopg/adk/__init__.py +5 -0
- sqlspec/adapters/psycopg/adk/store.py +944 -0
- sqlspec/adapters/psycopg/config.py +69 -35
- sqlspec/adapters/psycopg/data_dictionary.py +331 -0
- sqlspec/adapters/psycopg/driver.py +238 -81
- sqlspec/adapters/psycopg/litestar/__init__.py +5 -0
- sqlspec/adapters/psycopg/litestar/store.py +554 -0
- sqlspec/adapters/sqlite/__init__.py +2 -1
- sqlspec/adapters/sqlite/_type_handlers.py +86 -0
- sqlspec/adapters/sqlite/_types.py +1 -1
- sqlspec/adapters/sqlite/adk/__init__.py +5 -0
- sqlspec/adapters/sqlite/adk/store.py +572 -0
- sqlspec/adapters/sqlite/config.py +87 -15
- sqlspec/adapters/sqlite/data_dictionary.py +149 -0
- sqlspec/adapters/sqlite/driver.py +137 -54
- sqlspec/adapters/sqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/sqlite/litestar/store.py +318 -0
- sqlspec/adapters/sqlite/pool.py +18 -9
- sqlspec/base.py +45 -26
- sqlspec/builder/__init__.py +73 -4
- sqlspec/builder/_base.py +162 -89
- sqlspec/builder/_column.py +62 -29
- sqlspec/builder/_ddl.py +180 -121
- sqlspec/builder/_delete.py +5 -4
- sqlspec/builder/_dml.py +388 -0
- sqlspec/{_sql.py → builder/_factory.py} +53 -94
- sqlspec/builder/_insert.py +32 -131
- sqlspec/builder/_join.py +375 -0
- sqlspec/builder/_merge.py +446 -11
- sqlspec/builder/_parsing_utils.py +111 -17
- sqlspec/builder/_select.py +1457 -24
- sqlspec/builder/_update.py +11 -42
- sqlspec/cli.py +307 -194
- sqlspec/config.py +252 -67
- sqlspec/core/__init__.py +5 -4
- sqlspec/core/cache.py +17 -17
- sqlspec/core/compiler.py +62 -9
- sqlspec/core/filters.py +37 -37
- sqlspec/core/hashing.py +9 -9
- sqlspec/core/parameters.py +83 -48
- sqlspec/core/result.py +102 -46
- sqlspec/core/splitter.py +16 -17
- sqlspec/core/statement.py +36 -30
- sqlspec/core/type_conversion.py +235 -0
- sqlspec/driver/__init__.py +7 -6
- sqlspec/driver/_async.py +188 -151
- sqlspec/driver/_common.py +285 -80
- sqlspec/driver/_sync.py +188 -152
- sqlspec/driver/mixins/_result_tools.py +20 -236
- sqlspec/driver/mixins/_sql_translator.py +4 -4
- sqlspec/exceptions.py +75 -7
- sqlspec/extensions/adk/__init__.py +53 -0
- sqlspec/extensions/adk/_types.py +51 -0
- sqlspec/extensions/adk/converters.py +172 -0
- sqlspec/extensions/adk/migrations/0001_create_adk_tables.py +144 -0
- sqlspec/extensions/adk/migrations/__init__.py +0 -0
- sqlspec/extensions/adk/service.py +181 -0
- sqlspec/extensions/adk/store.py +536 -0
- sqlspec/extensions/aiosql/adapter.py +73 -53
- sqlspec/extensions/litestar/__init__.py +21 -4
- sqlspec/extensions/litestar/cli.py +54 -10
- sqlspec/extensions/litestar/config.py +59 -266
- sqlspec/extensions/litestar/handlers.py +46 -17
- sqlspec/extensions/litestar/migrations/0001_create_session_table.py +137 -0
- sqlspec/extensions/litestar/migrations/__init__.py +3 -0
- sqlspec/extensions/litestar/plugin.py +324 -223
- sqlspec/extensions/litestar/providers.py +25 -25
- sqlspec/extensions/litestar/store.py +265 -0
- sqlspec/loader.py +30 -49
- sqlspec/migrations/__init__.py +4 -3
- sqlspec/migrations/base.py +302 -39
- sqlspec/migrations/commands.py +611 -144
- sqlspec/migrations/context.py +142 -0
- sqlspec/migrations/fix.py +199 -0
- sqlspec/migrations/loaders.py +68 -23
- sqlspec/migrations/runner.py +543 -107
- sqlspec/migrations/tracker.py +237 -21
- sqlspec/migrations/utils.py +51 -3
- sqlspec/migrations/validation.py +177 -0
- sqlspec/protocols.py +66 -36
- sqlspec/storage/_utils.py +98 -0
- sqlspec/storage/backends/fsspec.py +134 -106
- sqlspec/storage/backends/local.py +78 -51
- sqlspec/storage/backends/obstore.py +278 -162
- sqlspec/storage/registry.py +75 -39
- sqlspec/typing.py +16 -84
- sqlspec/utils/config_resolver.py +153 -0
- sqlspec/utils/correlation.py +4 -5
- sqlspec/utils/data_transformation.py +3 -2
- sqlspec/utils/deprecation.py +9 -8
- sqlspec/utils/fixtures.py +4 -4
- sqlspec/utils/logging.py +46 -6
- sqlspec/utils/module_loader.py +2 -2
- sqlspec/utils/schema.py +288 -0
- sqlspec/utils/serializers.py +50 -2
- sqlspec/utils/sync_tools.py +21 -17
- sqlspec/utils/text.py +1 -2
- sqlspec/utils/type_guards.py +111 -20
- sqlspec/utils/version.py +433 -0
- {sqlspec-0.25.0.dist-info → sqlspec-0.27.0.dist-info}/METADATA +40 -21
- sqlspec-0.27.0.dist-info/RECORD +207 -0
- sqlspec/builder/mixins/__init__.py +0 -55
- sqlspec/builder/mixins/_cte_and_set_ops.py +0 -254
- sqlspec/builder/mixins/_delete_operations.py +0 -50
- sqlspec/builder/mixins/_insert_operations.py +0 -282
- sqlspec/builder/mixins/_join_operations.py +0 -389
- sqlspec/builder/mixins/_merge_operations.py +0 -592
- sqlspec/builder/mixins/_order_limit_operations.py +0 -152
- sqlspec/builder/mixins/_pivot_operations.py +0 -157
- sqlspec/builder/mixins/_select_operations.py +0 -936
- sqlspec/builder/mixins/_update_operations.py +0 -218
- sqlspec/builder/mixins/_where_clause.py +0 -1304
- sqlspec-0.25.0.dist-info/RECORD +0 -139
- sqlspec-0.25.0.dist-info/licenses/NOTICE +0 -29
- {sqlspec-0.25.0.dist-info → sqlspec-0.27.0.dist-info}/WHEEL +0 -0
- {sqlspec-0.25.0.dist-info → sqlspec-0.27.0.dist-info}/entry_points.txt +0 -0
- {sqlspec-0.25.0.dist-info → sqlspec-0.27.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# pyright: reportPrivateUsage=false
|
|
2
|
-
"""DELETE operation mixins.
|
|
3
|
-
|
|
4
|
-
Provides mixins for DELETE statement functionality including
|
|
5
|
-
FROM clause specification.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from typing import Optional
|
|
9
|
-
|
|
10
|
-
from mypy_extensions import trait
|
|
11
|
-
from sqlglot import exp
|
|
12
|
-
from typing_extensions import Self
|
|
13
|
-
|
|
14
|
-
from sqlspec.exceptions import SQLBuilderError
|
|
15
|
-
|
|
16
|
-
__all__ = ("DeleteFromClauseMixin",)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
@trait
|
|
20
|
-
class DeleteFromClauseMixin:
|
|
21
|
-
"""Mixin providing FROM clause for DELETE builders."""
|
|
22
|
-
|
|
23
|
-
__slots__ = ()
|
|
24
|
-
|
|
25
|
-
# Type annotations for PyRight - these will be provided by the base class
|
|
26
|
-
def get_expression(self) -> Optional[exp.Expression]: ...
|
|
27
|
-
def set_expression(self, expression: exp.Expression) -> None: ...
|
|
28
|
-
|
|
29
|
-
def from_(self, table: str) -> Self:
|
|
30
|
-
"""Set the target table for the DELETE statement.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
table: The table name to delete from.
|
|
34
|
-
|
|
35
|
-
Returns:
|
|
36
|
-
The current builder instance for method chaining.
|
|
37
|
-
"""
|
|
38
|
-
current_expr = self.get_expression()
|
|
39
|
-
if current_expr is None:
|
|
40
|
-
self.set_expression(exp.Delete())
|
|
41
|
-
current_expr = self.get_expression()
|
|
42
|
-
|
|
43
|
-
if not isinstance(current_expr, exp.Delete):
|
|
44
|
-
current_expr_type = type(current_expr).__name__
|
|
45
|
-
msg = f"Base expression for Delete is {current_expr_type}, expected Delete."
|
|
46
|
-
raise SQLBuilderError(msg)
|
|
47
|
-
|
|
48
|
-
setattr(self, "_table", table)
|
|
49
|
-
current_expr.set("this", exp.to_table(table))
|
|
50
|
-
return self
|
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
# pyright: reportPrivateUsage=false
|
|
2
|
-
"""INSERT operation mixins.
|
|
3
|
-
|
|
4
|
-
Provides mixins for INSERT statement functionality including
|
|
5
|
-
INTO clauses, VALUES clauses, and INSERT FROM SELECT operations.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from collections.abc import Sequence
|
|
9
|
-
from typing import Any, Optional, TypeVar, Union
|
|
10
|
-
|
|
11
|
-
from mypy_extensions import trait
|
|
12
|
-
from sqlglot import exp
|
|
13
|
-
from typing_extensions import Self
|
|
14
|
-
|
|
15
|
-
from sqlspec.exceptions import SQLBuilderError
|
|
16
|
-
from sqlspec.protocols import SQLBuilderProtocol
|
|
17
|
-
|
|
18
|
-
BuilderT = TypeVar("BuilderT", bound=SQLBuilderProtocol)
|
|
19
|
-
|
|
20
|
-
__all__ = ("InsertFromSelectMixin", "InsertIntoClauseMixin", "InsertValuesMixin")
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@trait
|
|
24
|
-
class InsertIntoClauseMixin:
|
|
25
|
-
"""Mixin providing INTO clause for INSERT builders."""
|
|
26
|
-
|
|
27
|
-
__slots__ = ()
|
|
28
|
-
|
|
29
|
-
# Type annotations for PyRight - these will be provided by the base class
|
|
30
|
-
def get_expression(self) -> Optional[exp.Expression]: ...
|
|
31
|
-
def set_expression(self, expression: exp.Expression) -> None: ...
|
|
32
|
-
|
|
33
|
-
def into(self, table: str) -> Self:
|
|
34
|
-
"""Set the target table for the INSERT statement.
|
|
35
|
-
|
|
36
|
-
Args:
|
|
37
|
-
table: The name of the table to insert data into.
|
|
38
|
-
|
|
39
|
-
Raises:
|
|
40
|
-
SQLBuilderError: If the current expression is not an INSERT statement.
|
|
41
|
-
|
|
42
|
-
Returns:
|
|
43
|
-
The current builder instance for method chaining.
|
|
44
|
-
"""
|
|
45
|
-
current_expr = self.get_expression()
|
|
46
|
-
if current_expr is None:
|
|
47
|
-
self.set_expression(exp.Insert())
|
|
48
|
-
current_expr = self.get_expression()
|
|
49
|
-
|
|
50
|
-
if not isinstance(current_expr, exp.Insert):
|
|
51
|
-
msg = "Cannot set target table on a non-INSERT expression."
|
|
52
|
-
raise SQLBuilderError(msg)
|
|
53
|
-
|
|
54
|
-
setattr(self, "_table", table)
|
|
55
|
-
current_expr.set("this", exp.to_table(table))
|
|
56
|
-
return self
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
@trait
|
|
60
|
-
class InsertValuesMixin:
|
|
61
|
-
"""Mixin providing VALUES and columns methods for INSERT builders."""
|
|
62
|
-
|
|
63
|
-
__slots__ = ()
|
|
64
|
-
|
|
65
|
-
# Type annotations for PyRight - these will be provided by the base class
|
|
66
|
-
def get_expression(self) -> Optional[exp.Expression]: ...
|
|
67
|
-
def set_expression(self, expression: exp.Expression) -> None: ...
|
|
68
|
-
|
|
69
|
-
_columns: Any # Provided by QueryBuilder
|
|
70
|
-
|
|
71
|
-
def add_parameter(self, value: Any, name: Optional[str] = None) -> tuple[Any, str]:
|
|
72
|
-
"""Add parameter - provided by QueryBuilder."""
|
|
73
|
-
msg = "Method must be provided by QueryBuilder subclass"
|
|
74
|
-
raise NotImplementedError(msg)
|
|
75
|
-
|
|
76
|
-
def _generate_unique_parameter_name(self, base_name: str) -> str:
|
|
77
|
-
"""Generate unique parameter name - provided by QueryBuilder."""
|
|
78
|
-
msg = "Method must be provided by QueryBuilder subclass"
|
|
79
|
-
raise NotImplementedError(msg)
|
|
80
|
-
|
|
81
|
-
def columns(self, *columns: Union[str, exp.Expression]) -> Self:
|
|
82
|
-
"""Set the columns for the INSERT statement and synchronize the _columns attribute on the builder."""
|
|
83
|
-
current_expr = self.get_expression()
|
|
84
|
-
if current_expr is None:
|
|
85
|
-
self.set_expression(exp.Insert())
|
|
86
|
-
current_expr = self.get_expression()
|
|
87
|
-
|
|
88
|
-
if not isinstance(current_expr, exp.Insert):
|
|
89
|
-
msg = "Cannot set columns on a non-INSERT expression."
|
|
90
|
-
raise SQLBuilderError(msg)
|
|
91
|
-
|
|
92
|
-
# Get the current table from the expression
|
|
93
|
-
current_this = current_expr.args.get("this")
|
|
94
|
-
if current_this is None:
|
|
95
|
-
msg = "Table must be set using .into() before setting columns."
|
|
96
|
-
raise SQLBuilderError(msg)
|
|
97
|
-
|
|
98
|
-
if columns:
|
|
99
|
-
# Create identifiers for columns
|
|
100
|
-
column_identifiers = [exp.to_identifier(col) if isinstance(col, str) else col for col in columns]
|
|
101
|
-
|
|
102
|
-
# Get table name from current this
|
|
103
|
-
table_name = current_this.this
|
|
104
|
-
|
|
105
|
-
# Create Schema object with table and columns
|
|
106
|
-
schema = exp.Schema(this=table_name, expressions=column_identifiers)
|
|
107
|
-
current_expr.set("this", schema)
|
|
108
|
-
# No columns specified - ensure we have just a Table object
|
|
109
|
-
elif isinstance(current_this, exp.Schema):
|
|
110
|
-
table_name = current_this.this
|
|
111
|
-
current_expr.set("this", exp.Table(this=table_name))
|
|
112
|
-
|
|
113
|
-
try:
|
|
114
|
-
cols = self._columns
|
|
115
|
-
if not columns:
|
|
116
|
-
cols.clear()
|
|
117
|
-
else:
|
|
118
|
-
cols[:] = [col if isinstance(col, str) else str(col) for col in columns]
|
|
119
|
-
except AttributeError:
|
|
120
|
-
pass
|
|
121
|
-
return self
|
|
122
|
-
|
|
123
|
-
def values(self, *values: Any, **kwargs: Any) -> Self:
|
|
124
|
-
"""Add a row of values to the INSERT statement.
|
|
125
|
-
|
|
126
|
-
Supports:
|
|
127
|
-
- values(val1, val2, val3)
|
|
128
|
-
- values(col1=val1, col2=val2)
|
|
129
|
-
- values(mapping)
|
|
130
|
-
|
|
131
|
-
Args:
|
|
132
|
-
*values: Either positional values or a single mapping.
|
|
133
|
-
**kwargs: Column-value pairs.
|
|
134
|
-
|
|
135
|
-
Returns:
|
|
136
|
-
The current builder instance for method chaining.
|
|
137
|
-
"""
|
|
138
|
-
current_expr = self.get_expression()
|
|
139
|
-
if current_expr is None:
|
|
140
|
-
self.set_expression(exp.Insert())
|
|
141
|
-
current_expr = self.get_expression()
|
|
142
|
-
|
|
143
|
-
if not isinstance(current_expr, exp.Insert):
|
|
144
|
-
msg = "Cannot add values to a non-INSERT expression."
|
|
145
|
-
raise SQLBuilderError(msg)
|
|
146
|
-
|
|
147
|
-
if kwargs:
|
|
148
|
-
if values:
|
|
149
|
-
msg = "Cannot mix positional values with keyword values."
|
|
150
|
-
raise SQLBuilderError(msg)
|
|
151
|
-
try:
|
|
152
|
-
cols = self._columns
|
|
153
|
-
if not cols:
|
|
154
|
-
self.columns(*kwargs.keys())
|
|
155
|
-
except AttributeError:
|
|
156
|
-
pass
|
|
157
|
-
row_exprs = []
|
|
158
|
-
for col, val in kwargs.items():
|
|
159
|
-
if isinstance(val, exp.Expression):
|
|
160
|
-
row_exprs.append(val)
|
|
161
|
-
else:
|
|
162
|
-
column_name = col if isinstance(col, str) else str(col)
|
|
163
|
-
if "." in column_name:
|
|
164
|
-
column_name = column_name.split(".")[-1]
|
|
165
|
-
param_name = self._generate_unique_parameter_name(column_name)
|
|
166
|
-
_, param_name = self.add_parameter(val, name=param_name)
|
|
167
|
-
row_exprs.append(exp.Placeholder(this=param_name))
|
|
168
|
-
elif len(values) == 1 and hasattr(values[0], "items"):
|
|
169
|
-
mapping = values[0]
|
|
170
|
-
try:
|
|
171
|
-
cols = self._columns
|
|
172
|
-
if not cols:
|
|
173
|
-
self.columns(*mapping.keys())
|
|
174
|
-
except AttributeError:
|
|
175
|
-
pass
|
|
176
|
-
row_exprs = []
|
|
177
|
-
for col, val in mapping.items():
|
|
178
|
-
if isinstance(val, exp.Expression):
|
|
179
|
-
row_exprs.append(val)
|
|
180
|
-
else:
|
|
181
|
-
column_name = col if isinstance(col, str) else str(col)
|
|
182
|
-
if "." in column_name:
|
|
183
|
-
column_name = column_name.split(".")[-1]
|
|
184
|
-
param_name = self._generate_unique_parameter_name(column_name)
|
|
185
|
-
_, param_name = self.add_parameter(val, name=param_name)
|
|
186
|
-
row_exprs.append(exp.Placeholder(this=param_name))
|
|
187
|
-
else:
|
|
188
|
-
try:
|
|
189
|
-
cols = self._columns
|
|
190
|
-
if cols and len(values) != len(cols):
|
|
191
|
-
msg = f"Number of values ({len(values)}) does not match the number of specified columns ({len(cols)})."
|
|
192
|
-
raise SQLBuilderError(msg)
|
|
193
|
-
except AttributeError:
|
|
194
|
-
pass
|
|
195
|
-
row_exprs = []
|
|
196
|
-
for i, v in enumerate(values):
|
|
197
|
-
if isinstance(v, exp.Expression):
|
|
198
|
-
row_exprs.append(v)
|
|
199
|
-
else:
|
|
200
|
-
try:
|
|
201
|
-
cols = self._columns
|
|
202
|
-
if cols and i < len(cols):
|
|
203
|
-
column_name = str(cols[i]).split(".")[-1] if "." in str(cols[i]) else str(cols[i])
|
|
204
|
-
param_name = self._generate_unique_parameter_name(column_name)
|
|
205
|
-
else:
|
|
206
|
-
param_name = self._generate_unique_parameter_name(f"value_{i + 1}")
|
|
207
|
-
except AttributeError:
|
|
208
|
-
param_name = self._generate_unique_parameter_name(f"value_{i + 1}")
|
|
209
|
-
_, param_name = self.add_parameter(v, name=param_name)
|
|
210
|
-
row_exprs.append(exp.Placeholder(this=param_name))
|
|
211
|
-
|
|
212
|
-
values_expr = exp.Values(expressions=[row_exprs])
|
|
213
|
-
current_expr.set("expression", values_expr)
|
|
214
|
-
return self
|
|
215
|
-
|
|
216
|
-
def add_values(self, values: Sequence[Any]) -> Self:
|
|
217
|
-
"""Add a row of values to the INSERT statement (alternative signature).
|
|
218
|
-
|
|
219
|
-
Args:
|
|
220
|
-
values: Sequence of values for the row.
|
|
221
|
-
|
|
222
|
-
Returns:
|
|
223
|
-
The current builder instance for method chaining.
|
|
224
|
-
"""
|
|
225
|
-
return self.values(*values)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
@trait
|
|
229
|
-
class InsertFromSelectMixin:
|
|
230
|
-
"""Mixin providing INSERT ... SELECT support for INSERT builders."""
|
|
231
|
-
|
|
232
|
-
__slots__ = ()
|
|
233
|
-
|
|
234
|
-
# Type annotations for PyRight - these will be provided by the base class
|
|
235
|
-
def get_expression(self) -> Optional[exp.Expression]: ...
|
|
236
|
-
def set_expression(self, expression: exp.Expression) -> None: ...
|
|
237
|
-
|
|
238
|
-
_table: Any # Provided by QueryBuilder
|
|
239
|
-
|
|
240
|
-
def add_parameter(self, value: Any, name: Optional[str] = None) -> tuple[Any, str]:
|
|
241
|
-
"""Add parameter - provided by QueryBuilder."""
|
|
242
|
-
msg = "Method must be provided by QueryBuilder subclass"
|
|
243
|
-
raise NotImplementedError(msg)
|
|
244
|
-
|
|
245
|
-
def from_select(self, select_builder: Any) -> Self:
|
|
246
|
-
"""Sets the INSERT source to a SELECT statement.
|
|
247
|
-
|
|
248
|
-
Args:
|
|
249
|
-
select_builder: A SelectBuilder instance representing the SELECT query.
|
|
250
|
-
|
|
251
|
-
Returns:
|
|
252
|
-
The current builder instance for method chaining.
|
|
253
|
-
|
|
254
|
-
Raises:
|
|
255
|
-
SQLBuilderError: If the table is not set or the select_builder is invalid.
|
|
256
|
-
"""
|
|
257
|
-
try:
|
|
258
|
-
if not self._table:
|
|
259
|
-
msg = "The target table must be set using .into() before adding values."
|
|
260
|
-
raise SQLBuilderError(msg)
|
|
261
|
-
except AttributeError:
|
|
262
|
-
msg = "The target table must be set using .into() before adding values."
|
|
263
|
-
raise SQLBuilderError(msg)
|
|
264
|
-
current_expr = self.get_expression()
|
|
265
|
-
if current_expr is None:
|
|
266
|
-
self.set_expression(exp.Insert())
|
|
267
|
-
current_expr = self.get_expression()
|
|
268
|
-
|
|
269
|
-
if not isinstance(current_expr, exp.Insert):
|
|
270
|
-
msg = "Cannot set INSERT source on a non-INSERT expression."
|
|
271
|
-
raise SQLBuilderError(msg)
|
|
272
|
-
subquery_parameters = select_builder._parameters
|
|
273
|
-
if subquery_parameters:
|
|
274
|
-
for p_name, p_value in subquery_parameters.items():
|
|
275
|
-
self.add_parameter(p_value, name=p_name)
|
|
276
|
-
select_expr = select_builder._expression
|
|
277
|
-
if select_expr and isinstance(select_expr, exp.Select):
|
|
278
|
-
current_expr.set("expression", select_expr.copy())
|
|
279
|
-
else:
|
|
280
|
-
msg = "SelectBuilder must have a valid SELECT expression."
|
|
281
|
-
raise SQLBuilderError(msg)
|
|
282
|
-
return self
|