Fixed fast execute functions with Python 3

This commit is contained in:
Daniele Varrazzo 2017-02-01 02:36:54 +00:00
parent a95fd3df1a
commit 2e2dcd536b
2 changed files with 6 additions and 4 deletions

View File

@ -1177,7 +1177,7 @@ def execute_batch(cur, sql, argslist, page_size=100):
"""
for page in _paginate(argslist, page_size=page_size):
sqls = [cur.mogrify(sql, args) for args in page]
cur.execute(";".join(sqls))
cur.execute(b";".join(sqls))
def execute_values(cur, sql, argslist, template=None, page_size=100):
@ -1219,5 +1219,7 @@ def execute_values(cur, sql, argslist, template=None, page_size=100):
for page in _paginate(argslist, page_size=page_size):
if template is None:
template = '(%s)' % ','.join(['%s'] * len(page[0]))
values = ",".join(cur.mogrify(template, args) for args in page)
values = b",".join(cur.mogrify(template, args) for args in page)
if isinstance(values, bytes) and _sys.version_info[0] > 2:
values = values.decode(_ext.encodings[cur.connection.encoding])
cur.execute(sql % (values,))

View File

@ -1829,7 +1829,7 @@ class TestFastExecute(ConnectingTestCase):
page_size=10)
# last command was 5 statements
self.assertEqual(sum(c == ';' for c in cur.query), 4)
self.assertEqual(sum(c == u';' for c in cur.query.decode('ascii')), 4)
cur.execute("select id, val from testfast order by id")
self.assertEqual(cur.fetchall(), [(i, i * 10) for i in range(25)])
@ -1886,7 +1886,7 @@ class TestFastExecute(ConnectingTestCase):
page_size=10)
# last statement was 5 tuples (one parens is for the fields list)
self.assertEqual(sum(c == '(' for c in cur.query), 6)
self.assertEqual(sum(c == '(' for c in cur.query.decode('ascii')), 6)
cur.execute("select id, val from testfast order by id")
self.assertEqual(cur.fetchall(), [(i, i * 10) for i in range(25)])