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

Constant Summary collapse

VERSION =
'1.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.last_dbhObject

The last database handle allocated. This may come from pooled connections or regular ones.



11
12
13
# File 'lib/rdbi.rb', line 11

def last_dbh
  @last_dbh
end

Class Method Details

.connect(klass, *args) ⇒ 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, and the connection will be automatically disconnected at the end of the block.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rdbi.rb', line 29

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

  return dbh unless block_given?

  begin
      yield dbh
  ensure
      dbh.disconnect rescue nil
  end
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.

If a block is provided, the connection is not disconnected at the end of the block.

Yields:

  • (dbh)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rdbi.rb', line 58

def self.connect_cached(klass, *args)
  args = args[0]
  pool_name = args[:pool_name] || :default

  dbh = nil

  if pool = RDBI::Pool[pool_name]
    dbh = pool.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.



84
85
86
# File 'lib/rdbi.rb', line 84

def self.ping(klass, *args)
  connect(klass, *args).ping
end

.pool(pool_name = :default) ⇒ Object

Retrieves an RDBI::Pool. See RDBI::Pool.[].



78
79
80
# File 'lib/rdbi.rb', line 78

def self.pool(pool_name=:default)
  RDBI::Pool[pool_name]
end