Module: Patriot::Util::DBClient

Included in:
JobStore::RDBJobStore
Defined in:
lib/patriot/util/db_client.rb,
lib/patriot/util/db_client/base.rb,
lib/patriot/util/db_client/record.rb,
lib/patriot/util/db_client/hash_record.rb

Overview

name space for connectors of RDBs

Defined Under Namespace

Classes: Base, HashRecord, Record

Constant Summary collapse

DB_CONFIG_KEYS =

a set of configuration keys for the connectors

["adapter", 
"database", 
"host", 
"port", 
"username", 
"password", 
"encoding", 
"pool"]

Instance Method Summary collapse

Instance Method Details

#connect(dbconf) {|a| ... } ⇒ Object

get connection to db. if block is given, call the given block and close the connection

Parameters:

  • dbconf (Hash)

    configuration for database type of database should be set with key :adapter

Yields:

  • block executed with a connection to the database. the connection is passed to the block as a argument

Yield Parameters:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/patriot/util/db_client.rb', line 41

def connect(dbconf, &blk)
  client = get_db_client(dbconf)
  return client unless block_given?
  begin 
    return yield client
  rescue Exception => e
    raise e
  ensure 
    client.close
  end
end

#read_dbconfig(prefix, config) ⇒ Object

read DB configuration from a given config

Parameters:



23
24
25
26
27
28
29
30
# File 'lib/patriot/util/db_client.rb', line 23

def read_dbconfig(prefix, config)
  db_config = {}
  Patriot::Util::DBClient::DB_CONFIG_KEYS.each do |k|
    v = config.get([prefix,k].join("."))
    db_config[k.to_sym] = v
  end
  return db_config
end