2
2
Supports python 3.6 and above
3
3
"""
4
4
import re
5
+ from collections import deque
5
6
from copy import deepcopy
7
+ from itertools import islice
6
8
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
8
13
9
14
from .connection import Connection as BaseConnection
10
15
11
16
12
17
class Cursor :
13
18
_lastrowid = 0
14
19
description = None
15
- arraysize = None
16
- rows = []
17
20
position = 0
21
+ arraysize = 100
18
22
autocommit = True
19
23
_rowcount = 0
24
+ ui_pattern = re .compile (r'^(UPDATE|INSERT)' )
25
+ u_pattern = re .compile (r'^INSERT' )
20
26
21
27
def __init__ (self , connection ):
22
28
self ._c = connection
29
+ self .rows = []
23
30
24
31
def callproc (self , procname , * params ):
25
32
pass
26
33
27
- def close (self ):
28
- self . _c . close ()
34
+ def close (self ): # TODO: Find out how to implement closing connection correctly
35
+ pass
29
36
30
- def _convert_param (self , p ):
37
+ @staticmethod
38
+ def _convert_param (p ):
39
+ if p is None :
40
+ return "NULL"
31
41
if isinstance (p , bool ):
32
42
return str (p )
33
- elif p is None :
34
- return "NULL"
35
43
return "'%s'" % p
36
44
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
+
37
53
def execute (self , query , params = None ):
38
54
if params :
39
55
query = query % tuple (self ._convert_param (param ) for param in params )
@@ -43,25 +59,16 @@ def execute(self, query, params=None):
43
59
44
60
self .rows = tuple (response .body .values ())[1 ] if len (response .body ) > 1 else []
45
61
46
- rc_pattern = re .compile (r'^(UPDATE|INSERT)' )
47
- if rc_pattern .match (query ):
62
+ if self .ui_pattern .match (query ):
48
63
try :
49
64
self ._rowcount = response .rowcount
50
65
except InterfaceError :
51
- pass
66
+ self . _rowcount = 1
52
67
else :
53
68
self ._rowcount = 1
54
69
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 )
65
72
return response
66
73
67
74
@property
@@ -76,16 +83,22 @@ def executemany(self, query, params):
76
83
return self .execute (query , params )
77
84
78
85
def fetchone (self ):
79
- pass
86
+ return self . rows [ 0 ] if len ( self . rows ) else None
80
87
81
88
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 ):
82
98
items = deepcopy (self .rows )
83
99
self .rows = []
84
100
return items
85
101
86
- def fetchall (self ):
87
- pass
88
-
89
102
def setinputsizes (self , sizes ):
90
103
pass
91
104
@@ -94,30 +107,24 @@ def setoutputsize(self, size, column=None):
94
107
95
108
96
109
class Connection (BaseConnection ):
97
- rows = []
98
110
_cursor = None
99
111
100
- server_version = 1
112
+ server_version = 2
101
113
102
114
def commit (self ):
103
115
pass
104
116
117
+ def rollback (self ):
118
+ pass
119
+
105
120
def close (self ):
106
- '''
107
- Close connection to the server
108
- '''
109
121
if self ._socket :
110
122
self ._socket .close ()
111
123
self ._socket = None
112
124
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
119
128
120
129
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