Module: DbMod::Statements::Configuration::Single::RequiredValue

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

Overview

Wrapper for a statement or prepared method that returns the first column of the first returned row. Strictly enforces that exactly one row should be returned by the SQL result, and will fail if zero or more than one row is returned.

def_statement(:a, 'SELECT 1').single(:value!)

def do_stuff
  a # => '1'
end

Class Method Summary collapse

Class Method Details

.call(results) ⇒ String

Enables this module to be passed to DbMod::Statements::Configuration.attach_result_processor as the wrapper function, where it will return the first column of the first row of the result set, or fail if anything other than exactly one row is returned.

Parameters:

  • results (Object)

    SQL result set

Returns:

  • (String)

    the first column of the first returned row



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

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

  row = results[0]
  row[row.keys.first]
end