Class: RDBr::Catalog::Connection
- Inherits:
-
Object
- Object
- RDBr::Catalog::Connection
- Defined in:
- lib/rdbr/catalog/connection.rb
Defined Under Namespace
Classes: Record
Instance Method Summary collapse
- #adapter_name ⇒ Object
- #database_product ⇒ Object
- #database_version ⇒ Object
- #disconnect ⇒ Object
-
#initialize(database_url) ⇒ Connection
constructor
A new instance of Connection.
- #quote_identifier(identifier) ⇒ Object
- #select_all(sql) ⇒ Object
- #select_all_optional(sql) ⇒ Object
Constructor Details
#initialize(database_url) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 17 |
# File 'lib/rdbr/catalog/connection.rb', line 12 def initialize(database_url) @record_class = Record @record_class.establish_connection(database_url) rescue ActiveRecord::AdapterNotSpecified, ActiveRecord::AdapterNotFound, ArgumentError => e raise ConnectionError, "Unable to configure database connection (#{e.class})" end |
Instance Method Details
#adapter_name ⇒ Object
19 20 21 22 23 |
# File 'lib/rdbr/catalog/connection.rb', line 19 def adapter_name connection.adapter_name.downcase rescue ActiveRecord::ActiveRecordError => e raise ConnectionError, "Unable to connect to database (#{e.class})" end |
#database_product ⇒ Object
31 32 33 34 35 |
# File 'lib/rdbr/catalog/connection.rb', line 31 def database_product connection.select_value('SELECT VERSION()') rescue ActiveRecord::ActiveRecordError => e raise ConnectionError, "Unable to read database product (#{e.class})" end |
#database_version ⇒ Object
25 26 27 28 29 |
# File 'lib/rdbr/catalog/connection.rb', line 25 def database_version connection.database_version rescue ActiveRecord::ActiveRecordError => e raise ConnectionError, "Unable to read database version (#{e.class})" end |
#disconnect ⇒ Object
57 58 59 |
# File 'lib/rdbr/catalog/connection.rb', line 57 def disconnect @record_class.connection_pool.disconnect! end |
#quote_identifier(identifier) ⇒ Object
53 54 55 |
# File 'lib/rdbr/catalog/connection.rb', line 53 def quote_identifier(identifier) connection.quote_column_name(identifier.to_s) end |
#select_all(sql) ⇒ Object
37 38 39 40 41 |
# File 'lib/rdbr/catalog/connection.rb', line 37 def select_all(sql) connection.select_all(sql).to_a rescue ActiveRecord::ActiveRecordError => e raise ConnectionError, "Database catalog query failed (#{e.class})" end |
#select_all_optional(sql) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/rdbr/catalog/connection.rb', line 43 def select_all_optional(sql) connection.select_all(sql).to_a rescue ActiveRecord::StatementInvalid [] rescue ActiveRecord::ActiveRecordError => e raise ConnectionError, "Database catalog query failed (#{e.class})" end |