Beispiel #1
0
def gml2sql(gml, field):
    """Converts GML into a psycopg SQL query"""
    gml = gml.replace('<gml:', '<')
    gml = gml.replace('</gml:', '</')
    gml = QuotedString(gml)
    gml = gml.getquoted()
    if field._type.startswith('multi'):
        # Enforce multi* type
        sql = 'ST_Multi(ST_GeomFromGML(%s))' % gml
    else:
        sql = 'ST_GeomFromGML(%s)' % gml
    gml = AsIs(sql)
    return gml
Beispiel #2
0
def escape(s):
    qs = QuotedString(s)
    if conn:
        qs.prepare(conn)
    return qs.getquoted()[1:-1]
Beispiel #3
0
def quote_param(value, dialect='psql'):
    #print(str(value)[0:70], type(value))

    if value is None:
        return "NULL"

    if isinstance(value, bytes):
        return "decode('%s', 'hex')::bytea" % binascii.hexlify(value).decode('ascii')

    if isinstance(value, memoryview):
        return "decode('%s', 'hex')::bytea" % binascii.hexlify(bytes(value)).decode('ascii')

    if isinstance(value, int) or isinstance(value, long):
        return str(value)

    if isinstance(value, float):
        return str(value)

    if isinstance(value, Decimal):
        return str(value)

    if isinstance(value, text):
        #value = value.replace(':',"\:")
        value = value.replace('%','%%')
        value = value.replace('\x00',' ')
        sql_string_value = SqlString(value)
        sql_string_value.encoding = 'utf-8'
        return sql_string_value.getquoted().decode("utf-8")

    if isinstance(value, str):
        #value = value.replace(':',"\:")
        value = value.replace('%','%%')
        value = value.replace('\x00',' ')
        sql_string_value = SqlString(value)
        sql_string_value.encoding = 'utf-8'
        return sql_string_value.getquoted().decode("utf-8")

    if isinstance(value, datetime):
        if dialect == 'oracle':
            return "timestamp '%s'" % value.isoformat(' ').split('.')[0]
        else:
            return "'%s'" % value.isoformat(' ')

    if isinstance(value, date):
        return "'%s'" % value.isoformat()

    if isinstance(value, dict):
        sql_string_value = SqlString(json.dumps(value))
        sql_string_value.encoding = 'utf-8'
        value = sql_string_value.getquoted().decode("utf-8")
        value = value.replace('%','%%')
        return value

    if isinstance(value, set):
        quote_func = lambda p: quote_param(p, dialect)
        return "(" + ','.join(map(quote_func, value)) + ")"

    if isinstance(value, tuple):
        quote_func = lambda p: quote_param(p, dialect)
        return "(" + ','.join(map(quote_func, value)) + ")"


    if isinstance(value, list):
        quote_func = lambda p: quote_param(p, dialect)

        try:
            return "(" + ','.join(map(quote_func, value)) + ")"
        except Exception as e:
            print(e)
            raise ValueError(value)

    raise ValueError("unhandled type: %s, %s" % (type(value), value))
Beispiel #4
0
def escape(s):
    qs = QuotedString(s)
    if conn:
        qs.prepare(conn)
    return qs.getquoted()[1:-1]
Beispiel #5
0
def quote_param(value, dialect='psql'):
    #print(str(value)[0:70], type(value))

    if value is None:
        return "NULL"

    if isinstance(value, bytes):
        return "decode('%s', 'hex')::bytea" % binascii.hexlify(value).decode('ascii')

    if isinstance(value, memoryview):
        return "decode('%s', 'hex')::bytea" % binascii.hexlify(bytes(value)).decode('ascii')

    if isinstance(value, int) or isinstance(value, long):
        return str(value)

    if isinstance(value, float):
        return str(value)

    if isinstance(value, Decimal):
        return str(value)

    if isinstance(value, text):
        #value = value.replace(':',"\:")
        value = value.replace('%','%%')
        value = value.replace('\x00',' ')
        sql_string_value = SqlString(value)
        sql_string_value.encoding = 'utf-8'
        return sql_string_value.getquoted().decode("utf-8")

    if isinstance(value, str):
        #value = value.replace(':',"\:")
        value = value.replace('%','%%')
        value = value.replace('\x00',' ')
        sql_string_value = SqlString(value)
        sql_string_value.encoding = 'utf-8'
        return sql_string_value.getquoted().decode("utf-8")

    if isinstance(value, datetime):
        if dialect == 'oracle':
            return "timestamp '%s'" % value.isoformat(' ').split('.')[0]
        else:
            return "'%s'" % value.isoformat(' ')

    if isinstance(value, date):
        return "'%s'" % value.isoformat()

    if isinstance(value, time):
        return "'%s'" % str(value)

    if isinstance(value, dict):
        sql_string_value = SqlString(json.dumps(value))
        sql_string_value.encoding = 'utf-8'
        value = sql_string_value.getquoted().decode("utf-8")
        value = value.replace('%','%%')
        return value

    if isinstance(value, set):
        quote_func = lambda p: quote_param(p, dialect)
        return "(" + ','.join(map(quote_func, value)) + ")"

    if isinstance(value, tuple):
        quote_func = lambda p: quote_param(p, dialect)
        return "(" + ','.join(map(quote_func, value)) + ")"


    if isinstance(value, list):
        quote_func = lambda p: quote_param(p, dialect)

        try:
            return "(" + ','.join(map(quote_func, value)) + ")"
        except Exception as e:
            print(e)
            raise ValueError(value)

    raise ValueError("unhandled type: %s, %s" % (type(value), value))
Beispiel #6
0
 def check_escape(self, string):
     string = str(string)
     if string.startswith("'") and string.endswith("'"):
         return string
     string = QuotedString(string)
     return string.getquoted()