Commit c909906a by Lukáš Lalinský

Correctly set first_frame_max_size to get results from execute()

parent 53c8b37d
......@@ -364,7 +364,7 @@ class AvaticaClient(object):
self._apply(request)
def prepareAndExecute(self, connectionId, statementId, sql, maxRowCount=-1):
def prepareAndExecute(self, connectionId, statementId, sql, max_rows_total=None, first_frame_max_size=None):
"""Prepares and immediately executes a statement.
:param connectionId:
......@@ -376,25 +376,30 @@ class AvaticaClient(object):
:param sql:
SQL query.
:param maxRowCount:
Maximum number of rows to return; negative means no limit.
:param max_rows_total:
The maximum number of rows that will be allowed for this query.
:param first_frame_max_size:
The maximum number of rows that will be returned in the first Frame returned for this query.
:returns:
Result set with the signature of the prepared statement and the first frame data.
"""
request = requests_pb2.PrepareAndExecuteRequest()
request.connection_id = connectionId
request.sql = sql
request.max_row_count = maxRowCount
request.max_rows_total = -1
request.statement_id = statementId
request.sql = sql
if max_rows_total is not None:
request.max_rows_total = max_rows_total
if first_frame_max_size is not None:
request.first_frame_max_size = first_frame_max_size
response_data = self._apply(request, 'ExecuteResponse')
response = responses_pb2.ExecuteResponse()
response.ParseFromString(response_data)
return response.results
def prepare(self, connectionId, sql, maxRowCount=-1):
def prepare(self, connectionId, sql, max_rows_total=None):
"""Prepares a statement.
:param connectionId:
......@@ -403,8 +408,8 @@ class AvaticaClient(object):
:param sql:
SQL query.
:param maxRowCount:
Maximum number of rows to return; negative means no limit.
:param max_rows_total:
The maximum number of rows that will be allowed for this query.
:returns:
Signature of the prepared statement.
......@@ -412,15 +417,15 @@ class AvaticaClient(object):
request = requests_pb2.PrepareRequest()
request.connection_id = connectionId
request.sql = sql
request.max_row_count = maxRowCount
request.max_rows_total = -1
if max_rows_total is not None:
request.max_rows_total = max_rows_total
response_data = self._apply(request)
response = responses_pb2.PrepareResponse()
response.ParseFromString(response_data)
return response.statement
def execute(self, connectionId, statementId, signature, parameterValues=None, maxRowCount=-1):
def execute(self, connectionId, statementId, signature, parameter_values=None, first_frame_max_size=None):
"""Returns a frame of rows.
The frame describes whether there may be another frame. If there is not
......@@ -436,11 +441,11 @@ class AvaticaClient(object):
:param signature:
common_pb2.Signature object
:param parameterValues:
:param parameter_values:
A list of parameter values, if statement is to be executed; otherwise ``None``.
:param maxRowCount:
Maximum number of rows to return; negative means no limit.
:param first_frame_max_size:
The maximum number of rows that will be returned in the first Frame returned for this query.
:returns:
Frame data, or ``None`` if there are no more.
......@@ -448,18 +453,19 @@ class AvaticaClient(object):
request = requests_pb2.ExecuteRequest()
request.statementHandle.id = statementId
request.statementHandle.connection_id = connectionId
if parameterValues is not None:
request.parameter_values.extend(parameterValues)
request.has_parameter_values = parameterValues is not None
request.statementHandle.signature.CopyFrom(signature)
# TODO ExecuteRequest has no max_row_count
if parameter_values is not None:
request.parameter_values.extend(parameter_values)
request.has_parameter_values = True
if first_frame_max_size is not None:
request.first_frame_max_size = first_frame_max_size
response_data = self._apply(request)
response = responses_pb2.ExecuteResponse()
response.ParseFromString(response_data)
return response.results
def fetch(self, connectionId, statementId, parameterValues=None, offset=0, fetchMaxRowCount=-1):
def fetch(self, connectionId, statementId, offset=0, frame_max_size=None):
"""Returns a frame of rows.
The frame describes whether there may be another frame. If there is not
......@@ -472,13 +478,10 @@ class AvaticaClient(object):
:param statementId:
ID of the statement to fetch rows from.
:param parameterValues:
A list of parameter values, if statement is to be executed; otherwise ``None``.
:param offset:
Zero-based offset of first row in the requested frame.
:param fetchMaxRowCount:
:param frame_max_size:
Maximum number of rows to return; negative means no limit.
:returns:
......@@ -488,7 +491,8 @@ class AvaticaClient(object):
request.connection_id = connectionId
request.statement_id = statementId
request.offset = offset
request.fetch_max_row_count = fetchMaxRowCount
if frame_max_size is not None:
request.frame_max_size = frame_max_size
response_data = self._apply(request)
response = responses_pb2.FetchResponse()
......
......@@ -155,7 +155,7 @@ class Cursor(object):
def _fetch_next_frame(self):
offset = self._frame.offset + len(self._frame.rows)
frame = self._connection._client.fetch(self._connection._id, self._id,
offset=offset, fetchMaxRowCount=self.itersize)
offset=offset, frame_max_size=self.itersize)
self._set_frame(frame)
def _process_results(self, results):
......@@ -198,17 +198,17 @@ class Cursor(object):
if self._id is None:
self._set_id(self._connection._client.createStatement(self._connection._id))
results = self._connection._client.prepareAndExecute(self._connection._id, self._id,
operation, maxRowCount=self.itersize)
operation, first_frame_max_size=self.itersize)
self._process_results(results)
else:
statement = self._connection._client.prepare(self._connection._id,
operation, maxRowCount=self.itersize)
operation)
self._set_id(statement.id)
self._set_signature(statement.signature)
results = self._connection._client.execute(self._connection._id, self._id,
statement.signature, self._transform_parameters(parameters),
maxRowCount=self.itersize)
first_frame_max_size=self.itersize)
self._process_results(results)
def executemany(self, operation, seq_of_parameters):
......@@ -217,13 +217,13 @@ class Cursor(object):
self._updatecount = -1
self._set_frame(None)
statement = self._connection._client.prepare(self._connection._id,
operation, maxRowCount=0)
operation, max_rows_total=0)
self._set_id(statement.id)
self._set_signature(statement.signature)
for parameters in seq_of_parameters:
self._connection._client.execute(self._connection._id, self._id,
statement.signature, self._transform_parameters(parameters),
maxRowCount=0)
first_frame_max_size=0)
def _transform_row(self, row):
"""Transforms a Row into Python values.
......
import unittest
import phoenixdb
from phoenixdb.tests import TEST_DB_URL
@unittest.skipIf(TEST_DB_URL is None, "these tests require the PHOENIXDB_TEST_DB_URL environment variable set to a clean database")
class PhoenixDatabaseTest(unittest.TestCase):
def test_select_literal(self):
db = phoenixdb.connect(TEST_DB_URL, autocommit=True)
self.addCleanup(db.close)
with db.cursor() as cursor:
cursor.execute("DROP TABLE IF EXISTS test")
cursor.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, text VARCHAR)")
cursor.executemany("UPSERT INTO test VALUES (?, ?)", [[i, 'text {}'.format(i)] for i in range(10)])
with db.cursor() as cursor:
cursor.itersize = 4
cursor.execute("SELECT * FROM test WHERE id>1 ORDER BY id")
self.assertEqual(cursor.fetchall(), [[i, 'text {}'.format(i)] for i in range(2, 10)])
def test_select_parameter(self):
db = phoenixdb.connect(TEST_DB_URL, autocommit=True)
self.addCleanup(db.close)
with db.cursor() as cursor:
cursor.execute("DROP TABLE IF EXISTS test")
cursor.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, text VARCHAR)")
cursor.executemany("UPSERT INTO test VALUES (?, ?)", [[i, 'text {}'.format(i)] for i in range(10)])
with db.cursor() as cursor:
cursor.itersize = 4
cursor.execute("SELECT * FROM test WHERE id>? ORDER BY id", [1])
self.assertEqual(cursor.fetchall(), [[i, 'text {}'.format(i)] for i in range(2, 10)])
self.assertFalse(True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment