From 0c369fee9b6ef70bdd92f1141635699d5ad9707b Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 14 May 2010 15:54:33 -0600 Subject: [PATCH] Add ability to define a write DB for use with split-DB installations --- framework/Core/lib/Horde/Core/Binder/Db.php | 27 +++++++++--- framework/Db/lib/Horde/Db/Adapter/Base.php | 67 ++++++++++++++++++++++++----- framework/Db/package.xml | 3 +- 3 files changed, 80 insertions(+), 17 deletions(-) diff --git a/framework/Core/lib/Horde/Core/Binder/Db.php b/framework/Core/lib/Horde/Core/Binder/Db.php index 2d9912293..6e166ac74 100644 --- a/framework/Core/lib/Horde/Core/Binder/Db.php +++ b/framework/Core/lib/Horde/Core/Binder/Db.php @@ -8,16 +8,32 @@ class Horde_Core_Binder_Db implements Horde_Injector_Binder /** * Handle Horde-style configuration arrays, PEAR DB/MDB2 arrays or DSNs, or * PDO DSNS. + * + * @return Horde_Db_Adapter_Base + * @throws Horde_Exception */ public function create(Horde_Injector $injector) { - $config = $GLOBALS['conf']['sql']; + return $this->_createDb($GLOBALS['conf']['sql'], $injector); + } + + protected function _createDb($config, $injector) + { + if (!empty($config['splitread'])) { + unset($config['splitread']); + $config['write_db'] = $this->_createDb($config, $injector); + $config = array_merge($config, $config['read']); + } + if (!isset($config['adapter'])) { if ($config['phptype'] == 'oci8') { $config['phptype'] = 'oci'; } - $config['adapter'] = $config['phptype'] == 'mysqli' ? 'mysqli' : 'pdo_' . $config['phptype']; + $config['adapter'] = ($config['phptype'] == 'mysqli') + ? 'mysqli' + : 'pdo_' . $config['phptype']; } + if (!empty($config['hostspec'])) { $config['host'] = $config['hostspec']; } @@ -32,11 +48,12 @@ class Horde_Core_Binder_Db implements Horde_Injector_Binder $adapter = str_replace(' ', '_' , ucwords(str_replace('_', ' ', basename($config['adapter'])))); $class = 'Horde_Db_Adapter_' . $adapter; - if (!class_exists($class)) { - throw new Horde_Exception('Adapter class "' . $class . '" not found'); + + if (class_exists($class)) { + return new $class($config); } - return new $class($config); + throw new Horde_Exception('Adapter class "' . $class . '" not found'); } public function equals(Horde_Injector_Binder $binder) diff --git a/framework/Db/lib/Horde/Db/Adapter/Base.php b/framework/Db/lib/Horde/Db/Adapter/Base.php index 47f500ead..14e489d6e 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base.php @@ -25,6 +25,7 @@ abstract class Horde_Db_Adapter_Base { /** * Config options + * * @var array */ protected $_config = array(); @@ -79,14 +80,28 @@ abstract class Horde_Db_Adapter_Base */ protected $_schemaMethods = array(); + /** + * Write DB + * + * @var Horde_Db_Adapter_Base + */ + protected $_write; + /*########################################################################## # Construct/Destruct ##########################################################################*/ /** - * @param array $config Configuration options and optional objects (logger, - * cache, etc.) + * Constructor. + * + * @param array $config Configuration options and optional objects: + *
+     * 'cache' - TODO
+     * 'charset' - (string) TODO
+     * 'logger' - TODO
+     * 'write_db' - (Horde_Db_Adapter_Base) Use this DB for write operations.
+     * 
*/ public function __construct($config) { @@ -441,6 +456,10 @@ abstract class Horde_Db_Adapter_Base */ public function insert($sql, $arg1 = null, $arg2 = null, $pk = null, $idValue = null, $sequenceName = null) { + if ($this->_write) { + return $this->_write->insert($sql, $arg1, $arg2, $pk, $idValue, $sequenceName); + } + $this->execute($sql, $arg1, $arg2); return isset($idValue) ? $idValue : $this->_connection->lastInsertId(); } @@ -454,6 +473,10 @@ abstract class Horde_Db_Adapter_Base */ public function update($sql, $arg1 = null, $arg2 = null) { + if ($this->_write) { + return $this->_write->update($sql, $arg1, $arg2); + } + $this->execute($sql, $arg1, $arg2); return $this->_rowCount; } @@ -467,6 +490,10 @@ abstract class Horde_Db_Adapter_Base */ public function delete($sql, $arg1 = null, $arg2 = null) { + if ($this->_write) { + return $this->_write->delete($sql, $arg1, $arg2); + } + $this->execute($sql, $arg1, $arg2); return $this->_rowCount; } @@ -476,7 +503,9 @@ abstract class Horde_Db_Adapter_Base */ public function transactionStarted() { - return $this->_transactionStarted; + return $this->_write + ? $this->_write->transactionStarted() + : $this->_transactionStarted; } /** @@ -484,8 +513,12 @@ abstract class Horde_Db_Adapter_Base */ public function beginDbTransaction() { - $this->_transactionStarted = true; - $this->_connection->beginTransaction(); + if ($this->_write) { + $this->_write->beginDbTransaction(); + } else { + $this->_transactionStarted = true; + $this->_connection->beginTransaction(); + } } /** @@ -493,8 +526,12 @@ abstract class Horde_Db_Adapter_Base */ public function commitDbTransaction() { - $this->_connection->commit(); - $this->_transactionStarted = false; + if ($this->_write) { + $this->_write->commitDbTransaction(); + } else { + $this->_connection->commit(); + $this->_transactionStarted = false; + } } /** @@ -505,8 +542,12 @@ abstract class Horde_Db_Adapter_Base { if (! $this->_transactionStarted) { return; } - $this->_connection->rollBack(); - $this->_transactionStarted = false; + if ($this->_write) { + $this->_write->rollbackDbTransaction(); + } else { + $this->_connection->rollBack(); + $this->_transactionStarted = false; + } } /** @@ -557,8 +598,12 @@ abstract class Horde_Db_Adapter_Base */ public function insertFixture($fixture, $tableName) { - /*@TODO*/ - return $this->execute("INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'); + if ($this->_write) { + return $this->_write->insertFixture($fixture, $tableName); + } else { + /*@TODO*/ + return $this->execute("INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'); + } } public function emptyInsertStatement($tableName) diff --git a/framework/Db/package.xml b/framework/Db/package.xml index 8ba7df198..0272a87b9 100644 --- a/framework/Db/package.xml +++ b/framework/Db/package.xml @@ -30,7 +30,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> beta BSD - * Initial release + * Add ability to define a write DB for use with split-DB installations. + * Initial release -- 2.11.0