From f92994a77d09b7d2badebcf5d55404a74820b7ca Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 14 May 2010 13:01:37 -0600 Subject: [PATCH] Sentmail SQL driver now supports split read/write operation --- imp/config/conf.xml | 4 +- imp/docs/CHANGES | 1 + imp/lib/Injector/Binder/Sentmail.php | 23 +++++++-- imp/lib/Sentmail.php | 27 +++++----- imp/lib/Sentmail/Sql.php | 96 +++++++++++++++--------------------- 5 files changed, 74 insertions(+), 77 deletions(-) diff --git a/imp/config/conf.xml b/imp/config/conf.xml index b58fbb7e8..52a5f3ede 100644 --- a/imp/config/conf.xml +++ b/imp/config/conf.xml @@ -203,8 +203,8 @@ be used to configure message limits or create favourite recipients lists. Enable this feature only if it doesn't violate privacy rules or laws in your country. What storage driver should we use?">sql - - + + 60 diff --git a/imp/docs/CHANGES b/imp/docs/CHANGES index 64e6a237d..8e395c930 100644 --- a/imp/docs/CHANGES +++ b/imp/docs/CHANGES @@ -2,6 +2,7 @@ v5.0-git -------- +[mms] Sentmail SQL driver now supports split read/write operation. [mms] Pass compose message data to mailer backend via stream; much more efficient, especially with larger messages (Request #8909). [mms] Add strip attachments support in DIMP. diff --git a/imp/lib/Injector/Binder/Sentmail.php b/imp/lib/Injector/Binder/Sentmail.php index 5f4cceaad..881465c16 100644 --- a/imp/lib/Injector/Binder/Sentmail.php +++ b/imp/lib/Injector/Binder/Sentmail.php @@ -16,12 +16,27 @@ class IMP_Injector_Binder_Sentmail implements Horde_Injector_Binder */ public function create(Horde_Injector $injector) { - if (empty($GLOBALS['conf']['sentmail']['driver'])) { - return IMP_Sentmail::factory(); + $driver = empty($GLOBALS['conf']['sentmail']['driver']) + ? 'Null' + : $GLOBALS['conf']['sentmail']['driver']; + $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'); + } + } elseif (strcasecmp($driver, 'None') === 0) { + $driver = 'Null'; } - $driver = $GLOBALS['conf']['sentmail']['driver']; - return IMP_Sentmail::factory($driver, Horde::getDriverConfig('sentmail', $driver)); + return IMP_Sentmail::factory($driver, $params); } /** diff --git a/imp/lib/Sentmail.php b/imp/lib/Sentmail.php index 366a511eb..7cae7d8d3 100644 --- a/imp/lib/Sentmail.php +++ b/imp/lib/Sentmail.php @@ -21,29 +21,26 @@ class IMP_Sentmail protected $_params = array(); /** - * Attempts to return a concrete IMP_Sentmail instance based on $driver. + * Attempts to return a concrete instance based on $driver. * - * @param string $driver The type of the concrete IMP_Sentmail subclass - * to return. The class name is based on the - * storage driver ($driver). The code is - * dynamically included. + * @param string $driver The type of the concrete subclass to return. + * The class name is based on the storage driver + * ($driver). * @param array $params A hash containing any additional configuration * or connection parameters a subclass might need. * - * @return IMP_Sentmail The newly created concrete IMP_Sentmail instance. + * @return IMP_Sentmail The newly created concrete instance. + * @throws IMP_Exception */ - static public function factory($driver = null, $params = array()) + static public function factory($driver, $params = array()) { - if ($driver && ($driver != 'none')) { - $class = 'IMP_Sentmail_' . ucfirst(basename($driver)); - if (class_exists($class)) { - try { - return new $class($params); - } catch (IMP_Exception $e) {} - } + $class = __CLASS__ . '_' . ucfirst(basename($driver)); + + if (class_exists($class)) { + return new $class($params); } - return new IMP_Sentmail($params); + throw new IMP_Exception('Driver not found: ' . $driver); } /** diff --git a/imp/lib/Sentmail/Sql.php b/imp/lib/Sentmail/Sql.php index c223618fa..fdca735a5 100644 --- a/imp/lib/Sentmail/Sql.php +++ b/imp/lib/Sentmail/Sql.php @@ -2,29 +2,18 @@ /** * IMP_Sentmail implementation for PHP's PEAR database abstraction layer. * - * Required values for $params: - *
- * 'phptype'       The database type (e.g. 'pgsql', 'mysql', etc.).
- * 'table'         The name of the foo table in 'database'.
- * 
- * - * Required by some database implementations: - *
- * 'database'      The name of the database.
- * 'hostspec'      The hostname of the database server.
- * 'protocol'      The communication protocol ('tcp', 'unix', etc.).
- * 'username'      The username with which to connect to the database.
- * 'password'      The password associated with 'username'.
- * 'options'       Additional options to pass to the database.
- * 'tty'           The TTY on which to connect to the database.
- * 'port'          The port on which to connect to the database.
- * 
- * * The table structure can be created by the scripts/sql/imp_sentmail.sql * script. * - * @author Jan Schneider - * @package IMP + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (GPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. + * + * @author Jan Schneider + * @author Michael Slusarz + * @category Horde + * @package IMP */ class IMP_Sentmail_Sql extends IMP_Sentmail { @@ -33,52 +22,47 @@ class IMP_Sentmail_Sql extends IMP_Sentmail * * @var DB */ - protected $_db; + 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 A hash containing connection parameters. + * @param array $params Parameters: + *
+     * 'db' - (DB) [REQUIRED] The DB instance.
+     * 'table' - (string) The name of the sentmail table.
+     *           DEFAULT: 'imp_sentmail'
+     * 'write_db' - (DB) The write DB instance.
+     * 
* * @throws IMP_Exception */ - protected function __construct($params = array()) + public function __construct(array $params = array()) { - parent::__construct($params); - - try { - Horde::assertDriverConfig($this->_params, 'storage', array('phptype', 'table')); - } catch (Horde_Exception $e) { - throw new IMP_Exception($e); + if (!isset($params['db'])) { + throw new IMP_Exception('Missing db parameter.'); } + $this->_db = $params['db']; - if (!isset($this->_params['database'])) { - $this->_params['database'] = ''; - } - if (!isset($this->_params['username'])) { - $this->_params['username'] = ''; - } - if (!isset($this->_params['hostspec'])) { - $this->_params['hostspec'] = ''; - } + $this->_write_db = isset($params['write_db']) + ? $params['write_db'] + : $this->_db; - /* Connect to the SQL server using the supplied parameters. */ - $this->_db = DB::connect($this->_params, - array('persistent' => !empty($this->_params['persistent']), - 'ssl' => !empty($this->_params['ssl']))); - if ($this->_db instanceof PEAR_Error) { - throw new IMP_Exception($this->_db); - } + unset($params['db'], $params['write_db']); - /* Set DB portability options. */ - switch ($this->_db->phptype) { - case 'mssql': - $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM); - break; + $params = array_merge(array( + 'table' => 'imp_sentmail' + ), $params); - default: - $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); - } + parent::__construct($params); } /** @@ -101,13 +85,13 @@ class IMP_Sentmail_Sql extends IMP_Sentmail $message_id, $action, $recipient, - (int)$success); + intval($success)); /* Log the query at a DEBUG log level. */ Horde::logMessage(sprintf('IMP_Sentmail_Sql::_log(): %s', $query), 'DEBUG'); /* Execute the query. */ - $result = $this->_db->query($query, $values); + $result = $this->_write_db->query($query, $values); /* Log errors. */ if ($result instanceof PEAR_Error) { @@ -208,7 +192,7 @@ class IMP_Sentmail_Sql extends IMP_Sentmail Horde::logMessage(sprintf('IMP_Sentmail_Sql::_deleteOldEntries(): %s', $query), 'DEBUG'); /* Execute the query. */ - $result = $this->_db->query($query, array($before)); + $result = $this->_write_db->query($query, array($before)); if ($result instanceof PEAR_Error) { Horde::logMessage($result, 'ERR'); } -- 2.11.0