Path

ez components / documentation / api reference / trunk / database


eZ Components trunk

Database: ezcDbFactory

[ Tutorial ] [ Oracle setup ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Class: ezcDbFactory

ezcDbFactory manages the list of known database drivers and is used to create their instances. [source]
Example:
 1.  $dbparams array(
 2.      'type'   => 'mysql',
 3.      'dbname' => 'test',
 4.      'user'   => 'john',
 5.      'pass'   => 'topsecret' );
 6.  $db ezcDbFactory::create$dbparams );
 7.  $db->query'SELECT * FROM tbl' );
Instead of passing an array with those parameters, you can also pass a DSN:
 1.  $dsn "mysql://root@localhost/geolocation";
 2.  $db ezcDbFactory::create$dsn );
Other examples of DSNs are:
 1.  $dsn "sqlite:///tmp/ezc.sqlite"// Disk based databases for SQLite.
 2.  $dsn "sqlite://:memory:";        // In memory databases for SQLite.
Note that this class does not deal with character sets automatically, you have to make sure that you do that yourself. For MySQL that means running a query "SET NAMES" for example. See the tutorial for some hints on this.

Method Summary

public static void addImplementation( $implementationName, $className )
Adds a database implementation to the list of known implementations.
public static ezcDbHandler create( $dbParams )
Creates and returns an instance of the specified ezcDbHandler implementation.
public static array(string) getImplementations( )
Returns a list with supported database implementations.
public static array parseDSN( $dsn )
Returns the Data Source Name as a structure containing the various parts of the DSN.

Methods

addImplementation

void addImplementation( string $implementationName, string $className )
Adds a database implementation to the list of known implementations.
$implementationName is the name of the implemenation. This name should be short and uniquely identify the database. $className is the class name of the class that implements the handler for this database.
Example:
 1.  class DB2Handler
 2.  {
 3.  }
 4.  ezcDbFactory::addImplementation'db2''DB2Handler' );
 5.  // ...
 6.  $dbparams array'handler' => 'db2'... );
 7.  $db ezcDbFactory::create$dbparams );

Parameters

Name Type Description
$implementationName string  
$className string  

create

ezcDbHandler create( mixed $dbParams )
Creates and returns an instance of the specified ezcDbHandler implementation.
Supported database parameters are:
  • phptype|type|handler|driver: Database implementation
  • user|username: Database user name
  • pass|password: Database user password
  • dbname|database: Database name
  • host|hostspec: Name of the host database is running on
  • port: TCP port
  • charset: Client character set
  • socket: UNIX socket path
The list above is actually driver-dependent and may be extended in the future. You can specify any parameters your database handler supports.

Parameters

Name Type Description
$dbParams mixed Database parameters (driver, host, port, user, pass, etc). May be specified either as array (key => val ....) or as DSN string. Format of the DSN is the same as accepted by PEAR::DB::parseDSN().

Throws

ClassDescription
ezcDbHandlerNotFoundException if the requested database handler could not be found.

getImplementations

array(string) getImplementations( )
Returns a list with supported database implementations.
Example:
 1.  ezcDbFactory::getImplementations();

parseDSN

array parseDSN( string $dsn )
Returns the Data Source Name as a structure containing the various parts of the DSN.
Additional keys can be added by appending a URI query string to the end of the DSN.
The format of the supplied DSN is in its fullest form:
 1.   phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
Most variations are allowed:
 1.   phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
 2.   phptype://username:password@hostspec/database_name
 3.   phptype://username:password@hostspec
 4.   phptype://username@hostspec
 5.   phptype://hostspec/database
 6.   phptype://hostspec
 7.   phptype(dbsyntax)
 8.   phptype
This function is 'borrowed' from PEAR /DB.php .

Parameters

Name Type Description
$dsn string Data Source Name to be parsed

Last updated: Mon, 21 Dec 2009