Class: Blazer::Adapters::SodaAdapter
Instance Attribute Summary
Attributes inherited from BaseAdapter
#data_source
Instance Method Summary
collapse
Methods inherited from BaseAdapter
#cachable?, #cancel, #cohort_analysis_statement, #cost, #explain, #initialize, #parameter_binding, #reconnect, #schema, #supports_cohort_analysis?
Instance Method Details
#preview_statement ⇒ Object
91
92
93
|
# File 'lib/blazer/adapters/soda_adapter.rb', line 91
def preview_statement
"SELECT * LIMIT 10"
end
|
#quoting ⇒ Object
100
101
102
|
# File 'lib/blazer/adapters/soda_adapter.rb', line 100
def quoting
:single_quote_escape
end
|
#run_statement(statement, comment) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/blazer/adapters/soda_adapter.rb', line 4
def run_statement(statement, )
require "json"
require "net/http"
require "uri"
columns = []
rows = []
error = nil
statement = statement.gsub(/--.+/, "")
statement = statement.gsub(/\/\*.+\*\//, "")
statement = statement.sub(/;\s*\z/, "")
statement = statement.squish
uri = URI(settings["url"])
uri.query = URI.encode_www_form("$query" => statement)
req = Net::HTTP::Get.new(uri)
req["X-App-Token"] = settings["app_token"] if settings["app_token"]
options = {
use_ssl: uri.scheme == "https",
open_timeout: 3,
read_timeout: 30
}
begin
res = Net::HTTP.start(uri.hostname, uri.port, options) do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
body = JSON.parse(res.body)
columns = JSON.parse(res["x-soda2-fields"])
column_types = columns.zip(JSON.parse(res["x-soda2-types"])).to_h
columns.reject! { |f| f.start_with?(":@") }
rows = body.map { |r| columns.map { |c| r[c] } }
columns.each_with_index do |column, i|
case column_types[column]
when "number"
if rows.all? { |r| r[i].to_i == r[i].to_f }
rows.each do |row|
row[i] = row[i].to_i
end
else
rows.each do |row|
row[i] = row[i].to_f
end
end
when "floating_timestamp"
if rows.all? { |r| r[i].end_with?("T00:00:00.000") }
rows.each do |row|
row[i] = Date.parse(row[i])
end
else
utc = ActiveSupport::TimeZone["Etc/UTC"]
rows.each do |row|
row[i] = utc.parse(row[i])
end
end
end
end
else
error = JSON.parse(res.body)["message"] rescue "Bad response: #{res.code}"
end
rescue => e
error = e.message
end
[columns, rows, error]
end
|
#tables ⇒ Object
95
96
97
|
# File 'lib/blazer/adapters/soda_adapter.rb', line 95
def tables
["all"]
end
|