Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support function fetchmany() and fetchall() in python SDK and UT #1215

Merged
merged 9 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions python/openmldb/dbapi/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,9 @@ def get_databases(self):
return self.connection._sdk.getDatabases()

def fetchone(self):

if self._resultSet is None: return "call fetchone"
if self._resultSet is None: raise DatabaseError("query data failed")
ok = self._resultSet.Next()
if not ok:
self.rowcount = -1
self._resultSet = None
self.__schema = None
self.__getMap = None
return None
values = []
for i in range(self.__schema.GetColumnCnt()):
Expand All @@ -362,7 +357,24 @@ def fetchone(self):

@connected
def fetchmany(self, size=None):
raise NotSupportedError("Unsupported in OpenMLDB")
if self._resultSet is None: raise DatabaseError("query data failed")
if size is None:
HuilinWu2 marked this conversation as resolved.
Show resolved Hide resolved
size = self.arraysize
elif size < 0:
raise Exception(f"Given size should greater than zero")
values = []
for k in range(size):
ok = self._resultSet.Next()
if not ok:
break
row = []
for i in range(self.__schema.GetColumnCnt()):
if self._resultSet.IsNULL(i):
row.append(None)
else:
row.append(self.__getMap[self.__schema.GetColumnType(i)](i))
values.append(tuple(row))
return values

def nextset(self):
raise NotSupportedError("Unsupported in OpenMLDB")
Expand All @@ -375,8 +387,8 @@ def setoutputsize(self, size, columns=()):

@connected
def fetchall(self):
raise NotSupportedError("Unsupported in OpenMLDB")

return self.fetchmany(size=self.rowcount)
@staticmethod
def substitute_in_query(string_query, parameters):
query = string_query
Expand Down
25 changes: 10 additions & 15 deletions python/openmldb/test/openmldb_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def test_basic(self):
connection.execute(insert5, ({"col2":"2020-12-29"}));

self.check_fetchmany(connection)
self.check_fetchall(connection)
self.check_exectute_many(connection,insert4)

data = {1000 : [1000, '2020-12-25', 'guangdon', '广州', 1],
Expand Down Expand Up @@ -88,6 +87,7 @@ def test_basic(self):
(1003, '2020-12-28', 'jiangxi', 'nanchang', 4),
(1004, '2020-12-29', 'hubei', 'wuhan', 5),
]
self.check_fetchall(connection, expectRows)
self.check_result(rs, expectRows, 0);
# test condition select
rs = connection.execute("select * from tsql1010 where col3 = 'hefei';");
Expand Down Expand Up @@ -207,21 +207,16 @@ def check_exectute_many(self,connection,sql):
pass

def check_fetchmany(self,connection):
try:
result = connection.execute("select * from tsql1010;")
print(result.fetchmany(size=2))
self.assertTrue(False)
except Exception as e:
pass
result = connection.execute("select * from tsql1010;")
self.assertTrue(result.fetchmany() == [(1002, '2020-12-27', 'fujian', 'fuzhou', 3)])
self.assertTrue(result.fetchmany(size=2) == [(1001, '2020-12-26', 'hefei', 'anhui', 2),(1000, '2020-12-25', 'guangdon', '广州', 1)])
self.assertTrue(result.fetchmany(size=4) == [(1004, '2020-12-29', 'hubei', 'wuhan', 5),(1003, '2020-12-28', 'jiangxi', 'nanchang', 4)])

def check_fetchall(self,connection):
try:
result = connection.execute("select * from tsql1010;")
print(result.fetchall())
self.assertTrue(False)
except Exception as e:
pass

def check_fetchall(self,connection, expect_row):
result = connection.execute("select * from tsql1010;")
result = sorted(result.fetchall(), key=lambda x: x[0])
self.assertTrue(result == expect_row)

def test_parameterized_query(self):
logging.info("test_parameterized_query...")
engine = db.create_engine('openmldb:///db_test?zk=127.0.0.1:6181&zkPath=/onebox')
Expand Down