Class: RDO::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/rdo/driver.rb

Overview

Abstract class that is subclassed by each specific driver.

Driver developers should be able to subclass this, then write specs and override the behaviours they need to change.

Ideally all instance method will be overridden by really robust drivers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Driver

Initialize the Driver with the given options.

Drivers SHOULD call super if overriding.

Parameters:

  • options (Hash) (defaults to: {})

    all options passed to the Driver, as a Symbol-keyed Hash.



25
26
27
# File 'lib/rdo/driver.rb', line 25

def initialize(options = {})
  @options = options.dup
end

Instance Attribute Details

#optionsObject (readonly)

Options passed to initialize.



17
18
19
# File 'lib/rdo/driver.rb', line 17

def options
  @options
end

Instance Method Details

#closeBoolean

Close the current connection, if it is open.

Drivers MUST override this.

Returns:

  • (Boolean)

    true if the connection was closed or was already closed, false if not



57
58
59
# File 'lib/rdo/driver.rb', line 57

def close
  false
end

#execute(statement, *bind_values) ⇒ Result

Execute a statement against the RDBMS.

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.

Drivers MUST override this.

Parameters:

  • statement (String)

    a string of SQL or DDL to be executed

  • *bind_values (Array)

    a list of parameters to substitute in the statement

Returns:

  • (Result)

    the result of the query



96
97
98
# File 'lib/rdo/driver.rb', line 96

def execute(statement, *bind_values)
  Result.new([])
end

#interpolate(stmt, params) ⇒ Object

Takes String stmt, which contains ? markers and interpolates the values in Array params.

Each value in Array params that is not NilClass, Fixnum or Float is passed to #quote on the Driver.

Non-numeric values are surrounded by String quote.

Parameters:

  • VALUE (String)

    stmt SQL, possibly containining ‘?’ markers.

  • VALUE (Array)

    params arguments to interpolate in place of the ? markers



140
141
142
# File 'lib/rdo/driver.rb', line 140

def interpolate(sql, params)
  # implemented in ext/rdo/rdo.c
end

#openBoolean

Open a connection to the RDBMS, if it is not already open.

If it is not possible to open a connection, an RDO::Exception is raised.

This is a no-op: subclasses MUST override this.

Returns:

  • (Boolean)

    true if a connection was opened or was already open, false if not.



37
38
39
# File 'lib/rdo/driver.rb', line 37

def open
  false
end

#open?Boolean

Check if the connection is currently open or not.

Drivers MUST override this.

Returns:

  • (Boolean)

    true if the connection is open, false otherwise



47
48
49
# File 'lib/rdo/driver.rb', line 47

def open?
  false
end

#prepare(statement) ⇒ StatementExecutor

Create a prepared statement to later be executed with some inputs.

Not all drivers support this natively, but it is emulated by default.

This is a default implementation for emulated prepared statements: drivers SHOULD override it if possible.

Parameters:

  • statement (String)

    a string of SQL or DDL, with ‘?’ placeholders for bind parameters

Returns:

  • (StatementExecutor)

    a prepared statement to later be executed



73
74
75
# File 'lib/rdo/driver.rb', line 73

def prepare(statement)
  emulated_statement_executor(statement)
end

#quote(value) ⇒ String

Escape a given value for safe interpolation into a statement.

The value may be any type of Object and will be formatted to a String as needed.

This should be avoided where the driver natively supports bind parameters.

Drivers MUST override this with a RDBMS-specific solution.

Parameters:

  • value (Object)

    the value to quote

Returns:

  • (String)

    a safely escaped value



114
115
# File 'lib/rdo/driver.rb', line 114

def quote(value)
end