Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
Merge branch 'release/0.11.0'
Browse files Browse the repository at this point in the history
* release/0.11.0:
  style: Stick to DBAPI 2.0 specification
  use dict literals instead of dict constructors
  fix param name and usage
  support bulk insert in cursor's executemany() method
  Switch readme to markdown.
  • Loading branch information
mdomke committed Jul 11, 2018
2 parents c61490e + d615e87 commit 03ce270
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
31 changes: 17 additions & 14 deletions festung/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,17 @@ def drop(self):

def execute(self, operation, parameters=None):
parameters = parameters or []
data = dict(sql=operation, params=[cast(p) for p in parameters])
response = self._request('POST', json=data).json()
self._iter = iter(response['data'])
self.lastrowid = response['last_row_id']
self._rowcount = response['rows_changed']
self._description = _generate_description(response['headers'])
data = {'sql': operation, 'params': [cast(p) for p in parameters]}
self._make_request(data)

def executemany(self, operation, parameters_sequence):
rowcount = 0
for parameters in parameters_sequence:
self.execute(operation, parameters)
rowcount += self._rowcount
if self.fetchone() is not NO_MORE_ROW:
raise ProgrammingError("The statement shall not produce a result.")
self._rowcount = rowcount
data = {
'sql': operation,
'bulk_params': [[cast(p) for p in row] for row in parameters_sequence],
}
self._make_request(data)
if self.fetchone() is not NO_MORE_ROW:
raise ProgrammingError("The statement shall not produce a result.")

def fetchone(self):
if self._iter is NO_EXECUTE_ITER:
Expand Down Expand Up @@ -229,9 +225,16 @@ def _raise_for_error(self, response):
error = response.json()['error']
except ValueError as e:
raise InternalError("Invalid JSON: {}".format(e))
except KeyError as e:
except KeyError:
raise InternalError("Missing error in response.")
type_ = error['type']
description = error['description']
exception_class = error_to_exception[type_]
raise exception_class(description)

def _make_request(self, data):
response = self._request('POST', json=data).json()
self._iter = iter(response['data'])
self.lastrowid = response['last_row_id']
self._rowcount = response['rows_changed']
self._description = _generate_description(response['headers'])
15 changes: 7 additions & 8 deletions tests/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,16 @@ def test_executemany(self, festung, cursor):
['def', 2, 1.2],
['ghi', 3, None],
]
n_rows_changed = [7, 8, 9]

for n_changed in n_rows_changed:
festung.add_json_response(
{'data': [], 'headers': [], 'last_row_id': 0, 'rows_changed': n_changed})
festung.add_json_response(
{'data': [], 'headers': [], 'last_row_id': 0, 'rows_changed': 3})

cursor.executemany(query, params_list)
for params, data in zip(params_list, festung.json_requests):
assert data['sql'] == query
assert data['params'] == params
assert cursor.rowcount == sum(n_rows_changed)

requests = festung.json_requests
assert len(requests) == 1
assert requests[0]['sql'] == query
assert requests[0]['bulk_params'] == params_list

def test_executemany_with_result(self, festung, cursor):
festung.add_json_response({
Expand Down

0 comments on commit 03ce270

Please sign in to comment.