Beispiel #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)
Beispiel #2
0
    def execute(self, statement, params=None, noresult=False):
        """NOTE: this is being overriden completely because the
        original from the base class expects to receive only a
        raw_cursor from raw_execute, and we need to receive also the
        rowid, as we cannot set it in the cursor object

        Execute a statement with the given parameters.

        @type statement: L{Expr} or C{str}
        @param statement: The statement to execute. It will be
            compiled if necessary.
        @param noresult: If True, no result will be returned.

        @raise DisconnectionError: Raised when the connection is lost.
            Reconnection happens automatically on rollback.

        @return: The result of C{self.result_factory}, or None if
            C{noresult} is True.
        """
        if self._closed:
            raise ClosedError("Connection is closed")
        self._ensure_connected()
        if isinstance(statement, Expr):
            if params is not None:
                raise ValueError("Can't pass parameters with expressions")
            state = State()
            statement = self.compile(statement, state)
            params = state.parameters
        statement = convert_param_marks(statement, "?", self.param_mark)
        raw_cursor, rowid = self.raw_execute(statement, params)
        if noresult:
            self._check_disconnect(raw_cursor.close)
            return None
        return self.result_factory(self, raw_cursor, rowid)
Beispiel #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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
0
 def connection_raw_execute(self, connection, raw_cursor,
                            statement, params):
     statement_to_log = statement
     if params:
         # There are some bind parameters so we want to insert them into
         # the sql statement so we can log the statement.
         query_params = list(connection.to_database(params))
         if connection.param_mark == '%s':
             # Double the %'s in the string so that python string formatting
             # can restore them to the correct number. Note that %s needs to
             # be preserved as that is where we are substituting values in.
             quoted_statement = re.sub(
                 "%%%", "%%%%", re.sub("%([^s])", r"%%\1", statement))
         else:
             # Double all the %'s in the statement so that python string
             # formatting can restore them to the correct number. Any %s in
             # the string should be preserved because the param_mark is not
             # %s.
             quoted_statement = re.sub("%", "%%", statement)
             quoted_statement = convert_param_marks(
                 quoted_statement, connection.param_mark, "%s")
         # We need to massage the query parameters a little to deal with
         # string parameters which represent encoded binary data.
         render_params = []
         for param in query_params:
             if isinstance(param, six.text_type):
                 if six.PY3:
                     render_params.append(ascii(param))
                 else:
                     render_params.append(repr(param.encode('utf8')))
             else:
                 render_params.append(repr(param))
         try:
             statement_to_log = quoted_statement % tuple(render_params)
         except TypeError:
             statement_to_log = \
                 "Unformattable query: %r with params %r." % (
                 statement, query_params)
     self._expanded_raw_execute(connection, raw_cursor, statement_to_log)
Beispiel #7
0
 def connection_raw_execute(self, connection, raw_cursor,
                            statement, params):
     statement_to_log = statement
     if params:
         # There are some bind parameters so we want to insert them into
         # the sql statement so we can log the statement.
         query_params = list(connection.to_database(params))
         if connection.param_mark == '%s':
             # Double the %'s in the string so that python string formatting
             # can restore them to the correct number. Note that %s needs to
             # be preserved as that is where we are substituting values in.
             quoted_statement = re.sub(
                 "%%%", "%%%%", re.sub("%([^s])", r"%%\1", statement))
         else:
             # Double all the %'s in the statement so that python string
             # formatting can restore them to the correct number. Any %s in
             # the string should be preserved because the param_mark is not
             # %s.
             quoted_statement = re.sub("%", "%%", statement)
             quoted_statement = convert_param_marks(
                 quoted_statement, connection.param_mark, "%s")
         # We need to massage the query parameters a little to deal with
         # string parameters which represent encoded binary data.
         render_params = []
         for param in query_params:
             if isinstance(param, unicode):
                 render_params.append(repr(param.encode('utf8')))
             else:
                 render_params.append(repr(param))
         try:
             statement_to_log = quoted_statement % tuple(render_params)
         except TypeError:
             statement_to_log = \
                 "Unformattable query: %r with params %r." % (
                 statement, query_params)
     self._expanded_raw_execute(connection, raw_cursor, statement_to_log)