Skip to content

Commit 0332c53

Browse files
committed
implemented fetchone logic
1 parent 4f6b408 commit 0332c53

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

tarantool/dbapi.py

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,54 @@
22
Supports python 3.6 and above
33
"""
44
import re
5+
from collections import deque
56
from copy import deepcopy
7+
from itertools import islice
68

7-
from tarantool.error import InterfaceError
9+
from pycallgraph import PyCallGraph
10+
from pycallgraph.output import GraphvizOutput
11+
12+
from tarantool.error import InterfaceError, ProgrammingError
813

914
from .connection import Connection as BaseConnection
1015

1116

1217
class Cursor:
1318
_lastrowid = 0
1419
description = None
15-
arraysize = None
16-
rows = []
1720
position = 0
21+
arraysize = 100
1822
autocommit = True
1923
_rowcount = 0
24+
ui_pattern = re.compile(r'^(UPDATE|INSERT)')
25+
u_pattern = re.compile(r'^INSERT')
2026

2127
def __init__(self, connection):
2228
self._c = connection
29+
self.rows = []
2330

2431
def callproc(self, procname, *params):
2532
pass
2633

27-
def close(self):
28-
self._c.close()
34+
def close(self): # TODO: Find out how to implement closing connection correctly
35+
pass
2936

30-
def _convert_param(self, p):
37+
@staticmethod
38+
def _convert_param(p):
39+
if p is None:
40+
return "NULL"
3141
if isinstance(p, bool):
3242
return str(p)
33-
elif p is None:
34-
return "NULL"
3543
return "'%s'" % p
3644

45+
@staticmethod
46+
def extract_last_row_id(body): # Need to be checked
47+
try:
48+
val = tuple(tuple(body.items())[0][-1].items())[-1][-1][0]
49+
except TypeError:
50+
val = 1
51+
return val
52+
3753
def execute(self, query, params=None):
3854
if params:
3955
query = query % tuple(self._convert_param(param) for param in params)
@@ -43,25 +59,16 @@ def execute(self, query, params=None):
4359

4460
self.rows = tuple(response.body.values())[1] if len(response.body) > 1 else []
4561

46-
rc_pattern = re.compile(r'^(UPDATE|INSERT)')
47-
if rc_pattern.match(query):
62+
if self.ui_pattern.match(query):
4863
try:
4964
self._rowcount = response.rowcount
5065
except InterfaceError:
51-
pass
66+
self._rowcount = 1
5267
else:
5368
self._rowcount = 1
5469

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)
70+
if self.u_pattern.match(query):
71+
self._lastrowid = self.extract_last_row_id(response.body)
6572
return response
6673

6774
@property
@@ -76,16 +83,22 @@ def executemany(self, query, params):
7683
return self.execute(query, params)
7784

7885
def fetchone(self):
79-
pass
86+
return self.rows[0] if len(self.rows) else None
8087

8188
def fetchmany(self, size):
89+
if len(self.rows) < size:
90+
items = self.rows
91+
self.rows = []
92+
else:
93+
items, self.rows = self.rows[:size], self.rows[size:]
94+
95+
return items if len(items) else []
96+
97+
def fetchall(self):
8298
items = deepcopy(self.rows)
8399
self.rows = []
84100
return items
85101

86-
def fetchall(self):
87-
pass
88-
89102
def setinputsizes(self, sizes):
90103
pass
91104

@@ -94,30 +107,24 @@ def setoutputsize(self, size, column=None):
94107

95108

96109
class Connection(BaseConnection):
97-
rows = []
98110
_cursor = None
99111

100-
server_version = 1
112+
server_version = 2
101113

102114
def commit(self):
103115
pass
104116

117+
def rollback(self):
118+
pass
119+
105120
def close(self):
106-
'''
107-
Close connection to the server
108-
'''
109121
if self._socket:
110122
self._socket.close()
111123
self._socket = None
112124

113-
def rollback(self):
114-
pass
115-
116-
@classmethod
117-
def cache(cls, cursor):
118-
cls._cursor = cursor
125+
def _set_cursor(self):
126+
self._cursor = Cursor(self)
127+
return self._cursor
119128

120129
def cursor(self, params=None):
121-
if not self._cursor:
122-
self._cursor = Cursor(self)
123-
return self._cursor
130+
return self._cursor or self._set_cursor()

0 commit comments

Comments
 (0)