Class: PostgresqlLoStreamer::Streamer

Inherits:
Object
  • Object
show all
Defined in:
lib/postgresql_lo_streamer/streamer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, object_identifier) ⇒ Streamer

Returns a new instance of Streamer.



4
5
6
7
# File 'lib/postgresql_lo_streamer/streamer.rb', line 4

def initialize(connection, object_identifier)
  @connection = connection
  @object_identifier = object_identifier
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



2
3
4
# File 'lib/postgresql_lo_streamer/streamer.rb', line 2

def connection
  @connection
end

#object_identifierObject

Returns the value of attribute object_identifier.



2
3
4
# File 'lib/postgresql_lo_streamer/streamer.rb', line 2

def object_identifier
  @object_identifier
end

Instance Method Details

#object_exists?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/postgresql_lo_streamer/streamer.rb', line 9

def object_exists?
  begin
    @connection.lo_open(@object_identifier, ::PG::INV_READ)
  rescue PG::Error => e
    if e.to_s.include? "does not exist"
      return false
    end
    raise
  end
  return true
end

#streamObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/postgresql_lo_streamer/streamer.rb', line 21

def stream
  return Enumerator.new do |y|
    @connection.transaction do
      lo = @connection.lo_open(@object_identifier, ::PG::INV_READ)
      while data = connection.lo_read(lo, 4096) do
        y << data
      end
      @connection.lo_close(lo)
    end
  end
end