def getquoted(self): obj = self._wrapped if isinstance(self._wrapped, unicode): encoding = encodings[self.encoding] obj = obj.encode(encoding) string = str(obj) length = len(string) if not self._conn: to = libpq.create_string_buffer('\0', (length * 2) + 1) libpq.PQescapeString(to, string, length) return "'%s'" % to.value if PG_VERSION < 0x090000: to = libpq.create_string_buffer('\0', (length * 2) + 1) err = libpq.c_int() libpq.PQescapeStringConn(self._conn._pgconn, to, string, length, err) if self._conn and self._conn._equote: return "E'%s'" % to.value return "'%s'" % to.value data_pointer = libpq.PQescapeLiteral(self._conn._pgconn, string, length) data = libpq.cast(data_pointer, libpq.c_char_p).value libpq.PQfreemem(data_pointer) return data
def parse_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
def _process_notifies(self): while True: pg_notify = libpq.PQnotifies(self._pgconn) if not pg_notify: break notify = Notify(pg_notify.contents.be_pid, pg_notify.contents.relname, pg_notify.contents.extra) self._notifies.append(notify) libpq.PQfreemem(pg_notify)
def getquoted(self): if self._wrapped is None: return 'NULL' to_length = libpq.c_uint() if self._conn: data_pointer = libpq.PQescapeByteaConn(self._conn._pgconn, str(self._wrapped), len(self._wrapped), libpq.pointer(to_length)) else: data_pointer = libpq.PQescapeBytea(self._wrapped, len(self._wrapped), libpq.pointer(to_length)) data = data_pointer[:to_length.value - 1] libpq.PQfreemem(data_pointer) if self._conn and self._conn._equote: return r"E'%s'::bytea" % data return r"'%s'::bytea" % data
def _pq_fetch_copy_out(self): is_text = isinstance(self._copyfile, TextIOBase) pgconn = self._conn._pgconn while True: buf = libpq.pointer(libpq.c_char_p()) length = libpq.PQgetCopyData(pgconn, buf, 0) if length > 0: value = buf.contents.value if is_text: value = typecasts.parse_unicode(value, length, self) libpq.PQfreemem(buf.contents) if value is None: return self._copyfile.write(value) elif length == -2: raise self._conn._create_exception() else: break self._clear_pgres() util.pq_clear_async(pgconn)