= PHP 4 * @Dependencies PDO_mysql.class.php * PDO_sqlite.class.php * PDOStatement_mysql.class.php * PDOStatement_sqlite.class.php * @Author Andrea Giammarchi * @Site http://www.devpro.it/ * @Mail andrea [ at ] 3site [ dot ] it * @Date 2005/10/13 * @LastModified 2005/12/01 21:40 * @Version 0.1b - tested, supports only PostgreSQL, MySQL or SQLITE databases */ class PDO { /** Modified on 2005/12/01 to support new PDO constants on PHP 5.1.X */ const FETCH_ASSOC = PDO_FETCH_ASSOC; const FETCH_NUM = PDO_FETCH_NUM; const FETCH_BOTH = PDO_FETCH_BOTH; const FETCH_OBJ = PDO_FETCH_OBJ; const FETCH_LAZY = PDO_FETCH_LAZY; const FETCH_BOUND = PDO_FETCH_BOUND; const ATTR_SERVER_VERSION = PDO_ATTR_SERVER_VERSION; const ATTR_CLIENT_VERSION = PDO_ATTR_CLIENT_VERSION; const ATTR_SERVER_INFO = PDO_ATTR_SERVER_INFO; const ATTR_PERSISTENT = PDO_ATTR_PERSISTENT; /** * 'Private' variables: * __driver:PDO_* Dedicated PDO database class */ var $__driver; /** * Public constructor * http://us2.php.net/manual/en/function.pdo-construct.php */ function PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) { $con = &$this->__getDNS($string_dsn); if($con['dbtype'] === 'mysql') { require_once('PDO_mysql.class.php'); if(isset($con['port'])) $con['host'] .= ':'.$con['port']; $this->__driver = new PDO_mysql( $con['host'], $con['dbname'], $string_username, $string_password ); } elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') { require_once('PDO_sqlite.class.php'); $this->__driver = new PDO_sqlite($con['dbname']); } elseif($con['dbtype'] === 'pgsql') { require_once('PDO_pgsql.class.php'); $string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}"; if(isset($con['port'])) $string_dsn .= " port={$con['port']}"; $this->__driver = new PDO_pgsql($string_dsn); } } /** UNSUPPORTED * Public method * http://us2.php.net/manual/en/function.pdo-begintransaction.php */ function beginTransaction() { $this->__driver->beginTransaction(); } /** NOT NATIVE BUT MAYBE USEFULL FOR PHP < 5.1 PDO DRIVER * Public method * Calls database_close function. * this->close( Void ):Boolean * @Return Boolean True on success, false otherwise */ function close() { return $this->__driver->close(); } /** UNSUPPORTED * Public method * http://us2.php.net/manual/en/function.pdo-commit.php */ function commit() { $this->__driver->commit(); } /** * Public method * http://us2.php.net/manual/en/function.pdo-exec.php */ function exec($query) { return $this->__driver->exec($query); } /** * Public method * http://us2.php.net/manual/en/function.pdo-errorcode.php */ function errorCode() { return $this->__driver->errorCode(); } /** * Public method * http://us2.php.net/manual/en/function.pdo-errorinfo.php */ function errorInfo() { return $this->__driver->errorInfo(); } /** NOT TOTALLY UNSUPPORTED * Public method * http://us2.php.net/manual/en/function.pdo-getattribute.php */ function getAttribute($attribute) { return $this->__driver->getAttribute($attribute); } /** * Public method * http://us2.php.net/manual/en/function.pdo-lastinsertid.php */ function lastInsertId() { return $this->__driver->lastInsertId(); } /** * Public method * http://us2.php.net/manual/en/function.pdo-prepare.php */ function prepare($query, $array = Array()) { return $this->__driver->prepare($query, $array = Array()); } /** * Public method * http://us2.php.net/manual/en/function.pdo-query.php */ function query($query) { return $this->__driver->query($query); } /** * Public method * http://us2.php.net/manual/en/function.pdo-quote.php */ function quote($string) { return $this->__driver->quote($string); } /** UNSUPPORTED * Public method * http://us2.php.net/manual/en/function.pdo-rollback.php */ function rollBack() { $this->__driver->rollBack(); } /** NOT TOTALLY UNSUPPORTED * Public method * http://us2.php.net/manual/en/function.pdo-setattribute.php */ function setAttribute($attribute, $mixed) { return $this->__driver->setAttribute($attribute, $mixed); } // PRIVATE METHOD [uncommented] function __getDNS(&$string) { $result = array(); $pos = strpos($string, ':'); $parameters = explode(';', substr($string, ($pos + 1))); $result['dbtype'] = strtolower(substr($string, 0, $pos)); for($a = 0, $b = count($parameters); $a < $b; $a++) { $tmp = explode('=', $parameters[$a]); if(count($tmp) == 2) $result[$tmp[0]] = $tmp[1]; else $result['dbname'] = $parameters[$a]; } return $result; } } } // If you have PHP 5.1 but want to test this class, declare PDO variables as _PDO variables else { /** * Class _PDO * (C) Andrea Giammarchi * Please read PDO class comments to know more */ class _PDO { const FETCH_ASSOC = PDO::FETCH_ASSOC; const FETCH_NUM = PDO::FETCH_NUM; const FETCH_BOTH = PDO::FETCH_BOTH; const FETCH_OBJ = PDO::FETCH_OBJ; const FETCH_LAZY = PDO::FETCH_LAZY; const FETCH_BOUND = PDO::FETCH_BOUND; const ATTR_SERVER_VERSION = PDO::ATTR_SERVER_VERSION; const ATTR_CLIENT_VERSION = PDO::ATTR_CLIENT_VERSION; const ATTR_SERVER_INFO = PDO::ATTR_SERVER_INFO; const ATTR_PERSISTENT = PDO::ATTR_PERSISTENT; var $__driver; function _PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) { $con = &$this->__getDNS($string_dsn); if($con['dbtype'] === 'mysql') { require_once('PDO_mysql.class.php'); if(isset($con['port'])) $con['host'] .= ':'.$con['port']; $this->__driver = new PDO_mysql( $con['host'], $con['dbname'], $string_username, $string_password ); } elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') { require_once('PDO_sqlite.class.php'); $this->__driver = new PDO_sqlite($con['dbname']); } elseif($con['dbtype'] === 'pgsql') { require_once('PDO_pgsql.class.php'); $string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}"; if(isset($con['port'])) $string_dsn .= " port={$con['port']}"; $this->__driver = new PDO_pgsql($string_dsn); } } function beginTransaction() { $this->__driver->beginTransaction(); } function close() { return $this->__driver->close(); } function commit() { $this->__driver->commit(); } function exec($query) { return $this->__driver->exec($query); } function errorCode() { return $this->__driver->errorCode(); } function errorInfo() { return $this->__driver->errorInfo(); } function getAttribute($attribute) { return $this->__driver->getAttribute($attribute); } function lastInsertId() { return $this->__driver->lastInsertId(); } function prepare($query, $array = Array()) { return $this->__driver->prepare($query, $array = Array()); } function query($query) { return $this->__driver->query($query); } function quote($string) { return $this->__driver->quote($string); } function rollBack() { $this->__driver->rollBack(); } function setAttribute($attribute, $mixed) { return $this->__driver->setAttribute($attribute, $mixed); } function __getDNS(&$string) { $result = array(); $pos = strpos($string, ':'); $parameters = explode(';', substr($string, ($pos + 1))); $result['dbtype'] = strtolower(substr($string, 0, $pos)); for($a = 0, $b = count($parameters); $a < $b; $a++) { $tmp = explode('=', $parameters[$a]); if(count($tmp) == 2) $result[$tmp[0]] = $tmp[1]; else $result['dbname'] = $parameters[$a]; } return $result; } } } ?>