Skip to content

Commit

Permalink
Added dbapi module with dbapi-appliance unit tests
Browse files Browse the repository at this point in the history
Co-authored-by: Denis Ignatenko <[email protected]>
  • Loading branch information
artembo and denis-ignatenko committed Aug 19, 2020
1 parent bd37703 commit 69fffb1
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ sophia

# Vim Swap files
*.sw[a-z]
.idea
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ env:
- OS=ubuntu DIST=trusty
- OS=ubuntu DIST=xenial
- OS=ubuntu DIST=bionic
- OS=ubuntu DIST=disco
- OS=debian DIST=jessie
- OS=debian DIST=stretch
- OS=debian DIST=buster
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ environment:
matrix:
- PYTHON: "C:\\Python27"
- PYTHON: "C:\\Python27-x64"
- PYTHON: "C:\\Python34"
- PYTHON: "C:\\Python34-x64"
- PYTHON: "C:\\Python35"
- PYTHON: "C:\\Python35-x64"
- PYTHON: "C:\\Python36"
- PYTHON: "C:\\Python36-x64"
- PYTHON: "C:\\Python37"
- PYTHON: "C:\\Python37-x64"
- PYTHON: "C:\\Python38"
- PYTHON: "C:\\Python38-x64"

install:
# install runtime dependencies
- "%PYTHON%\\python.exe -m pip install -r requirements.txt"
# install testing dependencies
- "%PYTHON%\\python.exe -m pip install pyyaml"
- "%PYTHON%\\python.exe -m pip install pyyaml dbapi-compliance==1.15.0"

build: off

Expand Down
2 changes: 1 addition & 1 deletion tarantool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def connectmesh(addrs=({'host': 'localhost', 'port': 3301},), user=None,

__all__ = ['connect', 'Connection', 'connectmesh', 'MeshConnection', 'Schema',
'Error', 'DatabaseError', 'NetworkError', 'NetworkWarning',
'SchemaError']
'SchemaError', 'dbapi']
55 changes: 47 additions & 8 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import ctypes
import ctypes.util

try:
from ctypes import c_ssize_t
except ImportError:
Expand All @@ -34,7 +35,8 @@
RequestSubscribe,
RequestUpdate,
RequestUpsert,
RequestAuthenticate
RequestAuthenticate,
RequestExecute
)
from tarantool.space import Space
from tarantool.const import (
Expand All @@ -49,11 +51,18 @@
ITERATOR_ALL
)
from tarantool.error import (
Error,
NetworkError,
DatabaseError,
InterfaceError,
SchemaError,
NetworkWarning,
OperationalError,
DataError,
IntegrityError,
InternalError,
ProgrammingError,
NotSupportedError,
SchemaReloadException,
warn
)
Expand All @@ -76,11 +85,20 @@ class Connection(object):
Also this class provides low-level interface to data manipulation
(insert/delete/update/select).
'''
Error = tarantool.error
# DBAPI Extension: supply exceptions as attributes on the connection
Error = Error
DatabaseError = DatabaseError
InterfaceError = InterfaceError
SchemaError = SchemaError
NetworkError = NetworkError
Warning = Warning
DataError = DataError
OperationalError = OperationalError
IntegrityError = IntegrityError
InternalError = InternalError
ProgrammingError = ProgrammingError
NotSupportedError = NotSupportedError
ImproperlyConfigured = Exception

def __init__(self, host, port,
user=None,
Expand All @@ -91,6 +109,7 @@ def __init__(self, host, port,
connect_now=True,
encoding=ENCODING_DEFAULT,
call_16=False,
use_list=True,
connection_timeout=CONNECTION_TIMEOUT):
'''
Initialize a connection to the server.
Expand Down Expand Up @@ -123,6 +142,7 @@ def __init__(self, host, port,
self._socket = None
self.connected = False
self.error = True
self.use_list = use_list
self.encoding = encoding
self.call_16 = call_16
self.connection_timeout = connection_timeout
Expand Down Expand Up @@ -260,7 +280,7 @@ def _send_request_wo_reconnect(self, request):
while True:
try:
self._socket.sendall(bytes(request))
response = Response(self, self._read_response())
response = Response(self, self._read_response(), self.use_list)
break
except SchemaReloadException as e:
self.update_schema(e.schema_version)
Expand Down Expand Up @@ -292,13 +312,12 @@ def check(): # Check that connection is alive
retbytes = self._sys_recv(sock_fd, buf, 1, flag)

err = 0
if os.name!= 'nt':
if os.name != 'nt':
err = ctypes.get_errno()
else:
err = ctypes.get_last_error()
self._socket.setblocking(True)


WWSAEWOULDBLOCK = 10035
if (retbytes < 0) and (err == errno.EAGAIN or
err == errno.EWOULDBLOCK or
Expand Down Expand Up @@ -445,7 +464,7 @@ def _join_v16(self, server_uuid):
self._socket.sendall(bytes(request))

while True:
resp = Response(self, self._read_response())
resp = Response(self, self._read_response(), self.use_list)
yield resp
if resp.code == REQUEST_TYPE_OK or resp.code >= REQUEST_TYPE_ERROR:
return
Expand All @@ -459,7 +478,7 @@ class JoinState:
self._socket.sendall(bytes(request))
state = JoinState.Handshake
while True:
resp = Response(self, self._read_response())
resp = Response(self, self._read_response(), self.use_list)
yield resp
if resp.code >= REQUEST_TYPE_ERROR:
return
Expand Down Expand Up @@ -488,7 +507,7 @@ def subscribe(self, cluster_uuid, server_uuid, vclock=None):
request = RequestSubscribe(self, cluster_uuid, server_uuid, vclock)
self._socket.sendall(bytes(request))
while True:
resp = Response(self, self._read_response())
resp = Response(self, self._read_response(), self.use_list)
yield resp
if resp.code >= REQUEST_TYPE_ERROR:
return
Expand Down Expand Up @@ -785,3 +804,23 @@ def generate_sync(self):
Need override for async io connection
'''
return 0

def execute(self, query, params=None):
'''
Execute SQL request.
Execute SQL query in database.
:param query: SQL syntax query
:type query: str
:param params: Bind values to use in query
:type params: list, dict
:return: query result data
:rtype: list
'''
if not params:
params = []
request = RequestExecute(self, query, params)
response = self._send_request(request)
return response
7 changes: 7 additions & 0 deletions tarantool/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
#
IPROTO_DATA = 0x30
IPROTO_ERROR = 0x31
#
IPROTO_METADATA = 0x32
IPROTO_SQL_TEXT = 0x40
IPROTO_SQL_BIND = 0x41
IPROTO_SQL_INFO = 0x42


IPROTO_GREETING_SIZE = 128
IPROTO_BODY_MAX_LEN = 2147483648
Expand All @@ -44,6 +50,7 @@
REQUEST_TYPE_EVAL = 8
REQUEST_TYPE_UPSERT = 9
REQUEST_TYPE_CALL = 10
REQUEST_TYPE_EXECUTE = 11
REQUEST_TYPE_PING = 64
REQUEST_TYPE_JOIN = 65
REQUEST_TYPE_SUBSCRIBE = 66
Expand Down
Loading

0 comments on commit 69fffb1

Please sign in to comment.