Skip to content

Commit

Permalink
Merge pull request #37 from blue-yonder/hoffmann-patch-executemanny-u…
Browse files Browse the repository at this point in the history
…pdate

set executemany to false for update statements if there is only one parameter fixes #36
  • Loading branch information
hoffmann committed May 6, 2015
2 parents 2bcdf4f + 7628c21 commit c57b70f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# HEAD

# 0.9.2
* change execute behaviour for updates fixes #36

# 0.9.1
* support for DISTRIBUTE BY table constraints

Expand Down
2 changes: 2 additions & 0 deletions sqlalchemy_exasol/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ def pre_exec(self):
self.statement = db_query
self.parameters = [[]]

if self.isupdate and len(self.parameters) == 1:
self.executemany = False

class EXADialect(default.DefaultDialect):
name = 'exasol'
Expand Down
125 changes: 125 additions & 0 deletions test/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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):
"""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 = [
(7, 'jack'),
(8, 'ed'),
(9, 'fred'),
(10, 'peter')
]
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().\
where(users.c.name == bindparam('oldname')).\
values(name=bindparam('newname'))

values = [{'oldname':'jack', 'newname':'jack2'},
{'oldname':'fred', 'newname':'fred2'}]

result = testing.db.execute(stmt, values)
expected = [
(7, 'jack2'),
(8, 'ed'),
(9, 'fred2'),
(10, 'chuck')
]
assert result.rowcount == -1
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)


0 comments on commit c57b70f

Please sign in to comment.