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, YAML

Defined Under Namespace

Classes: Array, CSV, Struct, YAML

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.



335
336
337
# File 'lib/rdbi/result.rb', line 335

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

Instance Method Details

#fetch(row_count) ⇒ Object

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



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/rdbi/result.rb', line 343

def fetch(row_count)
  rows = RDBI::Util.format_results(row_count, (@result.raw_fetch(row_count) || []))

  if rows.nil?
    return rows 
  elsif [:first, :last].include?(row_count)
    return convert_row(rows)
  else
    result = []
    rows.each do |row| 
      result << convert_row(row)
    end
  end
  return result
end

#firstObject

Returns the first result in the set.



362
363
364
# File 'lib/rdbi/result.rb', line 362

def first
  return fetch(:first)
end

#lastObject

Returns the last result in the set.

Warning: Depending on your database and drivers, calling this could have serious memory and performance implications!



372
373
374
# File 'lib/rdbi/result.rb', line 372

def last
  return fetch(:last)
end