public
class
JinnDatabaseManager
extends JinnBaseService
Provides an interface for working with an SQL database. The default
service interacts with a MySQL database; however, another replacement
database service exists which uses the PEAR:DB system for accessing
different kinds of databases. Database connections must be configured via
$GLOBALS['interJinn']['databases']. By default this service
works in harmony with instances of JinnDBConnection. The following is an example configuration:
The above structure can be described as any number of database connection
configurations for which the key is the registered name. Additionally, any
keys whose value is a string describes an alias to the database
configuration with that key. In this way database connections are named
and can be modified without having to change code anywhere in the
application. it is important to note that a default entry
must be defined. This facilitates database connection requests without the
need for a name-- a useful feature for services that make use of the
database.
Following is a detailed list of configuration fields supported by this
database service (replacement services may differ in the fields available
depending on their connection criteria):
type
The type of database connection being requested. This is used
to dispatch the connection request to the appropriate handler.
host
The IP or domain name of the machine hosting the database
server.
port
The port on which a connection to the database server should
be made. If omitted then the default MySQL database port is
assumed.
user
The username of the account with which to connect to the
database server.
password
The password associated with the username of the account with
which to connect to the database server.
database
The name of the database to which queries will be made.
Following is an example of retrieving a database connection, performing a
query, and processing the results:
// // Presumes we are in a class that at some level extends // JinnBaseClass. // $sDb = &$this->getServiceRef( 'dbManager' ); $db = &$sDb->getConnectionRef();
Warning:
Don't forget to define a default entry in your configuration,
it can be the key for a literal configuration, or an alias for an existing
named configuration.
Specifically added so that the service can be overridden with minimal
effort to provide handling for when a conneciton could not be properly
retrieved.
Constructor. Initializes data structures used by this service.
Source Code
function JinnDatabaseManager() { $this->___jinnConstruct(); }
___jinnConstruct
public ___jinnConstruct ( )
Constructor. Initializes data structures used by this service.
Source Code
function ___jinnConstruct() { parent::___jinnConstruct();
// // The $connectionPool is a 2 level pooling hash. The first level // links a set of connection parameters to an array of all connections // using those parameters. The second level contains the actual master // connection objects. // $this->connectionPool = array();
// // The $connectionLookup is a mapping of unique IDs as generated by // this class to associated resources. This makes it quick to free a // connection since we can just lookup the master object. // $this->connectionLookup = array();
// // Set default parameters to empty. Default should preferably be set // up in the project configuration file. // $this->defaultParams = $this->lookupDB();
Traverses the connection configuration and trims each value. I think
once upon a time this did more but eventually was pared down to what
remains *lol*.
Source Code
function checkConnectionParams( &$params ) { foreach( $params as $key => $value ) { $params[$key] = trim( $value ); } }
connectionFailureHandler
public &
connectionFailureHandler
(
object
&
$connection ,
object
&
$connection
)
Parameters:
$connection
-
The connection object that was returned for which the connection
failed.
$connection
-
A replacement connection object.
Since:
0.9.3
Specifically added so that the service can be overridden with minimal
effort to provide handling for when a conneciton could not be properly
retrieved. For instance the default behaviour generates a warning and
returns a connection object without a real connection to the database
server. This can be overriden to abort the script thus preventing
further problem. The return value may be used to try and re-attempt
the connection request and return the new object if valid. If not, and
the script aborts, then the original connectio should be returned.
Source Code
function &connectionFailureHandler( &$connection ) { return $connection; }
dispatchConnectionRequest
private array &
dispatchConnectionRequest
(
string
$type ,
array
$params
)
Parameters:
$type
-
The type of database connection to retrieve.
$params
-
The configuration array describing the database connection.
Returns:
An appropriate database connection for the given type. On failure
null is returned.
Attempts to retrieve a connection of the given database type and
returns it. If the type is not recognized then it assumes MySQL.
Source Code
function &dispatchConnectionRequest( $type, $params ) { $newConnection = null;
The name of a database or an explicity connection configuration
structure. If the value is null then the default database
is presumed.
Returns:
A database connection object upon which queries can be made and
retrieved.
Requests a database connection object with the given parameter
characteristics. This function uses pooling techniques so that the
first unused connection is returned or a completely new connection is
created if no pooled connections are available.
Source Code
function &getConnection( $params=null ) { $connection = &$this->getConnectionRef( $params );
The name of a database or an explicity connection configuration
structure. If the value is null then the default database
is presumed.
Returns:
A database connection object upon which queries can be made and
retrieved.
Requests a reference to a database connection object with the given
parameter characteristics. This function uses pooling techniques so
that the first unused connection is returned or a completely new
connection is created if no pooled connections are available.
Source Code
function &getConnectionRef( $requestParams=null ) { $params = $requestParams;
// // Check if a connection already exists. // if( isset( $this->connectionPool[$paramString] ) ) { // // One or more connections exist, so try and find an free // connection and return it. // foreach( $this->connectionPool[$paramString] as $poolId => $foo ) { $aConnection = &$this->connectionPool[$paramString][$poolId];
if( $aConnection->friendlyIsFree() ) { // // Mark the connection as in use. // $aConnection->friendlySetUsed();
// // Copy the object, this is what we actually return. // $newObject = $aConnection;
// // If we are here, then no connections were in the pool to meet // our needs and we must generate a new connection. // $type = null; if( is_array( $params ) && isset( $params['type'] ) ) { $type = $params['type']; }
// // Add the connection to the pool. // $this->connectionPool[$paramString][$newId] = &$newConnection; $this->connectionLookup[$newId] = &$newConnection;
The configuration structure for the requested database.
Attempts to find the database configuration having the requested name.
This functions performs a recursive lookup such that if a name
corresponds toa string value, then the string value is used as the
next lookup name. If no entry is found then null is
returned.