Module: DbMod::Statements::Configuration::Single::RequiredRow

Defined in:
lib/db_mod/statements/configuration/single/required_row.rb

Overview

Wrapper for a statement or prepared method that returns only the first row of the result set as a hash, to save manual unboxing. Raises an error unless exactly one row is returned.

def_statement(:a, 'SELECT a, b FROM foo').single(:row)

def do_stuff
  a # => { 'a' => '1', 'b' => '2'
end

Class Method Summary collapse

Class Method Details

.call(results) ⇒ Hash<String,String>

Enables this module to be passed to DbMod::Statements::Configuration.attach_result_processor as the wrapper function, where it will return the first row of the result set, or raise an exception if exactly one row is not returned.

Parameters:

  • results (Object)

    SQL result set

Returns:

  • (Hash<String,String>)

    the first row of the SQL result set returned by the query



25
26
27
28
29
30
# File 'lib/db_mod/statements/configuration/single/required_row.rb', line 25

def self.call(results)
  fail DbMod::Exceptions::NoResults unless results.any?
  fail DbMod::Exceptions::TooManyResults if results.count > 1

  results[0]
end