Class: Elefant::ConnectionAdapter
- Inherits:
-
Object
- Object
- Elefant::ConnectionAdapter
- Defined in:
- lib/elefant/connection_adapter.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #active_record? ⇒ Boolean
- #alive? ⇒ Boolean
- #db_name ⇒ Object
- #disconnect ⇒ Object
- #execute(stmt, params = []) ⇒ Object
- #info ⇒ Object
-
#initialize(connection = nil) ⇒ ConnectionAdapter
constructor
A new instance of ConnectionAdapter.
Constructor Details
#initialize(connection = nil) ⇒ ConnectionAdapter
Returns a new instance of ConnectionAdapter.
9 10 11 |
# File 'lib/elefant/connection_adapter.rb', line 9 def initialize(connection=nil) @connection = connection.nil? ? establish_new : validate!(connection) end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
7 8 9 |
# File 'lib/elefant/connection_adapter.rb', line 7 def connection @connection end |
Instance Method Details
#active_record? ⇒ Boolean
61 62 63 |
# File 'lib/elefant/connection_adapter.rb', line 61 def active_record? defined?(ActiveRecord::Base) == "constant" && ActiveRecord::Base.class == Class && !Elefant.configuration.disable_ar end |
#alive? ⇒ Boolean
42 43 44 45 46 47 |
# File 'lib/elefant/connection_adapter.rb', line 42 def alive? @connection.query "SELECT 1" true rescue PGError false end |
#db_name ⇒ Object
57 58 59 |
# File 'lib/elefant/connection_adapter.rb', line 57 def db_name @connection.db end |
#disconnect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/elefant/connection_adapter.rb', line 28 def disconnect begin if active_record? @connection = nil ActiveRecord::Base.clear_active_connections! else @connection.close end rescue => e # TODO: log something and do sensible stuff raise e end end |
#execute(stmt, params = []) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/elefant/connection_adapter.rb', line 13 def execute(stmt, params = []) begin params = nil if params.empty? r = @connection.exec(stmt, params) result = [] r.each do |t| result << t end result rescue PGError => e @connection.reset raise e end end |
#info ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/elefant/connection_adapter.rb', line 49 def info @info ||= { db_name: @connection.db, server_version: version_str(connection.server_version), client_version: (PG.respond_to?( :library_version ) ? version_str(PG.library_version) : 'unknown') } end |