Class: DBI::BaseDatabase
Overview
Provides the core-level functionality for DatabaseHandles.
If the method description says “DBD Required”, it’s the DBD’s responsibility to create this method.
Required methods unimplemented by the DBD will raise DBD::NotImplementedError.
“DBD Optional” methods are methods that do not have a default implementation but are optional due to the fact that many databases may not support these features (and emulating them would be prohibitive).
These methods raise DBI::NotSupportedError.
Otherwise, DBI will provide a general alternative which should meet the expectations of the documentation. However, DBDs can override every method in this class.
Direct Known Subclasses
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Get an attribute from the DatabaseHandle.
-
#[]=(attr, value) ⇒ Object
Set an attribute on the DatabaseHandle.
-
#columns(table) ⇒ Object
Return a map of the columns that exist in the provided table name.
-
#commit ⇒ Object
Schedule a commit to the database immediately.
-
#disconnect ⇒ Object
Disconnect from the database.
-
#do(statement, bindvars) ⇒ Object
Execute and complete the statement with the binds provided.
-
#execute(statement, bindvars = {}) ⇒ Object
Execute a statement with the binds provided.
-
#initialize(handle, attr) ⇒ BaseDatabase
constructor
A new instance of BaseDatabase.
-
#ping ⇒ Object
Ping the database to ensure the connection is still alive.
-
#prepare(statement) ⇒ Object
Prepare a cached statement, returning a StatementHandle.
-
#rollback ⇒ Object
Schedule a rollback to the database immediately.
-
#tables ⇒ Object
Return the tables available to the database connection.
Constructor Details
#initialize(handle, attr) ⇒ BaseDatabase
Returns a new instance of BaseDatabase.
22 23 24 25 26 |
# File 'lib/dbi/base_classes/database.rb', line 22 def initialize(handle, attr) @handle = handle @attr = {} attr.each {|k, v| self[k] = v} end |
Instance Method Details
#[](attr) ⇒ Object
Get an attribute from the DatabaseHandle. These are DBD specific and embody things like Auto-Commit support for transactional databases.
- DBD Authors
-
This messes with @attr directly.
127 128 129 |
# File 'lib/dbi/base_classes/database.rb', line 127 def [](attr) @attr[attr] end |
#[]=(attr, value) ⇒ Object
Set an attribute on the DatabaseHandle. DBD Optional.
132 133 134 |
# File 'lib/dbi/base_classes/database.rb', line 132 def []=(attr, value) raise NotSupportedError end |
#columns(table) ⇒ Object
Return a map of the columns that exist in the provided table name. DBD Required.
The result should be an array of DBI::ColumnInfo objects which have, at minimum, the following fields:
- name
-
the name of the column.
- type
-
This is not a field name in itself. You have two options:
- type_name
-
The name of the type as returned by the database
- dbi_type
-
A DBI::Type-conforming class that can be used to convert to a native type.
- precision
-
the precision (generally length) of the column
- scale
-
the scale (generally a secondary attribute to precision
that helps indicate length) of the column
60 61 62 |
# File 'lib/dbi/base_classes/database.rb', line 60 def columns(table) raise NotImplementedError end |
#commit ⇒ Object
Schedule a commit to the database immediately. DBD Optional.
69 70 71 |
# File 'lib/dbi/base_classes/database.rb', line 69 def commit raise NotSupportedError end |
#disconnect ⇒ Object
Disconnect from the database. DBD Required.
29 30 31 |
# File 'lib/dbi/base_classes/database.rb', line 29 def disconnect raise NotImplementedError end |
#do(statement, bindvars) ⇒ Object
Execute and complete the statement with the binds provided. Returns the row modified count (via BaseStatement#rows). Finishes the statement handle for you.
Roughly equivalent to:
sth = dbh.prepare("my statement)
sth.execute(my, bind, vars)
result = sth.rows
sth.finish
Returning the value stored in ‘result`.
114 115 116 117 118 119 |
# File 'lib/dbi/base_classes/database.rb', line 114 def do(statement, bindvars) stmt = execute(statement, bindvars) res = stmt.rows stmt.finish return res end |
#execute(statement, bindvars = {}) ⇒ Object
Execute a statement with the binds provided. Returns the statement handle unfinished.
This is roughly equivalent to:
sth = dbh.prepare("my statement")
sth.execute(my, bind, vars)
94 95 96 97 98 99 |
# File 'lib/dbi/base_classes/database.rb', line 94 def execute(statement, bindvars={}) stmt = prepare(statement) stmt.bind_params(bindvars) stmt.execute stmt end |
#ping ⇒ Object
Ping the database to ensure the connection is still alive. Boolean return, true for success. DBD Required.
35 36 37 |
# File 'lib/dbi/base_classes/database.rb', line 35 def ping raise NotImplementedError end |
#prepare(statement) ⇒ Object
Prepare a cached statement, returning a StatementHandle. DBD Required.
41 42 43 |
# File 'lib/dbi/base_classes/database.rb', line 41 def prepare(statement) raise NotImplementedError end |
#rollback ⇒ Object
Schedule a rollback to the database immediately. DBD Optional.
74 75 76 |
# File 'lib/dbi/base_classes/database.rb', line 74 def rollback raise NotSupportedError end |
#tables ⇒ Object
Return the tables available to the database connection.
- Note
-
the basic implementation returns an empty array.
81 82 83 |
# File 'lib/dbi/base_classes/database.rb', line 81 def tables [] end |