From: Michael M Slusarz Date: Wed, 19 May 2010 18:02:46 +0000 (-0600) Subject: Clean up DB examples in Skeleton X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=4b3dc162393c95a58c60d69a76632f77a98baf25;p=horde.git Clean up DB examples in Skeleton --- diff --git a/skeleton/docs/INSTALL b/skeleton/docs/INSTALL index c2d9979cc..25f407608 100644 --- a/skeleton/docs/INSTALL +++ b/skeleton/docs/INSTALL @@ -139,10 +139,8 @@ Configuring Skeleton mailing list. You will also need to make sure that the "horde" user in your database has - table-creation privileges, so that the tables that `PEAR DB`_ uses to - provide portable sequences can be created. - - .. _`PEAR DB`: http://pear.php.net/DB + table-creation privileges, so that the tables used to provide portable + sequences can be created. 3. Configuring Skeleton diff --git a/skeleton/lib/Driver/Sql.php b/skeleton/lib/Driver/Sql.php index 6ac879138..5374ffb88 100644 --- a/skeleton/lib/Driver/Sql.php +++ b/skeleton/lib/Driver/Sql.php @@ -2,25 +2,6 @@ /** * Skeleton storage 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'.
- * 'charset' - The database's internal charset.
- * 
- * - * 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/skeleton_foo.sql * script. * @@ -29,8 +10,9 @@ * 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 Your Name - * @package Skeleton + * @author Your Name + * @category Horde + * @package Skeleton */ class Skeleton_Driver_Sql extends Skeleton_Driver { @@ -49,148 +31,57 @@ class Skeleton_Driver_Sql extends Skeleton_Driver 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; - - /** - * Boolean indicating whether or not we're connected to the SQL server. + * Storage variable. * - * @var boolean + * @var array */ - protected $_connected = false; + protected $_foo = array(); /** * Constructs a new SQL storage object. * - * @param array $params A hash containing connection parameters. + * @param array $params Parameters: + *
+     * 'db' - (Horde_Db_Adapter_Base) [REQUIRED] The DB instance.
+     * 'table' - (string) The name of the SQL table.
+     *           DEFAULT: 'skeleton_foo'
+     * 
+ * + * @throws InvalidArgumentException */ - public function __construct($params = array()) + public function __construct(array $params = array()) { - $this->_params = $params; + if (!isset($params['db'])) { + throw new InvalidArgumentException('Missing db parameter.'); + } + $this->_db = $params['db']; + unset($params['db']); + + $this->_params = array_merge($this->_params, array( + 'table' => 'skeleton_foo' + ), $params); } /** * Retrieves the foos from the database. + * + * @throws Horde_Skeleton_Exception */ public function retrieve() { - /* Make sure we have a valid database connection. */ - $this->_connect(); - /* Build the SQL query. */ $query = 'SELECT * FROM ' . $this->_params['table'] . ' WHERE foo = ?'; $values = array($this->_params['bar']); - /* Log the query at a DEBUG log level. */ - Horde::logMessage(sprintf('Skeleton_Driver_Sql::retrieve(): %s', $query), 'DEBUG'); - /* Execute the query. */ - $result = $this->_db->query($query, $values); - - if ($result instanceof PEAR_Error) { - throw new Horde_Exception_Prior($result); - } - - $row = $result->fetchRow(DB_FETCHMODE_ASSOC); - if ($row instanceof PEAR_Error) { - throw new Horde_Exception_Prior($row); + try { + $rows = $this->_db->selectAll($query, $values); + } catch (Horde_Db_Exception $e) { + throw new Horde_Skeleton_Exception($e); } /* Store the retrieved values in the foo variable. */ - $this->_foo = array(); - while ($row && !($row instanceof PEAR_Error)) { - /* Add this new foo to the $_foo list. */ - $this->_foo[] = $row; - - /* Advance to the new row in the result set. */ - $row = $result->fetchRow(DB_FETCHMODE_ASSOC); - } - - $result->free(); - } - - /** - * Attempts to open a persistent connection to the SQL server. - * - * @throws Horde_Exception - */ - protected function _connect() - { - if ($this->_connected) { - return; - } - - Horde::assertDriverConfig($this->_params, 'storage', - array('phptype', 'charset', 'table')); - - if (!isset($this->_params['database'])) { - $this->_params['database'] = ''; - } - if (!isset($this->_params['username'])) { - $this->_params['username'] = ''; - } - if (!isset($this->_params['hostspec'])) { - $this->_params['hostspec'] = ''; - } - - /* Connect to the SQL server using the supplied parameters. */ - $this->_write_db = DB::connect($this->_params, - array('persistent' => !empty($this->_params['persistent']))); - if ($this->_write_db instanceof PEAR_Error) { - throw new Horde_Exception_Prior($this->_write_db); - } - - // Set DB portability options. - switch ($this->_write_db->phptype) { - case 'mssql': - $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM); - break; - - default: - $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); - } - - /* Check if we need to set up the read DB connection seperately. */ - if (!empty($this->_params['splitread'])) { - $params = array_merge($this->_params, $this->_params['read']); - $this->_db = DB::connect($params, - array('persistent' => !empty($params['persistent']))); - if ($this->_db instanceof PEAR_Error) { - throw new Horde_Exception_Prior($this->_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; - - default: - $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); - } - - } else { - /* Default to the same DB handle for the writer too. */ - $this->_db = $this->_write_db; - } - - $this->_connected = true; - } - - /** - * Disconnects from the SQL server and cleans up the connection. - */ - protected function _disconnect() - { - if ($this->_connected) { - $this->_connected = false; - $this->_db->disconnect(); - $this->_write_db->disconnect(); - } + $this->_foo = array_merge($this->_foo, $rows); } }