def _finish_tpc(self, command, fallback, xid): if xid: # committing/aborting a received transaction. if self.status != consts.STATUS_READY: raise exceptions.ProgrammingError( "tpc_commit/tpc_rollback with a xid " "must be called outside a transaction") self._execute_tpc_command(command, xid) else: # committing/aborting our own transaction. if not self._tpc_xid: raise exceptions.ProgrammingError( "tpc_commit/tpc_rollback with no parameter " "must be called in a two-phase transaction") if self.status == consts.STATUS_BEGIN: fallback() elif self.status == consts.STATUS_PREPARED: self._execute_tpc_command(command, self._tpc_xid) else: raise exceptions.InterfaceError( 'unexpected state in tpc_commit/tpc_rollback') self.status = consts.STATUS_READY self._tpc_xid = None
def tpc_begin(self, xid): if not isinstance(xid, Xid): xid = Xid.from_string(xid) if self.status != consts.STATUS_READY: raise exceptions.ProgrammingError( 'tpc_begin must be called outside a transaction') if self._autocommit: raise exceptions.ProgrammingError( "tpc_begin can't be called in autocommit mode") self._begin_transaction() self._tpc_xid = xid
def tpc_prepare(self): if not self._tpc_xid: raise exceptions.ProgrammingError( 'prepare must be called inside a two-phase transaction') self._execute_tpc_command('PREPARE TRANSACTION', self._tpc_xid) self.status = consts.STATUS_PREPARED
def cursor(self, name=None, cursor_factory=None, withhold=False, scrollable=None): if cursor_factory is None: cursor_factory = self.cursor_factory or Cursor cur = cursor_factory(self, name) if not isinstance(cur, Cursor): raise TypeError("cursor factory must be subclass of %s" % '.'.join([Cursor.__module__, Cursor.__name__])) if withhold: cur.withhold = withhold if scrollable is not None: cur.scrollable = scrollable if name and self._async: raise exceptions.ProgrammingError( "asynchronous connections cannot produce named cursors") cur._mark = self._mark return cur
def __init__(self, conn=None, oid=0, mode='', new_oid=0, new_file=None): self._conn = conn self._oid = oid self._mode = self._parse_mode(mode) self._smode = mode self._new_oid = new_oid self._new_file = new_file self._fd = -1 self._mark = conn._mark if conn.autocommit: raise exceptions.ProgrammingError( "can't use a lobject outside of transactions") self._open()
def _execute_green(self, query): """Execute version for green threads""" if self._async_cursor: raise exceptions.ProgrammingError( "a single async query can be executed on the same connection") self._async_cursor = True if not libpq.PQsendQuery(self._pgconn, ascii_to_bytes(query)): self._async_cursor = None return self._async_status = consts.ASYNC_WRITE try: _green_callback(self) except Exception: self.close() raise else: return util.pq_get_last_result(self._pgconn) finally: self._async_cursor = None self._async_status = consts.ASYNC_DONE
def check_async_(self, *args, **kwargs): if self._conn._async: raise exceptions.ProgrammingError( '%s cannot be used in asynchronous mode' % func.__name__) return func(self, *args, **kwargs)
def check_tpc_(self, *args, **kwargs): if self._tpc_xid: raise exceptions.ProgrammingError( '%s cannot be used during a two-phase transaction' % func.__name__) return func(self, *args, **kwargs)
def check_notrans_(self, *args, **kwargs): if self.status != consts.STATUS_READY: raise exceptions.ProgrammingError('not valid in transaction') return func(self, *args, **kwargs)
def check_unmarked_(self, *args, **kwargs): if self._mark != self._conn._mark: raise exceptions.ProgrammingError("lobject isn't valid anymore") return func(self, *args, **kwargs)