You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.0KB

  1. from ... import exc
  2. from ... import util
  3. from ...sql.base import _exclusive_against
  4. from ...sql.base import _generative
  5. from ...sql.base import ColumnCollection
  6. from ...sql.dml import Insert as StandardInsert
  7. from ...sql.elements import ClauseElement
  8. from ...sql.expression import alias
  9. from ...util.langhelpers import public_factory
  10. __all__ = ("Insert", "insert")
  11. class Insert(StandardInsert):
  12. """MySQL-specific implementation of INSERT.
  13. Adds methods for MySQL-specific syntaxes such as ON DUPLICATE KEY UPDATE.
  14. The :class:`~.mysql.Insert` object is created using the
  15. :func:`sqlalchemy.dialects.mysql.insert` function.
  16. .. versionadded:: 1.2
  17. """
  18. stringify_dialect = "mysql"
  19. @property
  20. def inserted(self):
  21. """Provide the "inserted" namespace for an ON DUPLICATE KEY UPDATE statement
  22. MySQL's ON DUPLICATE KEY UPDATE clause allows reference to the row
  23. that would be inserted, via a special function called ``VALUES()``.
  24. This attribute provides all columns in this row to be referenceable
  25. such that they will render within a ``VALUES()`` function inside the
  26. ON DUPLICATE KEY UPDATE clause. The attribute is named ``.inserted``
  27. so as not to conflict with the existing
  28. :meth:`_expression.Insert.values` method.
  29. .. tip:: The :attr:`_mysql.Insert.inserted` attribute is an instance
  30. of :class:`_expression.ColumnCollection`, which provides an
  31. interface the same as that of the :attr:`_schema.Table.c`
  32. collection described at :ref:`metadata_tables_and_columns`.
  33. With this collection, ordinary names are accessible like attributes
  34. (e.g. ``stmt.inserted.some_column``), but special names and
  35. dictionary method names should be accessed using indexed access,
  36. such as ``stmt.inserted["column name"]`` or
  37. ``stmt.inserted["values"]``. See the docstring for
  38. :class:`_expression.ColumnCollection` for further examples.
  39. .. seealso::
  40. :ref:`mysql_insert_on_duplicate_key_update` - example of how
  41. to use :attr:`_expression.Insert.inserted`
  42. """
  43. return self.inserted_alias.columns
  44. @util.memoized_property
  45. def inserted_alias(self):
  46. return alias(self.table, name="inserted")
  47. @_generative
  48. @_exclusive_against(
  49. "_post_values_clause",
  50. msgs={
  51. "_post_values_clause": "This Insert construct already "
  52. "has an ON DUPLICATE KEY clause present"
  53. },
  54. )
  55. def on_duplicate_key_update(self, *args, **kw):
  56. r"""
  57. Specifies the ON DUPLICATE KEY UPDATE clause.
  58. :param \**kw: Column keys linked to UPDATE values. The
  59. values may be any SQL expression or supported literal Python
  60. values.
  61. .. warning:: This dictionary does **not** take into account
  62. Python-specified default UPDATE values or generation functions,
  63. e.g. those specified using :paramref:`_schema.Column.onupdate`.
  64. These values will not be exercised for an ON DUPLICATE KEY UPDATE
  65. style of UPDATE, unless values are manually specified here.
  66. :param \*args: As an alternative to passing key/value parameters,
  67. a dictionary or list of 2-tuples can be passed as a single positional
  68. argument.
  69. Passing a single dictionary is equivalent to the keyword argument
  70. form::
  71. insert().on_duplicate_key_update({"name": "some name"})
  72. Passing a list of 2-tuples indicates that the parameter assignments
  73. in the UPDATE clause should be ordered as sent, in a manner similar
  74. to that described for the :class:`_expression.Update`
  75. construct overall
  76. in :ref:`updates_order_parameters`::
  77. insert().on_duplicate_key_update(
  78. [("name", "some name"), ("value", "some value")])
  79. .. versionchanged:: 1.3 parameters can be specified as a dictionary
  80. or list of 2-tuples; the latter form provides for parameter
  81. ordering.
  82. .. versionadded:: 1.2
  83. .. seealso::
  84. :ref:`mysql_insert_on_duplicate_key_update`
  85. """
  86. if args and kw:
  87. raise exc.ArgumentError(
  88. "Can't pass kwargs and positional arguments simultaneously"
  89. )
  90. if args:
  91. if len(args) > 1:
  92. raise exc.ArgumentError(
  93. "Only a single dictionary or list of tuples "
  94. "is accepted positionally."
  95. )
  96. values = args[0]
  97. else:
  98. values = kw
  99. inserted_alias = getattr(self, "inserted_alias", None)
  100. self._post_values_clause = OnDuplicateClause(inserted_alias, values)
  101. insert = public_factory(
  102. Insert, ".dialects.mysql.insert", ".dialects.mysql.Insert"
  103. )
  104. class OnDuplicateClause(ClauseElement):
  105. __visit_name__ = "on_duplicate_key_update"
  106. _parameter_ordering = None
  107. stringify_dialect = "mysql"
  108. def __init__(self, inserted_alias, update):
  109. self.inserted_alias = inserted_alias
  110. # auto-detect that parameters should be ordered. This is copied from
  111. # Update._proces_colparams(), however we don't look for a special flag
  112. # in this case since we are not disambiguating from other use cases as
  113. # we are in Update.values().
  114. if isinstance(update, list) and (
  115. update and isinstance(update[0], tuple)
  116. ):
  117. self._parameter_ordering = [key for key, value in update]
  118. update = dict(update)
  119. if isinstance(update, dict):
  120. if not update:
  121. raise ValueError(
  122. "update parameter dictionary must not be empty"
  123. )
  124. elif isinstance(update, ColumnCollection):
  125. update = dict(update)
  126. else:
  127. raise ValueError(
  128. "update parameter must be a non-empty dictionary "
  129. "or a ColumnCollection such as the `.c.` collection "
  130. "of a Table object"
  131. )
  132. self.update = update