Class: Blazer::Adapters::SqlAdapter
- Inherits:
-
BaseAdapter
- Object
- BaseAdapter
- Blazer::Adapters::SqlAdapter
- Defined in:
- lib/blazer/adapters/sql_adapter.rb
Instance Attribute Summary collapse
-
#connection_model ⇒ Object
readonly
Returns the value of attribute connection_model.
Attributes inherited from BaseAdapter
Instance Method Summary collapse
- #cost(statement) ⇒ Object
- #explain(statement) ⇒ Object
-
#initialize(data_source) ⇒ SqlAdapter
constructor
A new instance of SqlAdapter.
- #preview_statement ⇒ Object
- #reconnect ⇒ Object
- #run_statement(statement, comment) ⇒ Object
- #schema ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(data_source) ⇒ SqlAdapter
Returns a new instance of SqlAdapter.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 6 def initialize(data_source) super @connection_model = Class.new(Blazer::Connection) do def self.name "Blazer::Connection::#{object_id}" end establish_connection(data_source.settings["url"]) if data_source.settings["url"] end end |
Instance Attribute Details
#connection_model ⇒ Object (readonly)
Returns the value of attribute connection_model.
4 5 6 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 4 def connection_model @connection_model end |
Instance Method Details
#cost(statement) ⇒ Object
60 61 62 63 64 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 60 def cost(statement) result = explain(statement) match = /cost=\d+\.\d+..(\d+\.\d+) /.match(result) match[1] if match end |
#explain(statement) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 66 def explain(statement) if postgresql? || redshift? connection_model.connection.select_all("EXPLAIN #{statement}").rows.first.first end rescue nil end |
#preview_statement ⇒ Object
52 53 54 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 52 def preview_statement "SELECT * FROM {table} LIMIT 10" end |
#reconnect ⇒ Object
56 57 58 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 56 def reconnect connection_model.establish_connection(settings["url"]) end |
#run_statement(statement, comment) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 18 def run_statement(statement, comment) columns = [] rows = [] error = nil begin in_transaction do set_timeout(data_source.timeout) if data_source.timeout result = connection_model.connection.select_all("#{statement} /*#{comment}*/") columns = result.columns cast_method = Rails::VERSION::MAJOR < 5 ? :type_cast : :cast_value result.rows.each do |untyped_row| rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| untyped_row[i] ? result.column_types[c].send(cast_method, untyped_row[i]) : nil }) end end rescue ActiveRecord::StatementInvalid => e error = e..sub(/.+ERROR: /, "") error = Blazer::TIMEOUT_MESSAGE if Blazer::TIMEOUT_ERRORS.any? { |e| error.include?(e) } end [columns, rows, error] end |
#schema ⇒ Object
47 48 49 50 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 47 def schema result = data_source.run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_schema, table_name, column_name, data_type, ordinal_position FROM information_schema.columns WHERE table_schema IN (?) ORDER BY 1, 2", schemas])) result.rows.group_by { |r| [r[0], r[1]] }.map { |k, vs| {schema: k[0], table: k[1], columns: vs.sort_by { |v| v[4].to_i }.map { |v| {name: v[2], data_type: v[3]} }} } end |
#tables ⇒ Object
42 43 44 45 |
# File 'lib/blazer/adapters/sql_adapter.rb', line 42 def tables result = data_source.run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name FROM information_schema.tables WHERE table_schema IN (?) ORDER BY table_name", schemas])) result.rows.map(&:first) end |