From 732068ca9387eb5c741dc03c3f969a1c606e618e Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Wed, 12 Jan 2011 16:23:54 -0500 Subject: [PATCH] PEAR::DB -> Horde_Db for Kronlith_Driver_Sql, Dependency Injection --- kronolith/lib/Ajax/Application.php | 2 +- kronolith/lib/Driver.php | 32 ----- kronolith/lib/Driver/Sql.php | 198 ++++++++++++------------------ kronolith/lib/Injector/Factory/Driver.php | 64 ++++++++++ kronolith/lib/Kronolith.php | 8 +- kronolith/lib/Storage/kolab.php | 2 +- kronolith/lib/Storage/sql.php | 2 +- 7 files changed, 154 insertions(+), 154 deletions(-) create mode 100644 kronolith/lib/Injector/Factory/Driver.php diff --git a/kronolith/lib/Ajax/Application.php b/kronolith/lib/Ajax/Application.php index e5ad1cf51..bff402dc7 100644 --- a/kronolith/lib/Ajax/Application.php +++ b/kronolith/lib/Ajax/Application.php @@ -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; diff --git a/kronolith/lib/Driver.php b/kronolith/lib/Driver.php index 11e7834c3..3773e5917 100644 --- a/kronolith/lib/Driver.php +++ b/kronolith/lib/Driver.php @@ -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 diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index 4a54b38ba..8cdf14775 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -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 index 000000000..72e5a63e9 --- /dev/null +++ b/kronolith/lib/Injector/Factory/Driver.php @@ -0,0 +1,64 @@ +_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 diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 303fa2daf..173b585b7 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -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)) { diff --git a/kronolith/lib/Storage/kolab.php b/kronolith/lib/Storage/kolab.php index 1c35c9a34..bba2be442 100644 --- a/kronolith/lib/Storage/kolab.php +++ b/kronolith/lib/Storage/kolab.php @@ -9,7 +9,7 @@ * @author Stuart Binge * @package Kronolith */ -class Kronolith_Storage_kolab extends Kronolith_Storage +class Kronolith_Storage_Kolab extends Kronolith_Storage { protected $_params = array(); diff --git a/kronolith/lib/Storage/sql.php b/kronolith/lib/Storage/sql.php index 29d6e5f64..48e4340d7 100644 --- a/kronolith/lib/Storage/sql.php +++ b/kronolith/lib/Storage/sql.php @@ -6,7 +6,7 @@ * @author Michael J Rubinsky * @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. -- 2.11.0