Package grizzled :: Package db :: Module base :: Class DBDriver
[hide private]
[frames] | no frames]

Class DBDriver

source code

object --+
         |
        DBDriver
Known Subclasses:

Base class for all DB drivers.
Instance Methods [hide private]
 
__display_name(self) source code
 
_ensure_valid_table(self, cursor, table_name)
Determines whether a table name represents a legal table in the current database, throwing an Error if not.
source code
bool
_is_valid_table(self, cursor, table_name)
Determines whether a table name represents a legal table in the current database, throwing an Error if not.
source code
db
connect(self, host='localhost', port=None, user=None, password='', database=None)
Connect to the underlying database.
source code
object
do_connect(*__args, **__kw)
Connect to the actual underlying database, using the driver.
source code
str
get_display_name(*__args, **__kw)
Get the driver's name, for display.
source code
 
get_import(*__args, **__kw)
Get a bound import for the underlying DB API module.
source code
list of tuples
get_index_metadata(self, table, cursor)
Get the metadata for the indexes for a table.
source code
named tuple
get_rdbms_metadata(self, cursor)
Return data about the RDBMS: the product name, the version, etc.
source code
list
get_table_metadata(self, table, cursor)
Get the metadata for a table.
source code
list
get_tables(self, cursor)
Get the list of tables in the database.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]
  display_name
get a displayable name for the driver

Inherited from object: __class__

Method Details [hide private]

_ensure_valid_table(self, cursor, table_name)

source code 
Determines whether a table name represents a legal table in the current database, throwing an Error if not.
Parameters:
  • cursor (Cursor) - an open Cursor
  • table_name (str) - the table name
Raises:

_is_valid_table(self, cursor, table_name)

source code 
Determines whether a table name represents a legal table in the current database, throwing an Error if not.
Parameters:
  • cursor (Cursor) - an open Cursor
  • table_name (str) - the table name
Returns: bool
True if the table is valid, False if not

connect(self, host='localhost', port=None, user=None, password='', database=None)

source code 
Connect to the underlying database. Subclasses should not override this method. Instead, a subclass should override the do_connect() method.
Parameters:
  • host (str) - the host where the database lives
  • port (int) - the TCP port to use when connecting, or None
  • user (str) - the user to use when connecting, or None
  • password (str) - the password to use when connecting, or None
  • database (str) - the name of the database to which to connect
Returns: db
a db object representing the open database
Raises:

do_connect(*__args, **__kw)

source code 

Connect to the actual underlying database, using the driver. Subclasses must provide an implementation of this method. The method must return the result of the real DB API implementation's connect() method. For instance:

def do_connect():
    dbi = self.get_import()
    return dbi.connect(host=host, user=user, passwd=password,
                       database=database)

There is no need to catch exceptions; the DBDriver class's connect() method handles that.

Parameters:
  • host (str) - the host where the database lives
  • port (int) - the TCP port to use when connecting
  • user (str) - the user to use when connecting
  • password (str) - the password to use when connecting
  • database (str) - the name of the database to which to connect
Returns: object
a DB API-compliant object representing the open database
Decorators:
  • @abstract
Raises:

get_display_name(*__args, **__kw)

source code 
Get the driver's name, for display. The returned name ought to be a reasonable identifier for the database (e.g., 'SQL Server', 'MySQL'). All subclasses must provide an implementation of this method.
Returns: str
the driver's displayable name
Decorators:
  • @abstract

get_import(*__args, **__kw)

source code 

Get a bound import for the underlying DB API module. All subclasses must provide an implementation of this method. Here's an example, assuming the real underlying Python DB API module is 'foosql':

def get_import(self):
    import foosql
    return foosql
Returns:
a bound module
Decorators:
  • @abstract

get_index_metadata(self, table, cursor)

source code 

Get the metadata for the indexes for a table. Returns a list of tuples, one for each index. Each tuple consists of the following:

(index_name, [index_columns], description)

The tuple elements have the following meanings.

index_name
the index name
index_columns
a list of column names
description
index description, or None

The tuples are named tuples, so the fields may be referenced by the names above or by position.

The default implementation of this method returns None

Parameters:
  • table (str) - table name
  • cursor (Cursor) - a Cursor object from a recent query
Returns: list of tuples
the list of tuples, or None if not supported in the underlying database
Raises:

get_rdbms_metadata(self, cursor)

source code 

Return data about the RDBMS: the product name, the version, etc. The result is a named tuple, with the following fields.

vendor
The product vendor, if applicable, or None if not known
product
The name of the database product, or None if not known
version
The database product version, or None if not known
Parameters:
  • cursor (Cursor) - a Cursor object from a recent query
Returns: named tuple
the vendor information

get_table_metadata(self, table, cursor)

source code 

Get the metadata for a table. Returns a list of tuples, one for each column. Each tuple consists of the following:

(column_name, type_string, max_char_size, precision, scale, nullable)

The tuple elements have the following meanings.

column_name
the name of the column
type_string
the column type, as a string
max_char_size
the maximum size for a character field, or None
precision
the precision, for a numeric field; or None
scale
the scale, for a numeric field; or None
nullable
True if the column is nullable, False if it is not

The tuples are named tuples, so the fields may be referenced by the names above or by position.

The default implementation uses the DB API's cursor.description field. Subclasses are free to override this method to produce their own version that uses other means.

Parameters:
  • table (str) - the table name for which metadata is desired
  • cursor (Cursor) - a Cursor object from a recent query
Returns: list
list of tuples, as described above
Raises:

get_tables(self, cursor)

source code 
Get the list of tables in the database.
Parameters:
  • cursor (Cursor) - a Cursor object from a recent query
Returns: list
List of table names. The list will be empty if the database contains no tables.
Raises:
  • NotImplementedError - Capability not supported by database driver
  • Warning - Non-fatal warning
  • Error - Error

Property Details [hide private]

display_name

get a displayable name for the driver
Get Method:
__display_name(self)