Class: PostgresPR::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/postgres-pr/message.rb

Overview

Base class representing a PostgreSQL protocol message

Constant Summary collapse

MsgTypeMap =

One character message-typecode to class map

Hash.new { UnknownMessageType }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(buffer) ⇒ Object



45
46
47
48
49
# File 'lib/postgres-pr/message.rb', line 45

def self.create(buffer)
  obj = allocate
  obj.parse(buffer)
  obj
end

.dump(*args) ⇒ Object



51
52
53
# File 'lib/postgres-pr/message.rb', line 51

def self.dump(*args)
  new(*args).dump
end

.read(stream) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/postgres-pr/message.rb', line 23

def self.read(stream)
  type = read_exactly_n_bytes(stream, 1)
  length = read_exactly_n_bytes(stream, 4).unpack('N').first  # FIXME: length should be signed, not unsigned

  raise ParseError unless length >= 4

  # initialize buffer
  buffer = Buffer.of_size(1+length)
  buffer.write(type)
  buffer.write_int32(length)
  buffer.copy_from_stream(stream, length-4)
  
  MsgTypeMap[type].create(buffer)
end

.register_message_type(type) ⇒ Object



18
19
20
21
# File 'lib/postgres-pr/message.rb', line 18

def self.register_message_type(type)
  define_method(:message_type){type}
  MsgTypeMap[type] = self
end

Instance Method Details

#dump(body_size = 0) {|buffer| ... } ⇒ Object

Yields:

  • (buffer)

Raises:



55
56
57
58
59
60
61
62
# File 'lib/postgres-pr/message.rb', line 55

def dump(body_size=0)
  buffer = Buffer.of_size(5 +  body_size)
  buffer.write(self.message_type)
  buffer.write_int32(4 + body_size)
  yield buffer
  raise DumpError  unless buffer.at_end?
  return buffer.content
end

#parse(buffer) {|buffer| ... } ⇒ Object

Yields:

  • (buffer)

Raises:



64
65
66
67
68
# File 'lib/postgres-pr/message.rb', line 64

def parse(buffer)
  buffer.position = 5
  yield buffer
  raise ParseError, buffer.inspect unless buffer.at_end?
end