Beispiel #1
0
    def copy(self, sql, data, **kwargs):

        if self.closed():
            raise errors.Error('Cursor is closed')

        self.flush_to_query_ready()

        self.connection.write(messages.Query(sql))

        while True:
            message = self.connection.read_message()

            if isinstance(message, messages.ErrorResponse):
                raise errors.QueryError.from_error_response(message, sql)

            self.connection.process_message(message=message)
            if isinstance(message, messages.ReadyForQuery):
                break
            elif isinstance(message, messages.CopyInResponse):

                #write stuff
                if not hasattr(data, "read"):
                    self.connection.write(messages.CopyData(data))
                else:
                    # treat data as stream
                    self.connection.write(messages.CopyStream(data, **kwargs))

                self.connection.write(messages.CopyDone())

        if self.error is not None:
            raise self.error
Beispiel #2
0
    def execute(self, operation, parameters=None):

        if self.closed():
            raise errors.Error('Cursor is closed')

        if parameters:
            # optional requirement
            from psycopg2.extensions import adapt

            if isinstance(parameters, dict):
                for key in parameters:
                    v = adapt(parameters[key]).getquoted()
                    operation = operation.replace(':' + key, v)
            elif isinstance(parameters, tuple):
                operation = operation % tuple(
                    adapt(p).getquoted() for p in parameters)
            else:
                raise errors.Error(
                    "Argument 'parameters' must be dict or tuple")

        self.rowcount = 0
        self.buffered_rows = collections.deque()
        self.last_execution = operation
        self.connection.write(messages.Query(operation))

        self.fetch_rows()

        if self.error is not None:
            raise self.error
Beispiel #3
0
    def execute(self, operation, parameters=None):
        if self.closed():
            raise errors.Error('Cursor is closed')

        self.flush_to_query_ready()

        if parameters:
            # # optional requirement
            import six
            from psycopg2.extensions import adapt

            if isinstance(parameters, dict):
                for key in parameters:
                    param = parameters[key]
                    # Make sure adapt() behaves properly
                    if self.is_stringy(param) and six.PY2:
                        param = param.encode('utf8')
                    v = adapt(param).getquoted()

                    # Using a regex with word boundary to correctly handle params with similar names
                    # such as :s and :start
                    match_str = u':%s\\b' % str(key)
                    operation = re.sub(match_str,
                                       v.decode('utf-8'),
                                       operation,
                                       flags=re.UNICODE)
            elif isinstance(parameters, tuple):
                tlist = []
                for param in parameters:
                    if self.is_stringy(param) and six.PY2:
                        param = param.encode('utf8')
                    v = adapt(param).getquoted()
                    tlist.append(v.decode('utf-8'))

                operation = operation % tuple(tlist)
            else:
                raise errors.Error(
                    "Argument 'parameters' must be dict or tuple")

        self.rowcount = -1

        self.connection.write(messages.Query(operation))

        # read messages until we hit an Error, DataRow or ReadyForQuery
        while True:
            message = self.connection.read_message()
            # save the message because there's no way to undo the read
            self._message = message
            if isinstance(message, messages.ErrorResponse):
                raise errors.QueryError.from_error_response(message, operation)
            elif isinstance(message, messages.RowDescription):
                self.description = list(
                    map(lambda fd: Column(fd, self.unicode_error),
                        message.fields))
            elif isinstance(message, messages.DataRow):
                break
            elif isinstance(message, messages.ReadyForQuery):
                break
            else:
                self.connection.process_message(message)
Beispiel #4
0
    def _copy_internal(self, sql, datagen):
        if self.closed():
            raise errors.Error('Cursor is closed')

        self.connection.write(messages.Query(sql))

        while True:
            message = self.connection.read_message()
            if isinstance(message, messages.ErrorResponse):
                raise errors.QueryError.from_error_response(message, sql)
            elif isinstance(message, messages.ReadyForQuery):
                break
            elif isinstance(message, messages.CopyInResponse):
                # write stuff
                for line in datagen:
                    self.connection.write(messages.CopyData(line))
                self.connection.write(messages.CopyDone())
Beispiel #5
0
    def copy(self, sql, data):

        if self.closed():
            raise errors.Error('Cursor is closed')

        self.connection.write(messages.Query(sql))

        while True:
            message = self.connection.read_message()
            self._process_message(message=message)
            if isinstance(message, messages.ReadyForQuery):
                break
            elif isinstance(message, messages.CopyInResponse):
                #write stuff
                self.connection.write(messages.CopyData(data))
                self.connection.write(messages.CopyDone())

        if self.error is not None:
            raise self.error
Beispiel #6
0
    def execute(self, operation, parameters=None):

        if self.closed():
            raise errors.Error('Cursor is closed')

        if parameters:
            # optional requirement
            from psycopg2.extensions import adapt
            for key in parameters:
                v = adapt(parameters[key]).getquoted()
                operation = operation.replace(':' + key, v)

        self.rowcount = 0
        self.buffered_rows = collections.deque()
        self.last_execution = operation
        self.connection.write(messages.Query(operation))

        self.fetch_rows()

        if self.error is not None:
            raise self.error