Class: DBI::DBRC
- Inherits:
-
Object
- Object
- DBI::DBRC
- Defined in:
- lib/dbi/dbrc.rb
Overview
The DBRC class encapsulates a database resource config file.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
The version of the dbi-dbrc library
'1.1.7'
- @@windows =
Config::CONFIG['host_os'] =~ /mswin|msdos|win32|mingw|cygwin/i
Instance Attribute Summary collapse
-
#database ⇒ Object
(also: #db, #host)
The database or host to be connected to.
-
#dbrc_dir ⇒ Object
The directory where the .dbrc file is stored.
-
#dbrc_file ⇒ Object
The full path to the .dbrc file.
-
#driver ⇒ Object
The driver associated with the database.
-
#dsn ⇒ Object
Data source name, e.g.
-
#interval ⇒ Object
The interval, in seconds, between each connection attempt.
-
#maximum_reconnects ⇒ Object
(also: #max_reconn)
The maximum number of reconnects a program should make before giving up.
-
#password ⇒ Object
(also: #passwd)
The password associated with the database or host.
-
#timeout ⇒ Object
(also: #time_out)
The timeout, in seconds, for each connection attempt.
-
#user ⇒ Object
The user name used for the database or host connection.
Instance Method Summary collapse
-
#initialize(database, user = nil, dbrc_dir = nil) ⇒ DBRC
constructor
Returns a new DBI::DBRC object.
-
#inspect ⇒ Object
Inspection of the DBI::DBRC object.
Constructor Details
#initialize(database, user = nil, dbrc_dir = nil) ⇒ DBRC
Returns a new DBI::DBRC object. The contents of the object depend on the arguments passed to the constructor. If only a database name is passed, then the first entry found in the .dbrc file that matches that database is parsed. If a user name is also included, then the first entry that matches both the database and user name is parsed.
If a directory is passed as the third argument, then DBRC will look in that directory, instead of the default directory, for the .dbrc file.
If an entry cannot be found for the database, or database plus user combination, then a Error is raised. If the .dbrc file cannot be found, or is setup improperly with regards to permissions or properties then a DBI::DBRC::Error is raised.
See the README for the rules regarding .dbrc files and permissions.
Note that this library can also be used as a general password storage mechanism. In that case simply treat the ‘database’ as the host name, and ignore the DBI::DBRC#dsn and DBI::DBRC#driver methods.
Examples:
# Find the first match for 'some_database'
DBI::DBRC.new('some_database')
# Find the first match for 'foo_user@some_database'
DBI::DBRC.new('some_database', 'foo_user')
# Find the first match for 'foo_user@some_database' under /usr/local
DBI::DBRC.new('some_database', 'foo_usr', '/usr/local')
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dbi/dbrc.rb', line 89 def initialize(database, user=nil, dbrc_dir=nil) if dbrc_dir.nil? uid = Process.uid home = ENV['HOME'] || ENV['USERPROFILE'] if home.nil? if @@windows home ||= Sys::Admin.get_user(uid, :localaccount => true).dir else home ||= Sys::Admin.get_user(uid).dir end end # Default to the app data directory on Windows if no home dir found if @@windows && home.nil? @dbrc_file = File.join(File.basename(Dir::APPDATA), '.dbrc') else uid = Process.uid @dbrc_file = File.join(Sys::Admin.get_user(uid).dir, '.dbrc') end else raise Error, 'bad directory' unless File.directory?(dbrc_dir) @dbrc_file = File.join(dbrc_dir, '.dbrc') end @dbrc_dir = dbrc_dir @database = database @user = user encrypted = false # Win32 only @driver = nil @interval = nil @timeout = nil @maximum_reconnects = nil check_file() # Decrypt and re-encrypt the file if we're on MS Windows and the # file is encrypted. begin if @@windows && File.encrypted?(@dbrc_file) file_was_encrypted = true File.decrypt(@dbrc_file) end parse_dbrc_config_file() validate_data() convert_numeric_strings() create_dsn_string() ensure if @@windows && file_was_encrypted File.encrypt(@dbrc_file) end end end |
Instance Attribute Details
#database ⇒ Object Also known as: db, host
The database or host to be connected to.
27 28 29 |
# File 'lib/dbi/dbrc.rb', line 27 def database @database end |
#dbrc_dir ⇒ Object
The directory where the .dbrc file is stored.
52 53 54 |
# File 'lib/dbi/dbrc.rb', line 52 def dbrc_dir @dbrc_dir end |
#dbrc_file ⇒ Object
The full path to the .dbrc file.
55 56 57 |
# File 'lib/dbi/dbrc.rb', line 55 def dbrc_file @dbrc_file end |
#driver ⇒ Object
The driver associated with the database. This is used to internally to construct the DSN.
37 38 39 |
# File 'lib/dbi/dbrc.rb', line 37 def driver @driver end |
#dsn ⇒ Object
Data source name, e.g. “dbi:OCI8:your_database”.
40 41 42 |
# File 'lib/dbi/dbrc.rb', line 40 def dsn @dsn end |
#interval ⇒ Object
The interval, in seconds, between each connection attempt.
49 50 51 |
# File 'lib/dbi/dbrc.rb', line 49 def interval @interval end |
#maximum_reconnects ⇒ Object Also known as: max_reconn
The maximum number of reconnects a program should make before giving up.
43 44 45 |
# File 'lib/dbi/dbrc.rb', line 43 def maximum_reconnects @maximum_reconnects end |
#password ⇒ Object Also known as: passwd
The password associated with the database or host.
33 34 35 |
# File 'lib/dbi/dbrc.rb', line 33 def password @password end |
#timeout ⇒ Object Also known as: time_out
The timeout, in seconds, for each connection attempt.
46 47 48 |
# File 'lib/dbi/dbrc.rb', line 46 def timeout @timeout end |
#user ⇒ Object
The user name used for the database or host connection.
30 31 32 |
# File 'lib/dbi/dbrc.rb', line 30 def user @user end |
Instance Method Details
#inspect ⇒ Object
Inspection of the DBI::DBRC object. This is identical to the standard Ruby Object#inspect, except that the password field is filtered.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/dbi/dbrc.rb', line 148 def inspect str = instance_variables.map{ |iv| if iv == '@password' "#{iv}=[FILTERED]" else "#{iv}=#{instance_variable_get(iv).inspect}" end }.join(', ') "#<#{self.class}:0x#{(self.object_id*2).to_s(16)} " << str << ">" end |