Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbapi2 #161

Merged
merged 5 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ environment:
- 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%PYYAML%"
- "%PYTHON%\\python.exe -m pip install pyyaml%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']
67 changes: 63 additions & 4 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
RequestSubscribe,
RequestUpdate,
RequestUpsert,
RequestAuthenticate
RequestAuthenticate,
RequestExecute
)
from tarantool.space import Space
from tarantool.const import (
Expand All @@ -49,13 +50,21 @@
ITERATOR_ALL
)
from tarantool.error import (
Error,
NetworkError,
artembo marked this conversation as resolved.
Show resolved Hide resolved
DatabaseError,
InterfaceError,
ConfigurationError,
SchemaError,
NetworkWarning,
OperationalError,
DataError,
IntegrityError,
InternalError,
ProgrammingError,
NotSupportedError,
SchemaReloadException,
Warning,
warn
)
from tarantool.schema import Schema
Expand All @@ -77,12 +86,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
artembo marked this conversation as resolved.
Show resolved Hide resolved
Error = Error
DatabaseError = DatabaseError
InterfaceError = InterfaceError
ConfigurationError = ConfigurationError
SchemaError = SchemaError
NetworkError = NetworkError
Warning = Warning
DataError = DataError
OperationalError = OperationalError
IntegrityError = IntegrityError
InternalError = InternalError
ProgrammingError = ProgrammingError
NotSupportedError = NotSupportedError

def __init__(self, host, port,
user=None,
Expand All @@ -92,6 +109,7 @@ def __init__(self, host, port,
reconnect_delay=RECONNECT_DELAY,
connect_now=True,
encoding=ENCODING_DEFAULT,
use_list=True,
artembo marked this conversation as resolved.
Show resolved Hide resolved
call_16=False,
connection_timeout=CONNECTION_TIMEOUT):
'''
Expand Down Expand Up @@ -131,6 +149,7 @@ def __init__(self, host, port,
self.connected = False
self.error = True
self.encoding = encoding
self.use_list = use_list
self.call_16 = call_16
self.connection_timeout = connection_timeout
if connect_now:
Expand All @@ -143,6 +162,13 @@ def close(self):
self._socket.close()
self._socket = None

def is_closed(self):
'''
Returns the state of the Connection instance
:rtype: Boolean
'''
return self._socket is None

artembo marked this conversation as resolved.
Show resolved Hide resolved
def connect_basic(self):
if self.host == None:
self.connect_unix()
Expand Down Expand Up @@ -257,7 +283,7 @@ def _read_response(self):

def _send_request_wo_reconnect(self, request):
'''
:rtype: `Response` instance
:rtype: `Response` instance or subclass

:raise: NetworkError
'''
Expand All @@ -267,7 +293,7 @@ def _send_request_wo_reconnect(self, request):
while True:
try:
self._socket.sendall(bytes(request))
response = Response(self, self._read_response())
response = request.response_class(self, self._read_response())
break
except SchemaReloadException as e:
self.update_schema(e.schema_version)
Expand Down Expand Up @@ -792,3 +818,36 @@ def generate_sync(self):
Need override for async io connection
'''
return 0

def execute(self, query, params=None):
artembo marked this conversation as resolved.
Show resolved Hide resolved
'''
Execute SQL request.

Tarantool binary protocol for SQL requests
supports "qmark" and "named" param styles.
Sequence of values can be used for "qmark" style.
A mapping is used for "named" param style
without leading colon in the keys.

Example for "qmark" arguments:
>>> args = ['[email protected]']
>>> c.execute('select * from "users" where "email"=?', args)

Example for "named" arguments:
>>> args = {'email': '[email protected]'}
>>> c.execute('select * from "users" where "email"=:email', args)

:param query: SQL syntax query
:type query: str

:param params: Bind values to use in the query.
:type params: list, dict
artembo marked this conversation as resolved.
Show resolved Hide resolved

:return: query result data
:rtype: `Response` instance
'''
if not params:
params = []
request = RequestExecute(self, query, params)
response = self._send_request(request)
return response
artembo marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions tarantool/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
#
IPROTO_DATA = 0x30
IPROTO_ERROR = 0x31
#
IPROTO_METADATA = 0x32
IPROTO_SQL_TEXT = 0x40
IPROTO_SQL_BIND = 0x41
IPROTO_SQL_INFO = 0x42
IPROTO_SQL_INFO_ROW_COUNT = 0x00
IPROTO_SQL_INFO_AUTOINCREMENT_IDS = 0x01

IPROTO_GREETING_SIZE = 128
IPROTO_BODY_MAX_LEN = 2147483648
Expand All @@ -44,6 +51,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