cvs: pear /XML_Query2XML/XML/Query2XML/Driver ADOdb.php DB.php MDB2.php PDO.php

From: Date: Sat, 29 Sep 2007 23:08:26 +0000
Subject: cvs: pear /XML_Query2XML/XML/Query2XML/Driver ADOdb.php DB.php MDB2.php PDO.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-48710@lists.php.net to get a copy of this message
lukasfeiler Sat Sep 29 23:08:26 2007 UTC Added files: /pear/XML_Query2XML/XML/Query2XML/Driver ADOdb.php DB.php MDB2.php PDO.php Log: drivers for the different database abstraction layers

http://cvs.php.net/viewvc.cgi/pear/XML_Query2XML/XML/Query2XML/Driver/ADOdb.php?view=markup&rev=1.1 Index: pear/XML_Query2XML/XML/Query2XML/Driver/ADOdb.php +++ pear/XML_Query2XML/XML/Query2XML/Driver/ADOdb.php <?php /**This file contains the class XML_Query2XML_Driver_ADOdb. * * LICENSE: * This source file is subject to version 2.1 of the LGPL * that is bundled with this package in the file LICENSE. * * COPYRIGHT: * Empowered Media * http://www.empoweredmedia.com * 481 Eighth Avenue Suite 1530 * New York, NY 10001 * * @copyright Empowered Media 2006 * @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1 * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @package XML_Query2XML * @version $Id: ADOdb.php,v 1.1 2007/09/29 23:08:26 lukasfeiler Exp $ * @access private */ /**XML_Query2XML_Driver_ADOdb extends XML_Query2XML_Driver. */ require_once 'XML/Query2XML.php'; /**Driver for the database abstraction layer PEAR MDB2. * * usage: * <code> * $driver = XML_Query2XML_Driver::factory(NewADOConnection(...)); * </code> * * @access private * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @version Release: @package_version@ * @copyright Empowered Media 2006 * @package XML_Query2XML * @since Release 1.5.0RC1 */ class XML_Query2XML_Driver_ADOdb extends XML_Query2XML_Driver { /**In instance of a class that extends ADOConnection. * @var ADOConnection */ private $db = null; /**Constructor * * @throws XML_Query2XML_DBException If the ADOConnection instance passed as * argument was not connected to the database server. * @param ADOConnection $db An instance of ADOConnection. */ public function __construct(ADOConnection $db) { if (!$db->IsConnected()) { throw new XML_Query2XML_DBException( 'ADOConnection instance was not connected' ); } $db->SetFetchMode(ADODB_FETCH_ASSOC); $this->_db = $db; } /**Execute a SQL SELECT stement and fetch all records from the result set. * * @see XML_Query2XML_Driver::getAllRecords() * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql The SQL query as a string or an array. * @param string $configPath The config path; used for exception messages. * @return array An array of records. */ public function &getAllRecords($sql, $configPath) { $result =& $this->_prepareAndExecute($sql, $configPath); $records = array(); while ($record = $result->fetchRow()) { if (class_exists('PEAR_Error') && $record instanceof PEAR_Error) { //no unit test for this exception as it cannot be produced easily throw new XML_Query2XML_DBException( $configPath . ': Could not fetch rows for the following ' . 'SQL query: ' . $this->extractQueryString($sql) . '; ' . $record->toString() ); } $records[] = $record; } return $records; } /**Private method that will use ADOConnection::query() for simple and * ADOConnection::prepare() & ADOConnection::execute() for complex query * specifications. * * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql A string or an array. * @param string $configPath The config path used for exception messages. * @return DB_result */ private function _prepareAndExecute($sql, $configPath) { if (is_string($sql)) { /* * SIMPLE QUERY */ try { $result = $this->_db->query($sql); } catch (Exception $e) { /* * unit tests: ADOdbException/ * getXML/throwDBException.phpt * getFlatXML/throwDBException.phpt * _prepareAndExecute\throwDBException_simpleQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not run the following SQL query: ' . $sql . '; ' . $e->getMessage() ); } if (class_exists('PEAR_Error') && $result instanceof PEAR_Error) { /* * unit tests: ADOdbPEAR/ * getXML/throwDBException.phpt * getFlatXML/throwDBException.diff * _prepareAndExecute/throwDBException_simpleQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not run the following SQL query: ' . $sql . '; ' . $result->toString() ); } elseif ($result === false) { /* * unit tests: ADOdbDefault/ * getXML/throwDBException.phpt * getFlatXML/throwDBException.phpt * _prepareAndExecute/throwDBException_simpleQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not run the following SQL query: ' . $sql ); } } else { /* * PREPARE & EXECUTE */ $query =& $sql['query']; if (isset($this->_preparedQueries[$query])) { $queryHandle = $this->_preparedQueries[$query]; } else { //ADOdb centralizes all error-handling in execute() $queryHandle = $this->_db->prepare($query); $this->_preparedQueries[$query] =& $queryHandle; } /* * EXECUTE */ try { if (isset($sql['data'])) { $result = $this->_db->execute($queryHandle, $sql['data']); } else { $result = $this->_db->execute($queryHandle); } } catch (Exception $e) { /* * unit test: ADOdbException/ * _prepareAndExecute/throwDBException_complexQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not execute the following SQL ' . 'query: ' . $query . '; ' . $e->getMessage() ); } if ($result === false && function_exists('ADODB_Pear_Error')) { $result = ADODB_Pear_Error(); } if (class_exists('PEAR_Error') && $result instanceof PEAR_Error) { /* * unit test: ADOdbPEAR/ * _prepareAndExecute/throwDBException_complexQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not execute the following SQL query: ' . $query . '; ' . $result->toString() ); } elseif ($result === false) { /* * unit test: ADOdbDefault/ * _prepareAndExecute/throwDBException_complexQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not execute the following SQL query: ' . $query . ' (false was returned)' ); } } return $result; } } ?> http://cvs.php.net/viewvc.cgi/pear/XML_Query2XML/XML/Query2XML/Driver/DB.php?view=markup&rev=1.1 Index: pear/XML_Query2XML/XML/Query2XML/Driver/DB.php +++ pear/XML_Query2XML/XML/Query2XML/Driver/DB.php <?php /**This file contains the class XML_Query2XML_Driver_DB. * * LICENSE: * This source file is subject to version 2.1 of the LGPL * that is bundled with this package in the file LICENSE. * * COPYRIGHT: * Empowered Media * http://www.empoweredmedia.com * 481 Eighth Avenue Suite 1530 * New York, NY 10001 * * @copyright Empowered Media 2006 * @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1 * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @package XML_Query2XML * @version $Id: DB.php,v 1.1 2007/09/29 23:08:26 lukasfeiler Exp $ * @access private */ /**XML_Query2XML_Driver_DB extends XML_Query2XML_Driver. */ require_once 'XML/Query2XML.php'; /**As the method PEAR::isError() is used within XML_Query2XML_Driver_DB we require PEAR.php. */ require_once 'PEAR.php'; /**Driver for the database abstraction layer PEAR DB. * * usage: * <code> * $driver = XML_Query2XML_Driver::factory(DB::connect(...)); * </code> * * @access private * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @version Release: @package_version@ * @copyright Empowered Media 2006 * @package XML_Query2XML * @since Release 1.5.0RC1 */ class XML_Query2XML_Driver_DB extends XML_Query2XML_Driver { /**In instance of a class that extends DB_common. * @var DB_common */ private $db = null; /**Constructor * * @throws XML_Query2XML_DBException If the fetch mode cannot be set to * DB_FETCHMODE_ASSOC. * @param DB_Common $db An instance of a class that extends DB_Common. */ public function __construct(DB_common $db) { $fetchModeError = $db->setFetchMode(DB_FETCHMODE_ASSOC); if (PEAR::isError($fetchModeError)) { //no unit tests for this one throw new XML_Query2XML_DBException( 'Could not set fetch mode to DB_FETCHMODE_ASSOC: ' . $fetchModeError->toString() ); } $this->_db = $db; } /**Execute a SQL SELECT stement and fetch all records from the result set. * * @see XML_Query2XML_Driver::getAllRecords() * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql The SQL query as a string or an array. * @param string $configPath The config path; used for exception messages. * @return array An array of records. */ public function &getAllRecords($sql, $configPath) { $result =& $this->_prepareAndExecute($sql, $configPath); $records = array(); while ($record = $result->fetchRow()) { if (PEAR::isError($record)) { //no unit test for this exception as it cannot be produced easily throw new XML_Query2XML_DBException( $configPath . ': Could not fetch rows for the following ' . 'SQL query: ' . $this->extractQueryString($sql) . '; ' . $record->toString() ); } $records[] = $record; } return $records; } /**Private method that will use DB_Common::query() for simple and * DB_Common::prepare() & DB_Common::execute() for complex query specifications. * * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql A string or an array. * @param string $configPath The config path used for exception messages. * @return DB_result */ private function _prepareAndExecute($sql, $configPath) { if (is_string($sql)) { /* * simple query specification */ $result = $this->_db->query($sql); if (PEAR::isError($result)) { /* * unit tests: DB/ * getXML/throwDBException.phpt * getFlatXML\throwDBException.phpt * _prepareAndExecute\throwDBException_simpleQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not run the following SQL query: ' . $sql . '; ' . $result->toString() ); } if (!($result instanceof DB_result)) { /* * unit tests: DB/getXML/ * throwDBException_nullResultSet_simpleQuery_multipleRecords.phpt * throwDBException_nullResultSet_simpleQuery_singleRecord.phpt */ throw new XML_Query2XML_DBException( $configPath . ': the following SQL query returned no ' . 'result set: ' . $sql ); } } else { /* * complex query specification */ $query =& $sql['query']; if (isset($this->_preparedQueries[$query])) { $queryHandle = $this->_preparedQueries[$query]; } else { //PREPARE $queryHandle = $this->_db->prepare($query); if (PEAR::isError($queryHandle)) { /* * No unit test for this exception as DB's mysql and pgsql * drivers never return a PEAR error from prepare(). */ throw new XML_Query2XML_DBException( $configPath . ': Could not prepare the following SQL query: ' . $query . '; ' . $queryHandle->toString() ); } $this->_preparedQueries[$query] =& $queryHandle; } //EXECUTE if (isset($sql['data'])) { $result = $this->_db->execute($queryHandle, $sql['data']); } else { $result = $this->_db->execute($queryHandle); } if (PEAR::isError($result)) { /* * unit test: DB/_prepareAndExecute/ * throwDBException_complexQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not execute the following SQL query: ' . $query . '; ' . $result->toString() ); } if (!($result instanceof DB_result)) { /* * unit tests: DB/getXML/ * throwDBException_nullResultSet_complexQuery_multipleRecords.phpt * throwDBException_nullResultSet_complexQuery_singleRecord.phpt */ throw new XML_Query2XML_DBException( $configPath . ': the following SQL query returned no ' . 'result set: ' . $query ); } } return $result; } } ?> http://cvs.php.net/viewvc.cgi/pear/XML_Query2XML/XML/Query2XML/Driver/MDB2.php?view=markup&rev=1.1 Index: pear/XML_Query2XML/XML/Query2XML/Driver/MDB2.php +++ pear/XML_Query2XML/XML/Query2XML/Driver/MDB2.php <?php /**This file contains the class XML_Query2XML_Driver_MDB2. * * LICENSE: * This source file is subject to version 2.1 of the LGPL * that is bundled with this package in the file LICENSE. * * COPYRIGHT: * Empowered Media * http://www.empoweredmedia.com * 481 Eighth Avenue Suite 1530 * New York, NY 10001 * * @copyright Empowered Media 2006 * @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1 * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @package XML_Query2XML * @version $Id: MDB2.php,v 1.1 2007/09/29 23:08:26 lukasfeiler Exp $ * @access private */ /**XML_Query2XML_Driver_MDB2 extends XML_Query2XML_Driver. */ require_once 'XML/Query2XML.php'; /**As the method PEAR::isError() is used within XML_Query2XML_Driver_DB we require PEAR.php. */ require_once 'PEAR.php'; /**Driver for the database abstraction layer PEAR MDB2. * * usage: * <code> * $driver = XML_Query2XML_Driver::factory(MDB2::factory(...)); * </code> * * @access private * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @version Release: @package_version@ * @copyright Empowered Media 2006 * @package XML_Query2XML * @since Release 1.5.0RC1 */ class XML_Query2XML_Driver_MDB2 extends XML_Query2XML_Driver { /**In instance of a class that extends MDB2_Driver_Common. * @var MDB2_Driver_Common */ private $db = null; /**Constructor * * @throws XML_Query2XML_DBException If the fetch mode cannot be set to * MDB2_FETCHMODE_ASSOC. * @param MDB2_Driver_Common $db An instance of MDB2_Driver_Common. */ public function __construct(MDB2_Driver_Common $db) { $fetchModeError = $db->setFetchMode(MDB2_FETCHMODE_ASSOC); if (PEAR::isError($fetchModeError)) { throw new XML_Query2XML_DBException( 'Could not set fetch mode to DB_FETCHMODE_ASSOC: ' . $fetchModeError->toString() ); } $this->_db = $db; } /**Execute a SQL SELECT stement and fetch all records from the result set. * * @see XML_Query2XML_Driver::getAllRecords() * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql The SQL query as a string or an array. * @param string $configPath The config path; used for exception messages. * @return array An array of records. */ public function &getAllRecords($sql, $configPath) { $result =& $this->_prepareAndExecute($sql, $configPath); $records = array(); while ($record = $result->fetchRow()) { if (PEAR::isError($record)) { //no unit test for this exception as it cannot be produced easily throw new XML_Query2XML_DBException( $configPath . ': Could not fetch rows for the following ' . 'SQL query: ' . $this->extractQueryString($sql) . '; ' . $record->toString() ); } $records[] = $record; } return $records; } /**Private method that will use MDB2_Driver_Common::query() for simple and * MDB2_Driver_Common::prepare() & MDB2_Statement_Common::execute() for complex * query specifications. * * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql A string or an array. * @param string $configPath The config path used for exception messages. * @return MDB2_Result */ private function _prepareAndExecute($sql, $configPath) { if (is_string($sql)) { /* * simple query specification */ $result = $this->_db->query($sql); if (PEAR::isError($result)) { /* * unit tests: DB/ * getXML/throwDBException.phpt * getFlatXML\throwDBException.phpt * _prepareAndExecute\throwDBException_simpleQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not run the following SQL query: ' . $sql . '; ' . $result->toString() ); } } else { /* * complex query specification */ $query =& $sql['query']; if (isset($this->_preparedQueries[$query])) { $queryHandle = $this->_preparedQueries[$query]; } else { //PREPARE $queryHandle = $this->_db->prepare($query); if (PEAR::isError($queryHandle)) { /* * unit tests: (only if mysql or pgsql is used) * MDB2/_prepareAndExecute/throwDBException_complexQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not prepare the following SQL query: ' . $query . '; ' . $queryHandle->toString() ); } $this->_preparedQueries[$query] =& $queryHandle; } //EXECUTE if (isset($sql['data'])) { $result = $queryHandle->execute($sql['data']); } else { $result = $queryHandle->execute(); } if (PEAR::isError($result)) { /* * unit tests: * if sqlite is used: MDB2/_prepareAndExecute/ * throwDBException_complexQuery.phpt * if sqlite or mysql is sued: MDB2/getXML/ * throwDBException_nullResultSet_complexQuery_multipleRecords.phpt * throwDBException_nullResultSet_complexQuery_singleRecord.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not execute the following SQL query: ' . $query . '; ' . $result->toString() ); } } return $result; } } ?> http://cvs.php.net/viewvc.cgi/pear/XML_Query2XML/XML/Query2XML/Driver/PDO.php?view=markup&rev=1.1 Index: pear/XML_Query2XML/XML/Query2XML/Driver/PDO.php +++ pear/XML_Query2XML/XML/Query2XML/Driver/PDO.php <?php /**This file contains the class XML_Query2XML_Driver_PDO. * * LICENSE: * This source file is subject to version 2.1 of the LGPL * that is bundled with this package in the file LICENSE. * * COPYRIGHT: * Empowered Media * http://www.empoweredmedia.com * 481 Eighth Avenue Suite 1530 * New York, NY 10001 * * @copyright Empowered Media 2006 * @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1 * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @package XML_Query2XML * @version $Id: PDO.php,v 1.1 2007/09/29 23:08:26 lukasfeiler Exp $ * @access private */ /**XML_Query2XML_Driver_PDO extends XML_Query2XML_Driver. */ require_once 'XML/Query2XML.php'; /**Driver for the database abstraction layer PDO. * * usage: * <code> * $driver = XML_Query2XML_Driver::factory(new PDO(...)); * </code> * * @access private * @author Lukas Feiler <lukas.feiler@lukasfeiler.com> * @version Release: @package_version@ * @copyright Empowered Media 2006 * @package XML_Query2XML * @since Release 1.5.0RC1 */ class XML_Query2XML_Driver_PDO extends XML_Query2XML_Driver { /**In instance of PDO * @var PDO */ private $db = null; /**Constructor * * @throws XML_Query2XML_DBException If PDO::ATTR_ERRMODE cannot be set to * PDO::ERRMODE_EXCEPTION. * @param PDO $db An instance of PDO. */ public function __construct(PDO $db) { $success = $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (!$success) { //no unit tests for this one throw new XML_Query2XML_DBException( 'Could not set attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION' ); } $this->_db = $db; } /**Execute a SQL SELECT stement and fetch all records from the result set. * * @see XML_Query2XML_Driver::getAllRecords() * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql The SQL query as a string or an array. * @param string $configPath The config path; used for exception messages. * @return array An array of records. */ public function &getAllRecords($sql, $configPath) { $result =& $this->_prepareAndExecute($sql, $configPath); try { $records = $result->fetchAll(); } catch (PDOException $e) { /* * unit tests: PDO/getXML/ * throwDBException_nullResultSet_complexQuery_multipleRecords.phpt * throwDBException_nullResultSet_complexQuery_singleRecord.phpt * throwDBException_nullResultSet_simpleQuery_multipleRecords.phpt * throwDBException_nullResultSet_simpleQuery_singleRecord.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not fetch records for the following SQL ' . 'query: ' . $this->extractQueryString($sql) . '; ' . $e->getMessage() ); } return $records; } /**Private method that will use PDO::query() for simple and * PDO::prepare() & PDOStatement::execute() for complex query specifications. * * @throws XML_Query2XML_DBException If a database related error occures. * @param mixed $sql A string or an array. * @param string $configPath The config path used for exception messages. * @return PDOStatement */ private function _prepareAndExecute($sql, $configPath) { if (is_string($sql)) { /* * simple query specification */ try { $result = $this->_db->query($sql); } catch (PDOException $e) { /* * unit tests: PDO/ * getXML/throwDBException.phpt * getFlatXML/throwDBException.phpt * _prepareAndExecute\throwDBException_simpleQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not run the following SQL query: ' . $sql . '; ' . $e->getMessage() ); } } else { /* * complex query specification */ $query =& $sql['query']; if (isset($this->_preparedQueries[$query])) { $queryHandle = $this->_preparedQueries[$query]; } else { //PREPARE $queryHandle = $this->_db->prepare($query); if ($queryHandle === false) { /* * No unit test for this exception as neither the mysql, pgsql * or sqlite driver ever returns false from PDO::prepare(). */ throw new XML_Query2XML_DBException( $configPath . ': Could not prepare the following SQL ' . 'query - PDO::prepare() returned false: ' . $query ); } $this->_preparedQueries[$query] =& $queryHandle; } //EXECUTE try { if (isset($sql['data'])) { $queryHandle->execute($sql['data']); } else { $queryHandle->execute(); } } catch (PDOException $e) { /* * unit test: PDO/_prepareAndExecute * throwDBException_complexQuery.phpt */ throw new XML_Query2XML_DBException( $configPath . ': Could not execute the following SQL query: ' . $query . '; ' . $e->getMessage() ); } $result = $queryHandle; } $success = $result->setFetchMode(PDO::FETCH_ASSOC); if (!$success) { //no unit tests for this one throw new XML_Query2XML_DBException( 'Could not set fetch mode to PDO::FETCH_ASSOC' ); } return $result; } } ?>
« previous php.pear.cvs (#48710) next »