rdo 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -293,7 +293,7 @@ This method returns nil if there are no rows, so if you need to distinguish
293
293
  between NULL and no rows, you will need to check the result contents the
294
294
  longer way around.
295
295
 
296
- ### Logging Statements
296
+ ### Debugging
297
297
 
298
298
  A Logger instance (with an interface like that in Ruby stdlib) may be passed
299
299
  in the options when creating a connection. All queries will be logged with
@@ -303,6 +303,31 @@ DEBUG severity. Errors will be logged with FATAL severity.
303
303
  RDO.connect("postgres://user:pass@host/db", logger: Logger.new(STDOUT))
304
304
  ```
305
305
 
306
+ A logger with some support for highlighting errors etc and which shows
307
+ query execution times is configured (but disabled) by default. It is
308
+ found at `RDO::ColoredLogger`. You can enable it by specify a log level:
309
+
310
+ ``` ruby
311
+ RDO.connect("postgres://user:pass@host/db", log_level: Logger::DEBUG)
312
+ ```
313
+
314
+ If you want the log output to go somewhere other than stdout, instantiate
315
+ the logger manually.
316
+
317
+ ### Temporary debug output
318
+
319
+ Turning on debug logging globally is often a little overkill and too noisy.
320
+ You may enable debug logging in the context of a block, like so:
321
+
322
+ ``` ruby
323
+ connection.debug do
324
+ # call some methods that execute SQL
325
+ end
326
+ ```
327
+
328
+ The log level will be restored after the block has executed, even if an
329
+ Exception is raised.
330
+
306
331
  ## Contributing
307
332
 
308
333
  If you find a bug in RDO, send a pull request if you think you can fix it.
@@ -315,7 +340,10 @@ unintentionally.
315
340
 
316
341
  I haven't looked at what I need to change to have the drivers compile on
317
342
  Windows yet, but I will do. If anybody beats me to it, pull requests will
318
- be gladly accepted! I'll probably add some thin JDBC wrappers for jRuby.
343
+ be gladly accepted! I was going to write JDBC wrappers for JRuby, but have
344
+ decided to just aim for JRuby >= 1.6, which supports C extensions. This
345
+ hasn't yet been tested with RDO. I should be able to make it work, as the
346
+ parts of the Ruby API I use are very typical.
319
347
 
320
348
  ### Writing a driver for RDO
321
349
 
@@ -127,6 +127,17 @@ module RDO
127
127
  Statement.new(@driver.prepare(command), logger)
128
128
  end
129
129
 
130
+ # Use debug log level in the context of a block.
131
+ def debug
132
+ raise ArgumentError,
133
+ "RDO::Connection#debug requires a block" unless block_given?
134
+
135
+ reset, logger.level = logger.level, Logger::DEBUG
136
+ yield
137
+ ensure
138
+ logger.level = reset
139
+ end
140
+
130
141
  private
131
142
 
132
143
  # Normalizes the given options String or Hash into a Symbol-keyed Hash.
@@ -6,5 +6,5 @@
6
6
  ##
7
7
 
8
8
  module RDO
9
- VERSION = "0.1.5"
9
+ VERSION = "0.1.6"
10
10
  end
@@ -286,4 +286,39 @@ describe RDO::Connection do
286
286
  end
287
287
  end
288
288
  end
289
+
290
+ describe "#debug" do
291
+ let(:connection) { RDO::Connection.new("test://host") }
292
+ let(:driver) { double(:driver, open: true) }
293
+ let(:driver_class) { double(new: driver) }
294
+
295
+ before(:each) do
296
+ RDO::Connection.register_driver(:test, driver_class)
297
+ connection
298
+ end
299
+
300
+ it "sets the log level to debug" do
301
+ connection.logger.level = Logger::FATAL
302
+ level = nil
303
+ connection.debug { level = connection.logger.level }
304
+ level.should == Logger::DEBUG
305
+ end
306
+
307
+ it "restores the log level" do
308
+ connection.logger.level = Logger::FATAL
309
+ connection.debug { }
310
+ connection.logger.level.should == Logger::FATAL
311
+ end
312
+
313
+ context "when the block raise an Exception" do
314
+ it "restores the log level" do
315
+ connection.logger.level = Logger::FATAL
316
+ begin
317
+ connection.debug { raise "test" }
318
+ rescue
319
+ end
320
+ connection.logger.level.should == Logger::FATAL
321
+ end
322
+ end
323
+ end
289
324
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-11 00:00:00.000000000 Z
12
+ date: 2012-10-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec