class DBI::BaseDatabase

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.

Public Class Methods

new(handle, attr) click to toggle source
# File lib/dbi/base_classes/database.rb, line 21
def initialize(handle, attr)
    @handle = handle
    @attr   = {}
    attr.each {|k,v| self[k] = v} 
end

Public Instance Methods

[](attr) click to toggle source

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.

# File lib/dbi/base_classes/database.rb, line 126
def [](attr)
    @attr[attr]
end
[]=(attr, value) click to toggle source

Set an attribute on the DatabaseHandle. DBD Optional.

# File lib/dbi/base_classes/database.rb, line 131
def []=(attr, value)
    raise NotSupportedError
end
columns(table) click to toggle source

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

# File lib/dbi/base_classes/database.rb, line 59
def columns(table)
    raise NotImplementedError
end
commit() click to toggle source

Schedule a commit to the database immediately. DBD Optional.

# File lib/dbi/base_classes/database.rb, line 68
def commit
    raise NotSupportedError
end
disconnect() click to toggle source

Disconnect from the database. DBD Required.

# File lib/dbi/base_classes/database.rb, line 28
def disconnect
    raise NotImplementedError
end
do(statement, *bindvars) click to toggle source

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`.

# File lib/dbi/base_classes/database.rb, line 113
def do(statement, *bindvars)
    stmt = execute(statement, *bindvars)
    res = stmt.rows
    stmt.finish
    return res
end
execute(statement, *bindvars) click to toggle source

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)
# File lib/dbi/base_classes/database.rb, line 93
def execute(statement, *bindvars)
    stmt = prepare(statement)
    stmt.bind_params(*bindvars)
    stmt.execute
    stmt
end
ping() click to toggle source

Ping the database to ensure the connection is still alive. Boolean return, true for success. DBD Required.

# File lib/dbi/base_classes/database.rb, line 34
def ping
    raise NotImplementedError
end
prepare(statement) click to toggle source

Prepare a cached statement, returning a StatementHandle. DBD Required.

# File lib/dbi/base_classes/database.rb, line 40
def prepare(statement)
    raise NotImplementedError
end
rollback() click to toggle source

Schedule a rollback to the database immediately. DBD Optional.

# File lib/dbi/base_classes/database.rb, line 73
def rollback
    raise NotSupportedError
end
tables() click to toggle source

Return the tables available to the database connection.

Note

the basic implementation returns an empty array.

# File lib/dbi/base_classes/database.rb, line 80
def tables
    []
end