def finish(self): """This can only be called when the ``finish``` signal has been emitted. :returns: the result, which might be an object or a list depending on self.operation_type """ trace("connection_raw_execute_success", self._conn, self._async_cursor, self._statement, self._parameters) result = self._conn.result_factory(self._conn, self._async_cursor) if self.operation_type == AsyncQueryOperation.GET_ALL: # ResultSet.__iter__() retval = [] for values in result: obj = self.resultset._load_objects(result, values) retval.append(obj) elif self.operation_type == AsyncQueryOperation.GET_ONE: # ResultSet.one() values = result.get_one() retval = self.resultset._load_objects(result, values) else: raise NotImplementedError(self.operation_type) return retval
def execute(self, async_conn): """Executes a query within an asyncronous psycopg2 connection """ if self.status == self.STATUS_CANCELLED: return self.status = self.STATUS_EXECUTING # Async variant of Connection.execute() in storm/database.py state = State() statement = compile(self.expr, state) stmt = convert_param_marks(statement, "?", "%s") self._async_cursor = async_conn.cursor() self._async_conn = async_conn # This is postgres specific, see storm/databases/postgres.py self._statement = stmt.encode('utf-8') self._parameters = tuple(Connection.to_database(state.parameters)) trace("connection_raw_execute", self._conn, self._async_cursor, self._statement, self._parameters) self._async_cursor.execute(self._statement, self._parameters) # This can happen if another thread cancelled this while the cursor was # executing. In that case, it is not interested in the retval anymore if self.status == self.STATUS_CANCELLED: return self.status = self.STATUS_FINISHED glib.idle_add(self._on_finish)
def execute(self, async_conn): """Executes a query within an asyncronous psycopg2 connection """ if self.status == self.STATUS_CANCELLED: return self.status = self.STATUS_EXECUTING # Async variant of Connection.execute() in storm/database.py state = State() statement = compile(self.expr, state) stmt = convert_param_marks(statement, "?", "%s") self._async_cursor = async_conn.cursor() self._async_conn = async_conn # This is postgres specific, see storm/databases/postgres.py self._statement = stmt.encode('utf-8') self._parameters = tuple(Connection.to_database(state.parameters)) trace("connection_raw_execute", self._conn, self._async_cursor, self._statement, self._parameters) self._async_cursor.execute(self._statement, self._parameters) # This can happen if another thread cancelled this while the cursor was # executing. In that case, it is not interested in the retval anymore if self.status == self.STATUS_CANCELLED: return self.status = self.STATUS_FINISHED GLib.idle_add(self._on_finish)
def commit(self, close=False): """Commits a database. This needs to be done to submit the actually inserts to the database. :param close: If ``True``, the store will also be closed after committed. """ self._check_obsolete() self._committing = True # the cache will be cleared when commiting, so store them here # and autoreload them after commit touched_objs = [] for obj_info in self._cache.get_cached(): obj = obj_info.get_obj() if obj is not None: touched_objs.append(obj) super(StoqlibStore, self).commit() trace('transaction_commit', self) self._savepoints = [] self._dirties = [[]] # Reload objects on all other opened stores for obj in touched_objs: autoreload_object(obj) if close: self.close() self._committing = False
def commit(self, close=False): """Commits a database. This needs to be done to submit the actually inserts to the database. :param close: If ``True``, the store will also be closed after committed. """ self._check_obsolete() self._committing = True # the cache will be cleared when commiting, so store them here # and autoreload them after commit touched_objs = [] for obj_info in self._cache.get_cached(): obj = obj_info.get_obj() if obj is not None: touched_objs.append(obj) super(StoqlibStore, self).commit() trace('transaction_commit', self) self._pending_count = [0] self._savepoints = [] # Reload objects on all other opened stores for obj in touched_objs: autoreload_object(obj) if close: self.close() self._committing = False
def close(self): """Close the store. Closes the socket that represents that database connection, this needs to be called when you finished using the store. """ trace('transaction_close', self) self._check_obsolete() super(StoqlibStore, self).close() self.obsolete = True
def close(self): """Close the store. Closes the socket that represents that database connection, this needs to be called when you finished using the store. """ trace("transaction_close", self) self._check_obsolete() super(StoqlibStore, self).close() self.obsolete = True
def raw_execute(self, statement, params): """NOTE: this is being overriden completely because the original from the base class converts params to a tuple, and we need a dictionary! Execute a raw statement with the given parameters. It's acceptable to override this method in subclasses, but it is not intended to be called externally. If the global C{DEBUG} is True, the statement will be printed to standard out. @return: The dbapi cursor object, as fetched from L{build_raw_cursor}. """ rowid = None raw_cursor = self.build_raw_cursor() # newer cx_Oracle (>= 4.4) seems to really want str() instead # of unicode() statement = str(statement) if statement.startswith('INSERT INTO'): statement = statement + ' RETURNING ROWID INTO :out_rowid' # make sure params is a list; if it is a tuple we're screwed =) if params is None: params = list() elif not isinstance(params, list): params = list(params) rowid = raw_cursor.var(oracle.ROWID) params.append(rowid) if params: params = self.to_database(params) args = (statement, params) else: args = (statement, ) user_tracer_data = {} trace("connection_raw_execute", self, raw_cursor, statement, params or (), user_tracer_data) try: self._check_disconnect(raw_cursor.execute, *args) if rowid: rowid = rowid.getvalue() except Exception, error: trace("connection_raw_execute_error", self, raw_cursor, statement, params or (), user_tracer_data, error) raise
def commit(self, close=False): """Commits a database. This needs to be done to submit the actually inserts to the database. :param close: If ``True``, the store will also be closed after committed. """ self._check_obsolete() self._process_pending_objs() super(StoqlibStore, self).commit() trace("transaction_commit", self) if close: self.close()
def commit(self, close=False): """Commits a database. This needs to be done to submit the actually inserts to the database. :param close: If ``True``, the store will also be closed after committed. """ self._check_obsolete() self._process_pending_objs() super(StoqlibStore, self).commit() trace('transaction_commit', self) if close: self.close()
def get_result(self): """Get operation result. Note that this can only be called when the *finish* signal has been emitted. :returns: a :class:`AsyncResultSet` containing the result """ assert self.status == self.STATUS_FINISHED trace("connection_raw_execute_success", self._conn, self._async_cursor, self._statement, self._parameters) result = self._conn.result_factory(self._conn, self._async_cursor) return AsyncResultSet(self.resultset, result)
def execute(self, async_conn): """Executes a query within an asyncronous psycopg2 connection """ # Async variant of Connection.execute() in storm/database.py state = State() statement = compile(self.expr, state) stmt = convert_param_marks(statement, "?", "%s") self._async_cursor = async_conn.cursor() self._async_conn = async_conn # This is postgres specific, see storm/databases/postgres.py self._statement = stmt.encode("utf-8") self._parameters = tuple(Connection.to_database(state.parameters)) trace("connection_raw_execute", self._conn, self._async_cursor, self._statement, self._parameters) self._async_cursor.execute(self._statement, self._parameters)
def execute(self, async_conn): """Executes a query within an asyncronous psycopg2 connection """ # Async variant of Connection.execute() in storm/database.py state = State() statement = compile(self.expr, state) stmt = convert_param_marks(statement, "?", "%s") self._async_cursor = async_conn.cursor() self._async_conn = async_conn # This is postgres specific, see storm/databases/postgres.py self._statement = stmt.encode('utf-8') self._parameters = tuple(Connection.to_database(state.parameters)) trace("connection_raw_execute", self._conn, self._async_cursor, self._statement, self._parameters) self._async_cursor.execute(self._statement, self._parameters)
def __init__(self, database=None, cache=None): """ Creates a new store :param database: the database to connect to or ``None`` :param cache: storm cache to use or ``None`` """ self._savepoints = [] self.retval = None self.needs_retval = False self.obsolete = False if database is None: database = get_default_store().get_database() Store.__init__(self, database=database, cache=cache) _stores.add(self) trace('transaction_create', self) self._reset_pending_objs()
def __init__(self, database=None, cache=None): """ Creates a new store :param database: the database to connect to or ``None`` :param cache: storm cache to use or ``None`` """ self._savepoints = [] self.retval = None self.needs_retval = False self.obsolete = False if database is None: database = get_default_store().get_database() Store.__init__(self, database=database, cache=cache) # FIXME: Use weakref.WeakSet when we can depend on Python 2.7 _stores[self] = None trace("transaction_create", self) self._reset_pending_objs()
def __init__(self, database=None, cache=None): """ Creates a new store :param database: the database to connect to or ``None`` :param cache: storm cache to use or ``None`` """ self._committing = False self._savepoints = [] self._pending_count = [0] self.retval = True self.obsolete = False if database is None: database = get_default_store().get_database() Store.__init__(self, database=database, cache=cache) _stores.add(self) trace('transaction_create', self) self._setup_application_name()
def __init__(self, database=None, cache=None): """ Creates a new store :param database: the database to connect to or ``None`` :param cache: storm cache to use or ``None`` """ self._savepoints = [] self.retval = None self.needs_retval = False self.obsolete = False if database is None: database = get_default_store().get_database() Store.__init__(self, database=database, cache=cache) # FIXME: Use weakref.WeakSet when we can depend on Python 2.7 _stores[self] = None trace('transaction_create', self) self._reset_pending_objs()
def __init__(self, database=None, cache=None): """ Creates a new store :param database: the database to connect to or ``None`` :param cache: storm cache to use or ``None`` """ self._committing = False self._savepoints = [] # When using savepoints, this stack will hold what objects were changed # (created, deleted or edited) inside that savepoint. self._dirties = [[]] self.retval = True self.obsolete = False if database is None: database = get_default_store().get_database() Store.__init__(self, database=database, cache=cache) _stores.add(self) trace('transaction_create', self) self._setup_application_name()
def raw_execute(self, statement, params=None): """Execute a raw statement with the given parameters. It's acceptable to override this method in subclasses, but it is not intended to be called externally. If the global C{DEBUG} is True, the statement will be printed to standard out. @return: The dbapi cursor object, as fetched from L{build_raw_cursor}. """ raw_cursor = self.build_raw_cursor() trace("connection_raw_execute", self, raw_cursor, statement, params or ()) if params: args = (statement, tuple(self.to_database(params))) else: args = (statement,) try: self._check_disconnect(raw_cursor.execute, *args) except Exception, error: trace("connection_raw_execute_error", self, raw_cursor, statement, params or (), error) raise
def test_trace(self): stash = [] class Tracer(object): def m1(_, *args, **kwargs): stash.extend(["m1", args, kwargs]) def m2(_, *args, **kwargs): stash.extend(["m2", args, kwargs]) install_tracer(Tracer()) trace("m1", 1, 2, c=3) trace("m2") trace("m3") self.assertEquals(stash, ["m1", (1, 2), {"c": 3}, "m2", (), {}])
def test_trace(cleanup_tracers): stash = [] class Tracer(object): def m1(_, *args, **kwargs): stash.extend(["m1", args, kwargs]) def m2(_, *args, **kwargs): stash.extend(["m2", args, kwargs]) install_tracer(Tracer()) trace("m1", 1, 2, c=3) trace("m2") trace("m3") assert stash == ["m1", (1, 2), {"c": 3}, "m2", (), {}]
def test_capture_multiple(self): """L{CaptureTracer}s can be used as nested context managers.""" conn = StubConnection() def trace(statement): for tracer in get_tracers(): tracer.connection_raw_execute(conn, "cursor", statement, []) with CaptureTracer() as tracer1: trace("one") with CaptureTracer() as tracer2: trace("two") trace("three") self.assertEqual([], get_tracers()) self.assertEqual(["one", "two", "three"], tracer1.queries) self.assertEqual(["two"], tracer2.queries)
user_tracer_data = {} trace("connection_raw_execute", self, raw_cursor, statement, params or (), user_tracer_data) try: self._check_disconnect(raw_cursor.execute, *args) if rowid: rowid = rowid.getvalue() except Exception, error: trace("connection_raw_execute_error", self, raw_cursor, statement, params or (), user_tracer_data, error) raise else: trace("connection_raw_execute_success", self, raw_cursor, statement, params or (), user_tracer_data) return raw_cursor, rowid @staticmethod def to_database(params): """ Like L{Connection.to_database}, but returns a dictionary. params is a list of 1-item dictionaries, which must be merged into one big dictionary here; we also need to take care of stuff such as cx_Oracle not wanting to receive unicode, and converting eventual unicode's to UTF-8 strings """ new_params = [] for param in params:
""" raw_cursor = self.build_raw_cursor() trace("connection_raw_execute", self, raw_cursor, statement, params or ()) if params: args = (statement, tuple(self.to_database(params))) else: args = (statement,) try: self._check_disconnect(raw_cursor.execute, *args) except Exception, error: trace("connection_raw_execute_error", self, raw_cursor, statement, params or (), error) raise else: trace("connection_raw_execute_success", self, raw_cursor, statement, params or ()) return raw_cursor def _ensure_connected(self): """Ensure that we are connected to the database. If the connection is marked as dead, or if we can't reconnect, then raise DisconnectionError. """ if self._state == STATE_CONNECTED: return elif self._state == STATE_DISCONNECTED: raise DisconnectionError("Already disconnected") elif self._state == STATE_RECONNECT: try: self._raw_connection = self._database.raw_connect()