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.1.9'
@@windows =
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')


86
87
88
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
144
145
# File 'lib/dbi/dbrc.rb', line 86

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



24
25
26
# File 'lib/dbi/dbrc.rb', line 24

def database
  @database
end

#dbrc_dirObject

The directory where the .dbrc file is stored.



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

def dbrc_dir
  @dbrc_dir
end

#dbrc_fileObject

The full path to the .dbrc file.



52
53
54
# File 'lib/dbi/dbrc.rb', line 52

def dbrc_file
  @dbrc_file
end

#driverObject

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



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

def driver
  @driver
end

#dsnObject

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



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

def dsn
  @dsn
end

#intervalObject

The interval, in seconds, between each connection attempt.



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

def interval
  @interval
end

#maximum_reconnectsObject Also known as: max_reconn

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



40
41
42
# File 'lib/dbi/dbrc.rb', line 40

def maximum_reconnects
  @maximum_reconnects
end

#passwordObject Also known as: passwd

The password associated with the database or host.



30
31
32
# File 'lib/dbi/dbrc.rb', line 30

def password
  @password
end

#timeoutObject Also known as: time_out

The timeout, in seconds, for each connection attempt.



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

def timeout
  @timeout
end

#userObject

The user name used for the database or host connection.



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

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.



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dbi/dbrc.rb', line 150

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