From f425763b00d1bd69f515560a398fb64eee9f8f30 Mon Sep 17 00:00:00 2001 From: Philipp Bogensberger Date: Fri, 22 Aug 2014 14:51:41 +0200 Subject: [PATCH] executemany returns the result list now --- CHANGES.txt | 7 ++++++- docs/client.txt | 8 +++++++- src/crate/client/cursor.py | 4 +++- src/crate/client/cursor.txt | 2 ++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 26aa045c..3302b320 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -2,7 +2,12 @@ Changes for crate ================= - - use bulk_args in executemany to increase performance + - use bulk_args in executemany to increase performance: + With crate server >= 0.42.0 executemany uses bulk_args + and returns a list of results. + With crate server < 0.42.0 executemany still issues + a request for every parameter and doesn't return + any results. - improved docs formatting of field lists diff --git a/docs/client.txt b/docs/client.txt index a4cf2831..aa9b32e9 100644 --- a/docs/client.txt +++ b/docs/client.txt @@ -57,12 +57,18 @@ To bulk insert data you can use the `executemany` function:: ... [('Cloverleaf', '2007-03-11', 'Quasar', 7), ... ('Old Faithful', '2007-03-11', 'Quasar', 7)]) + [{u'rowcount': 1}, {u'rowcount': 1}] + +`executemany` returns a list of results for every parameter. Each result +contains a rowcount. If an error occures the rowcount is -2 and the result +may contain an `error_message` depending on the error. .. note:: If you are using a crate server version older than 0.42.0 the client will execute a single sql statement for every parameter in the parameter - sequence when you are using executemany. To avoid that overhead you can + sequence when you are using executemany. In this case, executemany doesn't + return any value. To avoid that overhead you can use ``execute`` and make use of multiple rows in the INSERT statement and provide a list of arguments with the length of ``number of inserted records * number of columns``:: diff --git a/src/crate/client/cursor.py b/src/crate/client/cursor.py index a074e435..0f09a7bf 100644 --- a/src/crate/client/cursor.py +++ b/src/crate/client/cursor.py @@ -77,9 +77,11 @@ def executemany(self, sql, seq_of_parameters): "rowcount": sum(row_counts) if row_counts else -1, "duration": sum(durations) if durations else -1, "rows": [], - "cols": self._result.get("cols", []) + "cols": self._result.get("cols", []), + "results": self._result.get("results") } self.rows = iter(self._result["rows"]) + return self._result["results"] def fetchone(self): """ diff --git a/src/crate/client/cursor.txt b/src/crate/client/cursor.txt index 3e885175..31eb9e7b 100644 --- a/src/crate/client/cursor.txt +++ b/src/crate/client/cursor.txt @@ -190,6 +190,8 @@ executemany >>> cursor = connection.cursor() >>> cursor.executemany('', (1,2,3)) + [{'rowcount': 3}, {'rowcount': 2}] + >>> cursor.rowcount 5 >>> cursor.duration