Class: DBI::DBRC

Inherits:
Object
  • Object
show all
Defined in:
lib/dbi/dbrc.rb

Overview

The DBRC class encapsulates a database resource config file.

Direct Known Subclasses

XML, YML

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =

The version of the dbi-dbrc library

'1.4.0'
WINDOWS =

:no-doc:

File::ALT_SEPARATOR

Instance Attribute Summary collapse

Instance Method Summary collapse

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


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
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dbi/dbrc.rb', line 102

def initialize(database, user=nil, dbrc_dir=nil)
  if dbrc_dir.nil?
    if WINDOWS
      home = Sys::Admin.get_user(Process.uid, :localaccount => true).dir
    else
      home = Dir.home(Etc.getpwuid.name)
    end

    # Default to the app data directory on Windows, or root on Unix, if
    # no home dir can be found.
    if home.nil?
      if WINDOWS
        home = Dir::APPDATA
      else
        home = '/'
      end
    end

    @dbrc_file = File.join(home, '.dbrc')
  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

  file_was_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

#databaseObject Also known as: db, host

The database or host to be connected to.



26
27
28
# File 'lib/dbi/dbrc.rb', line 26

def database
  @database
end

#dbrc_dirObject

The directory where the .dbrc file is stored.



65
66
67
# File 'lib/dbi/dbrc.rb', line 65

def dbrc_dir
  @dbrc_dir
end

#dbrc_fileObject

The full path to the .dbrc file.



68
69
70
# File 'lib/dbi/dbrc.rb', line 68

def dbrc_file
  @dbrc_file
end

#driverObject

The driver associated with the database. This is used to internally to construct the DSN.



44
45
46
# File 'lib/dbi/dbrc.rb', line 44

def driver
  @driver
end

#dsnObject

Data source name, e.g. “dbi:OCI8:your_database”.



47
48
49
# File 'lib/dbi/dbrc.rb', line 47

def dsn
  @dsn
end

#intervalObject

The interval, in seconds, between each connection attempt.



62
63
64
# File 'lib/dbi/dbrc.rb', line 62

def interval
  @interval
end

#maximum_reconnectsObject Also known as: max_reconn

The maximum number of reconnects a program should make before giving up.



50
51
52
# File 'lib/dbi/dbrc.rb', line 50

def maximum_reconnects
  @maximum_reconnects
end

#passwordObject Also known as: passwd

The password associated with the database or host.



37
38
39
# File 'lib/dbi/dbrc.rb', line 37

def password
  @password
end

#timeoutObject Also known as: time_out

The timeout, in seconds, for each connection attempt.



56
57
58
# File 'lib/dbi/dbrc.rb', line 56

def timeout
  @timeout
end

#userObject

The user name used for the database or host connection.



34
35
36
# File 'lib/dbi/dbrc.rb', line 34

def user
  @user
end

Instance Method Details

#inspectObject

Inspection of the DBI::DBRC object. This is identical to the standard Ruby Object#inspect, except that the password field is filtered.



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dbi/dbrc.rb', line 161

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