summaryrefslogtreecommitdiff
path: root/test/sql
diff options
context:
space:
mode:
authorIdan Kamara <idankk86@gmail.com>2012-12-06 00:11:52 +0200
committerIdan Kamara <idankk86@gmail.com>2012-12-06 00:11:52 +0200
commitd57c1c2ddd654a1077ab04ba7277828d9030c23d (patch)
tree6e4b93f19c175336329d64b78ab1acf806895a9b /test/sql
parent51839352a4a9d4b87bdca6c148ec0fd847b8630b (diff)
downloadsqlalchemy-d57c1c2ddd654a1077ab04ba7277828d9030c23d.tar.gz
compiler: add support for multirow inserts
Some databases support this syntax for inserts: INSERT INTO table (id, name) VALUES ('v1', 'v2'), ('v3', 'v4'); which greatly increases INSERT speed. It is now possible to pass a list of lists/tuples/dictionaries as the values param to the Insert construct. We convert it to a flat dictionary so we can continue using bind params. The above query will be converted to: INSERT INTO table (id, name) VALUES (:id, :name), (:id0, :name0); Currently only supported on postgresql, mysql and sqlite.
Diffstat (limited to 'test/sql')
-rw-r--r--test/sql/test_compiler.py15
-rw-r--r--test/sql/test_query.py11
2 files changed, 26 insertions, 0 deletions
diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py
index 50b425a01..3b0e421ae 100644
--- a/test/sql/test_compiler.py
+++ b/test/sql/test_compiler.py
@@ -2552,6 +2552,21 @@ class CRUDTest(fixtures.TestBase, AssertsCompiledSQL):
table.insert(inline=True),
"INSERT INTO sometable (foo) VALUES (foobar())", params={})
+ def test_multirow_insert(self):
+ data = [(1, 'a', 'b'), (2, 'a', 'b')]
+ result = "INSERT INTO mytable (myid, name, description) VALUES " \
+ "(%(myid)s, %(name)s, %(description)s), " \
+ "(%(myid0)s, %(name0)s, %(description0)s)"
+
+ stmt = insert(table1, data, dialect='postgresql')
+ self.assert_compile(stmt, result, dialect=postgresql.dialect())
+
+ stmt = table1.insert(values=data, dialect='postgresql')
+ self.assert_compile(stmt, result, dialect=postgresql.dialect())
+
+ stmt = table1.insert(dialect='postgresql').values(data)
+ self.assert_compile(stmt, result, dialect=postgresql.dialect())
+
def test_update(self):
self.assert_compile(
update(table1, table1.c.myid == 7),
diff --git a/test/sql/test_query.py b/test/sql/test_query.py
index 95e159316..9da9c2ff9 100644
--- a/test/sql/test_query.py
+++ b/test/sql/test_query.py
@@ -48,6 +48,17 @@ class QueryTest(fixtures.TestBase):
def teardown_class(cls):
metadata.drop_all()
+ def test_multirow_insert(self):
+ users.insert(values=[{'user_id':7, 'user_name':'jack'},
+ {'user_id':8, 'user_name':'ed'}]).execute()
+ rows = users.select().execute().fetchall()
+ self.assert_(rows[0] == (7, 'jack'))
+ self.assert_(rows[1] == (8, 'ed'))
+ users.insert(values=[(9, 'jack'), (10, 'ed')]).execute()
+ rows = users.select().execute().fetchall()
+ self.assert_(rows[2] == (9, 'jack'))
+ self.assert_(rows[3] == (10, 'ed'))
+
def test_insert_heterogeneous_params(self):
"""test that executemany parameters are asserted to match the
parameter set of the first."""