MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / OnDuplicateClause

Class OnDuplicateClause

lib/sqlalchemy/dialects/mysql/dml.py:222–274  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

220
221
222class OnDuplicateClause(SyntaxExtension, ClauseElement):
223 __visit_name__ = "on_duplicate_key_update"
224
225 _parameter_ordering: Optional[List[str]] = None
226
227 update: Dict[str, ColumnElement[Any]]
228 stringify_dialect = "mysql"
229
230 _traverse_internals = [
231 ("_parameter_ordering", InternalTraversal.dp_string_list),
232 ("update", InternalTraversal.dp_dml_values),
233 ]
234
235 def __init__(
236 self, inserted_alias: NamedFromClause, update: _UpdateArg
237 ) -> None:
238 self.inserted_alias = inserted_alias
239
240 # auto-detect that parameters should be ordered. This is copied from
241 # Update._proces_colparams(), however we don't look for a special flag
242 # in this case since we are not disambiguating from other use cases as
243 # we are in Update.values().
244 if isinstance(update, list) and (
245 update and isinstance(update[0], tuple)
246 ):
247 self._parameter_ordering = [key for key, value in update]
248 update = dict(update)
249
250 if isinstance(update, dict):
251 if not update:
252 raise ValueError(
253 "update parameter dictionary must not be empty"
254 )
255 elif isinstance(update, ColumnCollection):
256 update = dict(update)
257 else:
258 raise ValueError(
259 "update parameter must be a non-empty dictionary "
260 "or a ColumnCollection such as the `.c.` collection "
261 "of a Table object"
262 )
263
264 self.update = {
265 k: coercions.expect(
266 roles.ExpressionElementRole, v, type_=NULLTYPE, is_crud=True
267 )
268 for k, v in update.items()
269 }
270
271 def apply_to_insert(self, insert_stmt: StandardInsert) -> None:
272 insert_stmt.apply_syntax_extension_point(
273 self.append_replacing_same_type, "post_values"
274 )
275
276
277_UpdateArg = Union[

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected