class PgConduit::QueryStream

Execute a SQL query and provide the results as a stream @example Print username and email for all users

conn    = PG::Connection.open
stream  = PgConduit::QueryStream.new(conn)

stream.query('SELECT * FROM users').each_row do |row|
  puts "#{row['username']}, #{row['email']}"
end

Attributes

sql[R]

Public Class Methods

new(pool) click to toggle source

@param pool [ConnectionPool] A pool of PG::Connections

# File lib/pg_conduit/query_stream.rb, line 16
def initialize(pool)
  @pool = pool
end

Public Instance Methods

each_row() { |row| ... } click to toggle source

Execute query and yield each row @yield [Hash] A hash representing a single row from the result set

# File lib/pg_conduit/query_stream.rb, line 28
def each_row
  @pool.with do |conn|
    conn.send_query @sql
    conn.set_single_row_mode
    loop do
      res = conn.get_result
      break unless res
      res.check
      res.stream_each { |row| yield row }
    end
  end
end
query(sql) click to toggle source

@param sql [String] The SQL query to execute @return [self]

# File lib/pg_conduit/query_stream.rb, line 22
def query(sql)
  self.tap { @sql = sql }
end