Module: RDBI

Defined in:
lib/rdbi.rb,
lib/rdbi/version.rb

Defined Under Namespace

Modules: Type, Util Classes: Column, Cursor, Database, DisconnectedError, Driver, Error, Pool, Result, Schema, Statement, TransactionError

Constant Summary collapse

VERSION =
'1.2.2'

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.



9
10
11
# File 'lib/rdbi.rb', line 9

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rdbi.rb', line 27

def self.connect(klass, *args)

  klass = case klass
          when ::Class
            klass
          when ::String, ::Symbol
            Util.resolve_driver(klass)
          else
            raise ArgumentError.new("Invalid driver specification")
          end

  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)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rdbi.rb', line 63

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.



89
90
91
# File 'lib/rdbi.rb', line 89

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

.pool(pool_name = :default) ⇒ Object

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



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

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