Class: RDBI::Result::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/rdbi/result.rb

Overview

A result driver is a transformative element for RDBI::Result. Its design could be loosely described as a “fancy decorator”.

Usage and purpose is covered in the main RDBI::Result documentation. This section will largely serve the purpose of helping those who wish to implement result drivers themselves.

Creating a Result Driver

A result driver typically inherits from RDBI::Result::Driver and implements at least one method: fetch.

This fetch is not RDBI::Result#fetch, and doesn't have the same call semantics. Instead, it takes a single argument, the row_count, and typically passes that to RDBI::Result#raw_fetch to get results to process. It then returns the data transformed.

RDBI::Result::Driver additionally provides two methods, convert_row and convert_item, which leverage RDBI's type conversion facility (see RDBI::Type) to assist in type conversion. For performance reasons, RDBI chooses to convert on request instead of preemptively, so it is the driver implementor's job to do any conversion.

If you wish to implement a constructor in your class, please see RDBI::Result::Driver.new.

Direct Known Subclasses

Array, CSV, Struct

Defined Under Namespace

Classes: Array, CSV, Struct

Instance Method Summary collapse

Constructor Details

#initialize(result, *args) ⇒ Driver

Result driver constructor. This is the logic that associates the result driver for decoration over the result; if you wish to override this method, please call super before performing your own operations.


290
291
292
293
# File 'lib/rdbi/result.rb', line 290

def initialize(result, *args)
  @result = result
  @result.rewind
end

Instance Method Details

#convert_item(item, column) ⇒ Object

convert a single item (row element) with the specified result map.


318
319
320
# File 'lib/rdbi/result.rb', line 318

def convert_item(item, column)
  RDBI::Type::Out.convert(item, column, @result.type_hash)
end

#convert_row(row) ⇒ Object

convert an entire row of data with the specified result map (see RDBI::Type)


309
310
311
312
313
314
315
# File 'lib/rdbi/result.rb', line 309

def convert_row(row)
  newrow = []
  (row || []).each_with_index do |x, i|
    newrow.push(convert_item(x, @result.schema.columns[i]))
  end
  return newrow
end

#fetch(row_count) ⇒ Object

Fetch the result with any transformations. The default is to present the type converted array.


299
300
301
302
303
304
305
# File 'lib/rdbi/result.rb', line 299

def fetch(row_count)
  ary = (@result.raw_fetch(row_count) || []).enum_for.with_index.map do |item, i|
    convert_row(item)
  end

  RDBI::Util.format_results(row_count, ary)
end