Creole Class Reference

List of all members.

Static Public Member Functions

static registerDriver ($phptype, $dotpath)
 Register your own RDBMS driver class.
static deregisterDriver ($phptype)
 Removes the driver for a PHP type.
static getDriver ($phptype)
 Returns the class path to the driver registered for specified type.
static getConnection ($dsn, $flags=0)
 Create a new DB connection object and connect to the specified database.
static parseDSN ($dsn)
static import ($class)
 Include once a file specified in DOT notation.

Public Attributes

const PERSISTENT = 1
 Constant that indicates a connection object should be used.
const NO_ASSOC_LOWER = 16
 Flag to pass to the connection to indicate that no case conversions should be performed by ResultSet on keys of fetched rows.
const COMPAT_ASSOC_LOWER = 32
 Flag to pass to the connection to indicate that a to-lower case conversion should be performed by ResultSet on keys of fetched rows.
const COMPAT_RTRIM_STRING = 64
 Flag to pass to the connection to indicate that an rtrim() should be performed on strings (using ResultSet->getString(), etc.
const COMPAT_ALL = 96
 Flag to indicate that all compatibility flags should be set.

Static Private Attributes

static $driverMap
static $connectionMap = array()


Detailed Description

Definition at line 46 of file Creole.php.


Member Function Documentation

static Creole::deregisterDriver ( phptype  )  [static]

Removes the driver for a PHP type.

Note that this will remove user-registered drivers _and_ the default drivers.

Parameters:
string $phptype The PHP type for driver to de-register.
See also:
registerDriver()

Definition at line 137 of file Creole.php.

00138     {
00139         unset(self::$driverMap[$phptype]);
00140     }

static Creole::getConnection ( dsn,
flags = 0 
) [static]

Create a new DB connection object and connect to the specified database.

Parameters:
mixed $dsn "data source name", see the self::parseDSN method for a description of the dsn format. Can also be specified as an array of the format returned by DB::parseDSN().
int $flags Connection flags (e.g. PERSISTENT).
Returns:
Connection Newly created DB connection object
Exceptions:
SQLException 
See also:
self::parseDSN()

Definition at line 171 of file Creole.php.

References COMPAT_ALL, COMPAT_ASSOC_LOWER, COMPAT_RTRIM_STRING, import(), NO_ASSOC_LOWER, parseDSN(), PERSISTENT, and SQLException::setUserInfo().

00172     {
00173         if (is_array($dsn)) {
00174             $dsninfo = $dsn;
00175         } else {
00176             $dsninfo = self::parseDSN($dsn);
00177         }
00178 
00179     // gather any flags from the DSN
00180     if ( isset ( $dsninfo['persistent'] ) && ! empty ( $dsninfo['persistent'] ) )
00181       $flags |= Creole::PERSISTENT;
00182     if ( isset ( $dsninfo['compat_assoc_lower'] ) && ! empty ( $dsninfo['compat_assoc_lower'] ) )
00183       $flags |= Creole::COMPAT_ASSOC_LOWER;
00184     if ( isset ( $dsninfo['compat_rtrim_string'] ) && ! empty ( $dsninfo['compat_rtrim_string'] ) )
00185       $flags |= Creole::COMPAT_RTRIM_STRING;
00186     if ( isset ( $dsninfo['compat_all'] ) && ! empty ( $dsninfo['compat_all'] ) )
00187       $flags |= Creole::COMPAT_ALL;
00188 
00189     if ($flags & Creole::NO_ASSOC_LOWER) {
00190       trigger_error("The Creole::NO_ASSOC_LOWER flag has been deprecated, and is now the default behavior. Use Creole::COMPAT_ASSOC_LOWER to lowercase resulset keys.", E_USER_WARNING);
00191     }
00192 
00193         // sort $dsninfo by keys so the serialized result is always the same
00194         // for identical connection parameters, no matter what their order is
00195         ksort($dsninfo);
00196         $connectionMapKey = crc32(serialize($dsninfo + array('compat_flags' => ($flags & Creole::COMPAT_ALL))));
00197 
00198         // see if we already have a connection with these parameters cached
00199         if(isset(self::$connectionMap[$connectionMapKey]))
00200         {
00201             // persistent connections will be used if a non-persistent one was requested and is available
00202             // but a persistent connection will be created if a non-persistent one is present
00203 
00204       // TODO: impliment auto close of non persistent and replacing the
00205       // non persistent with the persistent object so as we dont have
00206       // both links open for no reason
00207 
00208             if( isset(self::$connectionMap[$connectionMapKey][1]) ) { // is persistent
00209                 // a persistent connection with these parameters is already there,
00210                 // so we return it, no matter what was specified as persistent flag
00211                 $con = self::$connectionMap[$connectionMapKey][1];
00212             } else {
00213                 // we don't have a persistent connection, and since the persistent
00214                 // flag wasn't set either, we just return the non-persistent connection
00215                 $con = self::$connectionMap[$connectionMapKey][0];
00216             }
00217 
00218             // if we're here, a non-persistent connection was already there, but
00219             // the user wants a persistent one, so it will be created
00220 
00221             if ($con->isConnected())
00222                 return $con;
00223         }
00224 
00225         // support "catchall" drivers which will themselves handle the details of connecting
00226         // using the proper RDBMS driver.
00227         if (isset(self::$driverMap['*'])) {
00228             $type = '*';
00229         } else {
00230             $type = $dsninfo['phptype'];
00231             if (!isset(self::$driverMap[$type])) {
00232                 throw new SQLException("No driver has been registered to handle connection type: $type");
00233             }
00234         }
00235 
00236         // may need to make this more complex if we add support
00237         // for 'dbsyntax'
00238         $clazz = self::import(self::$driverMap[$type]);
00239         $obj = new $clazz();
00240 
00241         if (!($obj instanceof Connection)) {
00242             throw new SQLException("Class does not implement creole.Connection interface: $clazz");
00243         }
00244 
00245         try {
00246             $obj->connect($dsninfo, $flags);
00247         } catch(SQLException $sqle) {
00248             $sqle->setUserInfo($dsninfo);
00249             throw $sqle;
00250         }
00251     $persistent = ($flags & Creole::PERSISTENT) === Creole::PERSISTENT;
00252         return self::$connectionMap[$connectionMapKey][(int)$persistent] = $obj;
00253     }

static Creole::getDriver ( phptype  )  [static]

Returns the class path to the driver registered for specified type.

Parameters:
string $phptype The phptype handled by driver (e.g. 'mysql', 'mssql', '*').
Returns:
string The driver class in dot-path notation (e.g. creole.drivers.mssql.MSSQLConnection) or NULL if no registered driver found.

Definition at line 148 of file Creole.php.

Referenced by DebugConnection::connect().

00149     {
00150         if (isset(self::$driverMap[$phptype])) {
00151             return self::$driverMap[$phptype];
00152         } else {
00153             return null;
00154         }
00155     }

static Creole::import ( class  )  [static]

Include once a file specified in DOT notation.

Package notation is expected to be relative to a location on the PHP include_path.

Parameters:
string $class
Returns:
string unqualified classname
Exceptions:
SQLException - if class does not exist and cannot load file
  • if after loading file class still does not exist

Definition at line 371 of file Creole.php.

Referenced by ODBCConnection::connect(), DebugConnection::connect(), and getConnection().

00371                                           {
00372         if (!class_exists($class, false)) {
00373             $path = strtr($class, '.', DIRECTORY_SEPARATOR) . '.php';
00374             $ret = include_once($path);
00375             if ($ret === false) {
00376                 throw new SQLException("Unable to load driver class: " . $class);
00377             }
00378             // get just classname ('path.to.ClassName' -> 'ClassName')
00379             $pos = strrpos($class, '.');
00380             if ($pos !== false) {
00381                 $class = substr($class, $pos + 1);
00382             }
00383             if (!class_exists($class)) {
00384                 throw new SQLException("Unable to find loaded class: $class (Hint: make sure classname matches filename)");
00385             }
00386         }
00387         return $class;
00388     }

static Creole::parseDSN ( dsn  )  [static]

Definition at line 286 of file Creole.php.

Referenced by getConnection().

00287     {
00288         if (is_array($dsn)) {
00289             return $dsn;
00290         }
00291 
00292         $parsed = array(
00293             'phptype'  => null,
00294             'username' => null,
00295             'password' => null,
00296             'protocol' => null,
00297             'hostspec' => null,
00298             'port'     => null,
00299             'socket'   => null,
00300             'database' => null
00301         );
00302 
00303     $preg_query = "!^(([a-z0-9]+)(\(([^()]+)\))?)(://((((([^@/:]+)(:([^@/]+))?)@)?((([a-z]+)\((([^?():]+)(:([^()?]+))?)\))|((([^/?:]+)(:([^/?]+))?))))/?)?([^?]+)?(\?(.+))?)?$!i";
00304 
00305     $info = array();
00306 
00307     if (preg_match($preg_query,$dsn,$info)) { // only if it is matching
00308 
00309       $parsed['phptype'] = @$info[2]; // Group 2 should always exist.
00310 
00311       // Don't know what to do with Group 4: phptype(xx) should => check first if available
00312 
00313       if (isset($info[5])) { // There is more than just the phptype
00314 
00315         if (strlen($info[10]) > 0) { // There is a username specified
00316           $parsed['username'] = @$info[10];
00317         }
00318 
00319         if (strlen($info[12]) > 0) { // There is a password specified
00320           $parsed['password'] = @$info[12];
00321         }
00322 
00323         if (strlen($info[15]) > 0) { // There is a protocol specified: protocol(hostspec)
00324           $parsed['protocol'] = @$info[15];
00325 
00326           if ($parsed["protocol"] === "unix") {
00327             $parsed['socket'] = @$info[16];
00328           } else {
00329             $parsed["hostspec"] = @$info[17];
00330             if (strlen($info[19]) > 0) {
00331               $parsed["port"] = @$info[19];
00332             }
00333           }
00334         } elseif (strlen($info[20]) > 0) {
00335           $parsed["hostspec"] = @$info[22];
00336 
00337           if ((isset($info[24]) && (strlen($info[24]) > 0))) { // There is a port set (not always available)
00338             $parsed["port"] = @$info[24];
00339           }
00340         }
00341 
00342         if ((isset($info[25])) && (strlen($info[25]) > 0)) { // There is a database
00343           $parsed["database"] = @$info[25];
00344         }
00345 
00346         if ((isset($info[27])) && (strlen($info[27]) >0)) { // There is a query
00347           $opts = explode('&', $info[27]);
00348           foreach ($opts as $opt) {
00349             list($key, $value) = explode('=', $opt);
00350             if (!isset($parsed[$key])) { // don't allow params overwrite
00351               $parsed[$key] = urldecode($value);
00352             }
00353           }
00354         }
00355 
00356       }
00357     }
00358 
00359         return $parsed;
00360     }

static Creole::registerDriver ( phptype,
dotpath 
) [static]

Register your own RDBMS driver class.

You can use this to specify your own class that replaces a default driver or adds support for a new driver. Register your own class by specifying the 'phptype' (e.g. mysql) and a dot-path notation to where your Connection class is relative to any location on the include path. You can also specify '*' as the phptype if you want to register a driver that will handle any native type (e.g. if creating a set of decorator classes that log SQL before calling native driver methods). YOU CAN ONLY REGISTER ONE CATCHALL ('*') DRIVER.

Note: the class you need to register is your Connection class because this is the class that's responsible for instantiating the other classes that are part of your driver. It is possible to mix & match drivers -- i.e. to write a custom driver where the Connection object just instantiates stock classes for ResultSet and PreparedStatement. Note that if you wanted to "override" only the ResultSet class you would also have to override the Connection and PreparedStatement classes so that they would return the correct ResultSet class. In the future we may implement a more "packaged" approach to drivers; for now we want to keep it simple.

Parameters:
string $phptype The phptype (mysql, mssql, etc.). This is first part of DSN URL (e.g. mysql://localhost/...). You may also specify '*' to register a driver that will "wrap" the any native drivers.
string $dotpath A dot-path locating your class. For example 'creole.drivers.mssql.MSSQLConnection' will be included like: include 'creole/drivers/mssql/MSSQLConnection.php' and the classname will be assumed to be 'MSSQLConnection'.
Returns:
void

Definition at line 126 of file Creole.php.

00127     {
00128         self::$driverMap[$phptype] = $dotpath;
00129     }


Member Data Documentation

Creole::$connectionMap = array() [static, private]

Definition at line 97 of file Creole.php.

Creole::$driverMap [static, private]

Initial value:

 array(  'mysql' => 'creole.drivers.mysql.MySQLConnection',
                                        'mysqli' => 'creole.drivers.mysqli.MySQLiConnection',
                                        'pgsql' => 'creole.drivers.pgsql.PgSQLConnection',
                                        'sqlite' => 'creole.drivers.sqlite.SQLiteConnection',
                                        'oracle' => 'creole.drivers.oracle.OCI8Connection',
                                        'mssql' => 'creole.drivers.mssql.MSSQLConnection',
                                        'odbc' => 'creole.drivers.odbc.ODBCConnection'
                                       )

Definition at line 83 of file Creole.php.

const Creole::COMPAT_ALL = 96

Flag to indicate that all compatibility flags should be set.

Definition at line 75 of file Creole.php.

Referenced by getConnection().

Flag to pass to the connection to indicate that a to-lower case conversion should be performed by ResultSet on keys of fetched rows.

Definition at line 64 of file Creole.php.

Referenced by ResultSetCommon::__construct(), SQLiteConnection::connect(), ODBCConnection::connect(), and getConnection().

Flag to pass to the connection to indicate that an rtrim() should be performed on strings (using ResultSet->getString(), etc.

).

Definition at line 70 of file Creole.php.

Referenced by ResultSetCommon::__construct(), and getConnection().

Flag to pass to the connection to indicate that no case conversions should be performed by ResultSet on keys of fetched rows.

Deprecated:
use COMPAT_ASSOC_LOWER

Definition at line 58 of file Creole.php.

Referenced by getConnection().

const Creole::PERSISTENT = 1

Constant that indicates a connection object should be used.

Definition at line 51 of file Creole.php.

Referenced by SQLiteConnection::connect(), PgSQLConnection::connect(), OCI8Connection::connect(), ODBCConnection::connect(), MySQLConnection::connect(), MSSQLConnection::connect(), and getConnection().


The documentation for this class was generated from the following file:

Generated on Wed May 6 23:10:49 2009 for fareofficelib by  doxygen 1.5.8