Module: DB2Query::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::DB2QueryConnection
Defined in:
lib/db2_query/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#exec_query(sql, formatter = {}, args = [], name = "SQL") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/db2_query/database_statements.rb', line 32

def exec_query(sql, formatter = {}, args = [], name = "SQL")
  binds, args = extract_binds_from_sql(sql, args)
  log(sql, name, binds, args) do
    begin
      if args.empty?
        stmt = @connection.run(sql)
      else
        stmt = @connection.run(sql, *args)
      end
      columns = stmt.columns.values.map { |col| col.name.downcase }
      rows = stmt.to_a
    ensure
      stmt.drop unless stmt.nil?
    end
    DB2Query::Result.new(columns, rows, formatter)
  end
end

#execute(sql, args = []) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/db2_query/database_statements.rb', line 24

def execute(sql, args = [])
  if args.empty?
    @connection.do(sql)
  else
    @connection.do(sql, *args)
  end
end

#query(sql) ⇒ Object



5
6
7
8
9
10
# File 'lib/db2_query/database_statements.rb', line 5

def query(sql)
  stmt = @connection.run(sql)
  stmt.to_a
ensure
  stmt.drop unless stmt.nil?
end

#query_rows(sql) ⇒ Object



12
13
14
# File 'lib/db2_query/database_statements.rb', line 12

def query_rows(sql)
  query(sql)
end

#query_value(sql, name = nil) ⇒ Object



16
17
18
# File 'lib/db2_query/database_statements.rb', line 16

def query_value(sql, name = nil)
  single_value_from_rows(query(sql))
end

#query_values(sql, name = nil) ⇒ Object



20
21
22
# File 'lib/db2_query/database_statements.rb', line 20

def query_values(sql, name = nil)
  query(sql).map(&:first)
end