Use Horde_Db
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 14 May 2010 22:08:14 +0000 (16:08 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 18 May 2010 17:17:01 +0000 (11:17 -0600)
Bug #9027: Possible fix to Horde_Alarm SQL issue

19 files changed:
framework/Alarm/lib/Horde/Alarm/Sql.php
framework/Alarm/package.xml
framework/Cache/lib/Horde/Cache/Sql.php
framework/Cache/package.xml
framework/Core/lib/Horde/Core/Binder/Alarm.php
framework/Core/lib/Horde/Core/Binder/Cache.php
framework/Core/lib/Horde/Core/Binder/Lock.php
framework/Core/lib/Horde/Core/Binder/Perms.php
framework/Core/lib/Horde/Core/Binder/Token.php
framework/Db/lib/Horde/Db/Adapter/Base.php
framework/Db/lib/Horde/Db/Adapter/Pdo/Oci.php
framework/Lock/lib/Horde/Lock/Sql.php
framework/Lock/package.xml
framework/Perms/lib/Horde/Perms/Permission/SqlObject.php
framework/Perms/lib/Horde/Perms/Sql.php
framework/Token/lib/Horde/Token/Sql.php
framework/Token/package.xml
imp/lib/Injector/Binder/Sentmail.php
imp/lib/Sentmail/Sql.php

index fc98ea3..43ba5c1 100644 (file)
@@ -23,27 +23,18 @@ class Horde_Alarm_Sql extends Horde_Alarm
     /**
      * Handle for the current database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
     protected $_db;
 
     /**
-     * Handle for the current database connection, used for writing. Defaults
-     * to the same handle as $_db if a separate write database is not required.
-     *
-     * @var DB
-     */
-    protected $_write_db;
-
-    /**
      * Constructor.
      *
      * @param array $params  Configuration parameters:
      * <pre>
-     * 'db' - (DB) [REQUIRED] The DB instance.
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
      * 'table' - (string) The name of the tokens table in 'database'.
      *           DEFAULT: 'horde_alarms'
-     * 'write_db' - (DB) The write DB instance.
      * </pre>
      *
      * @throws Horde_Alarm_Exception
@@ -54,12 +45,7 @@ class Horde_Alarm_Sql extends Horde_Alarm
             throw new Horde_Alarm_Exception('Missing db parameter.');
         }
         $this->_db = $params['db'];
-
-        $this->_write_db = isset($params['write_db'])
-            ? $params['write_db']
-            : $this->_db;
-
-        unset($params['db'], $params['write_db']);
+        unset($params['db']);
 
         $params = array_merge(array(
             'table' => 'horde_alarms'
@@ -90,28 +76,16 @@ class Horde_Alarm_Sql extends Horde_Alarm
             $values[] = (string)$user;
         }
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_list(): ' . $query, 'DEBUG');
-        }
 
-        $alarms = array();
-        $result = $this->_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $result = $this->_db->selectAll($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
 
-        while ($alarm = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
-            if ($alarm instanceof PEAR_Error) {
-                if ($this->_logger) {
-                    $this->_logger->log($alarm, 'INFO');
-                }
-                throw new Horde_Alarm_Exception($alarm);
-            }
-
-            $alarms[] = $this->_getHash($alarm);
+        $alarms = array();
+        foreach ($result as $val) {
+            $alarms[] = $this->_getHash($val);
         }
 
         return $alarms;
@@ -150,16 +124,10 @@ class Horde_Alarm_Sql extends Horde_Alarm
                          $this->_params['table'],
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_get(): ' . $query, 'DEBUG');
-        }
-
-        $alarm = $this->_db->getRow($query, array($id, $user), DB_FETCHMODE_ASSOC);
-        if ($alarm instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($alarm, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($alarm);
+        try {
+            $alarm = $this->_db->selectOne($query, array($id, $user));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
 
         if (empty($alarm)) {
@@ -190,16 +158,11 @@ class Horde_Alarm_Sql extends Horde_Alarm
             empty($alarm['text']) ? null : $this->_toDriver($alarm['text']),
             null
         );
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_add(): ' . $query, 'DEBUG');
-        }
 
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $this->_db->insert($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
     }
 
@@ -228,16 +191,10 @@ class Horde_Alarm_Sql extends Horde_Alarm
                         $alarm['id'],
                         isset($alarm['user']) ? $alarm['user'] : '');
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_update(): ' . $query, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $this->_db->update($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
     }
 
@@ -258,16 +215,10 @@ class Horde_Alarm_Sql extends Horde_Alarm
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
         $values = array(serialize($internal), $id, $user);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::internal(): ' . $query, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $this->_db->query($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
     }
 
@@ -286,19 +237,11 @@ class Horde_Alarm_Sql extends Horde_Alarm
                          $this->_params['table'],
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_exists(): ' . $query, 'DEBUG');
+        try {
+            return ($this->_db->selectValue($query, array($id, $user)) == 1);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
-
-        $result = $this->_db->getOne($query, array($id, $user));
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
-        }
-
-        return $result == 1;
     }
 
     /**
@@ -317,16 +260,10 @@ class Horde_Alarm_Sql extends Horde_Alarm
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
         $values = array((string)$snooze->setTimezone('UTC'), $id, $user);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_snooze(): ' . $query, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $this->_db->update($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
     }
 
@@ -346,19 +283,11 @@ class Horde_Alarm_Sql extends Horde_Alarm
                          $this->_params['table'],
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_isSnoozed(): ' . $query, 'DEBUG');
+        try {
+            return $this->_db->selectValue($query, array($id, $user, (string)$time->setTimezone('UTC')));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
-
-        $result = $this->_db->getOne($query, array($id, $user, (string)$time->setTimezone('UTC')));
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
-        }
-
-        return $result;
     }
 
     /**
@@ -376,16 +305,10 @@ class Horde_Alarm_Sql extends Horde_Alarm
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
         $values = array($id, $user);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_dismiss(): ' . $query, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $this->_db->update($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
     }
 
@@ -408,43 +331,25 @@ class Horde_Alarm_Sql extends Horde_Alarm
             $values[] = $user;
         }
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_delete(): ' . $query, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
+        try {
+            $this->_db->delete($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Alarm_Exception($e);
         }
     }
 
     /**
      * Garbage collects old alarms in the backend.
-     *
-     * @throws Horde_Alarm_Exception
      */
     protected function _gc()
     {
         $query = sprintf('DELETE FROM %s WHERE alarm_end IS NOT NULL AND alarm_end < ?', $this->_params['table']);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_gc(): ' . $query, 'DEBUG');
-        }
-
         $end = new Horde_Date(time());
 
-        $result = $this->_write_db->query($query, (string)$end->setTimezone('UTC'));
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($result);
-        }
-
-        return $result;
+        try {
+            $this->_db->delete($query, (string)$end->setTimezone('UTC'));
+        } catch (Horde_Db_Exception $e) {}
     }
 
     /**
@@ -454,37 +359,16 @@ class Horde_Alarm_Sql extends Horde_Alarm
      */
     public function initialize()
     {
-        $this->_initConn($this->_db);
-        if ($this->_write_db) {
-            $this->_initConn($this->_write_db);
-        }
-    }
-
-    /**
-     * Alarm specific initialization tasks.
-     */
-    protected function _initConn(DB_common $db)
-    {
         /* Handle any database specific initialization code to run. */
-        switch ($db->dbsyntax) {
-        case 'oci8':
+        switch ($this->_db->adapterName()) {
+        case 'PDO_Oci':
             $query = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
-
-            if ($this->_logger) {
-                $this->_logger->log(sprintf('SQL connection setup for Alarms, query = "%s"', $query), 'DEBUG');
-            }
-
-            $db->query($query);
+            $db->execute($query);
             break;
 
-        case 'pgsql':
+        case 'PDO_PostrgreSQL':
             $query = "SET datestyle TO 'iso'";
-
-            if ($this->_logger) {
-                $this->_logger->log(sprintf('SQL connection setup for Alarms, query = "%s"', $query), 'DEBUG');
-            }
-
-            $db->query($query);
+            $db->execute($query);
             break;
         }
     }
index 5daee15..799a54c 100644 (file)
   </required>
   <optional>
    <package>
-    <name>DB</name>
-    <channel>pear.php.net</channel>
+    <name>Db</name>
+    <channel>pear.horde.org</channel>
    </package>
    <package>
-    <name>Mail</name>
+    <name>Log</name>
     <channel>pear.horde.org</channel>
    </package>
    <package>
-    <name>Mime</name>
+    <name>Mail</name>
     <channel>pear.horde.org</channel>
    </package>
    <package>
-    <name>Notification</name>
+    <name>Mime</name>
     <channel>pear.horde.org</channel>
    </package>
    <package>
-    <name>Log</name>
+    <name>Notification</name>
     <channel>pear.horde.org</channel>
    </package>
    <package>
index f1a31a0..e540e05 100644 (file)
@@ -33,19 +33,11 @@ class Horde_Cache_Sql extends Horde_Cache_Base
     /**
      * Handle for the current database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
     protected $_db;
 
     /**
-     * Handle for the current database connection, used for writing. Defaults
-     * to the same handle as $_db if a separate write database isn't required.
-     *
-     * @var DB
-     */
-    protected $_write_db;
-
-    /**
      * The memory cache object to use, if configured.
      *
      * @var Horde_Cache
@@ -57,12 +49,11 @@ class Horde_Cache_Sql extends Horde_Cache_Base
      *
      * @param array $params  Parameters:
      * <pre>
-     * 'db' - (DB) [REQUIRED] The DB instance.
-     * 'table' - (string) The name of the cache table in 'database'.
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
+     * 'table' - (string) The name of the cache table.
      *           DEFAULT: 'horde_cache'
      * 'use_memorycache' - (Horde_Cache) Use this memory caching object to
      *                     cache the data (to avoid DB accesses).
-     * 'write_db' - (DB) The write DB instance.
      * </pre>
      *
      * @throws Horde_Cache_Exception
@@ -74,15 +65,11 @@ class Horde_Cache_Sql extends Horde_Cache_Base
         }
         $this->_db = $params['db'];
 
-        $this->_write_db = isset($params['write_db'])
-            ? $params['write_db']
-            : $this->_db;
-
         if (isset($params['use_memorycache'])) {
             $this->_mc = $params['use_memorycache'];
         }
 
-        unset($params['db'], $params['use_memorycache'], $params['write_db']);
+        unset($params['db'], $params['use_memorycache']);
 
         $params = array_merge(array(
             'table' => 'horde_cache',
@@ -105,12 +92,9 @@ class Horde_Cache_Sql extends Horde_Cache_Base
                  ' WHERE cache_expiration < ? AND cache_expiration <> 0';
         $values = array(time());
 
-        $result = $this->_write_db->query($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
-        }
+        try {
+            $this->_db->delete($query, $values);
+        } catch (Horde_Db_Exception $e) {}
     }
 
     /**
@@ -148,13 +132,13 @@ class Horde_Cache_Sql extends Horde_Cache_Base
             $values[] = $maxage;
         }
 
-        $result = $this->_db->getOne($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
+        try {
+            $result = $this->_db->selectValue($query, $values);
+        } catch (Horde_Db_Exception $e) (
             return false;
-        } elseif (is_null($result)) {
+        }
+
+        if (is_null($result)) {
             /* No rows were found - cache miss */
             if ($this->_logger) {
                 $this->_logger->log(sprintf('Cache miss: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
@@ -206,7 +190,9 @@ class Horde_Cache_Sql extends Horde_Cache_Base
         // Remove any old cache data and prevent duplicate keys
         $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE cache_id=?';
         $values = array($key);
-        $this->_write_db->query($query, $values);
+        try {
+            $this->_db->query($query, $values);
+        } catch (Horde_Db_Exception $e) {}
 
         /* Build SQL query. */
         $query = 'INSERT INTO ' . $this->_params['table'] .
@@ -214,11 +200,9 @@ class Horde_Cache_Sql extends Horde_Cache_Base
                  ' VALUES (?, ?, ?, ?)';
         $values = array($key, $timestamp, $expiration, $data);
 
-        $result = $this->_write_db->query($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
+        try {
+            $this->_db->query($query, $values);
+        } catch (Horde_Db_Exception $e) {
             return false;
         }
 
@@ -255,11 +239,9 @@ class Horde_Cache_Sql extends Horde_Cache_Base
             $values[] = time() - $lifetime;
         }
 
-        $result = $this->_db->getRow($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
+        try {
+            $result = $this->_db->selectValue($query, $values);
+        } catch (Horde_Db_Exception $e) {
             return false;
         }
 
@@ -297,11 +279,9 @@ class Horde_Cache_Sql extends Horde_Cache_Base
                  ' WHERE cache_id = ?';
         $values = array($key);
 
-        $result = $this->_write_db->query($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
+        try {
+            $this->_db->delete($query, $values);
+        } catch (Horde_Db_Exception $e) {
             return false;
         }
 
index 6ca6be4..e6aa6f7 100644 (file)
@@ -70,14 +70,18 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
     <min>1.7.0</min>
    </pearinstaller>
    <package>
+    <name>Exception</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Util</name>
     <channel>pear.horde.org</channel>
    </package>
   </required>
   <optional>
    <package>
-    <name>DB</name>
-    <channel>pear.php.net</channel>
+    <name>Db</name>
+    <channel>pear.horde.org</channel>
    </package>
    <package>
     <name>Log</name>
index 578d6d2..dd31aeb 100644 (file)
@@ -7,25 +7,13 @@ class Horde_Core_Binder_Alarm implements Horde_Injector_Binder
 {
     public function create(Horde_Injector $injector)
     {
-        if (empty($GLOBALS['conf']['alarms']['driver'])) {
-            $driver = null;
-            $params = array();
-        } else {
-            $driver = $GLOBALS['conf']['alarms']['driver'];
-            $params = Horde::getDriverConfig('alarms', $driver);
-        }
+        $driver = empty($GLOBALS['conf']['alarms']['driver'])
+            ? 'Null'
+            : $GLOBALS['conf']['alarms']['driver'];
+        $params = Horde::getDriverConfig('alarms', $driver);
 
         if (strcasecmp($driver, 'Sql') === 0) {
-            $write_db = $injector->getInstance('Horde_Db_Pear')->getOb();
-
-            /* Check if we need to set up the read DB connection
-             * separately. */
-            if (empty($params['splitread'])) {
-                $params['db'] = $write_db;
-            } else {
-                $params['write_db'] = $write_db;
-                $params['db'] = $injector->getInstance('Horde_Db_Pear')->getOb('read');
-            }
+            $params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
         }
 
         $params['logger'] = $injector->getInstance('Horde_Log_Logger');
@@ -36,13 +24,18 @@ class Horde_Core_Binder_Alarm implements Horde_Injector_Binder
          * through Horde_Alarms::handlers(). */
         /*
         $handler_params = array(
-            'notification' => $injector->getInstance('Horde_Notification'));
-        $alarm->addHandler('notify', new Horde_Alarm_Handler_Notification($handler_params));
+            'notification' => $injector->getInstance('Horde_Notification')
+        );
+        $alarm->addHandler('notify', new Horde_Alarm_Handler_Notification($handler_params)
+        );
+
         $handler_params = array(
             'notification' => $injector->getInstance('Horde_Notification'),
-            'icon' => (string)Horde_Themes::img('alerts/alarm.png'));
+            'icon' => (string)Horde_Themes::img('alerts/alarm.png')
+        );
         $alarm->addHandler('desktop', new Horde_Alarm_Handler_Desktop($handler_params));
         */
+
         $handler_params = array(
             'identity' => $injector->getInstance('Horde_Prefs_Identity'),
             'mail' => $injector->getInstance('Horde_Mail'),
index b7fbcff..ce31ed1 100644 (file)
@@ -21,16 +21,7 @@ class Horde_Core_Binder_Cache implements Horde_Injector_Binder
         if (strcasecmp($driver, 'Memcache') === 0) {
             $params['memcache'] = $injector->getInstance('Horde_Memcache');
         } elseif (strcasecmp($driver, 'Sql') === 0) {
-            $write_db = $injector->getInstance('Horde_Db_Pear')->getOb();
-
-            /* Check if we need to set up the read DB connection
-             * separately. */
-            if (empty($params['splitread'])) {
-                $params['db'] = $write_db;
-            } else {
-                $params['write_db'] = $write_db;
-                $params['db'] = $injector->getInstance('Horde_Db_Pear')->getOb('read');
-            }
+            $params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
 
             if (!empty($params['use_memorycache'])) {
                 $params['use_memorycache'] = $this->_getCacheInstance($params['use_memorycache'], $injector);
index 8fbeba4..d79ab58 100644 (file)
@@ -7,29 +7,19 @@ class Horde_Core_Binder_Lock implements Horde_Injector_Binder
 {
     public function create(Horde_Injector $injector)
     {
-        if (empty($GLOBALS['conf']['lock']['driver'])) {
+        $driver = empty($GLOBALS['conf']['lock']['driver'])
+            ? 'Null'
+            : $GLOBALS['conf']['lock']['driver'];
+
+        if (strcasecmp($driver, 'None') === 0) {
             $driver = 'Null';
-        } else {
-            $driver = $GLOBALS['conf']['lock']['driver'];
-            if (strcasecmp($driver, 'None') === 0) {
-                $driver = 'Null';
-            }
         }
 
         $params = Horde::getDriverConfig('lock', $driver);
         $params['logger'] = $injector->getInstance('Horde_Log_Logger');
 
         if (strcasecmp($driver, 'Sql') === 0) {
-            $write_db = $injector->getInstance('Horde_Db_Pear')->getOb();
-
-            /* Check if we need to set up the read DB connection
-             * separately. */
-            if (empty($params['splitread'])) {
-                $params['db'] = $write_db;
-            } else {
-                $params['write_db'] = $write_db;
-                $params['db'] = $injector->getInstance('Horde_Db_Pear')->getOb('read');
-            }
+            $params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
         }
 
         return Horde_Lock::factory($driver, $params);
index c5c2747..1a91d73 100644 (file)
@@ -7,13 +7,12 @@ class Horde_Core_Binder_Perms implements Horde_Injector_Binder
 {
     public function create(Horde_Injector $injector)
     {
-        $params = isset($GLOBALS['conf']['perms'])
-            ? Horde::getDriverConfig('perms', $GLOBALS['conf']['perms']['driver'])
-            : array();
-
         $driver = empty($GLOBALS['conf']['perms']['driver'])
             ? (empty($GLOBALS['conf']['datatree']['driver']) ? null : 'Datatree')
             : $GLOBALS['conf']['perms']['driver'];
+        $params = isset($GLOBALS['conf']['perms'])
+            ? Horde::getDriverConfig('perms', $driver)
+            : array();
 
         if (strcasecmp($driver, 'Datatree') === 0) {
             $dt_driver = $GLOBALS['conf']['datatree']['driver'];
@@ -22,22 +21,11 @@ class Horde_Core_Binder_Perms implements Horde_Injector_Binder
                 array_merge(Horde::getDriverConfig('datatree', $dt_driver), array('group' => 'horde.perms'))
             );
         } elseif (strcasecmp($driver, 'Sql') === 0) {
-            $write_db = $injector->getInstance('Horde_Db_Pear')->getOb();
-
-            /* Check if we need to set up the read DB connection
-             * separately. */
-            if (empty($params['splitread'])) {
-                $params['db'] = $write_db;
-            } else {
-                $params['write_db'] = $write_db;
-                $params['db'] = $injector->getInstance('Horde_Db_Pear')->getOb('read');
-            }
+            $params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
         }
 
-        $params = array_merge($params, array(
-            'cache' => $injector->getInstance('Horde_Cache'),
-            'logger' => $injector->getInstance('Horde_Log_Logger')
-        ));
+        $params['cache'] = $injector->getInstance('Horde_Cache');
+        $params['logger'] = $injector->getInstance('Horde_Log_Logger');
 
         return Horde_Perms::factory($driver, $params);
     }
index 340c3c4..9624ac8 100644 (file)
@@ -15,16 +15,7 @@ class Horde_Core_Binder_Token implements Horde_Injector_Binder
             : array();
 
         if (strcasecmp($driver, 'Sql') === 0) {
-            $write_db = $injector->getInstance('Horde_Db_Pear')->getOb();
-
-            /* Check if we need to set up the read DB connection
-             * separately. */
-            if (empty($params['splitread'])) {
-                $params['db'] = $write_db;
-            } else {
-                $params['write_db'] = $write_db;
-                $params['db'] = $injector->getInstance('Horde_Db_Pear')->getOb('read');
-            }
+            $params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
         } elseif (strcasecmp($driver, 'None') === 0) {
             $driver = 'Null';
         }
index 1b8c80f..682eb14 100644 (file)
@@ -134,11 +134,13 @@ abstract class Horde_Db_Adapter_Base
 
         // Create the database-specific (but not adapter specific) schema
         // object.
-        if (!$this->_schemaClass)
-            $this->_schemaClass = get_class($this).'_Schema';
+        if (!$this->_schemaClass) {
+            $this->_schemaClass = __CLASS__ . '_Schema';
+        }
         $this->_schema = new $this->_schemaClass($this, array(
             'cache' => $this->_cache,
-            'logger' => $this->_logger));
+            'logger' => $this->_logger
+        ));
         $this->_schemaMethods = array_flip(get_class_methods($this->_schema));
 
         $this->connect();
@@ -168,11 +170,7 @@ abstract class Horde_Db_Adapter_Base
     public function componentFactory($component, $args)
     {
         $class = str_replace('_Schema', '', $this->_schemaClass) . '_' . $component;
-        if (class_exists($class)) {
-            $class = new ReflectionClass($class);
-        } else {
-            $class = new ReflectionClass('Horde_Db_Adapter_Base_' . $component);
-        }
+        $class = new ReflectionClass(class_exists($class) ? $class : __CLASS__ . '_' . $component);
 
         return $class->newInstanceArgs($args);
     }
@@ -197,7 +195,7 @@ abstract class Horde_Db_Adapter_Base
             return call_user_func_array(array($this->_schema, $method), $args);
         }
 
-        throw new BadMethodCallException('Call to undeclared method "'.$method.'"');
+        throw new BadMethodCallException('Call to undeclared method "' . $method . '"');
     }
 
 
index d567b30..6d88b07 100644 (file)
@@ -89,6 +89,14 @@ class Horde_Db_Adapter_Pdo_Oci extends Horde_Db_Adapter_Pdo_Base
     }
 
     /**
+     * @return  string
+     */
+    public function adapterName()
+    {
+        return 'PDO_Oci';
+    }
+
+    /**
      * Get a description of the database table that $model is going to
      * reflect.
      */
index e240559..20f9ff2 100644 (file)
@@ -33,27 +33,18 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
     /**
      * Handle for the current database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
     private $_db;
 
     /**
-     * Handle for the current database connection, used for writing. Defaults
-     * to the same handle as $_db if a separate write database isn't required.
-     *
-     * @var DB
-     */
-    private $_write_db;
-
-    /**
      * Constructor.
      *
      * @param array $params  Parameters:
      * <pre>
-     * 'db' - (DB) [REQUIRED] The DB instance.
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
      * 'table' - (string) The name of the lock table in 'database'.
      *           DEFAULT: 'horde_locks'
-     * 'write_db' - (DB) The write DB instance.
      * </pre>
      *
      * @throws Horde_Lock_Exception
@@ -64,12 +55,7 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
             throw new Horde_Lock_Exception('Missing db parameter.');
         }
         $this->_db = $params['db'];
-
-        $this->_write_db = isset($params['write_db'])
-            ? $params['write_db']
-            : $this->_db;
-
-        unset($params['db'], $params['write_db']);
+        unset($params['db']);
 
         $params = array_merge(array(
             'table' => 'horde_locks'
@@ -80,7 +66,7 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
         /* Only do garbage collection if asked for, and then only 0.1% of the
          * time we create an object. */
         if (rand(0, 999) == 0) {
-            register_shutdown_function(array($this, '_doGC'));
+            register_shutdown_function(array($this, 'doGC'));
         }
     }
 
@@ -98,23 +84,11 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
                ' WHERE lock_id = ? AND lock_expiry_timestamp >= ?';
         $values = array($lockid, $now);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL Query by Horde_Lock_sql::getLockInfo(): ' . $sql, 'DEBUG');
-        }
-
-        $result = $this->_db->query($sql, $values);
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Lock_Exception($result);
-        }
-
-        $locks = array();
-        $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
-        if ($row instanceof PEAR_Error) {
-            return false;
+        try {
+            return $this->_db->selectOne($sql, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Lock_Exception($e);
         }
-
-        $result->free();
-        return $row;
     }
 
     /**
@@ -146,23 +120,16 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
             $values[] = $type;
         }
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL Query by Horde_Lock_sql::getLocks(): ' . $sql, 'DEBUG');
-        }
-
-        $result = $this->_db->query($sql, $values);
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Lock_Exception($result);
+        try {
+            $result = $this->_db->selectAll($sql, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Lock_Exception($e);
         }
 
         $locks = array();
-        $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
-        while ($row && !($row instanceof PEAR_Error)) {
+        foreach ($result as $val) {
             $locks[$row['lock_id']] = $row;
-            /* Advance to the new row in the result set. */
-            $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
         }
-        $result->free();
 
         return $locks;
     }
@@ -186,17 +153,10 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
                'WHERE lock_id = ?';
         $values = array($now, $expiry, $lockid);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL Query by Horde_Lock_sql::resetLock(): ' . $sql, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($sql, $values);
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Lock_Exception($result);
-        }
-
-        if ($this->_logger) {
-            $this->_logger->log(sprintf('Lock %s reset successfully.', $lockid), 'DEBUG');
+        try {
+            $this->_db->update($sql, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Lock_Exception($e);
         }
 
         return true;
@@ -229,13 +189,10 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
         $values = array($lockid, $requestor, $scope, $principal, $now, $now,
                         $expiration, $type);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL Query by Horde_Lock_sql::setLock(): ' . $sql, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($sql, $values);
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Lock_Exception($result);
+        try {
+            $this->_db->insert($sql, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Lock_Exception($e);
         }
 
         if ($this->_logger) {
@@ -262,13 +219,10 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
         $sql = 'DELETE FROM ' . $this->_params['table'] . ' WHERE lock_id = ?';
         $values = array($lockid);
 
-        if ($this->_logger) {
-            $this->_logger->log('SQL Query by Horde_Lock_sql::clearLock(): ' . $sql, 'DEBUG');
-        }
-
-        $result = $this->_write_db->query($sql, $values);
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Lock_Exception($result);
+        try {
+            $this->_db->delete($sql, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Lock_Exception($e);
         }
 
         if ($this->_logger) {
@@ -281,22 +235,19 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
     /**
      * Do garbage collection needed for the driver.
      */
-    private function _doGC()
+    public function doGC()
     {
         $now = time();
         $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE ' .
                  'lock_expiry_timestamp < ? AND lock_expiry_timestamp != 0';
         $values = array($now);
 
-        $result = $this->_write_db->query($query, $values);
-        if ($this->_logger) {
-            $this->_logger->log('SQL Query by Horde_Lock_sql::_doGC(): ' .  $sql, 'DEBUG');
-            if ($result instanceof PEAR_Error) {
-                $this->_logger->log($result, 'ERR');
-            } else {
-                $this->_logger->log(sprintf('Lock garbage collection cleared %d locks.', $this->_write_db->affectedRows()), 'DEBUG');
+        try {
+            $result = $this->_db->delete($query, $values);
+            if ($this->_logger) {
+                $this->_logger->log(sprintf('Lock garbage collection cleared %d locks.', $result), 'DEBUG');
             }
-        }
+        } catch (Horde_Db_Exception $e) {}
     }
 
 }
index 31b14bd..7d47662 100644 (file)
@@ -52,6 +52,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.7.0</min>
    </pearinstaller>
    <package>
+    <name>Exception</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Support</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -65,6 +69,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
   </required>
   <optional>
    <package>
+    <name>Db</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Log</name>
     <channel>pear.horde.org</channel>
    </package>
index 6334210..76224d5 100644 (file)
@@ -72,9 +72,11 @@ class Horde_Perms_Permission_SqlObject extends Horde_Perms_Permission
         }
         $query = 'UPDATE horde_perms SET perm_data = ? WHERE perm_id = ?';
         $params = array(serialize($this->data), $this->getId());
-        $result = $this->_write_db->query($query, $params);
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Perms_Exception($result);
+
+        try {
+            $this->_write_db->update($query, $params);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
         }
 
         $cache = $GLOBALS['injector']->getInstance('Horde_Cache');
index dd2937b..2da835f 100644 (file)
@@ -24,19 +24,11 @@ class Horde_Perms_Sql extends Horde_Perms
     /**
      * Handle for the current database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
     protected $_db;
 
     /**
-     * Handle for the current database connection, used for writing. Defaults
-     * to the same handle as $db if a separate write database is not required.
-     *
-     * @var DB
-     */
-    protected $_write_db;
-
-    /**
      * Incrementing version number if cached classes change.
      *
      * @var integer
@@ -56,10 +48,9 @@ class Horde_Perms_Sql extends Horde_Perms
      * @param array $params  Configuration parameters (in addition to base
      *                       Horde_Perms parameters):
      * <pre>
-     * 'db' - (DB) [REQUIRED] The DB instance.
-     * 'table' - (string) The name of the perms table in 'database'.
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
+     * 'table' - (string) The name of the perms table.
      *           DEFAULT: 'horde_perms'
-     * 'write_db' - (DB) The write DB instance.
      * </pre>
      *
      * @throws Horde_Perms_Exception
@@ -70,12 +61,7 @@ class Horde_Perms_Sql extends Horde_Perms
             throw new Horde_Perms_Exception('Missing db parameter.');
         }
         $this->_db = $params['db'];
-
-        $this->_write_db = isset($params['write_db'])
-            ? $params['write_db']
-            : $this->_db;
-
-        unset($params['db'], $params['write_db']);
+        unset($params['db']);
 
         $this->_params = array_merge(array(
             'table' => 'horde_perms'
@@ -131,11 +117,14 @@ class Horde_Perms_Sql extends Horde_Perms
         if (empty($perm)) {
             $query = 'SELECT perm_id, perm_data FROM ' .
                 $this->_params['table'] . ' WHERE perm_name = ?';
-            $result = $this->_db->getRow($query, array($name), DB_FETCHMODE_ASSOC);
 
-            if ($result instanceof PEAR_Error) {
-                throw new Horde_Perms_Exception($result);
-            } elseif (empty($result)) {
+            try {
+                $result = $this->_db->selectOne($query, array($name));
+            } catch (Horde_Db_Exception $e) {
+                throw new Horde_Perms_Exception($e);
+            }
+
+            if (empty($result)) {
                 throw new Horde_Perms_Exception('Does not exist');
             }
 
@@ -150,7 +139,7 @@ class Horde_Perms_Sql extends Horde_Perms
             $this->_permsCache[$name] = unserialize($perm);
         }
 
-        $this->_permsCache[$name]->setSQLOb($this->_write_db);
+        $this->_permsCache[$name]->setSQLOb($this->_db);
 
         return $this->_permsCache[$name];
     }
@@ -171,18 +160,21 @@ class Horde_Perms_Sql extends Horde_Perms
         } else {
             $query = 'SELECT perm_name, perm_data FROM ' .
                 $this->_params['table'] . ' WHERE perm_id = ?';
-            $result = $this->_db->getRow($query, array($id), DB_FETCHMODE_ASSOC);
 
-            if ($result instanceof PEAR_Error) {
-                throw new Horde_Perms_Exception($result);
-            } elseif (empty($result)) {
+            try {
+                $result = $this->_db->selectOne($query, array($id));
+            } catch (Horde_Db_Exception $e) {
+                throw new Horde_Perms_Exception($e);
+            }
+
+            if (empty($result)) {
                 throw new Horde_Perms_Exception('Does not exist');
             }
 
             $object = new Horde_Perms_Permission_SqlObject($result['perm_name'], $this->_cacheVersion);
             $object->setId($id);
             $object->setData(unserialize($result['perm_data']));
-            $object->setSQLOb($this->_write_db);
+            $object->setSQLOb($this->_db);
         }
 
         return $object;
@@ -195,7 +187,7 @@ class Horde_Perms_Sql extends Horde_Perms
      *
      * @param Horde_Perms_Permission_SqlObject $perm  The perm object.
      *
-     * @return TODO
+     * @return integer  Permission ID in the database.
      * @throws Horde_Perms_Exception
      */
     public function addPermission(Horde_Perms_Permission_SqlObject $perm)
@@ -208,8 +200,6 @@ class Horde_Perms_Sql extends Horde_Perms
         $this->_cache->expire('perm_sql' . $this->_cacheVersion . $name);
         $this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
 
-        $id = $this->_write_db->nextId($this->_params['table']);
-
         // remove root from the name
         $root = Horde_Perms::ROOT . ':';
         if (substr($name, 0, strlen($root)) == ($root)) {
@@ -229,15 +219,15 @@ class Horde_Perms_Sql extends Horde_Perms
         }
 
         $query = 'INSERT INTO ' . $this->_params['table'] .
-            ' (perm_id, perm_name, perm_parents) VALUES (?, ?, ?)';
-        $perm->setId($id);
+            ' (perm_name, perm_parents) VALUES (?, ?)';
 
-        $result = $this->_write_db->query($query, array($id, $name, $parents));
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Perms_Exception($result);
+        try {
+            $id = $this->_db->insert($query, array($name, $parents));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
         }
 
-        $perm->setSQLOb($this->_write_db);
+        $perm->setId($id);
         $perm->save();
 
         return $id;
@@ -248,13 +238,14 @@ class Horde_Perms_Sql extends Horde_Perms
      *
      * @param Horde_Perms_Permission_SqlObject $perm  The permission to
      *                                                remove.
-     * @param boolean $force                          Force to remove ever
+     * @param boolean $force                          Force to remove every
      *                                                child.
      *
-     * @return TODO
+     * @return boolean  True if permission was deleted.
      * @throws Horde_Perms_Exception
      */
-    public function removePermission(Horde_Perms_Permission_SqlObject $perm, $force = false)
+    public function removePermission(Horde_Perms_Permission_SqlObject $perm,
+                                     $force = false)
     {
         $name = $perm->getName();
         $this->_cache->expire('perm_sql' . $this->_cacheVersion . $name);
@@ -262,25 +253,35 @@ class Horde_Perms_Sql extends Horde_Perms
 
         $query = 'DELETE FROM ' . $this->_params['table'] .
             ' WHERE perm_name = ?';
-        $result = $this->_write_db->query($query, array($name));
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Perms_Exception($result);
-        } elseif ($force) {
-            return $result;
+
+        try {
+            $result = $this->_db->delete($query, array($name));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
+        }
+
+        if ($force) {
+            return (bool)$result;
         }
 
         $query = 'DELETE FROM ' . $this->_params['table'] .
             ' WHERE perm_name LIKE ?';
-        return $this->_write_db->query($query, array($name . ':%'));
+
+        try {
+            return (bool)$this->_db->delete($query, array($name . ':%'));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
+        }
     }
 
     /**
      * Returns the unique identifier of this permission.
      *
      * @param Horde_Perms_Permission_SqlObject $perm  The permission object to
-     *                                                 get the ID of.
+     *                                                get the ID of.
      *
      * @return integer  The unique id.
+     * @throws Horde_Perms_Exception
      */
     public function getPermissionId($permission)
     {
@@ -290,7 +291,12 @@ class Horde_Perms_Sql extends Horde_Perms
 
         $query = 'SELECT perm_id FROM ' . $this->_params['table'] .
             ' WHERE perm_name = ?';
-        return $this->_db->getOne($query, array($permission->getName()));
+
+        try {
+            return $this->_db->selectValue($query, array($permission->getName()));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
+        }
     }
 
     /**
@@ -308,9 +314,11 @@ class Horde_Perms_Sql extends Horde_Perms
         if ($exists === false) {
             $query = 'SELECT COUNT(*) FROM ' . $this->_params['table'] .
                 ' WHERE perm_name = ?';
-            $exists = $this->_db->getOne($query, array($permission));
-            if ($exists instanceof PEAR_Error) {
-                throw new Horde_Perms_Exception($exists);
+
+            try {
+                $exists = $this->_db->selectValue($query, array($permission));
+            } catch (Horde_Db_Exception $e) {
+                throw new Horde_Perms_Exception($e);
             }
 
             $this->_cache->set($key, (string)$exists);
@@ -332,10 +340,11 @@ class Horde_Perms_Sql extends Horde_Perms
     {
         $query = 'SELECT perm_parents FROM ' . $this->_params['table'] .
             ' WHERE perm_name = ?';
-        $parents = $this->_db->getOne($query, array($child));
 
-        if ($parents instanceof PEAR_Error) {
-            throw new Horde_Perms_Exception($parents);
+        try {
+            $parents = $this->_db->selectValue($query, array($child));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
         }
 
         if (empty($parents)) {
@@ -343,6 +352,7 @@ class Horde_Perms_Sql extends Horde_Perms
         }
 
         $parents = explode(':', $parents);
+
         return array_pop($parents);
     }
 
@@ -358,10 +368,14 @@ class Horde_Perms_Sql extends Horde_Perms
     {
         $query = 'SELECT perm_parents FROM ' .  $this->_params['table'] .
             ' WHERE perm_name = ?';
-        $result = $this->_db->getOne($query, array($child));
-        if ($result instanceof PEAR_Error) {
-            throw new Horde_Perms_Exception($result);
-        } elseif (empty($result)) {
+
+        try {
+            $result = $this->_db->selectValue($query, array($child));
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
+        }
+
+        if (empty($result)) {
             throw new Horde_Perms_Exception('Does not exist');
         }
 
@@ -393,12 +407,15 @@ class Horde_Perms_Sql extends Horde_Perms
     {
         $query = 'SELECT perm_id, perm_name FROM ' . $this->_params['table'] .
             ' ORDER BY perm_name ASC';
-        $tree = $this->_db->getAssoc($query);
-        if ($tree instanceof PEAR_Error) {
-            throw new Horde_Perms_Exception($tree);
+
+        try {
+            $tree = $this->_db->selectAssoc($query);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Perms_Exception($e);
         }
 
         $tree[Horde_Perms::ROOT] = Horde_Perms::ROOT;
+
         return $tree;
     }
 
index be35bc1..855e869 100644 (file)
 class Horde_Token_Sql extends Horde_Token_Driver
 {
     /**
-     * Handle for the current database connection.
+     * Handle for the database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
-    protected $_db = '';
-
-    /**
-     * Handle for the current database connection, used for writing. Defaults
-     * to the same handle as $_db if a separate write database is not required.
-     *
-     * @var DB
-     */
-    protected $_write_db;
+    protected $_db;
 
     /**
      * Constructor.
      *
      * @param array $params  Parameters:
      * <pre>
-     * 'db' - (DB) [REQUIRED] The DB instance.
-     * 'table' - (string) The name of the tokens table in 'database'.
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
+     * 'table' - (string) The name of the tokens table.
      *           DEFAULT: 'horde_tokens'
      * 'timeout' - (integer) The period (in seconds) after which an id is
      *             purged.
      *             DEFAULT: 86400 (24 hours)
-     * 'write_db' - (DB) The write DB instance.
      * </pre>
      *
      * @throws Horde_Token_Exception
@@ -61,14 +52,7 @@ class Horde_Token_Sql extends Horde_Token_Driver
             throw new Horde_Token_Exception('Missing db parameter.');
         }
         $this->_db = $params['db'];
-
-        if (isset($params['write_db'])) {
-            $this->_write_db = $params['write_db'];
-        } else {
-            $this->_write_db = $this->_db;
-        }
-
-        unset($params['db'], $params['write_db']);
+        unset($params['db']);
 
         $params = array_merge(array(
             'table' => 'horde_tokens',
@@ -92,12 +76,10 @@ class Horde_Token_Sql extends Horde_Token_Driver
         $values = array(time() - $this->_params['timeout']);
 
         /* Return an error if the update fails. */
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
-            throw new Horde_Token_Exception($result);
+        try {
+            $this->_db->delete($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Token_Exception($e);
         }
     }
 
@@ -117,15 +99,11 @@ class Horde_Token_Sql extends Horde_Token_Driver
 
         $values = array($this->_encodeRemoteAddress(), $tokenID);
 
-        $result = $this->_db->getOne($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
+        try {
+            return $this->_db->selectValue($query, $values);
+        } catch (Horde_Db_Exception $e) {
             return false;
         }
-
-        return !empty($result);
     }
 
     /**
@@ -144,12 +122,10 @@ class Horde_Token_Sql extends Horde_Token_Driver
 
         $values = array($this->_encodeRemoteAddress(), $tokenID, time());
 
-        $result = $this->_write_db->query($query, $values);
-        if ($result instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($result, 'ERR');
-            }
-            throw new Horde_Token_Exception($result);
+        try {
+            $this->_db->insert($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Horde_Token_Exception($e);
         }
     }
 
index a4256ad..0ff03e8 100644 (file)
@@ -70,8 +70,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   </required>
   <optional>
    <package>
-    <name>DB</name>
-    <channel>pear.php.net</channel>
+    <name>Db</name>
+    <channel>pear.horde.org</channel>
    </package>
    <package>
     <name>Log</name>
index 881465c..46bf05f 100644 (file)
@@ -22,16 +22,7 @@ class IMP_Injector_Binder_Sentmail implements Horde_Injector_Binder
         $params = Horde::getDriverConfig('sentmail', $driver);
 
         if (strcasecmp($driver, 'Sql') === 0) {
-            $write_db = $injector->getInstance('Horde_Db_Pear')->getOb();
-
-            /* Check if we need to set up the read DB connection
-             * separately. */
-            if (empty($params['splitread'])) {
-                $params['db'] = $write_db;
-            } else {
-                $params['write_db'] = $write_db;
-                $params['db'] = $injector->getInstance('Horde_Db_Pear')->getOb('read');
-            }
+            $params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
         } elseif (strcasecmp($driver, 'None') === 0) {
             $driver = 'Null';
         }
index 71e1850..3e3c8f0 100644 (file)
@@ -20,27 +20,18 @@ class IMP_Sentmail_Sql extends IMP_Sentmail_Driver
     /**
      * Handle for the current database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
-    protected $_db = '';
-
-    /**
-     * Handle for the current database connection, used for writing. Defaults
-     * to the same handle as $_db if a separate write database is not required.
-     *
-     * @var DB
-     */
-    protected $_write_db;
+    protected $_db;
 
     /**
      * Constructor.
      *
      * @param array $params  Parameters:
      * <pre>
-     * 'db' - (DB) [REQUIRED] The DB instance.
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
      * 'table' - (string) The name of the sentmail table.
      *           DEFAULT: 'imp_sentmail'
-     * 'write_db' - (DB) The write DB instance.
      * </pre>
      *
      * @throws IMP_Exception
@@ -51,12 +42,7 @@ class IMP_Sentmail_Sql extends IMP_Sentmail_Driver
             throw new IMP_Exception('Missing db parameter.');
         }
         $this->_db = $params['db'];
-
-        $this->_write_db = isset($params['write_db'])
-            ? $params['write_db']
-            : $this->_db;
-
-        unset($params['db'], $params['write_db']);
+        unset($params['db']);
 
         $params = array_merge(array(
             'table' => 'imp_sentmail'
@@ -77,26 +63,18 @@ class IMP_Sentmail_Sql extends IMP_Sentmail_Driver
     protected function _log($action, $message_id, $recipient, $success)
     {
         /* Build the SQL query. */
-        $query = sprintf('INSERT INTO %s (sentmail_id, sentmail_who, sentmail_ts, sentmail_messageid, sentmail_action, sentmail_recipient, sentmail_success) VALUES (?, ?, ?, ?, ?, ?, ?)',
-                         $this->_params['table']);
-        $values = array($this->_db->nextId($this->_params['table']),
-                        Horde_Auth::getAuth(),
-                        time(),
-                        $message_id,
-                        $action,
-                        $recipient,
-                        intval($success));
-
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('IMP_Sentmail_Sql::_log(): %s', $query), 'DEBUG');
+        $query = sprintf('INSERT INTO %s (sentmail_who, sentmail_ts, sentmail_messageid, sentmail_action, sentmail_recipient, sentmail_success) VALUES (?, ?, ?, ?, ?, ?)', $this->_params['table']);
+        $values = array(
+            Horde_Auth::getAuth(),
+            time(),
+            $message_id,
+            $action,
+            $recipient,
+            intval($success)
+        );
 
         /* Execute the query. */
-        $result = $this->_write_db->query($query, $values);
-
-        /* Log errors. */
-        if ($result instanceof PEAR_Error) {
-            Horde::logMessage($result, 'ERR');
-        }
+        $this->_db->insert($query, $values);
     }
 
     /**
@@ -119,29 +97,19 @@ class IMP_Sentmail_Sql extends IMP_Sentmail_Driver
             $where = sprintf(' AND sentmail_action in (%s)',
                              implode(', ', $filter));
         }
+
         $query = sprintf('SELECT sentmail_recipient, count(*) AS sentmail_count FROM %s WHERE sentmail_who = %s AND sentmail_success = 1%s GROUP BY sentmail_recipient ORDER BY sentmail_count DESC LIMIT %d',
                          $this->_params['table'],
                          $this->_db->quote(Horde_Auth::getAuth()),
                          $where,
                          $limit);
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('IMP_Sentmail_Sql::favouriteRecipients(): %s', $query), 'DEBUG');
-
         /* Execute the query. */
-        $recipients = $this->_db->getAll($query);
-        if ($recipients instanceof PEAR_Error) {
-            Horde::logMessage($recipients, 'ERR');
-            throw new IMP_Exception($recipients);
+        try {
+            return $this->_db->selectValues($query);
+        } catch (Horde_Db_Exception $e) {
+            throw new IMP_Exception($e);
         }
-
-        /* Extract email addresses. */
-        $favourites = array();
-        foreach ($recipients as $recipient) {
-            $favourites[] = reset($recipient);
-        }
-
-        return $favourites;
     }
 
     /**
@@ -163,17 +131,12 @@ class IMP_Sentmail_Sql extends IMP_Sentmail_Driver
             $query .= sprintf(' AND sentmail_who = %s', $this->_db->quote(Horde_Auth::getAuth()));
         }
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('IMP_Sentmail_Sql::numberOfRecipients(): %s', $query), 'DEBUG');
-
         /* Execute the query. */
-        $recipients = $this->_db->getOne($query, array(time() - $hours * 3600));
-        if ($recipients instanceof PEAR_Error) {
-            Horde::logMessage($recipients, 'ERR');
-            throw new IMP_Exception($recipients);
+        try {
+            return $this->_db->selectValue($query, array(time() - $hours * 3600));
+        } catch (Horde_Db_Exception $e) {
+            throw new IMP_Exception($e);
         }
-
-        return $recipients;
     }
 
     /**
@@ -188,14 +151,10 @@ class IMP_Sentmail_Sql extends IMP_Sentmail_Driver
         $query = sprintf('DELETE FROM %s WHERE sentmail_ts < ?',
                          $this->_params['table']);
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('IMP_Sentmail_Sql::_deleteOldEntries(): %s', $query), 'DEBUG');
-
         /* Execute the query. */
-        $result = $this->_write_db->query($query, array($before));
-        if ($result instanceof PEAR_Error) {
-            Horde::logMessage($result, 'ERR');
-        }
+        try {
+            $this->_db->delete($query, array($before));
+        } catch (Horde_Db_Exception $e) {}
     }
 
 }