Skip to content
Snippets Groups Projects
Commit 257f117c authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

Integrity error reparsing added

parent a718b420
No related branches found
No related tags found
No related merge requests found
......@@ -279,9 +279,7 @@ class SelectHandlingLayer(HandlingLayer):
self.poll = select.poll()
self.fdmap = {}
def close(self):
self.poll.close()
def register_channel(self, channel):
"""
Registers a file descriptor based channel.
......
......@@ -3,7 +3,7 @@ import Queue
class DatabaseDefinition(object):
def __init__(self, cccall, cbexcepts, cccall_args=(), cccall_kwargs={},
xwcb=lambda e: True, acs=lambda x: None, occ=lambda x: None,
oct=lambda x: None):
oct=lambda x: None, integ_err_cls=None):
"""
This initializes a database definition. Because different databases work
......@@ -45,6 +45,10 @@ class DatabaseDefinition(object):
at it's default.
@type oct: callable/1
@param integ_err_cls: class of IntegrityError exceptions. If .execute() throws an
exception of this class, and it's an instance of integ_err_cls, it will be rethrown
as given cursor's IntegrityError exception
@type integ_err_cls: clas
"""
if type(cbexcepts) in (tuple, list):
self.cb_excepts = tuple(cbexcepts)
......@@ -56,6 +60,7 @@ class DatabaseDefinition(object):
self.occ = occ
self.oct = oct
self.conn_lambda = lambda: cccall(*cccall_args, **cccall_kwargs) #: closure that returns a connection
self.integ_err_cls = None
def get_connection(self):
"""Returns a new connection object. This connects to the database with according
......@@ -224,6 +229,10 @@ class ConnectionPool(object):
Supports the context manager protocol.
"""
class IntegrityError(Exception):
"""Thrown when cursor raises an integrity error"""
def __init__(self, cp):
"""
Initializes the cursor wrapper.
......@@ -244,6 +253,8 @@ class ConnectionPool(object):
except self.cp.dd.cb_excepts as exc:
if not self.first_query: raise
if not self.cp.dd.xwcb(exc): raise
if isinstance(exc, self.cp.dd.integ_err_cls):
raise self.IntegrityError
self._reconnect()
continue
self.first_query = False
......@@ -256,6 +267,8 @@ class ConnectionPool(object):
except self.cp.dd.cb_excepts as exc:
if not self.first_query: raise
if not self.cp.dd.xwcb(exc): raise
if isinstance(exc, self.cp.dd.integ_err_cls):
raise self.IntegrityError
self._reconnect()
continue
self.first_query = False
......@@ -300,6 +313,9 @@ class ConnectionPool(object):
Supports the context manager protocol.
"""
class IntegrityError(Exception):
"""Thrown when cursor raises an integrity error"""
def __init__(self, cp):
super(Cursor, self).__init__(cp, False)
......@@ -309,6 +325,8 @@ class ConnectionPool(object):
self.cursor.execute(*args, **kwargs)
except self.cp.dd.cb_excepts as exc:
if not self.cp.dd.xwcb(exc): raise
if isinstance(exc, self.cp.dd.integ_err_cls):
raise self.IntegrityError
self._reconnect()
continue
break
......@@ -319,6 +337,8 @@ class ConnectionPool(object):
self.cursor.executemany(*args, **kwargs)
except self.cp.dd.cb_excepts as exc:
if not self.cp.dd.xwcb(exc): raise
if isinstance(exc, self.cp.dd.integ_err_cls):
raise self.IntegrityError
self._reconnect()
continue
break
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment