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.



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

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/blazer/data_source.rb', line 5

def id
  @id
end

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

#cacheObject



39
40
41
# File 'lib/blazer/data_source.rb', line 39

def cache
  settings["cache"]
end

#cache_key(statement) ⇒ Object



98
99
100
# File 'lib/blazer/data_source.rb', line 98

def cache_key(statement)
  ["blazer", "v2", id, Digest::MD5.hexdigest(statement)].join("/")
end

#clear_cache(statement) ⇒ Object



94
95
96
# File 'lib/blazer/data_source.rb', line 94

def clear_cache(statement)
  Blazer.cache.delete(cache_key(statement))
end

#linked_columnsObject



23
24
25
# File 'lib/blazer/data_source.rb', line 23

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

#nameObject



19
20
21
# File 'lib/blazer/data_source.rb', line 19

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

#postgresql?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/blazer/data_source.rb', line 112

def postgresql?
  connection_model.connection.adapter_name == "PostgreSQL"
end

#redshift?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/blazer/data_source.rb', line 116

def redshift?
  connection_model.connection.adapter_name == "Redshift"
end

#run_statement(statement, options = {}) ⇒ Object



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
90
91
92
# File 'lib/blazer/data_source.rb', line 47

def run_statement(statement, options = {})
  rows = nil
  error = nil
  cached_at = nil
  cache_key = self.cache_key(statement) if cache
  if cache && !options[:refresh_cache]
    value = Blazer.cache.read(cache_key)
    rows, cached_at = Marshal.load(value) if value
  end

  unless rows
    rows = []

    comment = "blazer"
    if options[:user].respond_to?(:id)
      comment << ",user_id:#{options[:user].id}"
    end
    if options[: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 options[:query].respond_to?(:id)
      comment << ",query_id:#{options[:query].id}"
    end

    in_transaction do
      begin
        connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}") if timeout && (postgresql? || redshift?)
        result = connection_model.connection.select_all("#{statement} /*#{comment}*/")
        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
      rescue ActiveRecord::StatementInvalid => e
        error = e.message.sub(/.+ERROR: /, "")
      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

#schemasObject



102
103
104
105
# File 'lib/blazer/data_source.rb', line 102

def schemas
  default_schema = (postgresql? || redshift?) ? "public" : connection_model.connection_config[:database]
  settings["schemas"] || [connection_model.connection_config[:schema] || default_schema]
end

#smart_columnsObject



27
28
29
# File 'lib/blazer/data_source.rb', line 27

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

#smart_variablesObject



31
32
33
# File 'lib/blazer/data_source.rb', line 31

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

#tablesObject



107
108
109
110
# File 'lib/blazer/data_source.rb', line 107

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

#timeoutObject



35
36
37
# File 'lib/blazer/data_source.rb', line 35

def timeout
  settings["timeout"]
end

#use_transaction?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/blazer/data_source.rb', line 43

def use_transaction?
  settings.key?("use_transaction") ? settings["use_transaction"] : true
end