Example #1
0
    def poll(self):
        if self.status == consts.STATUS_SETUP:
            self.status = consts.STATUS_CONNECTING
            return consts.POLL_WRITE

        if self.status == consts.STATUS_CONNECTING:
            res = self._poll_connecting()
            if res == consts.POLL_OK and self._async:
                return self._poll_setup_async()
            return res

        if self.status in (consts.STATUS_READY, consts.STATUS_BEGIN,
                           consts.STATUS_PREPARED):
            res = self._poll_query()

            if res == consts.POLL_OK and self._async and self._async_cursor:

                # Get the cursor object from the weakref
                curs = self._async_cursor()
                if curs is None:
                    util.pq_clear_async(self._pgconn)
                    raise exceptions.InterfaceError(
                        "the asynchronous cursor has disappeared")

                libpq.PQclear(curs._pgres)

                curs._pgres = util.pq_get_last_result(self._pgconn)
                try:
                    curs._pq_fetch()
                finally:
                    self._async_cursor = None
            return res

        return consts.POLL_ERROR
Example #2
0
    def _execute_green(self, query):
        """Execute version for green threads"""
        if self._async_cursor:
            raise exceptions.ProgrammingError(
                "a single async query can be executed on the same connection")

        self._async_cursor = True

        if not libpq.PQsendQuery(self._pgconn, query):
            self._async_cursor = None
            return

        self._async_status = consts.ASYNC_WRITE

        try:
            _green_callback(self)
            return util.pq_get_last_result(self._pgconn)
        except:
            util.pq_clear_async(self._pgconn)
            raise
        finally:
            self._async_cursor = None
            self._async_status = consts.ASYNC_DONE
Example #3
0
    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)
Example #4
0
    def _pq_fetch_copy_in(self):
        pgconn = self._conn._pgconn
        size = self._copysize
        error = 0
        while True:
            data = self._copyfile.read(size)
            if isinstance(self._copyfile, TextIOBase):
                data = data.encode(self._conn._py_enc)

            if not data:
                break

            res = libpq.PQputCopyData(pgconn, data, len(data))
            if res <= 0:
                error = 2
                break

        errmsg = None
        if error == 2:
            errmsg = 'error in PQputCopyData() call'

        libpq.PQputCopyEnd(pgconn, errmsg)
        self._clear_pgres()
        util.pq_clear_async(pgconn)
Example #5
0
    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)
Example #6
0
    def _pq_fetch_copy_in(self):
        pgconn = self._conn._pgconn
        size = self._copysize
        error = 0
        while True:
            data = self._copyfile.read(size)
            if isinstance(self._copyfile, TextIOBase):
                data = data.encode(self._conn._py_enc)

            if not data:
                break

            res = libpq.PQputCopyData(pgconn, data, len(data))
            if res <= 0:
                error = 2
                break

        errmsg = None
        if error == 2:
            errmsg = 'error in PQputCopyData() call'

        libpq.PQputCopyEnd(pgconn, errmsg)
        self._clear_pgres()
        util.pq_clear_async(pgconn)