Exemplo n.º 1
0
    def quote(self):
        to_length = libpq.c_uint()

        if self.connection:
            data_pointer = libpq.PQescapeByteaConn(self.connection._pgconn,
                str(self.obj), len(self.obj), libpq.pointer(to_length))
            template = r"E'%s'::bytea"
        else:
            data_pointer = libpq.PQescapeBytea(self.obj, len(self.obj),
                libpq.pointer(to_length))
            template = r"'%s'::bytea"

        data = data_pointer[:to_length.value - 1]
        libpq.PQfreemem(data_pointer)
        return template % data
Exemplo n.º 2
0
def cast_binary(value, length, cursor):
    to_length = libpq.c_uint()
    s = libpq.PQunescapeBytea(value, libpq.pointer(to_length))
    try:
        res = buffer(s[:to_length.value])
    finally:
        libpq.PQfreemem(s)
    return res