Module: RDBI

Extended by:
MethLab
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

Class Method Summary collapse

Class Attribute Details

.all_connectionsObject (readonly)

Every database handle allocated throughout the lifetime of the program. This functionality is subject to change and may be pruned during disconnection.



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

def all_connections
  @all_connections
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.

Yields:

  • (dbh)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rdbi.rb', line 34

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

  @all_connections ||= []
  @all_connections.push(dbh)

  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 a RDBI::Pool. The ‘default’ pool is the … default, but this may be manipulated by providing :pool_name to the connection arguments.

If a pool does not exist already, it will be created and a database handle instanced from your connection arguments.

If a pool already exists, your connection arguments will be ignored and it will instance from the Pool’s connection arguments.

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 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

.disconnect_allObject

Disconnects all known connections. See RDBI.all_connections.



96
97
98
# File 'lib/rdbi.rb', line 96

def self.disconnect_all
  @all_connections.each(&:disconnect)
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 a 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

.reconnect_allObject

Reconnects all known connections. See RDBI.all_connections.



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

def self.reconnect_all
  @all_connections.each(&:reconnect)
end