Class: Rake::DataTask::Sql

Inherits:
Object
  • Object
show all
Defined in:
lib/data_task/sql.rb

Class Method Summary collapse

Class Method Details

.get_single_int(r) ⇒ Object

Get a single integer via SQL.

Parameters:

  • r (Array)

    an array containing a single result from a query



29
30
31
32
33
# File 'lib/data_task/sql.rb', line 29

def self.get_single_int r
  parse_single_value r do
    Integer(r.first.first)
  end
end

.get_single_time(r) ⇒ Object

Get a single time via SQL.

Parameters:

  • r (Array)

    an array containing a single result from a query



39
40
41
42
43
44
# File 'lib/data_task/sql.rb', line 39

def self.get_single_time r
  parse_single_value r do
    t = Time.parse(r.first.first)
    DateTime.parse(t.to_s)
  end
end

.parse_single_value(r) {|r| ... } ⇒ Object

Parse a single string value into an object using the supplied type logic.

Parameters:

  • r (Array)

    an array (table) of arrays (rows), usually resulting from a database query

  • &type_logic (Block)

    code that takes the first

Yields:

  • (r)

Raises:

  • (TypeError)

    if r contains more than one row or column



14
15
16
17
18
19
20
21
22
23
# File 'lib/data_task/sql.rb', line 14

def self.parse_single_value r, &type_logic
  if r.nil? || r.empty? || r == [[]] || r == [[nil]]
    return nil
  elsif r.length > 1
    raise TypeError, 'Query must result in a single row'
  elsif r.first.length > 1
    raise TypeError, 'Query must result in a single column'
  end
  yield(r)
end