Class: Blazer::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer/data_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_modelObject (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

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/blazer/data_source.rb', line 3

def id
  @id
end

#settingsObject (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_columnsObject



21
22
23
# File 'lib/blazer/data_source.rb', line 21

def linked_columns
  settings["linked_columns"] || {}
end

#nameObject



17
18
19
# File 'lib/blazer/data_source.rb', line 17

def name
  settings["name"] || @id
end

#postgresql?Boolean

Returns:

  • (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.message.sub(/.+ERROR: /, "")
  end
  [rows, error]
end

#smart_columnsObject



25
26
27
# File 'lib/blazer/data_source.rb', line 25

def smart_columns
  settings["smart_columns"] || {}
end

#smart_variablesObject



29
30
31
# File 'lib/blazer/data_source.rb', line 29

def smart_variables
  settings["smart_variables"] || {}
end

#tablesObject



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

#timeoutObject



33
34
35
# File 'lib/blazer/data_source.rb', line 33

def timeout
  settings["timeout"]
end