From 9eaaf63a0ae415b971331878a06ec83caaf63ceb Mon Sep 17 00:00:00 2001 From: Peter Hoffmann Date: Wed, 29 Apr 2015 22:25:42 +0200 Subject: [PATCH 1/6] set executemany to false for update statements Fixes #36 --- sqlalchemy_exasol/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_exasol/base.py b/sqlalchemy_exasol/base.py index a810f4dc..71fa23cd 100644 --- a/sqlalchemy_exasol/base.py +++ b/sqlalchemy_exasol/base.py @@ -403,7 +403,9 @@ def pre_exec(self): raise TypeError('Data type not supported: %s' % type(value)) self.statement = db_query self.parameters = [[]] - + + if self.isupdate: + self.executemany = False class EXADialect(default.DefaultDialect): name = 'exasol' From 29a946e45c73c166380bbdc1df2387ed8fe3237d Mon Sep 17 00:00:00 2001 From: Peter Hoffmann Date: Wed, 6 May 2015 09:56:26 +0200 Subject: [PATCH 2/6] add test for update execute_manny --- sqlalchemy_exasol/base.py | 5 ++ test/test_update.py | 107 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/test_update.py diff --git a/sqlalchemy_exasol/base.py b/sqlalchemy_exasol/base.py index 71fa23cd..067c3034 100644 --- a/sqlalchemy_exasol/base.py +++ b/sqlalchemy_exasol/base.py @@ -403,8 +403,13 @@ def pre_exec(self): raise TypeError('Data type not supported: %s' % type(value)) self.statement = db_query self.parameters = [[]] +<<<<<<< Updated upstream if self.isupdate: +======= + + if self.isupdate and len(self.parameters) == 1: +>>>>>>> Stashed changes self.executemany = False class EXADialect(default.DefaultDialect): diff --git a/test/test_update.py b/test/test_update.py new file mode 100644 index 00000000..c77e8ba3 --- /dev/null +++ b/test/test_update.py @@ -0,0 +1,107 @@ +from sqlalchemy import * +from sqlalchemy import testing +from sqlalchemy.dialects import mysql +from sqlalchemy.engine import default +from sqlalchemy.testing import AssertsCompiledSQL, eq_, fixtures +from sqlalchemy.testing.schema import Table, Column + + +class _UpdateTestBase(object): + + @classmethod + def define_tables(cls, metadata): + Table('mytable', metadata, + Column('myid', Integer), + Column('name', String(30)), + Column('description', String(50))) + Table('myothertable', metadata, + Column('otherid', Integer), + Column('othername', String(30))) + Table('users', metadata, + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), + Column('name', String(30), nullable=False)) + Table('addresses', metadata, + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), + Column('user_id', None, ForeignKey('users.id')), + Column('name', String(30), nullable=False), + Column('email_address', String(50), nullable=False)) + Table('dingalings', metadata, + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), + Column('address_id', None, ForeignKey('addresses.id')), + Column('data', String(30))) + + @classmethod + def fixtures(cls): + return dict( + users=( + ('id', 'name'), + (7, 'jack'), + (8, 'ed'), + (9, 'fred'), + (10, 'chuck') + ), + addresses = ( + ('id', 'user_id', 'name', 'email_address'), + (1, 7, 'x', 'jack@bean.com'), + (2, 8, 'x', 'ed@wood.com'), + (3, 8, 'x', 'ed@bettyboop.com'), + (4, 8, 'x', 'ed@lala.com'), + (5, 9, 'x', 'fred@fred.com') + ), + dingalings = ( + ('id', 'address_id', 'data'), + (1, 2, 'ding 1/2'), + (2, 5, 'ding 2/5') + ), + ) + + + + +class UpdateTest(_UpdateTestBase, fixtures.TablesTest): + __backend__ = True + + def test_update_simple(self): + users = self.tables.users + testing.db.execute(users.update().values(name="peter").where(users.c.id == 10)) + expected = [ + (7, 'jack'), + (8, 'ed'), + (9, 'fred'), + (10, 'peter') + ] + self._assert_users(users, expected) + + def test_update_executemany(self): + users = self.tables.users + + stmt = users.update().\ + where(users.c.name == bindparam('oldname')).\ + values(name=bindparam('newname')) + + values = [{'oldname':'jack', 'newname':'jack2'}, + {'oldname':'fred', 'newname':'fred2'}] + + testing.db.execute(stmt, values) + expected = [ + (7, 'jack2'), + (8, 'ed'), + (9, 'fred2'), + (10, 'chuck') + ] + self._assert_users(users, expected) + + + def _assert_addresses(self, addresses, expected): + stmt = addresses.select().order_by(addresses.c.id) + eq_(testing.db.execute(stmt).fetchall(), expected) + + + def _assert_users(self, users, expected): + stmt = users.select().order_by(users.c.id) + eq_(testing.db.execute(stmt).fetchall(), expected) + + From cfc9105b6a652b17f9dd2c2376f79d1b68e9d656 Mon Sep 17 00:00:00 2001 From: Peter Hoffmann Date: Wed, 6 May 2015 09:59:10 +0200 Subject: [PATCH 3/6] fix typo --- sqlalchemy_exasol/base.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sqlalchemy_exasol/base.py b/sqlalchemy_exasol/base.py index 067c3034..81d60e61 100644 --- a/sqlalchemy_exasol/base.py +++ b/sqlalchemy_exasol/base.py @@ -403,13 +403,8 @@ def pre_exec(self): raise TypeError('Data type not supported: %s' % type(value)) self.statement = db_query self.parameters = [[]] -<<<<<<< Updated upstream - - if self.isupdate: -======= if self.isupdate and len(self.parameters) == 1: ->>>>>>> Stashed changes self.executemany = False class EXADialect(default.DefaultDialect): From 139c120e2afeee230085ed26c3cec22084ce1e73 Mon Sep 17 00:00:00 2001 From: Peter Hoffmann Date: Wed, 6 May 2015 10:23:57 +0200 Subject: [PATCH 4/6] assert rowcount in simple update --- test/test_update.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_update.py b/test/test_update.py index c77e8ba3..8ced7c99 100644 --- a/test/test_update.py +++ b/test/test_update.py @@ -66,13 +66,14 @@ class UpdateTest(_UpdateTestBase, fixtures.TablesTest): def test_update_simple(self): users = self.tables.users - testing.db.execute(users.update().values(name="peter").where(users.c.id == 10)) + result = testing.db.execute(users.update().values(name="peter").where(users.c.id == 10)) expected = [ (7, 'jack'), (8, 'ed'), (9, 'fred'), (10, 'peter') ] + assert result.rowcount == 1 self._assert_users(users, expected) def test_update_executemany(self): From 99a6c820c82bdac7ebd2f5b51e600bf566c5d0df Mon Sep 17 00:00:00 2001 From: Peter Hoffmann Date: Wed, 6 May 2015 11:06:51 +0200 Subject: [PATCH 5/6] add documentation and another test for rowcount --- test/test_update.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/test_update.py b/test/test_update.py index 8ced7c99..540782df 100644 --- a/test/test_update.py +++ b/test/test_update.py @@ -65,6 +65,7 @@ class UpdateTest(_UpdateTestBase, fixtures.TablesTest): __backend__ = True def test_update_simple(self): + """test simple update and assert that exasol returns the right rowcount""" users = self.tables.users result = testing.db.execute(users.update().values(name="peter").where(users.c.id == 10)) expected = [ @@ -75,8 +76,23 @@ def test_update_simple(self): ] assert result.rowcount == 1 self._assert_users(users, expected) + + def test_update_simple_multiple_rows_rowcount(self): + """test simple update and assert that exasol returns the right rowcount""" + users = self.tables.users + result = testing.db.execute(users.update().values(name="peter").where(users.c.id >= 9)) + expected = [ + (7, 'jack'), + (8, 'ed'), + (9, 'peter'), + (10, 'peter') + ] + assert result.rowcount == 2 + self._assert_users(users, expected) def test_update_executemany(self): + """test that update with executemany work as well, but rowcount + is undefined for executemany updates""" users = self.tables.users stmt = users.update().\ @@ -86,13 +102,14 @@ def test_update_executemany(self): values = [{'oldname':'jack', 'newname':'jack2'}, {'oldname':'fred', 'newname':'fred2'}] - testing.db.execute(stmt, values) + result = testing.db.execute(stmt, values) expected = [ (7, 'jack2'), (8, 'ed'), (9, 'fred2'), (10, 'chuck') ] + assert result.rowcount == -1 self._assert_users(users, expected) From 7628c2163df213197fe54d71458694eb7938a4f3 Mon Sep 17 00:00:00 2001 From: Peter Hoffmann Date: Wed, 6 May 2015 11:17:36 +0200 Subject: [PATCH 6/6] add changelog for new release --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index ad3402e4..4b99eb84 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ # HEAD +# 0.9.2 +* change execute behaviour for updates fixes #36 + # 0.9.1 * support for DISTRIBUTE BY table constraints