Module: Db2Query::DbStatements

Included in:
Connection
Defined in:
lib/db2_query/db_statements.rb

Instance Method Summary collapse

Instance Method Details

#exec_query(query, args = []) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/db2_query/db_statements.rb', line 41

def exec_query(query, args = [])
  sql, binds, args = query.exec_query_arguments(args)
  log(sql, binds, args) do
    pool do |client|
      stmt = client.run(sql, *args)
      columns = stmt.columns.values.map { |col| col.name.downcase }
      rows = stmt.to_a
      Db2Query::Result.new(columns, rows, query)
    ensure
      stmt.drop unless stmt.nil?
    end
  end
end

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



28
29
30
31
32
# File 'lib/db2_query/db_statements.rb', line 28

def execute(sql, args = [])
  pool do |client|
    client.do(sql, *args)
  end
end

#query(sql) ⇒ Object



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

def query(sql)
  pool do |client|
    stmt = client.run(sql)
    stmt.to_a
  ensure
    stmt.drop unless stmt.nil?
  end
end

#query_rows(sql) ⇒ Object



14
15
16
# File 'lib/db2_query/db_statements.rb', line 14

def query_rows(sql)
  query(sql)
end

#query_value(sql) ⇒ Object



18
19
20
21
22
# File 'lib/db2_query/db_statements.rb', line 18

def query_value(sql)
  rows = query(sql)
  row = rows.first
  row && row.first
end

#query_values(sql) ⇒ Object



24
25
26
# File 'lib/db2_query/db_statements.rb', line 24

def query_values(sql)
  query(sql).map(&:first)
end

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



34
35
36
37
38
39
# File 'lib/db2_query/db_statements.rb', line 34

def raw_query(sql, args = [])
  pool do |client|
    stmt = client.run(sql, *args)
    raw_result(stmt)
  end
end

#reset_id_sequence!(table_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/db2_query/db_statements.rb', line 55

def reset_id_sequence!(table_name)
  next_val = max_id(table_name) + 1
  execute <<-SQL
    ALTER TABLE #{table_name}
    ALTER COLUMN ID
    RESTART WITH #{next_val}
    SET INCREMENT BY 1
    SET NO CYCLE
    SET CACHE 500
    SET NO ORDER;
  SQL
end