Skip to content

Commit 4f6b408

Browse files
committed
workaround for lastrowid
1 parent 3776c0d commit 4f6b408

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

tarantool/dbapi.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,21 @@ def callproc(self, procname, *params):
2727
def close(self):
2828
self._c.close()
2929

30-
def execute(self, query, params=None):
31-
def convert_param(p):
32-
print('PARAM: ', p)
33-
if isinstance(p, bool):
34-
return str(p)
35-
elif p is None:
36-
return "NULL"
37-
return "'%s'" % p
30+
def _convert_param(self, p):
31+
if isinstance(p, bool):
32+
return str(p)
33+
elif p is None:
34+
return "NULL"
35+
return "'%s'" % p
3836

37+
def execute(self, query, params=None):
3938
if params:
40-
query = query % tuple(convert_param(param) for param in params)
39+
query = query % tuple(self._convert_param(param) for param in params)
4140

42-
print(query)
41+
# print(query)
4342
response = self._c.execute(query)
4443

45-
if len(response.body) > 1:
46-
self.rows = tuple(response.body.values())[1]
47-
else:
48-
self.rows = []
44+
self.rows = tuple(response.body.values())[1] if len(response.body) > 1 else []
4945

5046
rc_pattern = re.compile(r'^(UPDATE|INSERT)')
5147
if rc_pattern.match(query):
@@ -55,6 +51,17 @@ def convert_param(p):
5551
pass
5652
else:
5753
self._rowcount = 1
54+
55+
def extract_last_row_id(body): # Need to be checked
56+
try:
57+
val = tuple(tuple(body.items())[0][-1].items())[-1][-1][0]
58+
except TypeError:
59+
val = 1
60+
return val
61+
62+
u_pattern = re.compile(r'^INSERT')
63+
if u_pattern.match(query):
64+
self._lastrowid = extract_last_row_id(response.body)
5865
return response
5966

6067
@property
@@ -72,7 +79,6 @@ def fetchone(self):
7279
pass
7380

7481
def fetchmany(self, size):
75-
self._lastrowid += size
7682
items = deepcopy(self.rows)
7783
self.rows = []
7884
return items

tarantool/response.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,6 @@ def __str__(self):
244244
output.pop()
245245
return ''.join(output)
246246

247-
__repr__ = __str__
247+
def __repr__(self):
248+
return self.__str__()
249+

0 commit comments

Comments
 (0)