PEAR::DB -> Horde_Db for Kronlith_Driver_Sql, Dependency Injection
authorMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 12 Jan 2011 21:23:54 +0000 (16:23 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 13 Jan 2011 01:43:01 +0000 (20:43 -0500)
kronolith/lib/Ajax/Application.php
kronolith/lib/Driver.php
kronolith/lib/Driver/Sql.php
kronolith/lib/Injector/Factory/Driver.php [new file with mode: 0644]
kronolith/lib/Kronolith.php
kronolith/lib/Storage/kolab.php
kronolith/lib/Storage/sql.php

index e5ad1cf..bff402d 100644 (file)
@@ -850,7 +850,7 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
 
         $result = new stdClass;
         try {
-            $driver = Kronolith_Driver::factory('Ical', $params);
+            $driver = $GLOBALS['injector']->getInstance('Kronolith_Injector_Factory_Driver')->create('Ical', $params);
             $driver->open($this->_vars->url);
             $ical = $driver->getRemoteCalendar(false);
             $result->success = true;
index 11e7834..3773e59 100644 (file)
@@ -209,38 +209,6 @@ class Kronolith_Driver
     }
 
     /**
-     * Attempts to return a concrete Kronolith_Driver instance based on
-     * $driver.
-     *
-     * @param string $driver  The type of concrete Kronolith_Driver subclass
-     *                        to return.
-     *
-     * @param array $params   A hash containing any additional configuration or
-     *                        connection parameters a subclass might need.
-     *
-     * @return Kronolith_Driver  The newly created concrete Kronolith_Driver
-     *                           instance.
-     */
-    static public function factory($driver = null, $params = null)
-    {
-        $driver = basename($driver);
-        $class = 'Kronolith_Driver_' . $driver;
-
-        if (class_exists($class)) {
-            $driver = new $class($params);
-            try {
-                $driver->initialize();
-            } catch (Exception $e) {
-                $driver = new Kronolith_Driver($params, sprintf(_("The Calendar backend is not currently available: %s"), $e->getMessage()));
-            }
-        } else {
-            $driver = new Kronolith_Driver($params, sprintf(_("Unable to load the definition of %s."), $class));
-        }
-
-        return $driver;
-    }
-
-    /**
      * Stub to initiate a driver.
      *
      * @throws Kronolith_Exception
index 4a54b38..8cdf147 100644 (file)
@@ -18,19 +18,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
     /**
      * The object handle for the current database connection.
      *
-     * @var DB
+     * @var Horde_Db_Adapter
      */
     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;
-
-    /**
      * Cache events as we fetch them to avoid fetching the same event from the
      * DB twice.
      *
@@ -211,18 +203,13 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             $values[] = $calendar_id;
         }
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::exists(): user = "%s"; query = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query), 'DEBUG');
-
-        $event = $this->_db->getRow($query, $values, DB_FETCHMODE_ASSOC);
-        $this->handleError($event);
-
-        if ($event) {
-            return $event['event_id'];
+        try {
+            $event = $this->_db->selectValue($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
         }
 
-        return false;
+        return !$empty ? $event : false;
     }
 
     /**
@@ -344,17 +331,15 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             $values[] = $endInterval->format('Y-m-d H:i:s');
         }
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::_listEventsConditional(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $q, implode(',', $values)), 'DEBUG');
-
         /* Run the query. */
-        $qr = $this->_db->query($q, $values);
-        $this->handleError($qr);
+        try {
+            $qr = $this->_db->selectAll($q, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
 
         $events = array();
-        $row = $qr->fetchRow(DB_FETCHMODE_ASSOC);
-        while ($row && !($row instanceof PEAR_Error)) {
+        foreach ($qr as $row) {
             /* If the event did not have a UID before, we need to give
              * it one. */
             if (empty($row['event_uid'])) {
@@ -363,14 +348,10 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
                 /* Save the new UID for data integrity. */
                 $query = 'UPDATE ' . $this->_params['table'] . ' SET event_uid = ? WHERE event_id = ?';
                 $values = array($row['event_uid'], $row['event_id']);
-
-                /* Log the query at a DEBUG log level. */
-                Horde::logMessage(sprintf('Kronolith_Driver_Sql::_listEventsConditional(): user = %s; query = "%s"; values = "%s"',
-                                          $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-                $result = $this->_write_db->query($query, $values);
-                if ($result instanceof PEAR_Error) {
-                    Horde::logMessage($result, 'ERR');
+                try {
+                    $this->_db->update($query, $values);
+                } catch (Horde_Db_Exception $e) {
+                    throw new Kronolith_Exception($e);
                 }
             }
 
@@ -387,8 +368,6 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
                     $events[$row['event_uid']] = $row['event_id'];
                 }
             }
-
-            $row = $qr->fetchRow(DB_FETCHMODE_ASSOC);
         }
 
         return $events;
@@ -404,13 +383,14 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
     {
         $query = sprintf('SELECT count(*) FROM %s WHERE calendar_id = ?',
                          $this->_params['table']);
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::_countEvents(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query, $this->calendar), 'DEBUG');
 
         /* Run the query. */
-        $result = $this->_db->getOne($query, array($this->calendar));
-        $this->handleError($result);
+        try {
+            $result = $this->_db->selectOne($query, array($this->calendar));
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
+
         return $result;
     }
 
@@ -437,15 +417,14 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             ' event_exceptions, event_creator_id, event_resources,' .
             ' event_baseid, event_exceptionoriginaldate FROM ' .
             $this->_params['table'] . ' WHERE event_id = ? AND calendar_id = ?';
-        $values = array($eventId, $this->calendar);
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::getEvent(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-        $event = $this->_db->getRow($query, $values, DB_FETCHMODE_ASSOC);
-        $this->handleError($event);
+        $values = array($eventId, $this->calendar);
 
+        try {
+            $event = $this->_db->selectOne($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
         if ($event) {
             $this->_cache[$this->calendar][$eventId] = new $this->_eventClass($this, $event);
             return $this->_cache[$this->calendar][$eventId];
@@ -488,12 +467,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             $values = array_merge($values, $calendars);
         }
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::getByUID(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-        $events = $this->_db->getAll($query, $values, DB_FETCHMODE_ASSOC);
-        $this->handleError($events);
+        try {
+            $events = $this->_db->selectAll($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
         if (!count($events)) {
             throw new Horde_Exception_NotFound($uid . ' not found');
         }
@@ -558,12 +536,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $query .= ' WHERE event_id = ?';
         $values[] = $event->id;
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::saveEvent(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-        $result = $this->_write_db->query($query, $values);
-        $this->handleError($result);
+        try {
+            $result = $this->_db->update($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
 
         /* Log the modification of this item in the history log. */
         if ($event->uid) {
@@ -629,13 +606,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $values[] = $this->calendar;
         $query .= $cols_name . $cols_values;
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::saveEvent(): user = "%s"; query = "%s"; values = "%s"',
-                            $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-        $result = $this->_write_db->query($query, $values);
-        $this->handleError($result);
-
+        try {
+            $result = $this->_db->insert($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
         /* Log the creation of this item in the history log. */
         try {
             $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $event->uid, array('action' => 'add'), true);
@@ -724,13 +699,12 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $query = 'UPDATE ' . $this->_params['table'] . ' SET calendar_id = ? WHERE calendar_id = ? AND event_id = ?';
         $values = array($newCalendar, $this->calendar, $eventId);
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::move(): %s; values = "%s"',
-                                  $query, implode(',', $values)), 'DEBUG');
-
         /* Attempt the move query. */
-        $result = $this->_write_db->query($query, $values);
-        $this->handleError($result);
+        try {
+            $result = $this->_db->update($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
 
         return $event;
     }
@@ -747,12 +721,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE calendar_id = ?';
         $values = array($calendar);
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::delete(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-        $result = $this->_write_db->query($query, $values);
-        $this->handleError($result);
+        try {
+            $result = $this->_db->delete($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
     }
 
     /**
@@ -774,15 +747,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $isRecurring = $event->recurs();
 
         $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE event_id = ? AND calendar_id = ?';
-        $values = array($eventId, $this->calendar);
-
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::deleteEvent(): user = "%s"; query = "%s"; values = "%s"',
-                                  $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-        $result = $this->_write_db->query($query, $values);
-        $this->handleError($result);
-
+        try {
+            $result = $this->_db->delete($query, array($eventId, $this->calendar));
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
         /* Log the deletion of this item in the history log. */
         if ($event->uid) {
             try {
@@ -793,8 +762,6 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         }
 
         /* Remove the event from any resources that are attached to it */
-        //@TODO: Not sure this belongs _here_, but not sure about having to
-        //       call this _everywhere_ we delete an event?
         $resources = $event->getResources();
         if (count($resources)) {
             $rd = Kronolith::getDriver('Resource');
@@ -841,12 +808,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             $query = 'SELECT event_id FROM ' . $this->_params['table'] . ' WHERE event_baseid = ? AND calendar_id = ?';
             $values = array($original_uid, $this->calendar);
 
-            /* Log the query at a DEBUG log level. */
-            Horde::logMessage(sprintf('Kronolith_Driver_Sql::deleteEvent(): user = "%s"; query = "%s"; values = "%s"',
-                                      $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
-
-            $result = $this->_db->getCol($query, 0, $values);
-            $this->handleError($result);
+            try {
+                $result = $this->_db->selectValues($query, $values);
+            } catch (Horde_Db_Exception $e) {
+                throw new Kronolith_Exception($e);
+            }
             foreach ($result as $id) {
                 $this->deleteEvent($id, $silent);
             }
@@ -868,11 +834,11 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $sql = 'SELECT event_uid FROM kronolith_events WHERE calendar_id IN (' . str_repeat('?, ', count($calendar) - 1) . '?) '
             . 'AND event_uid IN (' . str_repeat('?,', count($uids) - 1) . '?)';
 
-        /* Log the query at a DEBUG log level. */
-        Horde::logMessage(sprintf('Kronolith_Driver_Sql::filterEventsByCalendar(): %s', $sql), 'DEBUG');
-
-        $result = $this->_db->getCol($sql, 0, array_merge($calendar, $uids));
-        $this->handleError($result);
+        try {
+            $result = $this->_db->selectValues($sql, array_merge($calendar, $uids));
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
 
         return $result;
     }
@@ -884,36 +850,32 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
      */
     public function initialize()
     {
+        if (empty($this->_params['db'])) {
+            throw new InvalidArgumentException('Missing required Horde_Db_Adapter instance');
+        }
         try {
-            $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'kronolith', 'calendar');
-            $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'kronolith', 'calendar');
+            $this->_db = $this->_params['db'];
         } catch (Horde_Exception $e) {
             throw new Kronolith_Exception($e);
         }
 
-        foreach (array($this->_db, $this->_write_db) as $db) {
-            /* Handle any database specific initialization code to run. */
-            switch ($db->dbsyntax) {
+        /* Handle any database specific initialization code to run. */
+        try {
+            switch ($this->_db->dbsyntax) {
             case 'oci8':
                 $query = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
-
-                /* Log the query at a DEBUG log level. */
-                Horde::logMessage(sprintf('Kronolith_Driver_Sql::_initConn(): user = "%s"; query = "%s"', $GLOBALS['registry']->getAuth(), $query), 'DEBUG');
-
-                $db->query($query);
+                $this->_db->execute($query);
                 break;
 
             case 'pgsql':
                 $query = "SET datestyle TO 'iso'";
-
-                /* Log the query at a DEBUG log level. */
-                Horde::logMessage(sprintf('Kronolith_Driver_Sql::_initConn(): user = "%s"; query = "%s"', $GLOBALS['registry']->getAuth(), $query), 'DEBUG');
-
-                $db->query($query);
+                $this->_db->execute($query);
                 break;
             }
-        }
 
+        } catch (Horde_Db_Exception $e) {
+            throw new Kronolith_Exception($e);
+        }
         $this->_params = array_merge(array(
             'table' => 'kronolith_events'
         ), $this->_params);
diff --git a/kronolith/lib/Injector/Factory/Driver.php b/kronolith/lib/Injector/Factory/Driver.php
new file mode 100644 (file)
index 0000000..72e5a63
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Horde_Injector based factory for Kronolith_Driver
+ */
+class Kronolith_Injector_Factory_Driver
+{
+    /**
+     * Instances.
+     *
+     * @var array
+     */
+    private $_instances = array();
+
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_injector = $injector;
+    }
+
+    /**
+     * Return the driver instance.
+     *
+     * @param string $driver  The storage backend to use
+     * @param array $params   Driver params
+     *
+     * @return Kronolith_Driver
+     * @throws Kronolith_Exception
+     */
+    public function create($driver, $params = array())
+    {
+        $key = $driver . md5(serialize($params));
+        if (!empty($this->_instances[$key])) {
+            return $this->_instances[$key];
+        }
+
+        $driver = basename($driver);
+        $class = 'Kronolith_Driver_' . $driver;
+        if (class_exists($class)) {
+            $driver = new $class($params);
+            try {
+                $driver->initialize();
+            } catch (Exception $e) {
+                $driver = new Kronolith_Driver($params, sprintf(_("The Calendar backend is not currently available: %s"), $e->getMessage()));
+            }
+        } else {
+            $driver = new Kronolith_Driver($params, sprintf(_("Unable to load the definition of %s."), $class));
+        }
+        $this->_instances[$key] = $driver;
+
+        return $driver;
+    }
+}
\ No newline at end of file
index 303fa2d..173b585 100644 (file)
@@ -2721,6 +2721,12 @@ class Kronolith
             case 'Sql':
             case 'Resource':
                 $params = Horde::getDriverConfig('calendar', 'sql');
+                if ($params['driverconfig'] != 'Horde') {
+                    // Custom
+                    $params['db'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Db')->create('kronolith', $params);
+                } else {
+                    $params['db'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbBase')->create();
+                }
                 break;
 
             case 'Kolab':
@@ -2747,7 +2753,7 @@ class Kronolith
                 break;
             }
 
-            self::$_instances[$driver] = Kronolith_Driver::factory($driver, $params);
+            self::$_instances[$driver] = $GLOBALS['injector']->getInstance('Kronolith_Injector_Factory_Driver')->create($driver, $params);
         }
 
         if (!is_null($calendar)) {
index 1c35c9a..bba2be4 100644 (file)
@@ -9,7 +9,7 @@
  * @author  Stuart Binge <omicron@mighty.co.za>
  * @package Kronolith
  */
-class Kronolith_Storage_kolab extends Kronolith_Storage
+class Kronolith_Storage_Kolab extends Kronolith_Storage
 {
     protected $_params = array();
 
index 29d6e5f..48e4340 100644 (file)
@@ -6,7 +6,7 @@
  * @author  Michael J Rubinsky <mrubinsk@horde.org>
  * @package Kronolith
  */
-class Kronolith_Storage_sql extends Kronolith_Storage
+class Kronolith_Storage_Sql extends Kronolith_Storage
 {
     /**
      * Handle for the current database connection, used for reading.