1
- """
2
- https://www.python.org/dev/peps/pep-0249/
3
- """
1
+ from __future__ import unicode_literals
2
+
4
3
import re
5
4
6
5
from tarantool .error import InterfaceError
7
6
from .connection import Connection as BaseConnection
8
7
8
+
9
9
update_insert_pattern = re .compile (r'^UPDATE|^INSERT' , re .IGNORECASE )
10
10
11
11
@@ -17,10 +17,10 @@ class Cursor:
17
17
arraysize = 1
18
18
autocommit = True
19
19
closed = False
20
+ rows = None
20
21
21
- def __init__ (self , connection ):
22
- self ._c = connection
23
- self .rows = None
22
+ def __init__ (self , conn ):
23
+ self ._c = conn
24
24
25
25
def callproc (self , procname , * params ): # TODO
26
26
"""
@@ -34,14 +34,15 @@ def callproc(self, procname, *params): # TODO
34
34
be made available through the standard .fetch*() methods.
35
35
"""
36
36
37
- def close (self ): # TODO
37
+ def close (self ):
38
38
"""
39
39
Close the cursor now (rather than whenever __del__ is called).
40
40
41
41
The cursor will be unusable from this point forward; an Error (or
42
42
subclass) exception will be raised if any operation is attempted with
43
43
the cursor.
44
44
"""
45
+ self ._c = None
45
46
46
47
@staticmethod
47
48
def _convert_param (p ):
@@ -90,13 +91,13 @@ def execute(self, query, params=None):
90
91
"""
91
92
if self .closed :
92
93
raise self ._c .ProgrammingError
94
+
93
95
if params :
94
96
query = query % tuple (
95
97
self ._convert_param (param ) for param in params )
96
98
97
99
response = self ._c .execute (query )
98
-
99
- self .rows = tuple (response .body .values ())[1 ] if len (
100
+ self .rows = tuple (response .data ) if len (
100
101
response .body ) > 1 else None
101
102
102
103
if update_insert_pattern .match (query ):
@@ -162,7 +163,7 @@ def fetchone(self):
162
163
.execute*() did not produce any result set or no call was issued yet.
163
164
"""
164
165
if self .rows is None :
165
- raise self ._c .Error
166
+ raise self ._c .ProgrammingError ( 'Nothing to fetch' )
166
167
return self .fetchmany (1 )[0 ] if len (self .rows ) else None
167
168
168
169
def fetchmany (self , size = None ):
@@ -189,7 +190,7 @@ def fetchmany(self, size=None):
189
190
size = size or self .arraysize
190
191
191
192
if self .rows is None :
192
- raise self ._c .ProgrammingError
193
+ raise self ._c .ProgrammingError ( 'Nothing to fetch' )
193
194
194
195
if len (self .rows ) < size :
195
196
items = self .rows
@@ -208,7 +209,7 @@ def fetchall(self):
208
209
.execute*() did not produce any result set or no call was issued yet.
209
210
"""
210
211
if self .rows is None :
211
- raise self ._c .ProgrammingError
212
+ raise self ._c .ProgrammingError ( 'Nothing to fetch' )
212
213
213
214
items = self .rows [:]
214
215
self .rows = []
@@ -217,8 +218,7 @@ def fetchall(self):
217
218
def setinputsizes (self , sizes ):
218
219
"""This can be used before a call to .execute*() to predefine memory
219
220
areas for the operation's parameters.
220
-
221
- sizes is specified as a sequence — one item for each input parameter.
221
+ sizes is specified as a sequence - one item for each input parameter.
222
222
The item should be a Type Object that corresponds to the input that
223
223
will be used, or it should be an integer specifying the maximum
224
224
length of a string parameter. If the item is None, then no predefined
@@ -231,7 +231,13 @@ def setinputsizes(self, sizes):
231
231
free to not use it."""
232
232
233
233
def setoutputsize (self , size , column = None ):
234
- pass
234
+ """Set a column buffer size for fetches of large columns (e.g. LONGs,
235
+ BLOBs, etc.). The column is specified as an index into the result
236
+ sequence. Not specifying the column will set the default size for all
237
+ large columns in the cursor.
238
+ This method would be used before the .execute*() method is invoked.
239
+ Implementations are free to have this method do nothing and users are
240
+ free to not use it."""
235
241
236
242
237
243
class Connection (BaseConnection ):
@@ -243,7 +249,7 @@ class Connection(BaseConnection):
243
249
server_version = 2
244
250
245
251
def connect (self ):
246
- super ().connect ()
252
+ super (Connection , self ).connect ()
247
253
return self
248
254
249
255
def commit (self ):
@@ -272,8 +278,8 @@ def rollback(self):
272
278
273
279
def execute (self , query , params = None ):
274
280
if self ._socket is None :
275
- raise self .ProgrammingError
276
- return super ().execute (query , params )
281
+ raise self .ProgrammingError ( 'Can not execute on closed connection' )
282
+ return super (Connection , self ).execute (query , params )
277
283
278
284
def close (self ):
279
285
"""
@@ -289,11 +295,7 @@ def close(self):
289
295
self ._socket .close ()
290
296
self ._socket = None
291
297
else :
292
- raise self .ProgrammingError
293
-
294
- def _set_cursor (self ):
295
- self ._cursor = Cursor (self )
296
- return self ._cursor
298
+ raise self .ProgrammingError ('Connection already closed' )
297
299
298
300
def cursor (self , params = None ):
299
301
"""
@@ -303,4 +305,4 @@ def cursor(self, params=None):
303
305
will have to emulate cursors using other means to the extent needed
304
306
by this specification.
305
307
"""
306
- return self . _cursor or self . _set_cursor ( )
308
+ return Cursor ( self )
0 commit comments