Add ability to define a write DB for use with split-DB installations
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 14 May 2010 21:54:33 +0000 (15:54 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 18 May 2010 17:17:01 +0000 (11:17 -0600)
framework/Core/lib/Horde/Core/Binder/Db.php
framework/Db/lib/Horde/Db/Adapter/Base.php
framework/Db/package.xml

index 2d99122..6e166ac 100644 (file)
@@ -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)
index 47f500e..14e489d 100644 (file)
@@ -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:
+     * <pre>
+     * 'cache' - TODO
+     * 'charset' - (string) TODO
+     * 'logger' - TODO
+     * 'write_db' - (Horde_Db_Adapter_Base) Use this DB for write operations.
+     * </pre>
      */
     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)
index 8ba7df1..0272a87 100644 (file)
@@ -30,7 +30,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
- <notes>* Initial release
+ <notes>* Add ability to define a write DB for use with split-DB installations.
+ * Initial release
  </notes>
  <contents>
   <dir name="/">