dbi-dbrc 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ == 1.1.2 - 18-Jul-2008
2
+ * Changed platform checking from RUBY_PLATFORM to Config::CONFIG['host_os']
3
+ so that other implementations won't choke.
4
+ * Updated the gemspec to add the sys-admin dependency.
5
+ * Added a rubyforge_project to the gemspec.
6
+ * Now has a separate gem for MS Windows.
7
+
1
8
  == 1.1.1 - 2-Aug-2007
2
9
  * DBRCError is now DBRC::Error.
3
10
  * Added a Rakefile with tasks for installation and testing.
@@ -1,21 +1,21 @@
1
- ###########################################################################
2
- # test.rb
3
- #
4
- # This script is provided for those without TestUnit installed and/or for
5
- # general futzing.
6
- ###########################################################################
7
- if File.basename(Dir.pwd) == "plain"
8
- Dir.chdir "../.."
9
- $LOAD_PATH.unshift Dir.pwd + "/lib"
10
- Dir.chdir "examples/plain"
11
- end
12
-
13
- require "pp"
14
- require "dbi/dbrc"
15
- include DBI
16
-
17
- puts "VERSION: " + DBRC::VERSION
18
-
19
- db = DBRC.new("foo","user1",Dir.pwd)
20
-
21
- pp db
1
+ ###########################################################################
2
+ # test.rb
3
+ #
4
+ # This script is provided for those without TestUnit installed and/or for
5
+ # general futzing.
6
+ ###########################################################################
7
+ if File.basename(Dir.pwd) == "plain"
8
+ Dir.chdir "../.."
9
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
10
+ Dir.chdir "examples/plain"
11
+ end
12
+
13
+ require "pp"
14
+ require "dbi/dbrc"
15
+ include DBI
16
+
17
+ puts "VERSION: " + DBRC::VERSION
18
+
19
+ db = DBRC.new("foo","user1",Dir.pwd)
20
+
21
+ pp db
@@ -1,28 +1,28 @@
1
- #######################################################################
2
- # test_xml.rb
3
- #
4
- # Simple test script that uses the DBRC::XML subclass.
5
- #######################################################################
6
- if File.basename(Dir.pwd) == "xml"
7
- Dir.chdir "../.."
8
- $LOAD_PATH.unshift Dir.pwd + "/lib"
9
- Dir.chdir "examples/xml"
10
- end
11
-
12
- require "dbi/dbrc"
13
- require "pp"
14
- include DBI
15
-
16
- puts "VERSION: " + DBRC::XML::VERSION
17
-
18
- # Use the .dbrc file in this directory
19
- db1 = DBRC::XML.new("foo",nil,Dir.pwd) # Get first entry found for 'foo'
20
- db2 = DBRC::XML.new("foo","user1",Dir.pwd) # Specify user
21
-
22
- puts "First entry found for 'foo': "
23
- pp db1
24
- puts "=" * 20
25
-
26
- puts "Entry for 'foo' with user 'bar': "
27
- pp db2
28
- puts "=" * 20
1
+ #######################################################################
2
+ # test_xml.rb
3
+ #
4
+ # Simple test script that uses the DBRC::XML subclass.
5
+ #######################################################################
6
+ if File.basename(Dir.pwd) == "xml"
7
+ Dir.chdir "../.."
8
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
9
+ Dir.chdir "examples/xml"
10
+ end
11
+
12
+ require "dbi/dbrc"
13
+ require "pp"
14
+ include DBI
15
+
16
+ puts "VERSION: " + DBRC::XML::VERSION
17
+
18
+ # Use the .dbrc file in this directory
19
+ db1 = DBRC::XML.new("foo",nil,Dir.pwd) # Get first entry found for 'foo'
20
+ db2 = DBRC::XML.new("foo","user1",Dir.pwd) # Specify user
21
+
22
+ puts "First entry found for 'foo': "
23
+ pp db1
24
+ puts "=" * 20
25
+
26
+ puts "Entry for 'foo' with user 'bar': "
27
+ pp db2
28
+ puts "=" * 20
@@ -1,28 +1,28 @@
1
- #######################################################################
2
- # test_xml.rb
3
- #
4
- # Simple test script that uses the DBRC::XML subclass.
5
- #######################################################################
6
- if File.basename(Dir.pwd) == "yml"
7
- Dir.chdir "../.."
8
- $LOAD_PATH.unshift Dir.pwd + "/lib"
9
- Dir.chdir "examples/yml"
10
- end
11
-
12
- require "dbi/dbrc"
13
- require "pp"
14
- include DBI
15
-
16
- puts "VERSION: " + DBRC::YML::VERSION
17
-
18
- # Use the .dbrc file in this directory
19
- db1 = DBRC::YML.new("foo",nil,Dir.pwd) # Get first entry found for 'foo'
20
- db2 = DBRC::YML.new("foo","user1",Dir.pwd) # Specify user
21
-
22
- puts "First entry found for 'foo': "
23
- pp db1
24
- puts "=" * 20
25
-
26
- puts "Entry for 'foo' with user 'bar': "
27
- pp db2
28
- puts "=" * 20
1
+ #######################################################################
2
+ # test_xml.rb
3
+ #
4
+ # Simple test script that uses the DBRC::XML subclass.
5
+ #######################################################################
6
+ if File.basename(Dir.pwd) == "yml"
7
+ Dir.chdir "../.."
8
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
9
+ Dir.chdir "examples/yml"
10
+ end
11
+
12
+ require "dbi/dbrc"
13
+ require "pp"
14
+ include DBI
15
+
16
+ puts "VERSION: " + DBRC::YML::VERSION
17
+
18
+ # Use the .dbrc file in this directory
19
+ db1 = DBRC::YML.new("foo",nil,Dir.pwd) # Get first entry found for 'foo'
20
+ db2 = DBRC::YML.new("foo","user1",Dir.pwd) # Specify user
21
+
22
+ puts "First entry found for 'foo': "
23
+ pp db1
24
+ puts "=" * 20
25
+
26
+ puts "Entry for 'foo' with user 'bar': "
27
+ pp db2
28
+ puts "=" * 20
@@ -1,229 +1,231 @@
1
- if RUBY_PLATFORM.match('mswin')
2
- require 'win32/file'
3
- require 'win32/dir'
4
- end
5
-
6
- require 'sys/admin'
7
- include Sys
8
-
9
- # The DBI module serves as a namespace only.
10
- module DBI
11
-
12
- # The DBRC class encapsulates a database resource config file.
13
- class DBRC
14
-
15
- # This error is raised if anything fails trying to read the config file.
16
- # the error that is raised.
17
- class Error < StandardError; end
18
-
19
- VERSION = '1.1.1'
20
- attr_accessor :database, :user, :password, :driver, :dsn
21
- attr_accessor :maximum_reconnects, :timeout, :interval, :dbrc_dir
22
-
23
- # Returns a new DBRC object. The contents of the object depend on the
24
- # arguments passed to the constructor. If only a database name is
25
- # passed, then the first entry found in the .dbrc file that matches that
26
- # database is parsed. If a user name is also included, then the first
27
- # entry that matches both the database and user name is parsed.
28
- #
29
- # If a directory is passed as the third argument, then DBRC will look
30
- # in that directory, instead of the default directory, for the .dbrc
31
- # file.
32
- #
33
- # If an entry cannot be found for the database, or database plus user
34
- # combination, then a Error is raised. If the .dbrc file cannot
35
- # be found, or is setup improperly with regards to permissions or
36
- # properties, a Error is raised.
37
- #
38
- def initialize(database, user=nil, dbrc_dir=nil)
39
- if dbrc_dir.nil?
40
- if RUBY_PLATFORM.match('mswin')
41
- home = ENV['USERPROFILE'] || ENV['HOME']
42
- file = nil
43
-
44
- if home
45
- file = File.join(home, '.dbrc')
46
- else
47
- file = File.join(File.basename(Dir::APPDATA), '.dbrc')
48
- end
49
-
50
- @dbrc_file = file
51
- else
52
- @dbrc_file = File.join(Admin.get_user(Process.uid).dir, '.dbrc')
53
- end
54
- else
55
- @dbrc_file = File.join(dbrc_dir, '.dbrc')
56
- end
57
-
58
- @database = database
59
- @user = user
60
- encrypted = false # Win32 only
61
-
62
- @driver = nil
63
- @interval = nil
64
- @timeout = nil
65
- @maximum_reconnects = nil
66
-
67
- check_file()
68
-
69
- # If on Win32 and the file is encrypted, decrypt it.
70
- if RUBY_PLATFORM.match("mswin") && File.encrypted?(@dbrc_file)
71
- encrypted = true
72
- File.decrypt(@dbrc_file)
73
- end
74
-
75
- parse_dbrc_config_file()
76
- validate_data()
77
- convert_numeric_strings()
78
- create_dsn_string()
79
-
80
- # If on Win32 and the file was encrypted, re-encrypt it
81
- if RUBY_PLATFORM.match("mswin") && encrypted
82
- File.encrypt(@dbrc_file)
83
- end
84
- end
85
-
86
- private
87
-
88
- # Ensure that the user/password has been set
89
- def validate_data
90
- unless @user
91
- raise Error, "no user found associated with #{@database}"
92
- end
93
-
94
- unless @password
95
- raise Error, "password not defined for #{@user}@#{@database}"
96
- end
97
- end
98
-
99
- # Converts strings that should be numbers into actual numbers
100
- def convert_numeric_strings
101
- @interval = @interval.to_i if @interval
102
- @timeout = @timeout.to_i if @timeout
103
- @maximum_reconnects = @maximum_reconnects.to_i if @maximum_reconnects
104
- end
105
-
106
- # Create the dsn string if the driver is defined
107
- def create_dsn_string
108
- @dsn = "dbi:#{@driver}:#{@database}" if @driver
109
- end
110
-
111
- # Check ownership and permissions
112
- def check_file(file=@dbrc_file)
113
- File.open(file){ |f|
114
-
115
- # Permissions must be set to 600 or better on Unix systems.
116
- # Must be hidden on Win32 systems.
117
- if RUBY_PLATFORM.match("mswin")
118
- unless File.hidden?(file)
119
- raise Error, "The .dbrc file must be hidden"
120
- end
121
- else
122
- unless (f.stat.mode & 077) == 0
123
- raise Error, "Bad .dbrc file permissions"
124
- end
125
- end
126
-
127
- # Only the owner may use it
128
- unless f.stat.owned?
129
- raise Error, "Not owner of .dbrc file"
130
- end
131
- }
132
- end
133
-
134
- # Parse the text out of the .dbrc file. This is the only method you
135
- # need to redefine if writing your own config handler.
136
- def parse_dbrc_config_file(file=@dbrc_file)
137
- IO.foreach(file){ |line|
138
- next if line =~ /^#/ # Ignore comments
139
- db, user, pwd, driver, timeout, max, interval = line.split
140
-
141
- next unless @database == db
142
-
143
- if @user
144
- next unless @user == user
145
- end
146
-
147
- @user = user
148
- @password = pwd
149
- @driver = driver
150
- @timeout = timeout
151
- @maximum_reconnects = max
152
- @interval = interval
153
- return
154
- }
155
-
156
- # If we reach here it means the database and/or user wasn't found
157
- if @user
158
- err = "no record found for #{@user}@#{@database}"
159
- else
160
- err = "no record found for #{@database}"
161
- end
162
-
163
- raise Error, err
164
- end
165
-
166
- alias_method(:db,:database)
167
- alias_method(:db=,:database=)
168
- alias_method(:passwd,:password)
169
- alias_method(:passwd=,:password=)
170
- alias_method(:max_reconn,:maximum_reconnects)
171
- alias_method(:max_reconn=,:maximum_reconnects=)
172
- alias_method(:time_out,:timeout)
173
- alias_method(:time_out=,:timeout=)
174
- end
175
-
176
- # A subclass of DBRC designed to handle .dbrc files in XML format. The
177
- # public methods of this class are identical to DBRC.
178
- class XML < DBRC
179
- require "rexml/document"
180
- include REXML
181
- private
182
- def parse_dbrc_config_file(file=@dbrc_file)
183
- doc = Document.new(File.new(file))
184
- fields = %w/user password driver interval timeout maximum_reconnects/
185
- doc.elements.each("/dbrc/database"){ |element|
186
- next unless element.attributes["name"] == database
187
- if @user
188
- next unless element.elements["user"].text == @user
189
- end
190
- fields.each{ |field|
191
- val = element.elements[field]
192
- unless val.nil?
193
- send("#{field}=",val.text)
194
- end
195
- }
196
- return
197
- }
198
- # If we reach here it means the database and/or user wasn't found
199
- raise Error, "No record found for #{@user}@#{@database}"
200
- end
201
- end
202
-
203
- # A subclass of DBRC designed to handle .dbrc files in YAML format. The
204
- # public methods of this class are identical to DBRC.
205
- class YML < DBRC
206
- require "yaml"
207
- private
208
- def parse_dbrc_config_file(file=@dbrc_file)
209
- config = YAML.load(File.open(file))
210
- config.each{ |hash|
211
- hash.each{ |db,info|
212
- next unless db == @database
213
- if @user
214
- next unless @user == info["user"]
215
- end
216
- @user = info["user"]
217
- @password = info["password"]
218
- @driver = info["driver"]
219
- @interval = info["interval"]
220
- @timeout = info["timeout"]
221
- @maximum_reconnects = info["max_reconn"]
222
- return
223
- }
224
- }
225
- # If we reach this point, it means the database wasn't found
226
- raise Error, "No entry found for #{@user}@#{@database}"
227
- end
228
- end
229
- end
1
+ require 'rbconfig'
2
+
3
+ if Config::CONFIG['host_os'].match('mswin')
4
+ require 'win32/file'
5
+ require 'win32/dir'
6
+ end
7
+
8
+ require 'sys/admin'
9
+ include Sys
10
+
11
+ # The DBI module serves as a namespace only.
12
+ module DBI
13
+
14
+ # The DBRC class encapsulates a database resource config file.
15
+ class DBRC
16
+
17
+ # This error is raised if anything fails trying to read the config file.
18
+ # the error that is raised.
19
+ class Error < StandardError; end
20
+
21
+ VERSION = '1.1.2'
22
+ attr_accessor :database, :user, :password, :driver, :dsn
23
+ attr_accessor :maximum_reconnects, :timeout, :interval, :dbrc_dir
24
+
25
+ # Returns a new DBRC object. The contents of the object depend on the
26
+ # arguments passed to the constructor. If only a database name is
27
+ # passed, then the first entry found in the .dbrc file that matches that
28
+ # database is parsed. If a user name is also included, then the first
29
+ # entry that matches both the database and user name is parsed.
30
+ #
31
+ # If a directory is passed as the third argument, then DBRC will look
32
+ # in that directory, instead of the default directory, for the .dbrc
33
+ # file.
34
+ #
35
+ # If an entry cannot be found for the database, or database plus user
36
+ # combination, then a Error is raised. If the .dbrc file cannot
37
+ # be found, or is setup improperly with regards to permissions or
38
+ # properties, a Error is raised.
39
+ #
40
+ def initialize(database, user=nil, dbrc_dir=nil)
41
+ if dbrc_dir.nil?
42
+ if RUBY_PLATFORM.match('mswin')
43
+ home = ENV['USERPROFILE'] || ENV['HOME']
44
+ file = nil
45
+
46
+ if home
47
+ file = File.join(home, '.dbrc')
48
+ else
49
+ file = File.join(File.basename(Dir::APPDATA), '.dbrc')
50
+ end
51
+
52
+ @dbrc_file = file
53
+ else
54
+ @dbrc_file = File.join(Admin.get_user(Process.uid).dir, '.dbrc')
55
+ end
56
+ else
57
+ @dbrc_file = File.join(dbrc_dir, '.dbrc')
58
+ end
59
+
60
+ @database = database
61
+ @user = user
62
+ encrypted = false # Win32 only
63
+
64
+ @driver = nil
65
+ @interval = nil
66
+ @timeout = nil
67
+ @maximum_reconnects = nil
68
+
69
+ check_file()
70
+
71
+ # If on Win32 and the file is encrypted, decrypt it.
72
+ if RUBY_PLATFORM.match("mswin") && File.encrypted?(@dbrc_file)
73
+ encrypted = true
74
+ File.decrypt(@dbrc_file)
75
+ end
76
+
77
+ parse_dbrc_config_file()
78
+ validate_data()
79
+ convert_numeric_strings()
80
+ create_dsn_string()
81
+
82
+ # If on Win32 and the file was encrypted, re-encrypt it
83
+ if RUBY_PLATFORM.match("mswin") && encrypted
84
+ File.encrypt(@dbrc_file)
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ # Ensure that the user/password has been set
91
+ def validate_data
92
+ unless @user
93
+ raise Error, "no user found associated with #{@database}"
94
+ end
95
+
96
+ unless @password
97
+ raise Error, "password not defined for #{@user}@#{@database}"
98
+ end
99
+ end
100
+
101
+ # Converts strings that should be numbers into actual numbers
102
+ def convert_numeric_strings
103
+ @interval = @interval.to_i if @interval
104
+ @timeout = @timeout.to_i if @timeout
105
+ @maximum_reconnects = @maximum_reconnects.to_i if @maximum_reconnects
106
+ end
107
+
108
+ # Create the dsn string if the driver is defined
109
+ def create_dsn_string
110
+ @dsn = "dbi:#{@driver}:#{@database}" if @driver
111
+ end
112
+
113
+ # Check ownership and permissions
114
+ def check_file(file=@dbrc_file)
115
+ File.open(file){ |f|
116
+
117
+ # Permissions must be set to 600 or better on Unix systems.
118
+ # Must be hidden on Win32 systems.
119
+ if RUBY_PLATFORM.match("mswin")
120
+ unless File.hidden?(file)
121
+ raise Error, "The .dbrc file must be hidden"
122
+ end
123
+ else
124
+ unless (f.stat.mode & 077) == 0
125
+ raise Error, "Bad .dbrc file permissions"
126
+ end
127
+ end
128
+
129
+ # Only the owner may use it
130
+ unless f.stat.owned?
131
+ raise Error, "Not owner of .dbrc file"
132
+ end
133
+ }
134
+ end
135
+
136
+ # Parse the text out of the .dbrc file. This is the only method you
137
+ # need to redefine if writing your own config handler.
138
+ def parse_dbrc_config_file(file=@dbrc_file)
139
+ IO.foreach(file){ |line|
140
+ next if line =~ /^#/ # Ignore comments
141
+ db, user, pwd, driver, timeout, max, interval = line.split
142
+
143
+ next unless @database == db
144
+
145
+ if @user
146
+ next unless @user == user
147
+ end
148
+
149
+ @user = user
150
+ @password = pwd
151
+ @driver = driver
152
+ @timeout = timeout
153
+ @maximum_reconnects = max
154
+ @interval = interval
155
+ return
156
+ }
157
+
158
+ # If we reach here it means the database and/or user wasn't found
159
+ if @user
160
+ err = "no record found for #{@user}@#{@database}"
161
+ else
162
+ err = "no record found for #{@database}"
163
+ end
164
+
165
+ raise Error, err
166
+ end
167
+
168
+ alias_method(:db,:database)
169
+ alias_method(:db=,:database=)
170
+ alias_method(:passwd,:password)
171
+ alias_method(:passwd=,:password=)
172
+ alias_method(:max_reconn,:maximum_reconnects)
173
+ alias_method(:max_reconn=,:maximum_reconnects=)
174
+ alias_method(:time_out,:timeout)
175
+ alias_method(:time_out=,:timeout=)
176
+ end
177
+
178
+ # A subclass of DBRC designed to handle .dbrc files in XML format. The
179
+ # public methods of this class are identical to DBRC.
180
+ class XML < DBRC
181
+ require "rexml/document"
182
+ include REXML
183
+ private
184
+ def parse_dbrc_config_file(file=@dbrc_file)
185
+ doc = Document.new(File.new(file))
186
+ fields = %w/user password driver interval timeout maximum_reconnects/
187
+ doc.elements.each("/dbrc/database"){ |element|
188
+ next unless element.attributes["name"] == database
189
+ if @user
190
+ next unless element.elements["user"].text == @user
191
+ end
192
+ fields.each{ |field|
193
+ val = element.elements[field]
194
+ unless val.nil?
195
+ send("#{field}=",val.text)
196
+ end
197
+ }
198
+ return
199
+ }
200
+ # If we reach here it means the database and/or user wasn't found
201
+ raise Error, "No record found for #{@user}@#{@database}"
202
+ end
203
+ end
204
+
205
+ # A subclass of DBRC designed to handle .dbrc files in YAML format. The
206
+ # public methods of this class are identical to DBRC.
207
+ class YML < DBRC
208
+ require "yaml"
209
+ private
210
+ def parse_dbrc_config_file(file=@dbrc_file)
211
+ config = YAML.load(File.open(file))
212
+ config.each{ |hash|
213
+ hash.each{ |db,info|
214
+ next unless db == @database
215
+ if @user
216
+ next unless @user == info["user"]
217
+ end
218
+ @user = info["user"]
219
+ @password = info["password"]
220
+ @driver = info["driver"]
221
+ @interval = info["interval"]
222
+ @timeout = info["timeout"]
223
+ @maximum_reconnects = info["max_reconn"]
224
+ return
225
+ }
226
+ }
227
+ # If we reach this point, it means the database wasn't found
228
+ raise Error, "No entry found for #{@user}@#{@database}"
229
+ end
230
+ end
231
+ end