sqlspec 0.13.1__py3-none-any.whl → 0.16.2__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.

Files changed (185) hide show
  1. sqlspec/__init__.py +71 -8
  2. sqlspec/__main__.py +12 -0
  3. sqlspec/__metadata__.py +1 -3
  4. sqlspec/_serialization.py +1 -2
  5. sqlspec/_sql.py +930 -136
  6. sqlspec/_typing.py +278 -142
  7. sqlspec/adapters/adbc/__init__.py +4 -3
  8. sqlspec/adapters/adbc/_types.py +12 -0
  9. sqlspec/adapters/adbc/config.py +116 -285
  10. sqlspec/adapters/adbc/driver.py +462 -340
  11. sqlspec/adapters/aiosqlite/__init__.py +18 -3
  12. sqlspec/adapters/aiosqlite/_types.py +13 -0
  13. sqlspec/adapters/aiosqlite/config.py +202 -150
  14. sqlspec/adapters/aiosqlite/driver.py +226 -247
  15. sqlspec/adapters/asyncmy/__init__.py +18 -3
  16. sqlspec/adapters/asyncmy/_types.py +12 -0
  17. sqlspec/adapters/asyncmy/config.py +80 -199
  18. sqlspec/adapters/asyncmy/driver.py +257 -215
  19. sqlspec/adapters/asyncpg/__init__.py +19 -4
  20. sqlspec/adapters/asyncpg/_types.py +17 -0
  21. sqlspec/adapters/asyncpg/config.py +81 -214
  22. sqlspec/adapters/asyncpg/driver.py +284 -359
  23. sqlspec/adapters/bigquery/__init__.py +17 -3
  24. sqlspec/adapters/bigquery/_types.py +12 -0
  25. sqlspec/adapters/bigquery/config.py +191 -299
  26. sqlspec/adapters/bigquery/driver.py +474 -634
  27. sqlspec/adapters/duckdb/__init__.py +14 -3
  28. sqlspec/adapters/duckdb/_types.py +12 -0
  29. sqlspec/adapters/duckdb/config.py +414 -397
  30. sqlspec/adapters/duckdb/driver.py +342 -393
  31. sqlspec/adapters/oracledb/__init__.py +19 -5
  32. sqlspec/adapters/oracledb/_types.py +14 -0
  33. sqlspec/adapters/oracledb/config.py +123 -458
  34. sqlspec/adapters/oracledb/driver.py +505 -531
  35. sqlspec/adapters/psqlpy/__init__.py +13 -3
  36. sqlspec/adapters/psqlpy/_types.py +11 -0
  37. sqlspec/adapters/psqlpy/config.py +93 -307
  38. sqlspec/adapters/psqlpy/driver.py +504 -213
  39. sqlspec/adapters/psycopg/__init__.py +19 -5
  40. sqlspec/adapters/psycopg/_types.py +17 -0
  41. sqlspec/adapters/psycopg/config.py +143 -472
  42. sqlspec/adapters/psycopg/driver.py +704 -825
  43. sqlspec/adapters/sqlite/__init__.py +14 -3
  44. sqlspec/adapters/sqlite/_types.py +11 -0
  45. sqlspec/adapters/sqlite/config.py +208 -142
  46. sqlspec/adapters/sqlite/driver.py +263 -278
  47. sqlspec/base.py +105 -9
  48. sqlspec/{statement/builder → builder}/__init__.py +12 -14
  49. sqlspec/{statement/builder/base.py → builder/_base.py} +184 -86
  50. sqlspec/{statement/builder/column.py → builder/_column.py} +97 -60
  51. sqlspec/{statement/builder/ddl.py → builder/_ddl.py} +61 -131
  52. sqlspec/{statement/builder → builder}/_ddl_utils.py +4 -10
  53. sqlspec/{statement/builder/delete.py → builder/_delete.py} +10 -30
  54. sqlspec/builder/_insert.py +421 -0
  55. sqlspec/builder/_merge.py +71 -0
  56. sqlspec/{statement/builder → builder}/_parsing_utils.py +49 -26
  57. sqlspec/builder/_select.py +170 -0
  58. sqlspec/{statement/builder/update.py → builder/_update.py} +16 -20
  59. sqlspec/builder/mixins/__init__.py +55 -0
  60. sqlspec/builder/mixins/_cte_and_set_ops.py +222 -0
  61. sqlspec/{statement/builder/mixins/_delete_from.py → builder/mixins/_delete_operations.py} +8 -1
  62. sqlspec/builder/mixins/_insert_operations.py +244 -0
  63. sqlspec/{statement/builder/mixins/_join.py → builder/mixins/_join_operations.py} +45 -13
  64. sqlspec/{statement/builder/mixins/_merge_clauses.py → builder/mixins/_merge_operations.py} +188 -30
  65. sqlspec/builder/mixins/_order_limit_operations.py +135 -0
  66. sqlspec/builder/mixins/_pivot_operations.py +153 -0
  67. sqlspec/builder/mixins/_select_operations.py +604 -0
  68. sqlspec/builder/mixins/_update_operations.py +202 -0
  69. sqlspec/builder/mixins/_where_clause.py +644 -0
  70. sqlspec/cli.py +247 -0
  71. sqlspec/config.py +183 -138
  72. sqlspec/core/__init__.py +63 -0
  73. sqlspec/core/cache.py +871 -0
  74. sqlspec/core/compiler.py +417 -0
  75. sqlspec/core/filters.py +830 -0
  76. sqlspec/core/hashing.py +310 -0
  77. sqlspec/core/parameters.py +1237 -0
  78. sqlspec/core/result.py +677 -0
  79. sqlspec/{statement → core}/splitter.py +321 -191
  80. sqlspec/core/statement.py +676 -0
  81. sqlspec/driver/__init__.py +7 -10
  82. sqlspec/driver/_async.py +422 -163
  83. sqlspec/driver/_common.py +545 -287
  84. sqlspec/driver/_sync.py +426 -160
  85. sqlspec/driver/mixins/__init__.py +2 -13
  86. sqlspec/driver/mixins/_result_tools.py +193 -0
  87. sqlspec/driver/mixins/_sql_translator.py +65 -14
  88. sqlspec/exceptions.py +5 -252
  89. sqlspec/extensions/aiosql/adapter.py +93 -96
  90. sqlspec/extensions/litestar/__init__.py +2 -1
  91. sqlspec/extensions/litestar/cli.py +48 -0
  92. sqlspec/extensions/litestar/config.py +0 -1
  93. sqlspec/extensions/litestar/handlers.py +15 -26
  94. sqlspec/extensions/litestar/plugin.py +21 -16
  95. sqlspec/extensions/litestar/providers.py +17 -52
  96. sqlspec/loader.py +423 -104
  97. sqlspec/migrations/__init__.py +35 -0
  98. sqlspec/migrations/base.py +414 -0
  99. sqlspec/migrations/commands.py +443 -0
  100. sqlspec/migrations/loaders.py +402 -0
  101. sqlspec/migrations/runner.py +213 -0
  102. sqlspec/migrations/tracker.py +140 -0
  103. sqlspec/migrations/utils.py +129 -0
  104. sqlspec/protocols.py +51 -186
  105. sqlspec/storage/__init__.py +1 -1
  106. sqlspec/storage/backends/base.py +37 -40
  107. sqlspec/storage/backends/fsspec.py +136 -112
  108. sqlspec/storage/backends/obstore.py +138 -160
  109. sqlspec/storage/capabilities.py +5 -4
  110. sqlspec/storage/registry.py +57 -106
  111. sqlspec/typing.py +136 -115
  112. sqlspec/utils/__init__.py +2 -2
  113. sqlspec/utils/correlation.py +0 -3
  114. sqlspec/utils/deprecation.py +6 -6
  115. sqlspec/utils/fixtures.py +6 -6
  116. sqlspec/utils/logging.py +0 -2
  117. sqlspec/utils/module_loader.py +7 -12
  118. sqlspec/utils/singleton.py +0 -1
  119. sqlspec/utils/sync_tools.py +17 -38
  120. sqlspec/utils/text.py +12 -51
  121. sqlspec/utils/type_guards.py +482 -235
  122. {sqlspec-0.13.1.dist-info → sqlspec-0.16.2.dist-info}/METADATA +7 -2
  123. sqlspec-0.16.2.dist-info/RECORD +134 -0
  124. sqlspec-0.16.2.dist-info/entry_points.txt +2 -0
  125. sqlspec/driver/connection.py +0 -207
  126. sqlspec/driver/mixins/_csv_writer.py +0 -91
  127. sqlspec/driver/mixins/_pipeline.py +0 -512
  128. sqlspec/driver/mixins/_result_utils.py +0 -140
  129. sqlspec/driver/mixins/_storage.py +0 -926
  130. sqlspec/driver/mixins/_type_coercion.py +0 -130
  131. sqlspec/driver/parameters.py +0 -138
  132. sqlspec/service/__init__.py +0 -4
  133. sqlspec/service/_util.py +0 -147
  134. sqlspec/service/base.py +0 -1131
  135. sqlspec/service/pagination.py +0 -26
  136. sqlspec/statement/__init__.py +0 -21
  137. sqlspec/statement/builder/insert.py +0 -288
  138. sqlspec/statement/builder/merge.py +0 -95
  139. sqlspec/statement/builder/mixins/__init__.py +0 -65
  140. sqlspec/statement/builder/mixins/_aggregate_functions.py +0 -250
  141. sqlspec/statement/builder/mixins/_case_builder.py +0 -91
  142. sqlspec/statement/builder/mixins/_common_table_expr.py +0 -90
  143. sqlspec/statement/builder/mixins/_from.py +0 -63
  144. sqlspec/statement/builder/mixins/_group_by.py +0 -118
  145. sqlspec/statement/builder/mixins/_having.py +0 -35
  146. sqlspec/statement/builder/mixins/_insert_from_select.py +0 -47
  147. sqlspec/statement/builder/mixins/_insert_into.py +0 -36
  148. sqlspec/statement/builder/mixins/_insert_values.py +0 -67
  149. sqlspec/statement/builder/mixins/_limit_offset.py +0 -53
  150. sqlspec/statement/builder/mixins/_order_by.py +0 -46
  151. sqlspec/statement/builder/mixins/_pivot.py +0 -79
  152. sqlspec/statement/builder/mixins/_returning.py +0 -37
  153. sqlspec/statement/builder/mixins/_select_columns.py +0 -61
  154. sqlspec/statement/builder/mixins/_set_ops.py +0 -122
  155. sqlspec/statement/builder/mixins/_unpivot.py +0 -77
  156. sqlspec/statement/builder/mixins/_update_from.py +0 -55
  157. sqlspec/statement/builder/mixins/_update_set.py +0 -94
  158. sqlspec/statement/builder/mixins/_update_table.py +0 -29
  159. sqlspec/statement/builder/mixins/_where.py +0 -401
  160. sqlspec/statement/builder/mixins/_window_functions.py +0 -86
  161. sqlspec/statement/builder/select.py +0 -221
  162. sqlspec/statement/filters.py +0 -596
  163. sqlspec/statement/parameter_manager.py +0 -220
  164. sqlspec/statement/parameters.py +0 -867
  165. sqlspec/statement/pipelines/__init__.py +0 -210
  166. sqlspec/statement/pipelines/analyzers/__init__.py +0 -9
  167. sqlspec/statement/pipelines/analyzers/_analyzer.py +0 -646
  168. sqlspec/statement/pipelines/context.py +0 -115
  169. sqlspec/statement/pipelines/transformers/__init__.py +0 -7
  170. sqlspec/statement/pipelines/transformers/_expression_simplifier.py +0 -88
  171. sqlspec/statement/pipelines/transformers/_literal_parameterizer.py +0 -1247
  172. sqlspec/statement/pipelines/transformers/_remove_comments_and_hints.py +0 -76
  173. sqlspec/statement/pipelines/validators/__init__.py +0 -23
  174. sqlspec/statement/pipelines/validators/_dml_safety.py +0 -290
  175. sqlspec/statement/pipelines/validators/_parameter_style.py +0 -370
  176. sqlspec/statement/pipelines/validators/_performance.py +0 -718
  177. sqlspec/statement/pipelines/validators/_security.py +0 -967
  178. sqlspec/statement/result.py +0 -435
  179. sqlspec/statement/sql.py +0 -1704
  180. sqlspec/statement/sql_compiler.py +0 -140
  181. sqlspec/utils/cached_property.py +0 -25
  182. sqlspec-0.13.1.dist-info/RECORD +0 -150
  183. {sqlspec-0.13.1.dist-info → sqlspec-0.16.2.dist-info}/WHEEL +0 -0
  184. {sqlspec-0.13.1.dist-info → sqlspec-0.16.2.dist-info}/licenses/LICENSE +0 -0
  185. {sqlspec-0.13.1.dist-info → sqlspec-0.16.2.dist-info}/licenses/NOTICE +0 -0
@@ -4,46 +4,28 @@ This module provides a fluent interface for building SQL queries safely,
4
4
  with automatic parameter binding and validation.
5
5
  """
6
6
 
7
- from dataclasses import dataclass, field
8
7
  from typing import Any, Optional
9
8
 
10
9
  from sqlglot import exp
11
10
 
12
- from sqlspec.statement.builder.base import QueryBuilder, SafeQuery
13
- from sqlspec.statement.builder.mixins import DeleteFromClauseMixin, ReturningClauseMixin, WhereClauseMixin
14
- from sqlspec.statement.result import SQLResult
15
- from sqlspec.typing import RowT
11
+ from sqlspec.builder._base import QueryBuilder, SafeQuery
12
+ from sqlspec.builder.mixins import DeleteFromClauseMixin, ReturningClauseMixin, WhereClauseMixin
13
+ from sqlspec.core.result import SQLResult
14
+ from sqlspec.exceptions import SQLBuilderError
16
15
 
17
16
  __all__ = ("Delete",)
18
17
 
19
18
 
20
- @dataclass(unsafe_hash=True)
21
- class Delete(QueryBuilder[RowT], WhereClauseMixin, ReturningClauseMixin, DeleteFromClauseMixin):
19
+ class Delete(QueryBuilder, WhereClauseMixin, ReturningClauseMixin, DeleteFromClauseMixin):
22
20
  """Builder for DELETE statements.
23
21
 
24
22
  This builder provides a fluent interface for constructing SQL DELETE statements
25
23
  with automatic parameter binding and validation. It does not support JOIN
26
24
  operations to maintain cross-dialect compatibility and safety.
27
-
28
- Example:
29
- ```python
30
- # Basic DELETE
31
- delete_query = Delete().from_("users").where("age < 18")
32
-
33
- # Even more concise with constructor
34
- delete_query = Delete("users").where("age < 18")
35
-
36
- # DELETE with parameterized conditions
37
- delete_query = (
38
- Delete()
39
- .from_("users")
40
- .where_eq("status", "inactive")
41
- .where_in("category", ["test", "demo"])
42
- )
43
- ```
44
25
  """
45
26
 
46
- _table: "Optional[str]" = field(default=None, init=False)
27
+ __slots__ = ("_table",)
28
+ _expression: Optional[exp.Expression]
47
29
 
48
30
  def __init__(self, table: Optional[str] = None, **kwargs: Any) -> None:
49
31
  """Initialize DELETE with optional table.
@@ -53,21 +35,21 @@ class Delete(QueryBuilder[RowT], WhereClauseMixin, ReturningClauseMixin, DeleteF
53
35
  **kwargs: Additional QueryBuilder arguments
54
36
  """
55
37
  super().__init__(**kwargs)
38
+ self._initialize_expression()
56
39
 
57
- # Initialize fields from dataclass
58
40
  self._table = None
59
41
 
60
42
  if table:
61
43
  self.from_(table)
62
44
 
63
45
  @property
64
- def _expected_result_type(self) -> "type[SQLResult[RowT]]":
46
+ def _expected_result_type(self) -> "type[SQLResult]":
65
47
  """Get the expected result type for DELETE operations.
66
48
 
67
49
  Returns:
68
50
  The ExecuteResult type for DELETE statements.
69
51
  """
70
- return SQLResult[RowT]
52
+ return SQLResult
71
53
 
72
54
  def _create_base_expression(self) -> "exp.Delete":
73
55
  """Create a new sqlglot Delete expression.
@@ -88,8 +70,6 @@ class Delete(QueryBuilder[RowT], WhereClauseMixin, ReturningClauseMixin, DeleteF
88
70
  """
89
71
 
90
72
  if not self._table:
91
- from sqlspec.exceptions import SQLBuilderError
92
-
93
73
  msg = "DELETE requires a table to be specified. Use from() to set the table."
94
74
  raise SQLBuilderError(msg)
95
75
 
@@ -0,0 +1,421 @@
1
+ """Safe SQL query builder with validation and parameter binding.
2
+
3
+ This module provides a fluent interface for building SQL queries safely,
4
+ with automatic parameter binding and validation.
5
+ """
6
+
7
+ from typing import TYPE_CHECKING, Any, Final, Optional
8
+
9
+ from sqlglot import exp
10
+ from typing_extensions import Self
11
+
12
+ from sqlspec.builder._base import QueryBuilder
13
+ from sqlspec.builder.mixins import InsertFromSelectMixin, InsertIntoClauseMixin, InsertValuesMixin, ReturningClauseMixin
14
+ from sqlspec.core.result import SQLResult
15
+ from sqlspec.exceptions import SQLBuilderError
16
+
17
+ if TYPE_CHECKING:
18
+ from collections.abc import Mapping, Sequence
19
+
20
+
21
+ __all__ = ("Insert",)
22
+
23
+ ERR_MSG_TABLE_NOT_SET: Final[str] = "The target table must be set using .into() before adding values."
24
+ ERR_MSG_VALUES_COLUMNS_MISMATCH: Final[str] = (
25
+ "Number of values ({values_len}) does not match the number of specified columns ({columns_len})."
26
+ )
27
+ ERR_MSG_INTERNAL_EXPRESSION_TYPE: Final[str] = "Internal error: expression is not an Insert instance as expected."
28
+ ERR_MSG_EXPRESSION_NOT_INITIALIZED: Final[str] = "Internal error: base expression not initialized."
29
+
30
+
31
+ class Insert(QueryBuilder, ReturningClauseMixin, InsertValuesMixin, InsertFromSelectMixin, InsertIntoClauseMixin):
32
+ """Builder for INSERT statements.
33
+
34
+ This builder facilitates the construction of SQL INSERT queries
35
+ in a safe and dialect-agnostic manner with automatic parameter binding.
36
+ """
37
+
38
+ __slots__ = ("_columns", "_table", "_values_added_count")
39
+
40
+ def __init__(self, table: Optional[str] = None, **kwargs: Any) -> None:
41
+ """Initialize INSERT with optional table.
42
+
43
+ Args:
44
+ table: Target table name
45
+ **kwargs: Additional QueryBuilder arguments
46
+ """
47
+ super().__init__(**kwargs)
48
+
49
+ # Initialize Insert-specific attributes
50
+ self._table: Optional[str] = None
51
+ self._columns: list[str] = []
52
+ self._values_added_count: int = 0
53
+
54
+ self._initialize_expression()
55
+
56
+ if table:
57
+ self.into(table)
58
+
59
+ def _create_base_expression(self) -> exp.Insert:
60
+ """Create a base INSERT expression.
61
+
62
+ This method is called by the base QueryBuilder during initialization.
63
+
64
+ Returns:
65
+ A new sqlglot Insert expression.
66
+ """
67
+ return exp.Insert()
68
+
69
+ @property
70
+ def _expected_result_type(self) -> "type[SQLResult]":
71
+ """Specifies the expected result type for an INSERT query.
72
+
73
+ Returns:
74
+ The type of result expected for INSERT operations.
75
+ """
76
+ return SQLResult
77
+
78
+ def _get_insert_expression(self) -> exp.Insert:
79
+ """Safely gets and casts the internal expression to exp.Insert.
80
+
81
+ Returns:
82
+ The internal expression as exp.Insert.
83
+
84
+ Raises:
85
+ SQLBuilderError: If the expression is not initialized or is not an Insert.
86
+ """
87
+ if self._expression is None:
88
+ raise SQLBuilderError(ERR_MSG_EXPRESSION_NOT_INITIALIZED)
89
+ if not isinstance(self._expression, exp.Insert):
90
+ raise SQLBuilderError(ERR_MSG_INTERNAL_EXPRESSION_TYPE)
91
+ return self._expression
92
+
93
+ def values(self, *values: Any, **kwargs: Any) -> "Self":
94
+ """Adds a row of values to the INSERT statement.
95
+
96
+ This method can be called multiple times to insert multiple rows,
97
+ resulting in a multi-row INSERT statement like `VALUES (...), (...)`.
98
+
99
+ Supports:
100
+ - values(val1, val2, val3)
101
+ - values(col1=val1, col2=val2)
102
+ - values(mapping)
103
+
104
+ Args:
105
+ *values: The values for the row to be inserted. The number of values
106
+ must match the number of columns set by `columns()`, if `columns()` was called
107
+ and specified any non-empty list of columns.
108
+ **kwargs: Column-value pairs for named values.
109
+
110
+ Returns:
111
+ The current builder instance for method chaining.
112
+
113
+ Raises:
114
+ SQLBuilderError: If `into()` has not been called to set the table,
115
+ or if `columns()` was called with a non-empty list of columns
116
+ and the number of values does not match the number of specified columns.
117
+ """
118
+ if not self._table:
119
+ raise SQLBuilderError(ERR_MSG_TABLE_NOT_SET)
120
+
121
+ if kwargs:
122
+ if values:
123
+ msg = "Cannot mix positional values with keyword values."
124
+ raise SQLBuilderError(msg)
125
+ return self.values_from_dict(kwargs)
126
+
127
+ if len(values) == 1:
128
+ try:
129
+ values_0 = values[0]
130
+ if hasattr(values_0, "items"):
131
+ return self.values_from_dict(values_0)
132
+ except (AttributeError, TypeError):
133
+ pass
134
+
135
+ insert_expr = self._get_insert_expression()
136
+
137
+ if self._columns and len(values) != len(self._columns):
138
+ msg = ERR_MSG_VALUES_COLUMNS_MISMATCH.format(values_len=len(values), columns_len=len(self._columns))
139
+ raise SQLBuilderError(msg)
140
+
141
+ value_placeholders: list[exp.Expression] = []
142
+ for i, value in enumerate(values):
143
+ if isinstance(value, exp.Expression):
144
+ value_placeholders.append(value)
145
+ elif hasattr(value, "expression") and hasattr(value, "sql"):
146
+ # Handle SQL objects (from sql.raw with parameters)
147
+ expression = getattr(value, "expression", None)
148
+ if expression is not None and isinstance(expression, exp.Expression):
149
+ # Merge parameters from SQL object into builder
150
+ if hasattr(value, "parameters"):
151
+ sql_parameters = getattr(value, "parameters", {})
152
+ for param_name, param_value in sql_parameters.items():
153
+ self.add_parameter(param_value, name=param_name)
154
+ value_placeholders.append(expression)
155
+ else:
156
+ # If expression is None, fall back to parsing the raw SQL
157
+ sql_text = getattr(value, "sql", "")
158
+ # Merge parameters even when parsing raw SQL
159
+ if hasattr(value, "parameters"):
160
+ sql_parameters = getattr(value, "parameters", {})
161
+ for param_name, param_value in sql_parameters.items():
162
+ self.add_parameter(param_value, name=param_name)
163
+ # Check if sql_text is callable (like Expression.sql method)
164
+ if callable(sql_text):
165
+ sql_text = str(value)
166
+ value_expr = exp.maybe_parse(sql_text) or exp.convert(str(sql_text))
167
+ value_placeholders.append(value_expr)
168
+ else:
169
+ if self._columns and i < len(self._columns):
170
+ column_str = str(self._columns[i])
171
+ column_name = column_str.rsplit(".", maxsplit=1)[-1] if "." in column_str else column_str
172
+ param_name = self._generate_unique_parameter_name(column_name)
173
+ else:
174
+ param_name = self._generate_unique_parameter_name(f"value_{i + 1}")
175
+ _, param_name = self.add_parameter(value, name=param_name)
176
+ value_placeholders.append(exp.var(param_name))
177
+
178
+ tuple_expr = exp.Tuple(expressions=value_placeholders)
179
+ if self._values_added_count == 0:
180
+ insert_expr.set("expression", exp.Values(expressions=[tuple_expr]))
181
+ else:
182
+ current_values = insert_expr.args.get("expression")
183
+ if isinstance(current_values, exp.Values):
184
+ current_values.expressions.append(tuple_expr)
185
+ else:
186
+ insert_expr.set("expression", exp.Values(expressions=[tuple_expr]))
187
+
188
+ self._values_added_count += 1
189
+ return self
190
+
191
+ def values_from_dict(self, data: "Mapping[str, Any]") -> "Self":
192
+ """Adds a row of values from a dictionary.
193
+
194
+ This is a convenience method that automatically sets columns based on
195
+ the dictionary keys and values based on the dictionary values.
196
+
197
+ Args:
198
+ data: A mapping of column names to values.
199
+
200
+ Returns:
201
+ The current builder instance for method chaining.
202
+
203
+ Raises:
204
+ SQLBuilderError: If `into()` has not been called to set the table.
205
+ """
206
+ if not self._table:
207
+ raise SQLBuilderError(ERR_MSG_TABLE_NOT_SET)
208
+
209
+ data_keys = list(data.keys())
210
+ if not self._columns:
211
+ self.columns(*data_keys)
212
+ elif set(self._columns) != set(data_keys):
213
+ msg = f"Dictionary keys {set(data_keys)} do not match existing columns {set(self._columns)}."
214
+ raise SQLBuilderError(msg)
215
+
216
+ return self.values(*[data[col] for col in self._columns])
217
+
218
+ def values_from_dicts(self, data: "Sequence[Mapping[str, Any]]") -> "Self":
219
+ """Adds multiple rows of values from a sequence of dictionaries.
220
+
221
+ This is a convenience method for bulk inserts from structured data.
222
+
223
+ Args:
224
+ data: A sequence of mappings, each representing a row of data.
225
+
226
+ Returns:
227
+ The current builder instance for method chaining.
228
+
229
+ Raises:
230
+ SQLBuilderError: If `into()` has not been called to set the table,
231
+ or if dictionaries have inconsistent keys.
232
+ """
233
+ if not data:
234
+ return self
235
+
236
+ first_dict = data[0]
237
+ if not self._columns:
238
+ self.columns(*first_dict.keys())
239
+
240
+ expected_keys = set(self._columns)
241
+ for i, row_dict in enumerate(data):
242
+ if set(row_dict.keys()) != expected_keys:
243
+ msg = (
244
+ f"Dictionary at index {i} has keys {set(row_dict.keys())} "
245
+ f"which do not match expected keys {expected_keys}."
246
+ )
247
+ raise SQLBuilderError(msg)
248
+
249
+ for row_dict in data:
250
+ self.values(*[row_dict[col] for col in self._columns])
251
+
252
+ return self
253
+
254
+ def on_conflict(self, *columns: str) -> "ConflictBuilder":
255
+ """Adds an ON CONFLICT clause with specified columns.
256
+
257
+ Args:
258
+ *columns: Column names that define the conflict. If no columns provided,
259
+ creates an ON CONFLICT without specific columns (catches all conflicts).
260
+
261
+ Returns:
262
+ A ConflictBuilder instance for chaining conflict resolution methods.
263
+
264
+ Example:
265
+ ```python
266
+ # ON CONFLICT (id) DO NOTHING
267
+ sql.insert("users").values(id=1, name="John").on_conflict(
268
+ "id"
269
+ ).do_nothing()
270
+
271
+ # ON CONFLICT (email, username) DO UPDATE SET updated_at = NOW()
272
+ sql.insert("users").values(...).on_conflict(
273
+ "email", "username"
274
+ ).do_update(updated_at=sql.raw("NOW()"))
275
+
276
+ # ON CONFLICT DO NOTHING (catches all conflicts)
277
+ sql.insert("users").values(...).on_conflict().do_nothing()
278
+ ```
279
+ """
280
+ return ConflictBuilder(self, columns)
281
+
282
+ def on_conflict_do_nothing(self, *columns: str) -> "Insert":
283
+ """Adds an ON CONFLICT DO NOTHING clause (convenience method).
284
+
285
+ Args:
286
+ *columns: Column names that define the conflict. If no columns provided,
287
+ creates an ON CONFLICT without specific columns.
288
+
289
+ Returns:
290
+ The current builder instance for method chaining.
291
+
292
+ Note:
293
+ This is a convenience method. For more control, use on_conflict().do_nothing().
294
+ """
295
+ return self.on_conflict(*columns).do_nothing()
296
+
297
+ def on_duplicate_key_update(self, **kwargs: Any) -> "Insert":
298
+ """Adds conflict resolution using the ON CONFLICT syntax (cross-database compatible).
299
+
300
+ Args:
301
+ **kwargs: Column-value pairs to update on conflict.
302
+
303
+ Returns:
304
+ The current builder instance for method chaining.
305
+
306
+ Note:
307
+ This method uses PostgreSQL-style ON CONFLICT syntax but SQLGlot will
308
+ transpile it to the appropriate syntax for each database (MySQL's
309
+ ON DUPLICATE KEY UPDATE, etc.).
310
+ """
311
+ if not kwargs:
312
+ return self
313
+ return self.on_conflict().do_update(**kwargs)
314
+
315
+
316
+ class ConflictBuilder:
317
+ """Builder for ON CONFLICT clauses in INSERT statements.
318
+
319
+ This builder provides a fluent interface for constructing conflict resolution
320
+ clauses using PostgreSQL-style syntax, which SQLGlot can transpile to other dialects.
321
+ """
322
+
323
+ __slots__ = ("_columns", "_insert_builder")
324
+
325
+ def __init__(self, insert_builder: "Insert", columns: tuple[str, ...]) -> None:
326
+ """Initialize ConflictBuilder.
327
+
328
+ Args:
329
+ insert_builder: The parent Insert builder
330
+ columns: Column names that define the conflict
331
+ """
332
+ self._insert_builder = insert_builder
333
+ self._columns = columns
334
+
335
+ def do_nothing(self) -> "Insert":
336
+ """Add DO NOTHING conflict resolution.
337
+
338
+ Returns:
339
+ The parent Insert builder for method chaining.
340
+
341
+ Example:
342
+ ```python
343
+ sql.insert("users").values(id=1, name="John").on_conflict(
344
+ "id"
345
+ ).do_nothing()
346
+ ```
347
+ """
348
+ insert_expr = self._insert_builder._get_insert_expression()
349
+
350
+ # Create ON CONFLICT with proper structure
351
+ conflict_keys = [exp.to_identifier(col) for col in self._columns] if self._columns else None
352
+ on_conflict = exp.OnConflict(conflict_keys=conflict_keys, action=exp.var("DO NOTHING"))
353
+
354
+ insert_expr.set("conflict", on_conflict)
355
+ return self._insert_builder
356
+
357
+ def do_update(self, **kwargs: Any) -> "Insert":
358
+ """Add DO UPDATE conflict resolution with SET clauses.
359
+
360
+ Args:
361
+ **kwargs: Column-value pairs to update on conflict.
362
+
363
+ Returns:
364
+ The parent Insert builder for method chaining.
365
+
366
+ Example:
367
+ ```python
368
+ sql.insert("users").values(id=1, name="John").on_conflict(
369
+ "id"
370
+ ).do_update(
371
+ name="Updated Name", updated_at=sql.raw("NOW()")
372
+ )
373
+ ```
374
+ """
375
+ insert_expr = self._insert_builder._get_insert_expression()
376
+
377
+ # Create SET expressions for the UPDATE
378
+ set_expressions = []
379
+ for col, val in kwargs.items():
380
+ if hasattr(val, "expression") and hasattr(val, "sql"):
381
+ # Handle SQL objects (from sql.raw with parameters)
382
+ expression = getattr(val, "expression", None)
383
+ if expression is not None and isinstance(expression, exp.Expression):
384
+ # Merge parameters from SQL object into builder
385
+ if hasattr(val, "parameters"):
386
+ sql_parameters = getattr(val, "parameters", {})
387
+ for param_name, param_value in sql_parameters.items():
388
+ self._insert_builder.add_parameter(param_value, name=param_name)
389
+ value_expr = expression
390
+ else:
391
+ # If expression is None, fall back to parsing the raw SQL
392
+ sql_text = getattr(val, "sql", "")
393
+ # Merge parameters even when parsing raw SQL
394
+ if hasattr(val, "parameters"):
395
+ sql_parameters = getattr(val, "parameters", {})
396
+ for param_name, param_value in sql_parameters.items():
397
+ self._insert_builder.add_parameter(param_value, name=param_name)
398
+ # Check if sql_text is callable (like Expression.sql method)
399
+ if callable(sql_text):
400
+ sql_text = str(val)
401
+ value_expr = exp.maybe_parse(sql_text) or exp.convert(str(sql_text))
402
+ elif isinstance(val, exp.Expression):
403
+ value_expr = val
404
+ else:
405
+ # Create parameter for regular values
406
+ param_name = self._insert_builder._generate_unique_parameter_name(col)
407
+ _, param_name = self._insert_builder.add_parameter(val, name=param_name)
408
+ value_expr = exp.Placeholder(this=param_name)
409
+
410
+ set_expressions.append(exp.EQ(this=exp.column(col), expression=value_expr))
411
+
412
+ # Create ON CONFLICT with proper structure
413
+ conflict_keys = [exp.to_identifier(col) for col in self._columns] if self._columns else None
414
+ on_conflict = exp.OnConflict(
415
+ conflict_keys=conflict_keys,
416
+ action=exp.var("DO UPDATE"),
417
+ expressions=set_expressions if set_expressions else None,
418
+ )
419
+
420
+ insert_expr.set("conflict", on_conflict)
421
+ return self._insert_builder
@@ -0,0 +1,71 @@
1
+ """Safe SQL query builder with validation and parameter binding.
2
+
3
+ This module provides a fluent interface for building SQL queries safely,
4
+ with automatic parameter binding and validation.
5
+ """
6
+
7
+ from typing import Any, Optional
8
+
9
+ from sqlglot import exp
10
+
11
+ from sqlspec.builder._base import QueryBuilder
12
+ from sqlspec.builder.mixins import (
13
+ MergeIntoClauseMixin,
14
+ MergeMatchedClauseMixin,
15
+ MergeNotMatchedBySourceClauseMixin,
16
+ MergeNotMatchedClauseMixin,
17
+ MergeOnClauseMixin,
18
+ MergeUsingClauseMixin,
19
+ )
20
+ from sqlspec.core.result import SQLResult
21
+
22
+ __all__ = ("Merge",)
23
+
24
+
25
+ class Merge(
26
+ QueryBuilder,
27
+ MergeUsingClauseMixin,
28
+ MergeOnClauseMixin,
29
+ MergeMatchedClauseMixin,
30
+ MergeNotMatchedClauseMixin,
31
+ MergeIntoClauseMixin,
32
+ MergeNotMatchedBySourceClauseMixin,
33
+ ):
34
+ """Builder for MERGE statements.
35
+
36
+ This builder provides a fluent interface for constructing SQL MERGE statements
37
+ (also known as UPSERT in some databases) with automatic parameter binding and validation.
38
+ """
39
+
40
+ __slots__ = ()
41
+ _expression: Optional[exp.Expression]
42
+
43
+ def __init__(self, target_table: Optional[str] = None, **kwargs: Any) -> None:
44
+ """Initialize MERGE with optional target table.
45
+
46
+ Args:
47
+ target_table: Target table name
48
+ **kwargs: Additional QueryBuilder arguments
49
+ """
50
+ super().__init__(**kwargs)
51
+ self._initialize_expression()
52
+
53
+ if target_table:
54
+ self.into(target_table)
55
+
56
+ @property
57
+ def _expected_result_type(self) -> "type[SQLResult]":
58
+ """Return the expected result type for this builder.
59
+
60
+ Returns:
61
+ The SQLResult type for MERGE statements.
62
+ """
63
+ return SQLResult
64
+
65
+ def _create_base_expression(self) -> "exp.Merge":
66
+ """Create a base MERGE expression.
67
+
68
+ Returns:
69
+ A new sqlglot Merge expression with empty clauses.
70
+ """
71
+ return exp.Merge(this=None, using=None, on=None, whens=exp.Whens(expressions=[]))