Ejemplo n.º 1
0
    def _build_row(self):
        row_num = self._rownumber

        # Create the row
        if self.row_factory:
            row = self.row_factory(self)
            is_tuple = False
        else:
            row = [None] * self._nfields
            is_tuple = True

        # Fill it
        n = self._nfields
        for i in xrange(n):
            if libpq.PQgetisnull(self._pgres, row_num, i):
                row[i] = None
            else:
                fast_parser = self._fast_parsers[i]
                if fast_parser:
                    row[i] = fast_parser(self._pgres, row_num, i)
                else:
                    length = libpq.PQgetlength(self._pgres, row_num, i)
                    val = ffi.buffer(libpq.PQgetvalue(
                        self._pgres, row_num, i), length)[:]
                    row[i] = typecasts.typecast(
                            self._casts[i], val, length, self)

        self._rownumber += 1

        if is_tuple:
            return tuple(row)
        return row
Ejemplo n.º 2
0
    def _build_row(self):
        row_num = self._rownumber

        # Create the row
        if self.row_factory:
            row = self.row_factory(self)
            is_tuple = False
        else:
            row = [None] * self._nfields
            is_tuple = True

        # Fill it
        n = self._nfields
        for i in xrange(n):
            if libpq.PQgetisnull(self._pgres, row_num, i):
                row[i] = None
            else:
                fast_parser = self._fast_parsers[i]
                if fast_parser:
                    row[i] = fast_parser(self._pgres, row_num, i)
                else:
                    length = libpq.PQgetlength(self._pgres, row_num, i)
                    val = ffi.buffer(libpq.PQgetvalue(self._pgres, row_num, i),
                                     length)[:]
                    row[i] = typecasts.typecast(self._casts[i], val, length,
                                                self)

        self._rownumber += 1

        if is_tuple:
            return tuple(row)
        return row
Ejemplo n.º 3
0
    def _build_row(self, row_num):

        # Create the row
        if self.row_factory:
            row = self.row_factory(self)
            is_tuple = False
        else:
            row = [None] * self._nfields
            is_tuple = True

        # Fill it
        n = self._nfields
        for i in xrange(n):

            # PQgetvalue will return an empty string for null values,
            # so check with PQgetisnull if the value is really null
            length = libpq.PQgetlength(self._pgres, row_num, i)
            val = ffi.buffer(libpq.PQgetvalue(self._pgres, row_num, i),
                    length)[:]
            if not val and libpq.PQgetisnull(self._pgres, row_num, i):
                val = None
            else:
                caster = self._casts[i]
                val = typecasts.typecast(caster, val, length, self)
            row[i] = val

        if is_tuple:
            return tuple(row)
        return row
Ejemplo n.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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)