Exemplo n.º 1
0
def format_value(column, value):
    if value is None or (type(value) is float and math.isnan(value)):
        return 'null'
    if type(value) is list:
        return '({0})'.format(
            ', '.join([format_value(column, v) for v in value]))
    if type(value) in [datetime.datetime, datetime.date, datetime.time]:
        return "'%s'" % value
    if type(value) is buffer:
        return u"'[BLOB]'"
    if column is None:
        try:
            return '%d' % int(value)
        except ValueError:
            return u"'%s'" % value
    if (isinstance(column.type, Boolean)
            and (type(value) is bool or value in ['true', 'false'])
            or type(value) is bool):
        return '%s' % str(value).lower()
    if isinstance(column.type, Float) or type(value) is float:
        try:
            return '%f' % float(value)
        except ValueError:
            pass
    if isinstance(column.type, Integer) or type(value) in (int, Decimal):
        try:
            return '%d' % int(value)
        except ValueError:
            pass
    return u"'%s'" % escape_statement(value).replace("'", "''")
Exemplo n.º 2
0
    def execute(self, query):
        cur = self.cursor()
        if not cur:
            raise Exception("Database is not connected")

        timer = LogTimer(logger, "Execution", "Executing:\n%s", query)
        try:
            return cur.execute(escape_statement(query))
        finally:
            timer.stop()