Exemple #1
0
    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
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #7
0
    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
Exemple #8
0
    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
Exemple #9
0
    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
Exemple #10
0
    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()
Exemple #11
0
    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()
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
    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)
Exemple #15
0
    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)
Exemple #16
0
    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()
Exemple #17
0
    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()
Exemple #18
0
    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()
Exemple #19
0
    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()
Exemple #20
0
    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()
Exemple #21
0
    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()
Exemple #22
0
    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()
Exemple #23
0
    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
Exemple #24
0
    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", (), {}])
Exemple #25
0
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", (), {}]
Exemple #26
0
    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", (), {}])
Exemple #27
0
    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)
Exemple #28
0
    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)
Exemple #29
0
        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:
Exemple #30
0
        """
        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()