示例#1
0
 def _query(self, query, bindata=None):
     """
     Execute a query on the storage layer (internal method).
     :param query: The query to execute.
     :param bindata: Data to bind to the given query.
     :raise Exception: If the query cannot be evaluated.
     """
     with self._lock:
         cursor = self.db.cursor()
         if bindata is None:
             cursor.execute(query)
         else:
             cursor.execute(query, bindata)
         dbcursor = DBCursor(cursor, self.db)
     return dbcursor
示例#2
0
    def _query(self, query, bindata=None):
        """
        Execute a query on the storage layer (internal method).
        :param query: The query to execute.
        :param bindata: Data to bind to the given query.
        :raise Exception: If the query cannot be evaluated.
        """
        self._lock.acquire()

        try:
            # postgresql doesn't support backticks: the query will be logged with backticks since
            # logging is handled in 'query()' but we will strip them out before executing the query.
            newquery = query.replace('`', '').strip()

            cursor = self.db.cursor()
            if bindata is None:
                cursor.execute(newquery)
            else:
                cursor.execute(newquery, bindata)

            # create our cursor instance
            dbcursor = DBCursor(cursor, self.db)

            if self._reInsert.match(newquery):
                # if this is an insert query try to get the last insert id:
                # this needs a separate query in postgresql: it can be done in a single query
                # adding 'RETURNING id' to the INSERT statement but then we would have added
                # a constraint on the name of the PRIMARY KEY for each table
                try:
                    cursor = self.db.cursor()
                    cursor.execute("SELECT LASTVAL();")
                    dbcursor.lastrowid = cursor.fetchone()[0]
                    dbcursor._cursor.lastrowid = dbcursor.lastrowid
                    cursor.close()
                except Exception as e:
                    # can't recover in any case
                    pass

        finally:
            self._lock.release()

        return dbcursor