11
11
12
12
import ctypes
13
13
import ctypes .util
14
+
14
15
try :
15
16
from ctypes import c_ssize_t
16
17
except ImportError :
34
35
RequestSubscribe ,
35
36
RequestUpdate ,
36
37
RequestUpsert ,
37
- RequestAuthenticate
38
+ RequestAuthenticate ,
39
+ RequestExecute
38
40
)
39
41
from tarantool .space import Space
40
42
from tarantool .const import (
49
51
ITERATOR_ALL
50
52
)
51
53
from tarantool .error import (
54
+ Error ,
52
55
NetworkError ,
53
56
DatabaseError ,
54
57
InterfaceError ,
55
58
SchemaError ,
56
59
NetworkWarning ,
60
+ OperationalError ,
61
+ DataError ,
62
+ IntegrityError ,
63
+ InternalError ,
64
+ ProgrammingError ,
65
+ NotSupportedError ,
57
66
SchemaReloadException ,
58
67
warn
59
68
)
@@ -76,11 +85,20 @@ class Connection(object):
76
85
Also this class provides low-level interface to data manipulation
77
86
(insert/delete/update/select).
78
87
'''
79
- Error = tarantool .error
88
+ # DBAPI Extension: supply exceptions as attributes on the connection
89
+ Error = Error
80
90
DatabaseError = DatabaseError
81
91
InterfaceError = InterfaceError
82
92
SchemaError = SchemaError
83
93
NetworkError = NetworkError
94
+ Warning = Warning
95
+ DataError = DataError
96
+ OperationalError = OperationalError
97
+ IntegrityError = IntegrityError
98
+ InternalError = InternalError
99
+ ProgrammingError = ProgrammingError
100
+ NotSupportedError = NotSupportedError
101
+ ImproperlyConfigured = Exception
84
102
85
103
def __init__ (self , host , port ,
86
104
user = None ,
@@ -91,6 +109,7 @@ def __init__(self, host, port,
91
109
connect_now = True ,
92
110
encoding = ENCODING_DEFAULT ,
93
111
call_16 = False ,
112
+ use_list = True ,
94
113
connection_timeout = CONNECTION_TIMEOUT ):
95
114
'''
96
115
Initialize a connection to the server.
@@ -123,6 +142,7 @@ def __init__(self, host, port,
123
142
self ._socket = None
124
143
self .connected = False
125
144
self .error = True
145
+ self .use_list = use_list
126
146
self .encoding = encoding
127
147
self .call_16 = call_16
128
148
self .connection_timeout = connection_timeout
@@ -260,7 +280,7 @@ def _send_request_wo_reconnect(self, request):
260
280
while True :
261
281
try :
262
282
self ._socket .sendall (bytes (request ))
263
- response = Response (self , self ._read_response ())
283
+ response = Response (self , self ._read_response (), self . use_list )
264
284
break
265
285
except SchemaReloadException as e :
266
286
self .update_schema (e .schema_version )
@@ -292,13 +312,12 @@ def check(): # Check that connection is alive
292
312
retbytes = self ._sys_recv (sock_fd , buf , 1 , flag )
293
313
294
314
err = 0
295
- if os .name != 'nt' :
315
+ if os .name != 'nt' :
296
316
err = ctypes .get_errno ()
297
317
else :
298
318
err = ctypes .get_last_error ()
299
319
self ._socket .setblocking (True )
300
320
301
-
302
321
WWSAEWOULDBLOCK = 10035
303
322
if (retbytes < 0 ) and (err == errno .EAGAIN or
304
323
err == errno .EWOULDBLOCK or
@@ -445,7 +464,7 @@ def _join_v16(self, server_uuid):
445
464
self ._socket .sendall (bytes (request ))
446
465
447
466
while True :
448
- resp = Response (self , self ._read_response ())
467
+ resp = Response (self , self ._read_response (), self . use_list )
449
468
yield resp
450
469
if resp .code == REQUEST_TYPE_OK or resp .code >= REQUEST_TYPE_ERROR :
451
470
return
@@ -459,7 +478,7 @@ class JoinState:
459
478
self ._socket .sendall (bytes (request ))
460
479
state = JoinState .Handshake
461
480
while True :
462
- resp = Response (self , self ._read_response ())
481
+ resp = Response (self , self ._read_response (), self . use_list )
463
482
yield resp
464
483
if resp .code >= REQUEST_TYPE_ERROR :
465
484
return
@@ -488,7 +507,7 @@ def subscribe(self, cluster_uuid, server_uuid, vclock=None):
488
507
request = RequestSubscribe (self , cluster_uuid , server_uuid , vclock )
489
508
self ._socket .sendall (bytes (request ))
490
509
while True :
491
- resp = Response (self , self ._read_response ())
510
+ resp = Response (self , self ._read_response (), self . use_list )
492
511
yield resp
493
512
if resp .code >= REQUEST_TYPE_ERROR :
494
513
return
@@ -785,3 +804,23 @@ def generate_sync(self):
785
804
Need override for async io connection
786
805
'''
787
806
return 0
807
+
808
+ def execute (self , query , params = None ):
809
+ '''
810
+ Execute SQL request.
811
+ Execute SQL query in database.
812
+
813
+ :param query: SQL syntax query
814
+ :type query: str
815
+
816
+ :param params: Bind values to use in query
817
+ :type params: list, dict
818
+
819
+ :return: query result data
820
+ :rtype: list
821
+ '''
822
+ if not params :
823
+ params = []
824
+ request = RequestExecute (self , query , params )
825
+ response = self ._send_request (request )
826
+ return response
0 commit comments