rdo 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,16 +10,28 @@ require "logger"
10
10
  module RDO
11
11
  # A Logger that outputs using color to highlight errors etc.
12
12
  class ColoredLogger < Logger
13
- def initialize(*)
14
- super
15
- self.formatter =
13
+ def formatter
14
+ @formatter ||=
16
15
  Proc.new do |severity, time, prog, msg|
17
16
  case severity
18
- when "DEBUG" then "\033[35mSQL\033[0m \033[36m~\033[0m %s\n" % msg
19
- when "FATAL" then "\033[31mERROR ~ %s\033[0m\n" % msg
20
- else "%s ~ %s\n" % [severity, msg]
21
- end
17
+ when "DEBUG"
18
+ format_sql(msg)
19
+ when "FATAL"
20
+ format_err(msg)
21
+ else
22
+ "%s ~ %s" % [severity, msg]
23
+ end + $/
22
24
  end
23
25
  end
26
+
27
+ private
28
+
29
+ def format_sql(sql)
30
+ "\033[35mSQL\033[0m \033[36m~\033[0m %s" % sql
31
+ end
32
+
33
+ def format_err(msg)
34
+ "\033[31mERROR ~ %s\033[0m" % msg
35
+ end
24
36
  end
25
37
  end
@@ -95,9 +95,17 @@ module RDO
95
95
  # @return [Result]
96
96
  # the result of the query
97
97
  def execute(statement, *bind_values)
98
- @driver.execute(statement, *bind_values).tap do
98
+ t = Time.now
99
+ @driver.execute(statement, *bind_values).tap do |rs|
100
+ rs.info[:execution_time] ||= Time.now - t
99
101
  if logger.debug?
100
- logger.debug("#{statement}#{" <Bind: #{bind_values.inspect}>" unless bind_values.empty?}")
102
+ logger.debug(
103
+ "(%.6fs) %s%s" % [
104
+ rs.execution_time,
105
+ statement,
106
+ ("<Bind: #{bind_values.inspect}>" unless bind_values.empty?)
107
+ ]
108
+ )
101
109
  end
102
110
  end
103
111
  rescue RDO::Exception => e
@@ -85,6 +85,14 @@ module RDO
85
85
  end
86
86
  end
87
87
 
88
+ # Get the time spent processing the statement.
89
+ #
90
+ # @return [Float]
91
+ # the time in seconds spent executing the query
92
+ def execution_time
93
+ info[:execution_time].to_f
94
+ end
95
+
88
96
  # Iterate over all rows returned by the connection.
89
97
  #
90
98
  # For each row, a Symbol-keyed Hash is yielded into the block.
@@ -31,9 +31,17 @@ module RDO
31
31
  # @param [Object...] args
32
32
  # bind parameters to use in place of '?'
33
33
  def execute(*bind_values)
34
- @executor.execute(*bind_values).tap do
34
+ t = Time.now
35
+ @executor.execute(*bind_values).tap do |rs|
36
+ rs.info[:execution_time] ||= Time.now - t
35
37
  if logger.debug?
36
- logger.debug("#{command}#{" <Bind: #{bind_values.inspect}>" unless bind_values.empty?}")
38
+ logger.debug(
39
+ "(%.6fs) %s %s" % [
40
+ rs.execution_time,
41
+ command,
42
+ ("<Bind: #{bind_values.inspect}>" unless bind_values.empty?)
43
+ ]
44
+ )
37
45
  end
38
46
  end
39
47
  rescue RDO::Exception => e
@@ -6,5 +6,5 @@
6
6
  ##
7
7
 
8
8
  module RDO
9
- VERSION = "0.1.0"
9
+ VERSION = "0.1.1"
10
10
  end
@@ -17,7 +17,7 @@ describe RDO::Statement do
17
17
 
18
18
  describe "#execute" do
19
19
  let(:executor) { double(command: "SELECT * FROM bob WHERE ?", execute: result) }
20
- let(:result) { stub(:result) }
20
+ let(:result) { stub(:result, info: {}, execution_time: 0.0) }
21
21
 
22
22
  it "delegates to the executor" do
23
23
  executor.should_receive(:execute).with(1, 2).and_return(result)
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.0
4
+ version: 0.1.1
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-10 00:00:00.000000000 Z
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec