Class: Blazer::DataSource
- Inherits:
-
Object
- Object
- Blazer::DataSource
- Extended by:
- Forwardable
- Defined in:
- lib/blazer/data_source.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #adapter ⇒ Object
- #bind_params(statement, variables) ⇒ Object
- #cache ⇒ Object
- #cache_expires_in ⇒ Object
- #cache_mode ⇒ Object
- #cache_slow_threshold ⇒ Object
- #clear_cache(statement) ⇒ Object
- #delete_results(run_id) ⇒ Object
-
#initialize(id, settings) ⇒ DataSource
constructor
A new instance of DataSource.
- #linked_columns ⇒ Object
- #local_time_suffix ⇒ Object
- #name ⇒ Object
- #quote(value) ⇒ Object
- #result_cache ⇒ Object
- #run_results(run_id) ⇒ Object
- #run_statement(statement, options = {}) ⇒ Object
- #smart_columns ⇒ Object
- #smart_variables ⇒ Object
- #sub_variables(statement, vars) ⇒ Object
- #timeout ⇒ Object
- #variable_defaults ⇒ Object
Constructor Details
#initialize(id, settings) ⇒ DataSource
Returns a new instance of DataSource.
9 10 11 12 |
# File 'lib/blazer/data_source.rb', line 9 def initialize(id, settings) @id = id @settings = settings end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/blazer/data_source.rb', line 5 def id @id end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
5 6 7 |
# File 'lib/blazer/data_source.rb', line 5 def settings @settings end |
Instance Method Details
#adapter ⇒ Object
14 15 16 |
# File 'lib/blazer/data_source.rb', line 14 def adapter settings["adapter"] || detect_adapter end |
#bind_params(statement, variables) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/blazer/data_source.rb', line 172 def bind_params(statement, variables) if parameter_binding == :positional locations = [] variables.each do |k, v| i = 0 while (idx = statement.index("{#{k}}", i)) locations << [v, idx] i = idx + 1 end end variables.each do |k, v| statement = statement.gsub("{#{k}}", "?") end [statement, locations.sort_by(&:last).map(&:first)] elsif parameter_binding == :numeric variables.each_with_index do |(k, v), i| # add trailing space if followed by digit # try to keep minimal to avoid fixing invalid queries like SELECT{var} statement = statement.gsub(/#{Regexp.escape("{#{k}}")}(\d)/, "$#{i + 1} \\1").gsub("{#{k}}", "$#{i + 1}") end [statement, variables.values] elsif parameter_binding.respond_to?(:call) parameter_binding.call(statement, variables) elsif parameter_binding.nil? [sub_variables(statement, variables), []] else raise Blazer::Error, "Unknown bind parameters" end end |
#cache ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/blazer/data_source.rb', line 42 def cache @cache ||= begin if settings["cache"].is_a?(Hash) settings["cache"] elsif settings["cache"] { "mode" => "all", "expires_in" => settings["cache"] } else { "mode" => "off" } end end end |
#cache_expires_in ⇒ Object
63 64 65 |
# File 'lib/blazer/data_source.rb', line 63 def cache_expires_in (cache["expires_in"] || 60).to_f end |
#cache_mode ⇒ Object
59 60 61 |
# File 'lib/blazer/data_source.rb', line 59 def cache_mode cache["mode"] end |
#cache_slow_threshold ⇒ Object
67 68 69 |
# File 'lib/blazer/data_source.rb', line 67 def cache_slow_threshold (cache["slow_threshold"] || 15).to_f end |
#clear_cache(statement) ⇒ Object
143 144 145 |
# File 'lib/blazer/data_source.rb', line 143 def clear_cache(statement) result_cache.delete_statement(statement) end |
#delete_results(run_id) ⇒ Object
83 84 85 |
# File 'lib/blazer/data_source.rb', line 83 def delete_results(run_id) result_cache.delete_run(run_id) end |
#linked_columns ⇒ Object
22 23 24 |
# File 'lib/blazer/data_source.rb', line 22 def linked_columns settings["linked_columns"] || {} end |
#local_time_suffix ⇒ Object
71 72 73 |
# File 'lib/blazer/data_source.rb', line 71 def local_time_suffix @local_time_suffix ||= Array(settings["local_time_suffix"]) end |
#name ⇒ Object
18 19 20 |
# File 'lib/blazer/data_source.rb', line 18 def name settings["name"] || @id end |
#quote(value) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/blazer/data_source.rb', line 147 def quote(value) if quoting == :backslash_escape || quoting == :single_quote_escape # only need to support types generated by process_vars if value.is_a?(Integer) || value.is_a?(Float) value.to_s elsif value.nil? "NULL" else value = value.to_formatted_s(:db) if value.is_a?(ActiveSupport::TimeWithZone) if quoting == :backslash_escape "'#{value.gsub("\\") { "\\\\" }.gsub("'") { "\\'" }}'" else "'#{value.gsub("'", "''")}'" end end elsif quoting.respond_to?(:call) quoting.call(value) elsif quoting.nil? raise Blazer::Error, "Quoting not specified" else raise Blazer::Error, "Unknown quoting" end end |
#result_cache ⇒ Object
75 76 77 |
# File 'lib/blazer/data_source.rb', line 75 def result_cache @result_cache ||= Blazer::ResultCache.new(self) end |
#run_results(run_id) ⇒ Object
79 80 81 |
# File 'lib/blazer/data_source.rb', line 79 def run_results(run_id) result_cache.read_run(run_id) end |
#run_statement(statement, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/blazer/data_source.rb', line 96 def run_statement(statement, = {}) statement = Statement.new(statement, self) if statement.is_a?(String) statement.bind unless statement.bind_statement result = nil if cache_mode != "off" if [:refresh_cache] clear_cache(statement) # for checks else result = result_cache.read_statement(statement) end end unless result comment = "blazer".dup if [:user].respond_to?(:id) comment << ",user_id:#{[:user].id}" end if [:user].respond_to?(Blazer.user_name) # only include letters, numbers, and spaces to prevent injection comment << ",user_name:#{[:user].send(Blazer.user_name).to_s.gsub(/[^a-zA-Z0-9 ]/, "")}" end if [:query].respond_to?(:id) comment << ",query_id:#{[:query].id}" end if [:check] comment << ",check_id:#{[:check].id},check_emails:#{[:check].emails}" end if [:run_id] comment << ",run_id:#{[:run_id]}" end result = run_statement_helper(statement, comment, ) end if [:async] && [:run_id] run_id = [:run_id] begin result_cache.write_run(run_id, result) rescue result = Blazer::Result.new(self, [], [], "Error storing the results of this query :(", nil, false) result_cache.write_run(run_id, result) end end result end |
#smart_columns ⇒ Object
26 27 28 |
# File 'lib/blazer/data_source.rb', line 26 def smart_columns settings["smart_columns"] || {} end |
#smart_variables ⇒ Object
30 31 32 |
# File 'lib/blazer/data_source.rb', line 30 def smart_variables settings["smart_variables"] || {} end |
#sub_variables(statement, vars) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/blazer/data_source.rb', line 87 def sub_variables(statement, vars) statement = statement.dup vars.each do |var, value| # use block form to disable back-references statement.gsub!("{#{var}}") { quote(value) } end statement end |
#timeout ⇒ Object
38 39 40 |
# File 'lib/blazer/data_source.rb', line 38 def timeout settings["timeout"] end |
#variable_defaults ⇒ Object
34 35 36 |
# File 'lib/blazer/data_source.rb', line 34 def variable_defaults settings["variable_defaults"] || {} end |