Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * The default database handle is a PDO object.
- * This will be used for normal database interaction.
- */
- $db = new PDO('mysql:host=127.0.0.1;dbname=test', 'user', 'password');
- /**
- * But if I run into legacy code which needs ADOdb to do something
- * I would like to use the existing PDO connection instead of wasting
- * resources making a second connection to the same database.
- */
- require_once('adodb/adodb.inc.php');
- require_once('adodb/drivers/adodb-pdo.inc.php');
- class CustomAdodbPdo extends ADODB_pdo {
- function __construct($pdo) {
- try {
- $this->_connectionID = $pdo;
- } catch (Exception $e) {
- $this->_connectionID = false;
- $this->_errorno = -1;
- $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
- return false;
- }
- if ($this->_connectionID) {
- $this->dsnType = strtolower(PDO::ATTR_DRIVER_NAME);
- switch (ADODB_ASSOC_CASE) {
- case 0: $m = PDO::CASE_LOWER; break;
- case 1: $m = PDO::CASE_UPPER; break;
- default:
- case 2: $m = PDO::CASE_NATURAL; break;
- }
- $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
- $class = 'ADODB_pdo_'.$this->dsnType;
- switch ($this->dsnType) {
- case 'oci':
- case 'mysql':
- case 'pgsql':
- case 'mssql':
- include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
- break;
- }
- if (class_exists($class))
- $this->_driver = new $class();
- else
- $this->_driver = new ADODB_pdo_base();
- $this->_driver->_connectionID = $this->_connectionID;
- $this->_UpdatePDO();
- return true;
- }
- $this->_driver = new ADODB_pdo_base();
- return false;
- }
- }
- $ADODB_NEWCONNECTION = 'CustomPdoFactory';
- function &CustomPdoFactory() {
- global $db;
- $obj = new CustomAdodbPdo($db);
- return $obj;
- }
- // The ADONewConnection() now uses the existing PDO connection "$db"
- $adodb = &ADONewConnection();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment