Class: Blazer::DataSource
- Inherits:
-
Object
- Object
- Blazer::DataSource
- Defined in:
- lib/blazer/data_source.rb
Instance Attribute Summary collapse
-
#connection_model ⇒ Object
readonly
Returns the value of attribute connection_model.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #cache ⇒ Object
- #cache_key(statement) ⇒ Object
- #clear_cache(statement) ⇒ Object
-
#initialize(id, settings) ⇒ DataSource
constructor
A new instance of DataSource.
- #linked_columns ⇒ Object
- #local_time_suffix ⇒ Object
- #mysql? ⇒ Boolean
- #name ⇒ Object
- #postgresql? ⇒ Boolean
- #reconnect ⇒ Object
- #redshift? ⇒ Boolean
- #run_statement(statement, options = {}) ⇒ Object
- #schemas ⇒ Object
- #smart_columns ⇒ Object
- #smart_variables ⇒ Object
- #tables ⇒ Object
- #timeout ⇒ Object
- #use_transaction? ⇒ Boolean
- #variable_defaults ⇒ Object
Constructor Details
#initialize(id, settings) ⇒ DataSource
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/blazer/data_source.rb', line 7 def initialize(id, settings) @id = id @settings = settings @connection_model = Class.new(Blazer::Connection) do def self.name "Blazer::Connection::#{object_id}" end establish_connection(settings["url"]) if settings["url"] end end |
Instance Attribute Details
#connection_model ⇒ Object (readonly)
Returns the value of attribute connection_model.
5 6 7 |
# File 'lib/blazer/data_source.rb', line 5 def connection_model @connection_model end |
#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
#cache ⇒ Object
43 44 45 |
# File 'lib/blazer/data_source.rb', line 43 def cache settings["cache"] end |
#cache_key(statement) ⇒ Object
117 118 119 |
# File 'lib/blazer/data_source.rb', line 117 def cache_key(statement) ["blazer", "v2", id, Digest::MD5.hexdigest(statement)].join("/") end |
#clear_cache(statement) ⇒ Object
113 114 115 |
# File 'lib/blazer/data_source.rb', line 113 def clear_cache(statement) Blazer.cache.delete(cache_key(statement)) end |
#linked_columns ⇒ Object
23 24 25 |
# File 'lib/blazer/data_source.rb', line 23 def linked_columns settings["linked_columns"] || {} end |
#local_time_suffix ⇒ Object
47 48 49 |
# File 'lib/blazer/data_source.rb', line 47 def local_time_suffix @local_time_suffix ||= Array(settings["local_time_suffix"]) end |
#mysql? ⇒ Boolean
139 140 141 |
# File 'lib/blazer/data_source.rb', line 139 def mysql? ["MySQL", "Mysql2", "Mysql2Spatial"].include?(adapter_name) end |
#name ⇒ Object
19 20 21 |
# File 'lib/blazer/data_source.rb', line 19 def name settings["name"] || @id end |
#postgresql? ⇒ Boolean
131 132 133 |
# File 'lib/blazer/data_source.rb', line 131 def postgresql? ["PostgreSQL", "PostGIS"].include?(adapter_name) end |
#reconnect ⇒ Object
143 144 145 |
# File 'lib/blazer/data_source.rb', line 143 def reconnect connection_model.establish_connection(settings["url"]) end |
#redshift? ⇒ Boolean
135 136 137 |
# File 'lib/blazer/data_source.rb', line 135 def redshift? ["Redshift"].include?(adapter_name) end |
#run_statement(statement, options = {}) ⇒ Object
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/blazer/data_source.rb', line 55 def run_statement(statement, = {}) rows = nil error = nil cached_at = nil cache_key = self.cache_key(statement) if cache if cache && ![:refresh_cache] value = Blazer.cache.read(cache_key) rows, cached_at = Marshal.load(value) if value end unless rows rows = [] comment = "blazer" if [:user].respond_to?(:id) comment << ",user_id:#{options[:user].id}" end if [:user].respond_to?(Blazer.user_name) # only include letters, numbers, and spaces to prevent injection comment << ",user_name:#{options[:user].send(Blazer.user_name).to_s.gsub(/[^a-zA-Z0-9 ]/, "")}" end if [:query].respond_to?(:id) comment << ",query_id:#{options[:query].id}" end in_transaction do begin if timeout if postgresql? || redshift? connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}") elsif mysql? connection_model.connection.execute("SET max_execution_time = #{timeout.to_i * 1000}") else raise Blazer::TimeoutNotSupported, "Timeout not supported for #{adapter_name} adapter" end end result = connection_model.connection.select_all("#{statement} /*#{comment}*/") cast_method = Rails::VERSION::MAJOR < 5 ? :type_cast : :cast_value result.each do |untyped_row| row = {} untyped_row.each do |k, v| row[k] = result.column_types.empty? ? v : result.column_types[k].send(cast_method, v) end rows << row end rescue ActiveRecord::StatementInvalid => e error = e..sub(/.+ERROR: /, "") error = Blazer::TIMEOUT_MESSAGE if Blazer::TIMEOUT_ERRORS.any? { |e| error.include?(e) } end end Blazer.cache.write(cache_key, Marshal.dump([rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache end [rows, error, cached_at] end |
#schemas ⇒ Object
121 122 123 124 |
# File 'lib/blazer/data_source.rb', line 121 def schemas default_schema = (postgresql? || redshift?) ? "public" : connection_model.connection_config[:database] settings["schemas"] || [connection_model.connection_config[:schema] || default_schema] end |
#smart_columns ⇒ Object
27 28 29 |
# File 'lib/blazer/data_source.rb', line 27 def smart_columns settings["smart_columns"] || {} end |
#smart_variables ⇒ Object
31 32 33 |
# File 'lib/blazer/data_source.rb', line 31 def smart_variables settings["smart_variables"] || {} end |
#tables ⇒ Object
126 127 128 129 |
# File 'lib/blazer/data_source.rb', line 126 def tables rows, error, cached_at = run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema IN (?)", schemas])) Hash[rows.group_by { |r| r["table_name"] }.map { |t, f| [t, f.sort_by { |f| f["ordinal_position"] }.map { |f| f.slice("column_name", "data_type") }] }.sort_by { |t, _f| t }] end |
#timeout ⇒ Object
39 40 41 |
# File 'lib/blazer/data_source.rb', line 39 def timeout settings["timeout"] end |
#use_transaction? ⇒ Boolean
51 52 53 |
# File 'lib/blazer/data_source.rb', line 51 def use_transaction? settings.key?("use_transaction") ? settings["use_transaction"] : true end |
#variable_defaults ⇒ Object
35 36 37 |
# File 'lib/blazer/data_source.rb', line 35 def variable_defaults settings["variable_defaults"] || {} end |