Module: RDBI
- Defined in:
- lib/rdbi.rb
Defined Under Namespace
Modules: Type, Util Classes: Column, Cursor, Database, DisconnectedError, Driver, Error, Pool, Result, Schema, Statement, TransactionError
Class Attribute Summary collapse
-
.last_dbh ⇒ Object
The last database handle allocated.
Class Method Summary collapse
-
.connect(klass, *args) {|dbh| ... } ⇒ Object
connect() takes a class name, which may be represented as:.
-
.connect_cached(klass, *args) {|dbh| ... } ⇒ Object
connect_cached() works similarly to connect, but yields a database handle copied from an RDBI::Pool.
-
.ping(klass, *args) ⇒ Object
Connects to and pings the database.
-
.pool(pool_name = :default) ⇒ Object
Retrieves an RDBI::Pool.
Class Attribute Details
.last_dbh ⇒ Object
The last database handle allocated. This may come from pooled connections or regular ones.
8 9 10 |
# File 'lib/rdbi.rb', line 8 def last_dbh @last_dbh end |
Class Method Details
.connect(klass, *args) {|dbh| ... } ⇒ Object
connect() takes a class name, which may be represented as:
-
The full class name, such as RDBI::Driver::Mock
-
A symbol representing the significant portion, such as :Mock, which corresponds to RDBI::Driver::Mock
-
A string representing the same data as the symbol.
Additionally, arguments that are passed on to the driver for consumption may be passed. Please refer to the driver documentation for more information.
connect() returns an instance of RDBI::Database. In the instance a block is provided, it will be called upon connection success, with the RDBI::Database object provided in as the first argument.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rdbi.rb', line 25 def self.connect(klass, *args) klass = RDBI::Util.class_from_class_or_symbol(klass, self::Driver) driver = klass.new(*args) dbh = self.last_dbh = driver.new_handle yield dbh if block_given? return dbh end |
.connect_cached(klass, *args) {|dbh| ... } ⇒ Object
connect_cached() works similarly to connect, but yields a database handle copied from an RDBI::Pool. The ‘default’ pool is the … default, but this may be manipulated by setting :pool_name in the connection arguments.
If a pool does not exist already, it will be created and a database handle instantiated using your connection arguments.
If a pool already exists, your connection arguments will be ignored and it will instantiate from the Pool’s connection arguments.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rdbi.rb', line 46 def self.connect_cached(klass, *args) args = args[0] pool_name = args[:pool_name] || :default dbh = nil if RDBI::Pool[pool_name] dbh = RDBI::Pool[pool_name].get_dbh else dbh = RDBI::Pool.new(pool_name, [klass, args]).get_dbh end self.last_dbh = dbh yield dbh if block_given? return dbh end |
.ping(klass, *args) ⇒ Object
Connects to and pings the database. Arguments are the same as for RDBI.connect.
72 73 74 |
# File 'lib/rdbi.rb', line 72 def self.ping(klass, *args) connect(klass, *args).ping end |