25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/execute_sql.rb', line 25
def execute_sql(sql, mode: :print, klass: HashWithIndifferentAccess)
sql_query = ExecuteSql::SqlQuery.new("#{sql}".strip).execute
rows = sql_query.data.rows
cols = sql_query.data.columns
case mode.to_s
when 'print'
puts Terminal::Table.new(rows: rows, headings: cols)
when 'array'
result = rows.map do |row|
record = klass.new
cols.each_with_index.map do |col, index|
record[col] = row[index]
end
record
end
result
when 'raw'
rows
when 'single'
rows.flatten[0]
when 'none'
nil
else
[]
end
end
|