示例#1
0
文件: db.py 项目: coderbone/SickRage
    def action(self, query, *args, **kwargs):
        """
        Execute single query

        :param query: Query string
        :return: query results
        """

        with self.lock:
            if not query:
                return

            sqlResult = None
            attempt = 0

            try:
                while attempt < 5:
                    try:
                        logging.db("{}: {} with args {} and kwargs {}".format(self.filename, query, args, kwargs))
                        sqlResult = self.execute(query, *args, **kwargs)
                        raise StopIteration
                    except (sqlite3.OperationalError, sqlite3.DatabaseError) as e:
                        logging.error("DB error: {}".format(ex(e)))
                        attempt += 1
                        time.sleep(1)
            except StopIteration:pass
            finally:self.commit()

            return sqlResult
示例#2
0
文件: db.py 项目: coderbone/SickRage
    def mass_action(self, querylist, *args, **kwargs):
        """
        Execute multiple queries

        :param querylist: list of queries
        :return: list of results
        """

        with self.lock:
            sqlResult = []
            attempt = 0

            try:
                while attempt < 5:
                    try:
                        sqlResult += [
                            self.execute(x[0], *x[1] if len(x) > 1 else self.execute(x[0], **kwargs), **kwargs)
                            for x in filter(lambda q: q is not None, querylist)
                            ]

                        logging.db("Transaction {} of {} queries executed of ".format(len(sqlResult), len(querylist)))
                        raise StopIteration
                    except (sqlite3.OperationalError, sqlite3.DatabaseError) as e:
                        logging.error("DB error: {}".format(ex(e)))
                        attempt += 1
                        sqlResult = []
                        time.sleep(1)
                        self.connection.rollback()
            except StopIteration:pass
            finally:self.commit()

            return sqlResult
示例#3
0
    def action(self, query, *args, **kwargs):
        """
        Execute single query

        :param query: Query string
        :return: query results
        """

        with self.lock:
            if not query:
                return

            sqlResult = None
            attempt = 0

            try:
                while attempt < 5:
                    try:
                        logging.db("{}: {} with args {} and kwargs {}".format(
                            self.filename, query, args, kwargs))
                        sqlResult = self.execute(query, *args, **kwargs)
                        raise StopIteration
                    except (sqlite3.OperationalError,
                            sqlite3.DatabaseError) as e:
                        logging.error("DB error: {}".format(ex(e)))
                        attempt += 1
                        time.sleep(1)
            except StopIteration:
                pass
            finally:
                self.commit()

            return sqlResult
示例#4
0
文件: db.py 项目: cisval/SickRage
    def action(self, query, args=None, fetchall=False, fetchone=False):
        """
        Execute single query

        :param query: Query string
        :param args: Arguments to query string
        :param fetchall: Boolean to indicate all results must be fetched
        :param fetchone: Boolean to indicate one result must be fetched (to walk results for instance)
        :return: query results
        """

        with self.lock:
            if not query:
                return

            sqlResult = None
            attempt = 0

            while attempt < 5:
                try:
                    if not args:
                        logging.db(self.filename + ": " + query)
                    else:
                        logging.db(self.filename + ": " + query +
                                   " with args " + str(args))

                    sqlResult = self.execute(query,
                                             args,
                                             fetchall=fetchall,
                                             fetchone=fetchone)
                except sqlite3.OperationalError as e:
                    if "unable to open database file" in e.args[
                            0] or "database is locked" in e.args[0]:
                        logging.warning("DB error: {}".format(ex(e)))
                        attempt += 1
                        time.sleep(1)
                    else:
                        logging.error("DB error: {}".format(ex(e)))
                        raise
                except sqlite3.DatabaseError as e:
                    logging.error("Fatal error executing query: {}".format(
                        ex(e)))
                    raise
                except Exception as e:
                    pass
                finally:
                    self.commit()
                    break

            return sqlResult
示例#5
0
文件: db.py 项目: Sinap/SickRage
    def action(self, query, args=None, fetchall=False, fetchone=False):
        """
        Execute single query

        :param query: Query string
        :param args: Arguments to query string
        :param fetchall: Boolean to indicate all results must be fetched
        :param fetchone: Boolean to indicate one result must be fetched (to walk results for instance)
        :return: query results
        """

        with self.lock:
            if not query:
                return

            sqlResult = None
            attempt = 0

            while attempt < 5:
                try:
                    if not args:
                        logging.db(self.filename + ": " + query)
                    else:
                        logging.db(self.filename + ": " + query + " with args " + str(args))

                    sqlResult = self.execute(query, args, fetchall=fetchall, fetchone=fetchone)
                except sqlite3.OperationalError as e:
                    if "unable to open database file" in e.args[0] or "database is locked" in e.args[0]:
                        logging.warning("DB error: {}".format(ex(e)))
                        attempt += 1
                        time.sleep(1)
                    else:
                        logging.error("DB error: {}".format(ex(e)))
                        raise
                except sqlite3.DatabaseError as e:
                    logging.error("Fatal error executing query: {}".format(ex(e)))
                    raise
                finally:
                    self.commit()
                    break

            return sqlResult
示例#6
0
    def mass_action(self, querylist, *args, **kwargs):
        """
        Execute multiple queries

        :param querylist: list of queries
        :return: list of results
        """

        with self.lock:
            sqlResult = []
            attempt = 0

            try:
                while attempt < 5:
                    try:
                        sqlResult += [
                            self.execute(
                                x[0], *x[1] if len(x) > 1 else self.execute(
                                    x[0], **kwargs), **kwargs)
                            for x in filter(lambda q: q is not None, querylist)
                        ]

                        logging.db(
                            "Transaction {} of {} queries executed of ".format(
                                len(sqlResult), len(querylist)))
                        raise StopIteration
                    except (sqlite3.OperationalError,
                            sqlite3.DatabaseError) as e:
                        logging.error("DB error: {}".format(ex(e)))
                        attempt += 1
                        sqlResult = []
                        time.sleep(1)
                        self.connection.rollback()
            except StopIteration:
                pass
            finally:
                self.commit()

            return sqlResult