Ejemplo n.º 1
0
    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"])
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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 = unicode_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"])
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    def _pq_fetch_tuples(self):
        with self._conn._lock:
            self._nfields = libpq.PQnfields(self._pgres)
            self._no_tuples = False
            description = []
            casts = []
            fast_parsers = []
            for i in xrange(self._nfields):
                ftype = libpq.PQftype(self._pgres, i)
                fsize = libpq.PQfsize(self._pgres, i)
                fmod = libpq.PQfmod(self._pgres, i)
                if fmod > 0:
                    fmod -= 4   # TODO: sizeof(int)

                if fsize == -1:
                    if ftype == 1700:   # NUMERIC
                        isize = fmod >> 16
                    else:
                        isize = fmod
                else:
                    isize = fsize

                if ftype == 1700:
                    prec = (fmod >> 16) & 0xFFFF
                    scale = fmod & 0xFFFF
                else:
                    prec = scale = None

                casts.append(self._get_cast(ftype))
                description.append(Column(
                    name=ffi.string(libpq.PQfname(self._pgres, i))\
                            .decode(self._conn._py_enc),
                    type_code=ftype,
                    display_size=None,
                    internal_size=isize,
                    precision=prec,
                    scale=scale,
                    null_ok=None,
                ))

                fast_parser = None
                if is_32bits:
                    # disable all fast parsers to avoid portability problems
                    pass
                elif ftype == 21 or ftype == 23:
                    fast_parser = libpq.PQEgetint, ffi.new("int32_t*")
                elif ftype == 20:
                    fast_parser = libpq.PQEgetlong, ffi.new("int64_t*")
                elif ftype == 700:
                    fast_parser = libpq.PQEgetfloat, ffi.new("float*")
                elif ftype == 701:
                    fast_parser = libpq.PQEgetdouble, ffi.new("double*")
                fast_parsers.append(fast_parser)


            self._description = tuple(description)
            self._casts = casts
            self._fast_parsers = fast_parsers
Ejemplo n.º 8
0
def parse_binary(value, length, cursor):
    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
Ejemplo n.º 9
0
    def _pq_fetch_tuples(self):
        with self._conn._lock:
            self._nfields = libpq.PQnfields(self._pgres)
            self._no_tuples = False
            description = []
            casts = []
            fast_parsers = []
            for i in xrange(self._nfields):
                ftype = libpq.PQftype(self._pgres, i)
                fsize = libpq.PQfsize(self._pgres, i)
                fmod = libpq.PQfmod(self._pgres, i)
                if fmod > 0:
                    fmod -= 4  # TODO: sizeof(int)

                if fsize == -1:
                    if ftype == 1700:  # NUMERIC
                        isize = fmod >> 16
                    else:
                        isize = fmod
                else:
                    isize = fsize

                if ftype == 1700:
                    prec = (fmod >> 16) & 0xFFFF
                    scale = fmod & 0xFFFF
                else:
                    prec = scale = None

                casts.append(self._get_cast(ftype))
                description.append(Column(
                    name=ffi.string(libpq.PQfname(self._pgres, i))\
                            .decode(self._conn._py_enc),
                    type_code=ftype,
                    display_size=None,
                    internal_size=isize,
                    precision=prec,
                    scale=scale,
                    null_ok=None,
                ))

                fast_parser = None
                if is_32bits:
                    # disable all fast parsers to avoid portability problems
                    pass
                elif ftype == 21 or ftype == 23:
                    fast_parser = libpq.PQEgetint, ffi.new("int32_t*")
                elif ftype == 20:
                    fast_parser = libpq.PQEgetlong, ffi.new("int64_t*")
                elif ftype == 700:
                    fast_parser = libpq.PQEgetfloat, ffi.new("float*")
                elif ftype == 701:
                    fast_parser = libpq.PQEgetdouble, ffi.new("double*")
                fast_parsers.append(fast_parser)

            self._description = tuple(description)
            self._casts = casts
            self._fast_parsers = fast_parsers
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    def read(self, size=-1):
        """Read at most size bytes or to the end of the large object."""
        if size < 0:
            where = self.tell()
            end = self.seek(0, os.SEEK_END)
            self.seek(where, os.SEEK_SET)
            size = end - where

        binary_mode = self._mode & consts.LOBJECT_BINARY
        if size == 0:
            return b'' if binary_mode else ''

        buf = ffi.new('char []', size)
        length = libpq.lo_read(self._conn._pgconn, self._fd, buf, size)
        if length < 0:
            return

        return ffi.buffer(buf, length)[:] if binary_mode else \
               ffi.string(buf).decode(self._conn._py_enc)
Ejemplo n.º 16
0
    def read(self, size=-1):
        """Read at most size bytes or to the end of the large object."""
        if size < 0:
            where = self.tell()
            end = self.seek(0, os.SEEK_END)
            self.seek(where, os.SEEK_SET)
            size = end - where

        binary_mode = self._mode & consts.LOBJECT_BINARY
        if size == 0:
            return b'' if binary_mode else ''

        buf = ffi.new('char []', size)
        length = libpq.lo_read(self._conn._pgconn, self._fd, buf, size)
        if length < 0:
            return

        return ffi.buffer(buf, length)[:] if binary_mode else \
               ffi.string(buf).decode(self._conn._py_enc)
Ejemplo n.º 17
0
    def _pq_fetch_copy_out(self):
        is_text = isinstance(self._copyfile, TextIOBase)
        pgconn = self._conn._pgconn
        while True:
            buf = ffi.new('char **')
            length = libpq.PQgetCopyData(pgconn, buf, 0)

            if length > 0:
                if buf[0] == ffi.NULL:
                    return
                value = ffi.buffer(buf[0], length)
                if is_text:
                    value = typecasts.parse_unicode(value[:], length, self)

                self._copyfile.write(value)
            elif length == -2:
                raise self._conn._create_exception(cursor=self)
            else:
                break

        self._clear_pgres()
        util.pq_clear_async(pgconn)
Ejemplo n.º 18
0
    def _pq_fetch_copy_out(self):
        is_text = isinstance(self._copyfile, TextIOBase)
        pgconn = self._conn._pgconn
        while True:
            buf = ffi.new('char **')
            length = libpq.PQgetCopyData(pgconn, buf, 0)

            if length > 0:
                if buf[0] == ffi.NULL:
                    return
                value = ffi.buffer(buf[0], length)
                if is_text:
                    value = typecasts.parse_unicode(value[:], length, self)

                self._copyfile.write(value)
            elif length == -2:
                raise self._conn._create_exception(cursor=self)
            else:
                break

        self._clear_pgres()
        util.pq_clear_async(pgconn)
Ejemplo n.º 19
0
 def cancel(self):
     err_length = 256
     errbuf = ffi.new('char[]', err_length)
     if libpq.PQcancel(self._cancel, errbuf, err_length) == 0:
         raise exceptions.OperationalError(ffi.string(errbuf))
Ejemplo n.º 20
0
 def cancel(self):
     err_length = 256
     errbuf = ffi.new('char[]', err_length)
     if libpq.PQcancel(self._cancel, errbuf, err_length) == 0:
         raise exceptions.OperationalError(ffi.string(errbuf))