Class: RDBI::Result::Driver::Struct

Inherits:
RDBI::Result::Driver show all
Defined in:
lib/rdbi/result.rb

Overview

Yields Struct objects instead of arrays for the rows. What this means is that you will recieve a single array of Structs, each struct representing a row of the database.

example:

results = dbh.execute("select foo, bar from my_table").fetch(:all, :Struct)

results[0].foo        # first row, foo column
results[10].bar       # 11th row, bar column

Instance Method Summary collapse

Methods inherited from RDBI::Result::Driver

#convert_item, #convert_row

Constructor Details

#initialize(result, *args) ⇒ Struct

Returns a new instance of Struct.



377
378
379
# File 'lib/rdbi/result.rb', line 377

def initialize(result, *args)
  super
end

Instance Method Details

#fetch(row_count) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/rdbi/result.rb', line 381

def fetch(row_count)
  column_names = @result.schema.columns.map(&:name)

  klass = ::Struct.new(*column_names)

  structs = super

  if [:first, :last].include?(row_count) 
    if structs
      return klass.new(*structs)
    else
      return structs
    end
  end

  structs.collect! { |row| klass.new(*row) }

  return RDBI::Util.format_results(row_count, structs)
end