Class: RDO::Connection
- Inherits:
-
Object
- Object
- RDO::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/rdo/connection.rb
Overview
Wrapper class to manage Driver classes.
This is the user-facing connection class. Users do not instantiate drivers directly.
Instance Attribute Summary collapse
-
#logger ⇒ Object
A Logger (from ruby stdlib).
-
#options ⇒ Object
readonly
Options passed to initialize.
Class Method Summary collapse
-
.drivers ⇒ Hash
List all known drivers, as a Hash mapping the URI scheme to the Class.
-
.register_driver(name, klass) ⇒ Object
Register a known driver class for the given URI scheme name.
Instance Method Summary collapse
-
#debug ⇒ Object
Use debug log level in the context of a block.
-
#execute(statement, *bind_values) ⇒ Result
Execute a statement with the configured Driver.
-
#initialize(uri, options = {}) ⇒ RDO::Connection
constructor
Initialize a new Connection.
-
#prepare(command) ⇒ Statement
Create a prepared statement to later be executed with some inputs.
Constructor Details
#initialize(uri, options = {}) ⇒ RDO::Connection
Initialize a new Connection.
This method instantiates the necessary driver.
If no suitable driver is loaded, an RDO::Exception is raised.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rdo/connection.rb', line 65 def initialize(uri, = {}) = (uri).merge(()) @logger = .fetch(:logger, default_logger) @logger.level = [:log_level] if .key?(:log_level) unless self.class.drivers.key?([:driver]) raise RDO::Exception, "Unregistered driver #{@options[:driver].inspect}" end @driver = self.class.drivers[[:driver]].new() @driver.open or raise RDO::Exception, "Unable to connect, but the driver did not provide a reason" end |
Instance Attribute Details
#logger ⇒ Object
A Logger (from ruby stdlib)
45 46 47 |
# File 'lib/rdo/connection.rb', line 45 def logger @logger end |
#options ⇒ Object (readonly)
Options passed to initialize.
42 43 44 |
# File 'lib/rdo/connection.rb', line 42 def end |
Class Method Details
.drivers ⇒ Hash
List all known drivers, as a Hash mapping the URI scheme to the Class.
23 24 25 |
# File 'lib/rdo/connection.rb', line 23 def drivers @drivers ||= {} end |
.register_driver(name, klass) ⇒ Object
Register a known driver class for the given URI scheme name.
34 35 36 |
# File 'lib/rdo/connection.rb', line 34 def register_driver(name, klass) drivers[name.to_s] = klass end |
Instance Method Details
#debug ⇒ Object
Use debug log level in the context of a block.
131 132 133 134 135 136 137 138 139 |
# File 'lib/rdo/connection.rb', line 131 def debug raise ArgumentError, "RDO::Connection#debug requires a block" unless block_given? reset, logger.level = logger.level, Logger::DEBUG yield ensure logger.level = reset end |
#execute(statement, *bind_values) ⇒ Result
Execute a statement with the configured Driver.
The statement can either be a read, or a write operation. Placeholders marked by ‘?’ may be interpolated in the statement, so that bind parameters can be safely provided.
Where the RDBMS natively supports bind parameters, this functionality is used; otherwise, the values are quoted using #quote.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rdo/connection.rb', line 98 def execute(statement, *bind_values) t = Time.now @driver.execute(statement, *bind_values).tap do |rs| rs.info[:execution_time] ||= Time.now - t if logger.debug? logger.debug( "(%.6f) %s %s" % [ rs.execution_time, statement, ("<Bind: #{bind_values.inspect}>" unless bind_values.empty?) ] ) end end rescue RDO::Exception => e logger.fatal(e.) if logger.fatal? raise end |
#prepare(command) ⇒ Statement
Create a prepared statement to later be executed with some inputs.
Not all drivers support this natively, but it is emulated by default.
126 127 128 |
# File 'lib/rdo/connection.rb', line 126 def prepare(command) Statement.new(@driver.prepare(command), logger) end |