class Ensql::ActiveRecordAdapter

Wraps an ActiveRecord connection pool to implement the {Adapter} interface for ActiveRecord. Requires an ActiveRecord connection to be configured and established. By default, uses the connection pool on ActiveRecord::Base. Other pools can be passed to the constructor.

@example

require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'mydb')
Ensql.adapter = Ensql::ActiveRecordAdapter.new
# Use database configuration for the Widget model instead
Ensql.adapter = Ensql::ActiveRecordAdapter.new(Widget)

@see SUPPORTED_ACTIVERECORD_VERSIONS

Public Class Methods

new(base = ActiveRecord::Base) click to toggle source

@param base [Class] an ActiveRecord class to source connections from

# File lib/ensql/active_record_adapter.rb, line 53
def initialize(base = ActiveRecord::Base)
  @base = base
end
pool(base = ActiveRecord::Base) click to toggle source

Wrap the raw connections from an Active Record connection pool. This allows us to safely checkout the underlying database connection for use in a database specific adapter.

Ensql.adapter = MySqliteAdapter.new(ActiveRecordAdapter.pool)

@param base [Class] an ActiveRecord class to source connections from @return [PoolWrapper] a pool adapter for raw connections

# File lib/ensql/active_record_adapter.rb, line 38
def self.pool(base = ActiveRecord::Base)
  PoolWrapper.new do |client_block|
    base.connection_pool.with_connection { |connection| client_block.call connection.raw_connection }
  end
end

Public Instance Methods

fetch_count(sql) click to toggle source

@visibility private

# File lib/ensql/active_record_adapter.rb, line 84
def fetch_count(sql)
  with_connection { |c| c.exec_update(sql) }
end
fetch_each_row(sql) { |deserialize_types(row, column_types)| ... } click to toggle source

@visibility private

# File lib/ensql/active_record_adapter.rb, line 63
def fetch_each_row(sql, &block)
  return to_enum(:fetch_each_row, sql) unless block

  result = with_connection { |c| c.exec_query(sql) }
  # AR populates `column_types` with the types of any columns that haven't
  # already been type casted by pg decoders. If present, we need to
  # deserialize them now.
  if result.column_types.any?
    result.each { |row| yield deserialize_types(row, result.column_types) }
  else
    result.each(&block)
  end
end
fetch_rows(sql) click to toggle source

@visibility private

# File lib/ensql/active_record_adapter.rb, line 58
def fetch_rows(sql)
  fetch_each_row(sql).to_a
end
literalize(value) click to toggle source

@visibility private

# File lib/ensql/active_record_adapter.rb, line 89
def literalize(value)
  with_connection { |c| c.quote(value) }
end
run(sql) click to toggle source

@visibility private

# File lib/ensql/active_record_adapter.rb, line 78
def run(sql)
  with_connection { |c| c.execute(sql) }
  nil
end

Private Instance Methods

deserialize_types(row, column_types) click to toggle source
# File lib/ensql/active_record_adapter.rb, line 99
def deserialize_types(row, column_types)
  column_types.each { |column, type| row[column] = type.deserialize(row[column]) }
  row
end
with_connection(&block) click to toggle source
# File lib/ensql/active_record_adapter.rb, line 95
def with_connection(&block)
  @base.connection_pool.with_connection(&block)
end