cvs: pear /DBA DBA.php DBA_Relational.php DBA_Simple.php DBA_Table.php
| From: | Brent Cook | Date: | Thu, 18 Jul 2002 19:10:39 +0000 |
| Subject: | cvs: pear /DBA DBA.php DBA_Relational.php DBA_Simple.php DBA_Table.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5179@lists.php.net to get a copy of this message | ||
busterb Thu Jul 18 15:10:39 2002 EDT
Modified files:
/pear/DBA DBA.php DBA_Relational.php DBA_Simple.php DBA_Table.php
Log:
modified various functions to operate more statically.
this frees functions like createTable from requiring a host object by making
them independent.
Index: pear/DBA/DBA.php diff -u pear/DBA/DBA.php:1.13 pear/DBA/DBA.php:1.14 --- pear/DBA/DBA.php:1.13 Wed Jul 17 12:57:20 2002 +++ pear/DBA/DBA.php Thu Jul 18 15:10:38 2002 @@ -18,9 +18,11 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA.php,v 1.13 2002/07/17 16:57:20 busterb Exp $ +// $Id: DBA.php,v 1.14 2002/07/18 19:10:38 busterb Exp $ // +require_once('PEAR.php'); + /** * DBA is a set of classes for handling and extending Berkeley DB style * databases. It works around some of the quirks in the built-in dba @@ -35,20 +37,34 @@ class DBA { /** - * Creates a new DBA object - * - * @static - * @param string $driver type of storage object to return - * @return object DBA storage object, returned by reference - */ + * Creates a new DBA object + * + * @static + * @param string $driver type of storage object to return + * @return object DBA storage object, returned by reference + */ function &create($driver = 'simple') { + echo "$driver\n"; if (!function_exists('dba_open') || ($driver=='simple')) { require_once 'DB/DBA/DBA_Simple.php'; return new DBA_Simple(); - } else { + } elseif (($driver == 'db3') || ($driver == 'gdbm')){ require_once 'DB/DBA/DBA_Builtin.php'; return new DBA_Builtin($driver); + } else { + return PEAR::raiseError('Unknown DBA driver, '.$driver); + } + } + + function exists($name, $driver = 'simple') + { + if (!function_exists('dba_open') || ($driver=='simple')) { + return (file_exists($name.'.idx') && file_exists($name.'.dat')); + } elseif (($driver == 'db3') || ($driver == 'gdbm')){ + return file_exists($name); + } else { + return PEAR::raiseError('Unknown DBA driver, '.$driver); } } } Index: pear/DBA/DBA_Relational.php diff -u pear/DBA/DBA_Relational.php:1.39 pear/DBA/DBA_Relational.php:1.40 --- pear/DBA/DBA_Relational.php:1.39 Wed Jul 17 12:57:20 2002 +++ pear/DBA/DBA_Relational.php Thu Jul 18 15:10:38 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Relational.php,v 1.39 2002/07/17 16:57:20 busterb Exp $ +// $Id: DBA_Relational.php,v 1.40 2002/07/18 19:10:38 busterb Exp $ require_once 'PEAR.php'; require_once 'DB/DBA/DBA_Table.php'; @@ -45,13 +45,6 @@ var $_tables=array(); /** - * Handle to manager table; used for handling multi-table operations - * @access private - * @var object - */ - var $_manager; - - /** * Location of table data files * @access private * @var string @@ -86,8 +79,10 @@ $this->_driver = $driver; - // initialize the manager table - $this->_manager =& new DBA_Table($this->_driver); + if (!$this->tableExists('_tables')) { + $this->createTable('_tables', + array('name'=>array(DBA_TYPE=>DBA_VARCHAR, DBA_SIZE=>30))); + } } // }}} @@ -170,7 +165,7 @@ */ function tableExists($tableName) { - return $this->_manager->tableExists($this->_home.$tableName); + return DBA::exists($this->_home.$tableName, $this->_driver); } // }}} @@ -185,7 +180,8 @@ */ function createTable($tableName, $schema) { - return $this->_manager->create($this->_home.$tableName, $schema); + return DBA_Table::create($this->_home.$tableName, $schema, + $this->_driver); } // }}} @@ -343,7 +339,7 @@ */ function sort($fields, $order='a', $rows) { - return $this->_manager->sort($fields, $order, $rows); + return DBA_Table::sort($fields, $order, $rows); } // }}} @@ -360,7 +356,7 @@ */ function project($fields, $rows) { - return $this->_manager->project($fields, $rows); + return DBA_Table::project($fields, $rows); } // }}} @@ -375,7 +371,7 @@ */ function unique($rows) { - return $this->_manager->unique($rows); + return DBA_Table::unique($rows); } // }}} Index: pear/DBA/DBA_Simple.php diff -u pear/DBA/DBA_Simple.php:1.26 pear/DBA/DBA_Simple.php:1.27 --- pear/DBA/DBA_Simple.php:1.26 Wed Jul 17 12:57:20 2002 +++ pear/DBA/DBA_Simple.php Thu Jul 18 15:10:38 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Simple.php,v 1.26 2002/07/17 16:57:20 busterb Exp $ +// $Id: DBA_Simple.php,v 1.27 2002/07/18 19:10:38 busterb Exp $ require_once 'PEAR.php'; @@ -148,7 +148,7 @@ break; case 'c': // should we create a new database? - if (!(file_exists($idx_name) || file_exists($dat_name))) { + if (!DBA_Simple::db_exists($dbName)) { $file_mode = 'w+b'; $this->_writable = true; $this->_readable = true; Index: pear/DBA/DBA_Table.php diff -u pear/DBA/DBA_Table.php:1.51 pear/DBA/DBA_Table.php:1.52 --- pear/DBA/DBA_Table.php:1.51 Thu Jul 18 01:50:55 2002 +++ pear/DBA/DBA_Table.php Thu Jul 18 15:10:38 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Table.php,v 1.51 2002/07/18 05:50:55 busterb Exp $ +// $Id: DBA_Table.php,v 1.52 2002/07/18 19:10:38 busterb Exp $ // require_once 'PEAR.php'; require_once 'DB/DBA/DBA.php'; @@ -121,6 +121,11 @@ */ var $_primaryKey=array(); + /** + * Name of the DBA driver to use + */ + var $_driver; + // }}} // {{{ DBA_Table($driver = 'simple') @@ -135,6 +140,7 @@ $this->PEAR(); // initialize the internal dba object $this->_dba =& DBA::create($driver); + $this->_driver = $driver; } // }}} @@ -219,37 +225,45 @@ // {{{ create($tableName, $schema) /** - * Creates a new table. Note, this closes any open table. + * Creates a new table. Note, this is a static function, and operates + * independently of a table object. It no longer closes an open table. * * @param string $tableName name of the table to create * @param array $schema field schema for the table * @return object PEAR_Error on failure */ - function create($tableName, $schema) + function create($tableName, $schema, $driver) { + echo "$tableName, $schema, $driver\n"; // pack the schema - $packedSchema = $this->_packSchema($schema + + $packedSchema = DBA_Table::_packSchema($schema + array('_rowid'=>array(DBA_TYPE => DBA_INTEGER, DBA_DEFAULT => 0, DBA_AUTOINCREMENT => true), '_timestamp'=>array(DBA_TYPE => DBA_TIMESTAMP, DBA_DEFAULT => 'time()', DBA_DEFAULTTYPE => DBA_FUNCTION))); + if (PEAR::isError($packedSchema)) { return $packedSchema; } - $r = $this->_dba->open($tableName, 'n'); + $dba = DBA::create($driver); + if (PEAR::isError($dba)) { + return $dba; + } + + $r = $dba->open($tableName, 'n'); if (PEAR::isError($r)) { return $r; } - $r = $this->_dba->insert(DBA_SCHEMA_KEY, $packedSchema); + $r = $dba->insert(DBA_SCHEMA_KEY, $packedSchema); if (PEAR::isError($r)) { return $r; } - $r = $this->_dba->close(); + $r = $dba->close(); if (PEAR::isError($r)) { return $r; } @@ -282,7 +296,7 @@ */ function tableExists($tableName) { - return $this->_dba->db_exists($tableName); + return DBA::exists($tableName, $this->_driver); } // }}} @@ -1014,40 +1028,6 @@ } // }}} - // {{{ _sortCmpA($a, $b) - /** - * Comparison function for sorting ascending order - * - * @access private - * @return int - */ - function _sortCmpA($a, $b) - { - foreach ($this->_sortFields as $field) { - if ($a[$field] < $b[$field]) return -1; - if ($a[$field] > $b[$field]) return 1; - } - return 0; - } - // }}} - - // {{{ _sortCmpD($a, $b) - /** - * Comparison function for sorting descending order - * - * @access private - * @return int - */ - function _sortCmpD($a, $b) - { - foreach ($this->_sortFields as $field) { - if ($a[$field] < $b[$field]) return 1; - if ($a[$field] > $b[$field]) return -1; - } - return 0; - } - // }}} - // {{{ _parseFieldString($fieldString, $possibleFields) /** * explodes a string of field names into an array @@ -1083,32 +1063,71 @@ */ function sort($fields, $order, $rows) { + global $_dba_sort_fields; + if (!function_exists('_dbaSortCmpA')) { + // {{{ _dbaSortCmpA($a, $b) + /** + * Comparison function for sorting ascending order + * + * @access private + * @return int + */ + function _dbaSortCmpA($a, $b) + { + global $_dba_sort_fields; + foreach ($_dba_sort_fields as $field) { + if ($a[$field] < $b[$field]) return -1; + if ($a[$field] > $b[$field]) return 1; + } + return 0; + } + // }}} + + // {{{ _dbaSortCmpD($a, $b) + /** + * Comparison function for sorting descending order + * + * @access private + * @return int + */ + function _dbaSortCmpD($a, $b) + { + global $_dba_sort_fields; + foreach ($_dba_sort_fields as $field) { + if ($a[$field] < $b[$field]) return 1; + if ($a[$field] > $b[$field]) return -1; + } + return 0; + } + // }}} + } + if (is_array($rows)) { if (is_string($fields)) { // parse the sort string to produce an array of sort fields // we pass the sortfields as a member variable because // uasort does not pass extra parameters to the comparison // function - $this->_sortFields = $this->_parseFieldString($fields, + $_dba_sort_fields = DBA_Table::_parseFieldString($fields, reset($rows)); } else { if (is_array($fields)) { // we already have an array of sort fields - $this->_sortFields = $fields; + $_dba_sort_fieldssortFields = $fields; } } if ($order=='a') { - uasort($rows, array($this, '_sortCmpA')); + uasort($rows, '_dbaSortCmpA'); } elseif ($order=='d') { - uasort($rows, array($this, '_sortCmpD')); + uasort($rows, '_dbaSortCmpD'); } else { - return $this->raiseError("$order is not a valid sort order"); + return DBA_Table::raiseError("$order is not a valid sort order"); } return $rows; } else { - return $this->raiseError('no rows to sort specified'); + return DBA_Table::raiseError('no rows to sort specified'); } } // }}} @@ -1128,7 +1147,7 @@ if (is_array($rows)) { $projectFields = array(); if (is_string($fields)) { - $projectFields = $this->_parseFieldString($fields, + $projectFields = DBA_Table::_parseFieldString($fields, reset($rows)); } else { if (is_array($fields)) { @@ -1144,7 +1163,7 @@ } return $projectedRows; } else { - return $this->raiseError('no rows to sort specified'); + return DBA_Table::raiseError('no rows to sort specified'); } } // }}} @@ -1190,7 +1209,7 @@ } return $results; } else { - return $this->raiseError('no rows to sort specified'); + return DBA_Table::raiseError('no rows to sort specified'); } } // }}}
Index: pear/DBA/DBA.php diff -u pear/DBA/DBA.php:1.13 pear/DBA/DBA.php:1.14 --- pear/DBA/DBA.php:1.13 Wed Jul 17 12:57:20 2002 +++ pear/DBA/DBA.php Thu Jul 18 15:10:38 2002 @@ -18,9 +18,11 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA.php,v 1.13 2002/07/17 16:57:20 busterb Exp $ +// $Id: DBA.php,v 1.14 2002/07/18 19:10:38 busterb Exp $ // +require_once('PEAR.php'); + /** * DBA is a set of classes for handling and extending Berkeley DB style * databases. It works around some of the quirks in the built-in dba @@ -35,20 +37,34 @@ class DBA { /** - * Creates a new DBA object - * - * @static - * @param string $driver type of storage object to return - * @return object DBA storage object, returned by reference - */ + * Creates a new DBA object + * + * @static + * @param string $driver type of storage object to return + * @return object DBA storage object, returned by reference + */ function &create($driver = 'simple') { + echo "$driver\n"; if (!function_exists('dba_open') || ($driver=='simple')) { require_once 'DB/DBA/DBA_Simple.php'; return new DBA_Simple(); - } else { + } elseif (($driver == 'db3') || ($driver == 'gdbm')){ require_once 'DB/DBA/DBA_Builtin.php'; return new DBA_Builtin($driver); + } else { + return PEAR::raiseError('Unknown DBA driver, '.$driver); + } + } + + function exists($name, $driver = 'simple') + { + if (!function_exists('dba_open') || ($driver=='simple')) { + return (file_exists($name.'.idx') && file_exists($name.'.dat')); + } elseif (($driver == 'db3') || ($driver == 'gdbm')){ + return file_exists($name); + } else { + return PEAR::raiseError('Unknown DBA driver, '.$driver); } } } Index: pear/DBA/DBA_Relational.php diff -u pear/DBA/DBA_Relational.php:1.39 pear/DBA/DBA_Relational.php:1.40 --- pear/DBA/DBA_Relational.php:1.39 Wed Jul 17 12:57:20 2002 +++ pear/DBA/DBA_Relational.php Thu Jul 18 15:10:38 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Relational.php,v 1.39 2002/07/17 16:57:20 busterb Exp $ +// $Id: DBA_Relational.php,v 1.40 2002/07/18 19:10:38 busterb Exp $ require_once 'PEAR.php'; require_once 'DB/DBA/DBA_Table.php'; @@ -45,13 +45,6 @@ var $_tables=array(); /** - * Handle to manager table; used for handling multi-table operations - * @access private - * @var object - */ - var $_manager; - - /** * Location of table data files * @access private * @var string @@ -86,8 +79,10 @@ $this->_driver = $driver; - // initialize the manager table - $this->_manager =& new DBA_Table($this->_driver); + if (!$this->tableExists('_tables')) { + $this->createTable('_tables', + array('name'=>array(DBA_TYPE=>DBA_VARCHAR, DBA_SIZE=>30))); + } } // }}} @@ -170,7 +165,7 @@ */ function tableExists($tableName) { - return $this->_manager->tableExists($this->_home.$tableName); + return DBA::exists($this->_home.$tableName, $this->_driver); } // }}} @@ -185,7 +180,8 @@ */ function createTable($tableName, $schema) { - return $this->_manager->create($this->_home.$tableName, $schema); + return DBA_Table::create($this->_home.$tableName, $schema, + $this->_driver); } // }}} @@ -343,7 +339,7 @@ */ function sort($fields, $order='a', $rows) { - return $this->_manager->sort($fields, $order, $rows); + return DBA_Table::sort($fields, $order, $rows); } // }}} @@ -360,7 +356,7 @@ */ function project($fields, $rows) { - return $this->_manager->project($fields, $rows); + return DBA_Table::project($fields, $rows); } // }}} @@ -375,7 +371,7 @@ */ function unique($rows) { - return $this->_manager->unique($rows); + return DBA_Table::unique($rows); } // }}} Index: pear/DBA/DBA_Simple.php diff -u pear/DBA/DBA_Simple.php:1.26 pear/DBA/DBA_Simple.php:1.27 --- pear/DBA/DBA_Simple.php:1.26 Wed Jul 17 12:57:20 2002 +++ pear/DBA/DBA_Simple.php Thu Jul 18 15:10:38 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Simple.php,v 1.26 2002/07/17 16:57:20 busterb Exp $ +// $Id: DBA_Simple.php,v 1.27 2002/07/18 19:10:38 busterb Exp $ require_once 'PEAR.php'; @@ -148,7 +148,7 @@ break; case 'c': // should we create a new database? - if (!(file_exists($idx_name) || file_exists($dat_name))) { + if (!DBA_Simple::db_exists($dbName)) { $file_mode = 'w+b'; $this->_writable = true; $this->_readable = true; Index: pear/DBA/DBA_Table.php diff -u pear/DBA/DBA_Table.php:1.51 pear/DBA/DBA_Table.php:1.52 --- pear/DBA/DBA_Table.php:1.51 Thu Jul 18 01:50:55 2002 +++ pear/DBA/DBA_Table.php Thu Jul 18 15:10:38 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Table.php,v 1.51 2002/07/18 05:50:55 busterb Exp $ +// $Id: DBA_Table.php,v 1.52 2002/07/18 19:10:38 busterb Exp $ // require_once 'PEAR.php'; require_once 'DB/DBA/DBA.php'; @@ -121,6 +121,11 @@ */ var $_primaryKey=array(); + /** + * Name of the DBA driver to use + */ + var $_driver; + // }}} // {{{ DBA_Table($driver = 'simple') @@ -135,6 +140,7 @@ $this->PEAR(); // initialize the internal dba object $this->_dba =& DBA::create($driver); + $this->_driver = $driver; } // }}} @@ -219,37 +225,45 @@ // {{{ create($tableName, $schema) /** - * Creates a new table. Note, this closes any open table. + * Creates a new table. Note, this is a static function, and operates + * independently of a table object. It no longer closes an open table. * * @param string $tableName name of the table to create * @param array $schema field schema for the table * @return object PEAR_Error on failure */ - function create($tableName, $schema) + function create($tableName, $schema, $driver) { + echo "$tableName, $schema, $driver\n"; // pack the schema - $packedSchema = $this->_packSchema($schema + + $packedSchema = DBA_Table::_packSchema($schema + array('_rowid'=>array(DBA_TYPE => DBA_INTEGER, DBA_DEFAULT => 0, DBA_AUTOINCREMENT => true), '_timestamp'=>array(DBA_TYPE => DBA_TIMESTAMP, DBA_DEFAULT => 'time()', DBA_DEFAULTTYPE => DBA_FUNCTION))); + if (PEAR::isError($packedSchema)) { return $packedSchema; } - $r = $this->_dba->open($tableName, 'n'); + $dba = DBA::create($driver); + if (PEAR::isError($dba)) { + return $dba; + } + + $r = $dba->open($tableName, 'n'); if (PEAR::isError($r)) { return $r; } - $r = $this->_dba->insert(DBA_SCHEMA_KEY, $packedSchema); + $r = $dba->insert(DBA_SCHEMA_KEY, $packedSchema); if (PEAR::isError($r)) { return $r; } - $r = $this->_dba->close(); + $r = $dba->close(); if (PEAR::isError($r)) { return $r; } @@ -282,7 +296,7 @@ */ function tableExists($tableName) { - return $this->_dba->db_exists($tableName); + return DBA::exists($tableName, $this->_driver); } // }}} @@ -1014,40 +1028,6 @@ } // }}} - // {{{ _sortCmpA($a, $b) - /** - * Comparison function for sorting ascending order - * - * @access private - * @return int - */ - function _sortCmpA($a, $b) - { - foreach ($this->_sortFields as $field) { - if ($a[$field] < $b[$field]) return -1; - if ($a[$field] > $b[$field]) return 1; - } - return 0; - } - // }}} - - // {{{ _sortCmpD($a, $b) - /** - * Comparison function for sorting descending order - * - * @access private - * @return int - */ - function _sortCmpD($a, $b) - { - foreach ($this->_sortFields as $field) { - if ($a[$field] < $b[$field]) return 1; - if ($a[$field] > $b[$field]) return -1; - } - return 0; - } - // }}} - // {{{ _parseFieldString($fieldString, $possibleFields) /** * explodes a string of field names into an array @@ -1083,32 +1063,71 @@ */ function sort($fields, $order, $rows) { + global $_dba_sort_fields; + if (!function_exists('_dbaSortCmpA')) { + // {{{ _dbaSortCmpA($a, $b) + /** + * Comparison function for sorting ascending order + * + * @access private + * @return int + */ + function _dbaSortCmpA($a, $b) + { + global $_dba_sort_fields; + foreach ($_dba_sort_fields as $field) { + if ($a[$field] < $b[$field]) return -1; + if ($a[$field] > $b[$field]) return 1; + } + return 0; + } + // }}} + + // {{{ _dbaSortCmpD($a, $b) + /** + * Comparison function for sorting descending order + * + * @access private + * @return int + */ + function _dbaSortCmpD($a, $b) + { + global $_dba_sort_fields; + foreach ($_dba_sort_fields as $field) { + if ($a[$field] < $b[$field]) return 1; + if ($a[$field] > $b[$field]) return -1; + } + return 0; + } + // }}} + } + if (is_array($rows)) { if (is_string($fields)) { // parse the sort string to produce an array of sort fields // we pass the sortfields as a member variable because // uasort does not pass extra parameters to the comparison // function - $this->_sortFields = $this->_parseFieldString($fields, + $_dba_sort_fields = DBA_Table::_parseFieldString($fields, reset($rows)); } else { if (is_array($fields)) { // we already have an array of sort fields - $this->_sortFields = $fields; + $_dba_sort_fieldssortFields = $fields; } } if ($order=='a') { - uasort($rows, array($this, '_sortCmpA')); + uasort($rows, '_dbaSortCmpA'); } elseif ($order=='d') { - uasort($rows, array($this, '_sortCmpD')); + uasort($rows, '_dbaSortCmpD'); } else { - return $this->raiseError("$order is not a valid sort order"); + return DBA_Table::raiseError("$order is not a valid sort order"); } return $rows; } else { - return $this->raiseError('no rows to sort specified'); + return DBA_Table::raiseError('no rows to sort specified'); } } // }}} @@ -1128,7 +1147,7 @@ if (is_array($rows)) { $projectFields = array(); if (is_string($fields)) { - $projectFields = $this->_parseFieldString($fields, + $projectFields = DBA_Table::_parseFieldString($fields, reset($rows)); } else { if (is_array($fields)) { @@ -1144,7 +1163,7 @@ } return $projectedRows; } else { - return $this->raiseError('no rows to sort specified'); + return DBA_Table::raiseError('no rows to sort specified'); } } // }}} @@ -1190,7 +1209,7 @@ } return $results; } else { - return $this->raiseError('no rows to sort specified'); + return DBA_Table::raiseError('no rows to sort specified'); } } // }}}