Class: SQLite3::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/metric_system/sqlite3_extensions.rb

Instance Method Summary collapse

Constructor Details

#initialize(sql, statement) ⇒ Query

Returns a new instance of Query.



62
63
64
65
66
# File 'lib/metric_system/sqlite3_extensions.rb', line 62

def initialize(sql, statement)
  expect! statement => SQLite3::Statement

  @sql, @statement = sql, statement
end

Instance Method Details

#ask(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/metric_system/sqlite3_extensions.rb', line 81

def ask(*args)
  results = run(*args)
  row = results.first
  results.reset

  if !row               then  nil
  elsif row.length == 1 then  row.first
  else                        row
  end
end

#run(*args) ⇒ Object



68
69
70
71
# File 'lib/metric_system/sqlite3_extensions.rb', line 68

def run(*args)
  # STDERR.puts "Q: #{@sql} #{args.map(&:inspect).join(", ")}"
  @statement.execute *args
end

#select(*args) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/metric_system/sqlite3_extensions.rb', line 73

def select(*args)
  @klass ||= SQLite3::Record.for_columns(@statement.columns)

  run(*args).map do |rec|
    @klass.build *rec
  end
end