How to use the pg8000.protocol.ReceiveMessage function in pg8000

To help you get started, we’ve selected a few pg8000 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
count = struct.unpack("!h", data[:2])[0]
        data = data[2:]
        fields = []
        for i in range(count):
            null = data.find("\x00")
            field = {"name": data[:null]}
            data = data[null + 1:]
            field["table_oid"], field["column_attrnum"], field["type_oid"], \
                field["type_size"], field["type_modifier"], \
                field["format"] = struct.unpack("!ihihih", data[:18])
            data = data[18:]
            fields.append(field)
        return RowDescription(fields)


class CommandComplete(ReceiveMessage):
    def __init__(self, command, rows=None, oid=None):
        self.command = command
        self.rows = rows
        self.oid = oid

    @staticmethod
    def create_from_data(data):
        values = data[:-1].split(" ")
        args = {}
        args['command'] = values[0]
        if args['command'] in (
                "INSERT", "DELETE", "UPDATE", "MOVE", "FETCH", "COPY"):
            args['rows'] = int(values[-1])
            if args['command'] == "INSERT":
                args['oid'] = int(values[1])
        else:
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
    @staticmethod
    def create_from_data(data):
        values = data[:-1].split(" ")
        args = {}
        args['command'] = values[0]
        if args['command'] in (
                "INSERT", "DELETE", "UPDATE", "MOVE", "FETCH", "COPY"):
            args['rows'] = int(values[-1])
            if args['command'] == "INSERT":
                args['oid'] = int(values[1])
        else:
            args['command'] = data[:-1]
        return CommandComplete(**args)


class DataRow(ReceiveMessage):
    def __init__(self, fields):
        self.fields = fields

    @staticmethod
    def create_from_data(data):
        count = struct.unpack("!h", data[:2])[0]
        data = data[2:]
        fields = []
        for i in range(count):
            val_len = struct.unpack("!i", data[:4])[0]
            data = data[4:]
            if val_len == -1:
                fields.append(None)
            else:
                fields.append(data[:val_len])
                data = data[val_len:]
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
# Byte1('K') - Identifier.
    # Int32(12) - Message length, including self.
    # Int32 - Process ID.
    # Int32 - Secret key.
    @staticmethod
    def create_from_data(data):
        process_id, secret_key = struct.unpack("!2i", data)
        return BackendKeyData(process_id, secret_key)


##
# Message representing a query with no data.
# <p>
# Stability: This is an internal class.  No stability guarantee is made.
class NoData(ReceiveMessage):
    # Byte1('n') - Identifier.
    # Int32(4) - Message length, including self.
    @staticmethod
    def create_from_data(data):
        return _NO_DATA
_NO_DATA = NoData()


##
# Message representing a successful Parse.
# </p><p>
# Stability: This is an internal class.  No stability guarantee is made.
class ParseComplete(ReceiveMessage):
    # Byte1('1') - Identifier.
    # Int32(4) - Message length, including self.
    @staticmethod</p>
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
return "" % (
            self.backend_pid, self.condition, self.additional_info)

    @staticmethod
    def create_from_data(data):
        backend_pid = struct.unpack("!i", data[:4])[0]
        data = data[4:]
        null = data.find("\x00")
        condition = data[:null]
        data = data[null + 1:]
        null = data.find("\x00")
        additional_info = data[:null]
        return NotificationResponse(backend_pid, condition, additional_info)


class ParameterDescription(ReceiveMessage):
    def __init__(self, type_oids):
        self.type_oids = type_oids

    @staticmethod
    def create_from_data(data):
        count = struct.unpack("!h", data[:2])[0]
        type_oids = struct.unpack("!" + "i" * count, data[2:])
        return ParameterDescription(type_oids)


class RowDescription(ReceiveMessage):
    def __init__(self, fields):
        self.fields = fields

    @staticmethod
    def create_from_data(data):
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
# String - Runtime parameter name.
    # String - Runtime parameter value.
    @staticmethod
    def create_from_data(data):
        key = data[:data.find("\x00")]
        value = data[data.find("\x00") + 1:-1]
        return ParameterStatus(key, value)


##
# BackendKeyData message sent from backend.  Contains a connection's process
# ID and a secret key.  Can be used to terminate the connection's current
# actions, such as a long running query.  Not supported by pg8000 yet.
# <p>
# Stability: This is an internal class.  No stability guarantee is made.
class BackendKeyData(ReceiveMessage):
    def __init__(self, process_id, secret_key):
        self.process_id = process_id
        self.secret_key = secret_key

    # Byte1('K') - Identifier.
    # Int32(12) - Message length, including self.
    # Int32 - Process ID.
    # Int32 - Secret key.
    @staticmethod
    def create_from_data(data):
        process_id, secret_key = struct.unpack("!2i", data)
        return BackendKeyData(process_id, secret_key)


##
# Message representing a query with no data.</p>
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
# Any number of these, followed by a zero byte:
    #   Byte1 - code identifying the field type (see responseKeys)
    #   String - field value
    @staticmethod
    def create_from_data(data):
        return NoticeResponse(**NoticeResponse.dataIntoDict(data))


##
# A message sent in case of a server-side error.  Contains the same properties
# that {@link NoticeResponse NoticeResponse} contains.
# <p>
# Stability: Added in pg8000 v1.03.  Required properties severity, code, and
# msg are guaranteed for v1.xx.  Other properties should be checked with
# hasattr before accessing.
class ErrorResponse(ReceiveMessage):
    def __init__(self, **kwargs):
        for arg, value in kwargs.items():
            setattr(self, arg, value)

    def __repr__(self):
        return "" % (
            self.severity, self.code, self.msg)

    def createException(self):
        return errors.ProgrammingError(self.severity, self.code, self.msg)

    @staticmethod
    def create_from_data(data):
        return ErrorResponse(**NoticeResponse.dataIntoDict(data))

</p>
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
# <p>
# Stability: This is an internal class.  No stability guarantee is made.
class PortalSuspended(ReceiveMessage):
    # Byte1('s') - Identifier.
    # Int32(4) - Message length, including self.
    @staticmethod
    def create_from_data(data):
        return _PORTAL_SUSPENDED
_PORTAL_SUSPENDED = PortalSuspended()


##
# Message representing the backend is ready to process a new query.
# </p><p>
# Stability: This is an internal class.  No stability guarantee is made.
class ReadyForQuery(ReceiveMessage):
    def __init__(self, status):
        # I = Idle, T = Idle in Transaction, E = idle in failed transaction.
        self.status = status

    def __repr__(self):
        return "" % {
            "I": "Idle", "T": "Idle in Transaction",
            "E": "Idle in Failed Transaction"}[self.status]

    # Byte1('Z') - Identifier.
    # Int32(5) - Message length, including self.
    # Byte1 -   Status indicator.
    @staticmethod
    def create_from_data(data):
        return ReadyForQuery(data)
</p>
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
def createException(self):
        return errors.ProgrammingError(self.severity, self.code, self.msg)

    @staticmethod
    def create_from_data(data):
        return ErrorResponse(**NoticeResponse.dataIntoDict(data))


##
# A message sent if this connection receives a NOTIFY that it was LISTENing
# for.
# <p>
# Stability: Added in pg8000 v1.03.  When limited to accessing properties from
# a notification event dispatch, stability is guaranteed for v1.xx.
class NotificationResponse(ReceiveMessage):
    def __init__(self, backend_pid, condition, additional_info):
        self.backend_pid = backend_pid
        self.condition = condition
        self.additional_info = additional_info

    ##
    # An integer representing the process ID of the backend that triggered
    # the NOTIFY.
    # </p><p>
    # Stability: Added in pg8000 v1.03, stability guaranteed for v1.xx.
    backend_pid = None

    ##
    # The name of the notification fired.
    # </p><p>
    # Stability: Added in pg8000 v1.03, stability guaranteed for v1.xx.</p>
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
# Stability: This is an internal class.  No stability guarantee is made.
class CloseComplete(ReceiveMessage):
    # Byte1('3') - Identifier.
    # Int32(4) - Message length, including self.
    @staticmethod
    def create_from_data(data):
        return _CLOSE_COMPLETE
_CLOSE_COMPLETE = CloseComplete()


##
# Message representing data from an Execute has been received, but more data
# exists in the portal.
# <p>
# Stability: This is an internal class.  No stability guarantee is made.
class PortalSuspended(ReceiveMessage):
    # Byte1('s') - Identifier.
    # Int32(4) - Message length, including self.
    @staticmethod
    def create_from_data(data):
        return _PORTAL_SUSPENDED
_PORTAL_SUSPENDED = PortalSuspended()


##
# Message representing the backend is ready to process a new query.
# </p><p>
# Stability: This is an internal class.  No stability guarantee is made.
class ReadyForQuery(ReceiveMessage):
    def __init__(self, status):
        # I = Idle, T = Idle in Transaction, E = idle in failed transaction.
        self.status = status</p>
github mfenniak / pg8000 / pg8000 / protocol.py View on Github external
class CopyDone(SendMessage, ReceiveMessage):
    # Byte1('c') - Identifier.
    # Int32(4) - Message length, including self.

    @staticmethod
    def create_from_data(data):
        return _COPY_DONE

    def serialize(self):
        return 'c\x00\x00\x00\x04'

_COPY_DONE = CopyDone()
_COPY_DONE_SERIALIZED = _COPY_DONE.serialize()


class CopyOutResponse(ReceiveMessage):
    # Byte1('H')
    # Int32(4) - Length of message contents in bytes, including self.
    # Int8(1) - 0 textual, 1 binary
    # Int16(2) - Number of columns
    # Int16(N) - Format codes for each column (0 text, 1 binary)

    def __init__(self, is_binary, column_formats):
        self.is_binary = is_binary
        self.column_formats = column_formats

    @staticmethod
    def create_from_data(data):
        is_binary, num_cols = struct.unpack('!bh', data[:3])
        column_formats = struct.unpack('!' + ('h' * num_cols), data[3:])
        return CopyOutResponse(is_binary, column_formats)