def getquoted(self): obj = self._wrapped if isinstance(self._wrapped, unicode): obj = obj.encode(self.encoding) string = str(obj) length = len(string) if not self._conn: to = ffi.new('char []', ((length * 2) + 1)) libpq.PQescapeString(to, string, length) return "'%s'" % ffi.string(to) if PG_VERSION < 0x090000: to = ffi.new('char []', ((length * 2) + 1)) err = ffi.new('int *') libpq.PQescapeStringConn(self._conn._pgconn, to, string, length, err) if self._conn and self._conn._equote: return "E'%s'" % ffi.string(to) return "'%s'" % ffi.string(to) data_pointer = libpq.PQescapeLiteral(self._conn._pgconn, string, length) data = ffi.string(data_pointer) libpq.PQfreemem(data_pointer) return data
def getquoted(self): if self._wrapped is None: return b'NULL' to_length = ffi.new('size_t *') _wrapped = self._wrapped if isinstance(_wrapped, six.text_type): _wrapped = ascii_to_bytes(_wrapped) elif isinstance(_wrapped, _bytearray_types): _wrapped = six.binary_type(_wrapped) elif not six.PY3 and isinstance(_wrapped, buffer): _wrapped = bytes(_wrapped) _wrapped = ffi.new('unsigned char[]', _wrapped) if self._conn: data_pointer = libpq.PQescapeByteaConn( self._conn._pgconn, _wrapped, len(self._wrapped), to_length) else: data_pointer = libpq.PQescapeBytea( _wrapped, len(self._wrapped), to_length) data = ffi.string(data_pointer)[:to_length[0] - 1] libpq.PQfreemem(data_pointer) if self._conn and self._conn._equote: return b''.join([b"E'", data, b"'::bytea"]) return b''.join([b"'", data, b"'::bytea"])
def getquoted(self): obj = self._wrapped if isinstance(obj, six.text_type): obj = obj.encode(self.encoding) else: assert isinstance(obj, six.binary_type) string = obj length = len(string) to_length = (length * 2) + 1 to = ffi.new('char []', to_length) if not self._conn: libpq.PQescapeString(to, string, length) return b''.join([b"'", ffi.string(to), b"'"]) if PG_VERSION < 0x090000: err = ffi.new('int *') libpq.PQescapeStringConn( self._conn._pgconn, to, string, length, err) if self._conn and self._conn._equote: return b''.join([b"E'", ffi.string(to), b"'"]) return b''.join([b"'", ffi.string(to), b"'"]) data_pointer = libpq.PQescapeLiteral( self._conn._pgconn, string, length) data = ffi.string(data_pointer) libpq.PQfreemem(data_pointer) return data
def parse_binary(value, length, cursor): if value is None: return None to_length = ffi.new('size_t *') s = libpq.PQunescapeBytea(ffi.new('unsigned char[]', value), to_length) try: res = ffi.buffer(s, to_length[0])[:] finally: libpq.PQfreemem(s) return memoryview(res) if six.PY3 else buffer(res)
def parse_binary(value, length, cursor): if value is None: return None to_length = ffi.new('size_t *') s = libpq.PQunescapeBytea(ffi.new('unsigned char[]', str(value)), to_length) try: res = buffer(ffi.buffer(s, to_length[0])[:]) finally: libpq.PQfreemem(s) return res
def _process_notifies(self): while True: pg_notify = libpq.PQnotifies(self._pgconn) if not pg_notify: break notify = Notify(pg_notify.be_pid, ffi.string(pg_notify.relname).decode(self._py_enc), ffi.string(pg_notify.extra).decode(self._py_enc)) self._notifies.append(notify) libpq.PQfreemem(pg_notify)
def getquoted(self): if self._wrapped is None: return 'NULL' to_length = ffi.new('size_t *') _wrapped = ffi.new('unsigned char[]', str(self._wrapped)) if self._conn: data_pointer = libpq.PQescapeByteaConn(self._conn._pgconn, _wrapped, len(self._wrapped), to_length) else: data_pointer = libpq.PQescapeBytea(_wrapped, len(self._wrapped), to_length) data = ffi.string(data_pointer)[:to_length[0] - 1] libpq.PQfreemem(data_pointer) if self._conn and self._conn._equote: return r"E'%s'::bytea" % data return r"'%s'::bytea" % data