]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Rename MySQL dml.insert().values to .inserted
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 9 Sep 2017 01:34:39 +0000 (21:34 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 9 Sep 2017 01:41:13 +0000 (21:41 -0400)
Changed the name of the ``.values`` attribute of the new MySQL
INSERT..ON DUPLICATE KEY UPDATE construct to ``.inserted``, as
:class:`.Insert` already has a method called :meth:`.Insert.values`.
The ``.inserted`` attribute ultimately renders the MySQL ``VALUES()``
function.

Change-Id: I8da8e30a3077698385a4b77e2c2032e2d1ff10b2
Fixes: #4072
doc/build/changelog/migration_12.rst
doc/build/changelog/unreleased_12/4072.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mysql/base.py
lib/sqlalchemy/dialects/mysql/dml.py
test/dialect/mysql/test_compiler.py
test/dialect/mysql/test_on_duplicate.py

index 4cfaf38c4048bab7c70d9a8b31b032813edbc490..87c08dc21705f6282df2659fafb14bf9a7b0ba96 100644 (file)
@@ -1336,7 +1336,7 @@ This :class:`~.expression.Insert` subclass adds a new method
         values(id='some_id', data='some data to insert')
 
     on_conflict_stmt = insert_stmt.on_duplicate_key_update(
-        data=stmt.values.data,
+        data=stmt.inserted.data,
         status='U'
     )
 
diff --git a/doc/build/changelog/unreleased_12/4072.rst b/doc/build/changelog/unreleased_12/4072.rst
new file mode 100644 (file)
index 0000000..3e5e83f
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, mysql
+    :tickets: 4072
+
+    Changed the name of the ``.values`` attribute of the new MySQL
+    INSERT..ON DUPLICATE KEY UPDATE construct to ``.inserted``, as
+    :class:`.Insert` already has a method called :meth:`.Insert.values`.
+    The ``.inserted`` attribute ultimately renders the MySQL ``VALUES()``
+    function.
\ No newline at end of file
index 976ff367b4fb8f462ae9b619e806273ce337d0cb..b1b249001515456f0232ccb3003db39a115d2d21 100644 (file)
@@ -332,8 +332,8 @@ INSERT...ON DUPLICATE KEY UPDATE (Upsert)
 MySQL allows "upserts" (update or insert)
 of rows into a table via the ``ON DUPLICATE KEY UPDATE`` clause of the
 ``INSERT`` statement.  A candidate row will only be inserted if that row does
-not match an existing primary or unique key in the table; otherwise, an UPDATE will
-be performed.   The statement allows for separate specification of the
+not match an existing primary or unique key in the table; otherwise, an UPDATE
+will be performed.   The statement allows for separate specification of the
 values to INSERT versus the values for UPDATE.
 
 SQLAlchemy provides ``ON DUPLICATE KEY UPDATE`` support via the MySQL-specific
@@ -347,7 +347,7 @@ the generative method :meth:`~.mysql.dml.Insert.on_duplicate_key_update`::
         data='inserted value')
 
     on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
-        data=insert_stmt.values.data,
+        data=insert_stmt.inserted.data,
         status='U'
     )
 
@@ -381,7 +381,7 @@ as values::
     unless they are manually specified explicitly in the parameters.
 
 In order to refer to the proposed insertion row, the special alias
-:attr:`~.mysql.dml.Insert.values` is available as an attribute on
+:attr:`~.mysql.dml.Insert.inserted` is available as an attribute on
 the :class:`.mysql.dml.Insert` object; this object is a
 :class:`.ColumnCollection` which contains all columns of the target
 table::
@@ -394,11 +394,11 @@ table::
         author='jlh')
     do_update_stmt = stmt.on_duplicate_key_update(
         data="updated value",
-        author=stmt.values.author
+        author=stmt.inserted.author
     )
     conn.execute(do_update_stmt)
 
-When rendered, the "values" namespace will produce the expression
+When rendered, the "inserted" namespace will produce the expression
 ``VALUES(<columnname>)``.
 
 .. versionadded:: 1.2 Added support for MySQL ON DUPLICATE KEY UPDATE clause
@@ -921,7 +921,7 @@ class MySQLCompiler(compiler.SQLCompiler):
                 val.type = column.type
                 value_text = self.process(val.self_group(), use_schema=False)
             elif isinstance(val, elements.ColumnClause) \
-                    and val.table is on_duplicate.values_alias:
+                    and val.table is on_duplicate.inserted_alias:
                 value_text = 'VALUES(' + self.preparer.quote(column.name) + ')'
             else:
                 value_text = self.process(val.self_group(), use_schema=False)
index 743510317b1330a76c51eebdc3bfeaeb67d22e13..217dc7a2a0933d329684ce65a6c1875122406cd4 100644 (file)
@@ -18,26 +18,27 @@ class Insert(StandardInsert):
     """
 
     @property
-    def values(self):
-        """Provide the ``values`` namespace for an ON DUPLICATE KEY UPDATE statement
+    def inserted(self):
+        """Provide the "inserted" namespace for an ON DUPLICATE KEY UPDATE statement
 
         MySQL's ON DUPLICATE KEY UPDATE clause allows reference to the row
         that would be inserted, via a special function called ``VALUES()``.
         This attribute provides all columns in this row to be referenaceable
         such that they will render within a ``VALUES()`` function inside the
-        ON DUPLICATE KEY UPDATE clause.
+        ON DUPLICATE KEY UPDATE clause.    The attribute is named ``.inserted``
+        so as not to conflict with the existing :meth:`.Insert.values` method.
 
         .. seealso::
 
             :ref:`mysql_insert_on_duplicate_key_update` - example of how
-            to use :attr:`.Insert.values`
+            to use :attr:`.Insert.inserted`
 
         """
-        return self.values_alias.columns
+        return self.inserted_alias.columns
 
     @util.memoized_property
-    def values_alias(self):
-        return alias(self.table, name='values')
+    def inserted_alias(self):
+        return alias(self.table, name='inserted')
 
     @_generative
     def on_duplicate_key_update(self, **kw):
@@ -61,8 +62,8 @@ class Insert(StandardInsert):
             :ref:`mysql_insert_on_duplicate_key_update`
 
         """
-        values_alias = getattr(self, 'values_alias', None)
-        self._post_values_clause = OnDuplicateClause(values_alias, kw)
+        inserted_alias = getattr(self, 'inserted_alias', None)
+        self._post_values_clause = OnDuplicateClause(inserted_alias, kw)
         return self
 
 
@@ -72,8 +73,8 @@ insert = public_factory(Insert, '.dialects.mysql.insert')
 class OnDuplicateClause(ClauseElement):
     __visit_name__ = 'on_duplicate_key_update'
 
-    def __init__(self, values_alias, update):
-        self.values_alias = values_alias
+    def __init__(self, inserted_alias, update):
+        self.inserted_alias = inserted_alias
         if not update or not isinstance(update, dict):
             raise ValueError('update parameter must be a non-empty dictionary')
         self.update = update
index 3663a4800ee622b3d5666f2dc545e337ecbac243..e8731e5329ec7dceae3887ea83fb6fb3d14f0a87 100644 (file)
@@ -704,10 +704,10 @@ class InsertOnDuplicateTest(fixtures.TestBase, AssertsCompiledSQL):
         )
 
     def test_from_values(self):
-        stmt = insert(
-            self.table, [{'id': 1, 'bar': 'ab'}, {'id': 2, 'bar': 'b'}])
+        stmt = insert(self.table).values(
+            [{'id': 1, 'bar': 'ab'}, {'id': 2, 'bar': 'b'}])
         stmt = stmt.on_duplicate_key_update(
-            bar=stmt.values.bar, baz=stmt.values.baz)
+            bar=stmt.inserted.bar, baz=stmt.inserted.baz)
         expected_sql = (
             'INSERT INTO foos (id, bar) VALUES (%s, %s), (%s, %s) '
             'ON DUPLICATE KEY UPDATE bar = VALUES(bar), baz = VALUES(baz)'
@@ -715,8 +715,8 @@ class InsertOnDuplicateTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(stmt, expected_sql)
 
     def test_from_literal(self):
-        stmt = insert(
-            self.table, [{'id': 1, 'bar': 'ab'}, {'id': 2, 'bar': 'b'}])
+        stmt = insert(self.table).values(
+            [{'id': 1, 'bar': 'ab'}, {'id': 2, 'bar': 'b'}])
         stmt = stmt.on_duplicate_key_update(bar=literal_column('bb'))
         expected_sql = (
             'INSERT INTO foos (id, bar) VALUES (%s, %s), (%s, %s) '
@@ -725,8 +725,8 @@ class InsertOnDuplicateTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(stmt, expected_sql)
 
     def test_python_values(self):
-        stmt = insert(
-            self.table, [{'id': 1, 'bar': 'ab'}, {'id': 2, 'bar': 'b'}])
+        stmt = insert(self.table).values(
+            [{'id': 1, 'bar': 'ab'}, {'id': 2, 'bar': 'b'}])
         stmt = stmt.on_duplicate_key_update(bar="foobar")
         expected_sql = (
             'INSERT INTO foos (id, bar) VALUES (%s, %s), (%s, %s) '
index 2ff4a58ccb324532f4d07c08927e2662551093df..9a026f9edc4e7fdb71cb43d71a00f0d8e66bee91 100644 (file)
@@ -29,8 +29,9 @@ class OnDuplicateTest(fixtures.TablesTest):
         foos = self.tables.foos
         with testing.db.connect() as conn:
             conn.execute(insert(foos, dict(id=1, bar='b', baz='bz')))
-            stmt = insert(foos, [dict(id=1, bar='ab'), dict(id=2, bar='b')])
-            stmt = stmt.on_duplicate_key_update(bar=stmt.values.bar)
+            stmt = insert(foos).values(
+                [dict(id=1, bar='ab'), dict(id=2, bar='b')])
+            stmt = stmt.on_duplicate_key_update(bar=stmt.inserted.bar)
             result = conn.execute(stmt)
             eq_(result.inserted_primary_key, [2])
             eq_(
@@ -41,17 +42,17 @@ class OnDuplicateTest(fixtures.TablesTest):
     def test_last_inserted_id(self):
         foos = self.tables.foos
         with testing.db.connect() as conn:
-            stmt = insert(foos{"bar": "b", "baz": "bz"})
+            stmt = insert(foos).values({"bar": "b", "baz": "bz"})
             result = conn.execute(
                 stmt.on_duplicate_key_update(
-                    bar=stmt.values.bar, baz="newbz")
+                    bar=stmt.inserted.bar, baz="newbz")
             )
             eq_(result.inserted_primary_key, [1])
 
-            stmt = insert(foos{"id": 1, "bar": "b", "baz": "bz"})
+            stmt = insert(foos).values({"id": 1, "bar": "b", "baz": "bz"})
             result = conn.execute(
                 stmt.on_duplicate_key_update(
-                    bar=stmt.values.bar, baz="newbz")
+                    bar=stmt.inserted.bar, baz="newbz")
             )
             eq_(result.inserted_primary_key, [1])