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
-
#initialize(id, settings) ⇒ DataSource
constructor
A new instance of DataSource.
- #linked_columns ⇒ Object
- #name ⇒ Object
- #postgresql? ⇒ Boolean
- #run_statement(statement) ⇒ Object
- #smart_columns ⇒ Object
- #smart_variables ⇒ Object
- #tables ⇒ Object
- #timeout ⇒ Object
Constructor Details
#initialize(id, settings) ⇒ DataSource
Returns a new instance of DataSource.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/blazer/data_source.rb', line 5 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.
3 4 5 |
# File 'lib/blazer/data_source.rb', line 3 def connection_model @connection_model end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/blazer/data_source.rb', line 3 def id @id end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
3 4 5 |
# File 'lib/blazer/data_source.rb', line 3 def settings @settings end |
Instance Method Details
#linked_columns ⇒ Object
21 22 23 |
# File 'lib/blazer/data_source.rb', line 21 def linked_columns settings["linked_columns"] || {} end |
#name ⇒ Object
17 18 19 |
# File 'lib/blazer/data_source.rb', line 17 def name settings["name"] || @id end |
#postgresql? ⇒ Boolean
66 67 68 |
# File 'lib/blazer/data_source.rb', line 66 def postgresql? ["PostgreSQL", "Redshift"].include?(connection_model.connection.adapter_name) end |
#run_statement(statement) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/blazer/data_source.rb', line 37 def run_statement(statement) rows = [] error = nil begin connection_model.transaction do connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}") if timeout && postgresql? result = connection_model.connection.select_all(statement) result.each do |untyped_row| row = {} untyped_row.each do |k, v| row[k] = result.column_types.empty? ? v : result.column_types[k].send(:type_cast, v) end rows << row end raise ActiveRecord::Rollback end rescue ActiveRecord::StatementInvalid => e error = e..sub(/.+ERROR: /, "") end [rows, error] end |
#smart_columns ⇒ Object
25 26 27 |
# File 'lib/blazer/data_source.rb', line 25 def smart_columns settings["smart_columns"] || {} end |
#smart_variables ⇒ Object
29 30 31 |
# File 'lib/blazer/data_source.rb', line 29 def smart_variables settings["smart_variables"] || {} end |
#tables ⇒ Object
59 60 61 62 63 64 |
# File 'lib/blazer/data_source.rb', line 59 def tables default_schema = postgresql? ? "public" : connection_model.connection_config[:database] schema = connection_model.connection_config[:schema] || default_schema rows, error = run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema])) 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
33 34 35 |
# File 'lib/blazer/data_source.rb', line 33 def timeout settings["timeout"] end |