示例#1
0
    def _exec_cmd(self, curs, sql, args, quiet = False, prefix = None):
        """Internal tool: Run SQL on cursor."""
        if self.options.verbose:
            self.log.debug("exec_cmd: %s" % skytools.quote_statement(sql, args))

        _pfx = ""
        if prefix:
            _pfx = "[%s] " % prefix
        curs.execute(sql, args)
        ok = True
        rows = curs.fetchall()
        for row in rows:
            try:
                code = row['ret_code']
                msg = row['ret_note']
            except KeyError:
                self.log.error("Query does not conform to exec_cmd API:")
                self.log.error("SQL: %s" % skytools.quote_statement(sql, args))
                self.log.error("Row: %s" % repr(row.copy()))
                sys.exit(1)
            level = code / 100
            if level == 1:
                self.log.debug("%s%d %s" % (_pfx, code, msg))
            elif level == 2:
                if quiet:
                    self.log.debug("%s%d %s" % (_pfx, code, msg))
                else:
                    self.log.info("%s%s" % (_pfx, msg,))
            elif level == 3:
                self.log.warning("%s%s" % (_pfx, msg,))
            else:
                self.log.error("%s%s" % (_pfx, msg,))
                self.log.debug("Query was: %s" % skytools.quote_statement(sql, args))
                ok = False
        return (ok, rows)
示例#2
0
    def _exec_cmd(self, curs, sql, args, quiet = False, prefix = None):
        """Internal tool: Run SQL on cursor."""
        if self.options.verbose:
            self.log.debug("exec_cmd: %s" % skytools.quote_statement(sql, args))

        _pfx = ""
        if prefix:
            _pfx = "[%s] " % prefix
        curs.execute(sql, args)
        ok = True
        rows = curs.fetchall()
        for row in rows:
            try:
                code = row['ret_code']
                msg = row['ret_note']
            except KeyError:
                self.log.error("Query does not conform to exec_cmd API:")
                self.log.error("SQL: %s" % skytools.quote_statement(sql, args))
                self.log.error("Row: %s" % repr(row.copy()))
                sys.exit(1)
            level = code / 100
            if level == 1:
                self.log.debug("%s%d %s" % (_pfx, code, msg))
            elif level == 2:
                if quiet:
                    self.log.debug("%s%d %s" % (_pfx, code, msg))
                else:
                    self.log.info("%s%s" % (_pfx, msg,))
            elif level == 3:
                self.log.warning("%s%s" % (_pfx, msg,))
            else:
                self.log.error("%s%s" % (_pfx, msg,))
                self.log.debug("Query was: %s" % skytools.quote_statement(sql, args))
                ok = False
        return (ok, rows)
示例#3
0
 def node_cmd(self, node_name, sql, args, quiet = False):
     """Execute SQL command on particular node."""
     db = self.get_node_database(node_name)
     if not db:
         self.log.warning("ignoring cmd for dead node '%s': %s" % (
             node_name, skytools.quote_statement(sql, args)))
         return None
     return self.exec_cmd(db, sql, args, quiet = quiet)
示例#4
0
 def exec_query(self, db, sql, args):
     """Run regular query SQL on db."""
     self.log.debug("exec_query: %s" % skytools.quote_statement(sql, args))
     curs = db.cursor()
     curs.execute(sql, args)
     res = curs.fetchall()
     db.commit()
     return res
示例#5
0
 def node_cmd(self, node_name, sql, args, quiet = False):
     """Execute SQL command on particular node."""
     db = self.get_node_database(node_name)
     if not db:
         self.log.warning("ignoring cmd for dead node '%s': %s" % (
             node_name, skytools.quote_statement(sql, args)))
         return None
     return self.exec_cmd(db, sql, args, quiet = quiet)
示例#6
0
 def exec_query(self, db, sql, args):
     """Run regular query SQL on db."""
     self.log.debug("exec_query: %s" % skytools.quote_statement(sql, args))
     curs = db.cursor()
     curs.execute(sql, args)
     res = curs.fetchall()
     db.commit()
     return res
示例#7
0
    def display_table(self, db, desc, sql, args=(), fields=(), fieldfmt=None):
        """Display multirow query as a table."""

        self.log.debug("display_table: %s", skytools.quote_statement(sql, args))
        curs = db.cursor()
        curs.execute(sql, args)
        rows = curs.fetchall()
        db.commit()
        if len(rows) == 0:
            return 0
        if not fieldfmt:
            fieldfmt = {}

        if not fields:
            fields = [f[0] for f in curs.description]

        widths = [15] * len(fields)
        for row in rows:
            for i, k in enumerate(fields):
                rlen = row[k] and len(str(row[k])) or 0
                widths[i] = widths[i] > rlen and widths[i] or rlen
        widths = [w + 2 for w in widths]

        fmt = '%%-%ds' * (len(widths) - 1) + '%%s'
        fmt = fmt % tuple(widths[:-1])
        if desc:
            print(desc)
        print(fmt % tuple(fields))
        print(fmt % tuple(['-' * (w - 2) for w in widths]))
        #print(fmt % tuple(['-'*15] * len(fields)))
        for row in rows:
            vals = []
            for field in fields:
                val = row[field]
                if field in fieldfmt:
                    val = fieldfmt[field](val)
                vals.append(val)
            print(fmt % tuple(vals))
        print('\n')
        return 1
示例#8
0
 def exec_stmt(self, db, sql, args):
     """Run regular non-query SQL on db."""
     self.log.debug("exec_stmt: %s" % skytools.quote_statement(sql, args))
     curs = db.cursor()
     curs.execute(sql, args)
     db.commit()
示例#9
0
 def exec_stmt(self, db, sql, args):
     """Run regular non-query SQL on db."""
     self.log.debug("exec_stmt: %s" % skytools.quote_statement(sql, args))
     curs = db.cursor()
     curs.execute(sql, args)
     db.commit()