Esempio n. 1
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)
Esempio n. 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)
Esempio n. 3
0
 def raw_execute(self, statement, params):
     """
     Like L{Connection.raw_execute}, but encode the statement to
     UTF-8 if it is unicode.
     """
     if type(statement) is unicode:
         # psycopg breaks with unicode statements.
         statement = statement.encode("UTF-8")
     return Connection.raw_execute(self, statement, params)
Esempio n. 4
0
 def raw_execute(self, statement, params):
     """
     Like L{Connection.raw_execute}, but encode the statement to
     UTF-8 if it is unicode.
     """
     if type(statement) is unicode:
         # psycopg breaks with unicode statements.
         statement = statement.encode("UTF-8")
     return Connection.raw_execute(self, statement, params)
Esempio n. 5
0
    def setUp(self):
        super(TimeoutTracerTestBase, self).setUp()
        self.tracer = self.tracer_class()
        self.raw_cursor = self.mocker.mock()
        self.statement = self.mocker.mock()
        self.params = self.mocker.mock()

        # Some data is kept in the connection, so we use a proxy to
        # allow things we don't care about here to happen.
        class Connection(object):
            pass

        self.connection = self.mocker.proxy(Connection())
Esempio n. 6
0
    def execute(self, statement, params=None, noresult=False):
        if (isinstance(statement, Insert) and
            statement.primary_variables is not Undef):

            result = Connection.execute(self, statement, params)

            # The lastrowid value will be set if:
            #  - the table had an AUTO INCREMENT column, and
            #  - the column was not set during the insert or set to 0
            #
            # If these conditions are met, then lastrowid will be the
            # value of the first such column set.  We assume that it
            # is the first undefined primary key variable.
            if result._raw_cursor.lastrowid:
                for variable in statement.primary_variables:
                    if not variable.is_defined():
                        variable.set(result._raw_cursor.lastrowid,
                                     from_db=True)
                        break
            if noresult:
                result = None
            return result
        return Connection.execute(self, statement, params, noresult)
Esempio n. 7
0
    def execute(self, statement, params=None, noresult=False):
        if (isinstance(statement, Insert) and
            statement.primary_variables is not Undef):

            result = Connection.execute(self, statement, params)

            # The lastrowid value will be set if:
            #  - the table had an AUTO INCREMENT column, and
            #  - the column was not set during the insert or set to 0
            #
            # If these conditions are met, then lastrowid will be the
            # value of the first such column set.  We assume that it
            # is the first undefined primary key variable.
            if result._raw_cursor.lastrowid:
                for variable in statement.primary_variables:
                    if not variable.is_defined():
                        variable.set(result._raw_cursor.lastrowid,
                                     from_db=True)
                        break
            if noresult:
                result = None
            return result
        return Connection.execute(self, statement, params, noresult)
Esempio n. 8
0
    def execute(self, statement, params=None, noresult=False):
        """Execute a statement with the given parameters.

        This extends the L{Connection.execute} method to add support
        for automatic retrieval of inserted primary keys to link
        in-memory objects with their specific rows.
        """
        if (isinstance(statement, Insert) and self._database._version >= 80200
                and statement.primary_variables is not Undef
                and statement.primary_columns is not Undef):

            # Here we decorate the Insert statement with a Returning
            # expression, so that we get back in the result the values
            # for the primary key just inserted.  This prevents a round
            # trip to the database for obtaining these values.

            result = Connection.execute(self, Returning(statement), params)
            for variable, value in zip(statement.primary_variables,
                                       result.get_one()):
                result.set_variable(variable, value)
            return result

        return Connection.execute(self, statement, params, noresult)
Esempio n. 9
0
    def execute(self, statement, params=None, noresult=False):
        """Execute a statement with the given parameters.

        This extends the L{Connection.execute} method to add support
        for automatic retrieval of inserted primary keys to link
        in-memory objects with their specific rows.
        """
        if (isinstance(statement, Insert) and
            self._database._version >= 80200 and
            statement.primary_variables is not Undef and
            statement.primary_columns is not Undef):

            # Here we decorate the Insert statement with a Returning
            # expression, so that we get back in the result the values
            # for the primary key just inserted.  This prevents a round
            # trip to the database for obtaining these values.

            result = Connection.execute(self, Returning(statement), params)
            for variable, value in zip(statement.primary_variables,
                                       result.get_one()):
                result.set_variable(variable, value)
            return result

        return Connection.execute(self, statement, params, noresult)
Esempio n. 10
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)
Esempio n. 11
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)
Esempio n. 12
0
    def raw_execute(self, statement, params=None, _end=False):
        """Execute a raw statement with the given parameters.

        This method will automatically retry on locked database errors.
        This should be done by pysqlite, but it doesn't work with
        versions < 2.3.4, so we make sure the timeout is respected
        here.
        """
        if _end:
            self._in_transaction = False
        elif not self._in_transaction:
            # See story at the end to understand why we do BEGIN manually.
            self._in_transaction = True
            self._raw_connection.execute("BEGIN")

        # Remember the time at which we started the operation.  If pysqlite
        # handles the timeout correctly, we won't retry the operation, because
        # the timeout will have expired when the raw_execute() returns.
        started = now()
        while True:
            try:
                return Connection.raw_execute(self, statement, params)
            except sqlite.OperationalError, e:
                if str(e) != "database is locked":
                    raise
                elif now() - started < self._database._timeout:
                    # pysqlite didn't handle the timeout correctly,
                    # so we sleep a little and then retry.
                    sleep(0.1)
                else:
                    # The operation failed due to being unable to get a
                    # lock on the database.  In this case, we are still
                    # in a transaction.
                    if _end:
                        self._in_transaction = True
                    raise
Esempio n. 13
0
    def raw_execute(self, statement, params=None, _end=False):
        """Execute a raw statement with the given parameters.

        This method will automatically retry on locked database errors.
        This should be done by pysqlite, but it doesn't work with
        versions < 2.3.4, so we make sure the timeout is respected
        here.
        """
        if _end:
            self._in_transaction = False
        elif not self._in_transaction:
            # See story at the end to understand why we do BEGIN manually.
            self._in_transaction = True
            self._raw_connection.execute("BEGIN")

        # Remember the time at which we started the operation.  If pysqlite
        # handles the timeout correctly, we won't retry the operation, because
        # the timeout will have expired when the raw_execute() returns.
        started = now()
        while True:
            try:
                return Connection.raw_execute(self, statement, params)
            except sqlite.OperationalError as e:
                if ustr(e) != "database is locked":
                    raise
                elif now() - started < self._database._timeout:
                    # pysqlite didn't handle the timeout correctly,
                    # so we sleep a little and then retry.
                    sleep(0.1)
                else:
                    # The operation failed due to being unable to get a
                    # lock on the database.  In this case, we are still
                    # in a transaction.
                    if _end:
                        self._in_transaction = True
                    raise
Esempio n. 14
0
    def raw_execute(self, statement, params=None, _started=None):
        """Execute a raw statement with the given parameters.

        This method will automatically retry on locked database errors.
        This should be done by pysqlite, but it doesn't work with
        versions < 2.3.4, so we make sure the timeout is respected
        here.
        """
        if not self._in_transaction:
            # See story at the end to understand why we do BEGIN manually.
            self._in_transaction = True
            self._raw_connection.execute("BEGIN")
        while True:
            try:
                return Connection.raw_execute(self, statement, params)
            except sqlite.OperationalError, e:
                if str(e) != "database is locked":
                    raise
                if _started is None:
                    _started = now()
                elif now() - _started < self._database._timeout:
                    sleep(0.1)
                else:
                    raise
Esempio n. 15
0
    def raw_execute(self, statement, params=None, _started=None):
        """Execute a raw statement with the given parameters.

        This method will automatically retry on locked database errors.
        This should be done by pysqlite, but it doesn't work with
        versions < 2.3.4, so we make sure the timeout is respected
        here.
        """
        if not self._in_transaction:
            # See story at the end to understand why we do BEGIN manually.
            self._in_transaction = True
            self._raw_connection.execute("BEGIN")
        while True:
            try:
                return Connection.raw_execute(self, statement, params)
            except sqlite.OperationalError, e:
                if str(e) != "database is locked":
                    raise
                if _started is None:
                    _started = now()
                elif now() - _started < self._database._timeout:
                    sleep(0.1)
                else:
                    raise