cvs: pear /DB_DBA DBA.php DBA_Relational.php DBA_Table.php
| From: | Brent Cook | Date: | Fri, 21 Jun 2002 05:23:33 +0000 |
| Subject: | cvs: pear /DB_DBA DBA.php DBA_Relational.php DBA_Table.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4718@lists.php.net to get a copy of this message | ||
busterb Fri Jun 21 01:23:33 2002 EDT
Modified files:
/pear/DB_DBA DBA.php DBA_Relational.php DBA_Table.php
Log:
More cleanups and updates. DBA_Table.php is working now; still need to work on error handling.
DBA_Relational appears to work, but incomplete error handling prevents it from running all current
tests.
Index: pear/DB_DBA/DBA.php diff -u pear/DB_DBA/DBA.php:1.1 pear/DB_DBA/DBA.php:1.2 --- pear/DB_DBA/DBA.php:1.1 Thu Jun 20 19:32:55 2002 +++ pear/DB_DBA/DBA.php Fri Jun 21 01:23:32 2002 @@ -16,7 +16,7 @@ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // -// $Id: DBA.php,v 1.1 2002/06/20 23:32:55 busterb Exp $ +// $Id: DBA.php,v 1.2 2002/06/21 05:23:32 busterb Exp $ // class DBA { @@ -30,10 +30,10 @@ function create($driver = 'simple') { if (!function_exists('dba_open') || ($driver=='simple')) { - require_once 'DB/DBA_Simple.php'; + require_once 'DB_DBA/DBA_Simple.php'; return new DBA_Simple(); } else { - require_once 'DB/DBA_Builtin.php'; + require_once 'DB_DBA/DBA_Builtin.php'; return new DBA_Builtin($driver); } } Index: pear/DB_DBA/DBA_Relational.php diff -u pear/DB_DBA/DBA_Relational.php:1.1 pear/DB_DBA/DBA_Relational.php:1.2 --- pear/DB_DBA/DBA_Relational.php:1.1 Thu Jun 20 19:32:55 2002 +++ pear/DB_DBA/DBA_Relational.php Fri Jun 21 01:23:32 2002 @@ -16,9 +16,10 @@ // | Author: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // -// $Id: DBA_Relational.php,v 1.1 2002/06/20 23:32:55 busterb Exp $ +// $Id: DBA_Relational.php,v 1.2 2002/06/21 05:23:32 busterb Exp $ -require_once('DB_DBA/DBA_Table.php'); +require_once 'PEAR.php'; +require_once 'DB_DBA/DBA_Table.php'; /** * a relational database manager for DBM-style databases @@ -34,18 +35,22 @@ // location of table data files var $_home; + // table driver to use + var $_driver; + /** * Constructor * @param $_home */ - function DBA_Relational ($_home = '') + function DBA_Relational ($home = '', $driver = 'simple') { // add trailing slash if not present - if (substr($_home, -1) != '/') { - $this->_home = $_home.'/'; + if (substr($home, -1) != '/') { + $this->_home = $home.'/'; } else { - $this->_home = $_home; + $this->_home = $home; } + $this->_driver = $driver; } function close () @@ -56,7 +61,7 @@ } /** - * Opens a database. + * Opens a table * @param $tableName * @param $mode */ @@ -64,10 +69,9 @@ { if (!isset($this->_tables[$tableName])) { - $this->_tables[$tableName] = new DBA_Table($this->_home.$tableName, - $mode); + $this->_tables[$tableName] = new DBA_Table($this->_driver); - if (!$this->_tables[$tableName]->isOpen()) { + if (!$this->_tables[$tableName]->tableExists($tableName)) { unset($this->_tables[$tableName]); return PEAR::raiseError("Table: '$tableName' does not exist"); } @@ -76,7 +80,7 @@ if ($this->_tables[$tableName]->isOpen()) { if ((($mode == 'r') && $this->_tables[$tableName]->isReadable()) - || (($mode == 'w') && $this->_tables[$tableName]->isWritable())) { + || (($mode == 'w') && $this->_tables[$tableName]->isWritable())){ return true; } else { $this->_tables[$tableName]->close(); @@ -85,7 +89,7 @@ return $this->_tables[$tableName]->open($this->_home.$tableName, $mode); } - function formatResults($results, $fields=null) + function formatResults($results, $fields = null) { if (is_array($results) && sizeof($results)) { @@ -140,22 +144,26 @@ // check if this table object exists if (!isset($this->_tables[$tableName])) { $this->_tables[$tableName] = new DBA_Table(); - else + } else { return false; // the table object exists, so the table must exist + } // ask if the table really exists - if (!$this->_tables[$tableName]->exists($this->_home.$tableName)) - return $this->_tables[$tableName]->create($this->_home.$tableName, $schema); - else + if (!$this->_tables[$tableName]->tableExists($this->_home.$tableName)) { + return $this->_tables[$tableName]->create($this->_home.$tableName, + $schema); + } else { return false; + } } function isOpen ($tableName) { - if (isset($this->_tables[$tableName])) + if (isset($this->_tables[$tableName])) { return $this->_tables[$tableName]->isOpen(); - else + } else { return false; + } } function dropTable ($tableName) @@ -171,34 +179,38 @@ function insertRow ($tableName, $data) { - if ($this->openTable($tableName, 'w')) + if ($this->openTable($tableName, 'w')) { return $this->_tables[$tableName]->insertRow($data); - else + } else { return false; + } } function replaceRow ($tableName, $key, $data) { - if ($this->openTable($tableName, 'w')) + if ($this->openTable($tableName, 'w')) { return $this->_tables[$tableName]->replaceRow($key, $data); - else + } else { return false; + } } function deleteRow ($tableName, $key) { - if ($this->openTable($tableName, 'w')) + if ($this->openTable($tableName, 'w')) { return $this->_tables[$tableName]->deleteRow($key); - else + } else { return false; + } } function fetchRow ($tableName, $key) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->fetchRow($key); - else + } else { return false; + } } function select ($tableName, $query, $rows=null) @@ -212,34 +224,38 @@ function sort ($tableName, $fields, $order='a', $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->sort($fields, $order, $rows); - else + } else { return false; + } } function project ($tableName, $fields, $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->project($fields, $rows); - else + } else { return false; + } } function unique ($tableName, $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->unique($rows); - else + } else { return false; + } } function finalizeRows($tableName, $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->finalizeRows($rows); - else + } else { return false; + } } function _validateTable (&$table, &$rows, &$fields, $altName) @@ -300,18 +316,23 @@ function join ($tableA, $tableB, $rawQuery) { // validate tables - if (!$this->_validateTable($tableA, $rowsA, $fieldsA, 'A')) + if (!$this->_validateTable($tableA, $rowsA, $fieldsA, 'A')) { return false; - if (!$this->_validateTable($tableB, $rowsB, $fieldsB, 'B')) + } + if (!$this->_validateTable($tableB, $rowsB, $fieldsB, 'B')) { return false; + } // check for empty tables - if (is_null($rowsA) && !is_null($rowsB)) + if (is_null($rowsA) && !is_null($rowsB)) { return $rowsB; - if (!is_null($rowsA) && is_null($rowsB)) + } + if (!is_null($rowsA) && is_null($rowsB)) { return $rowsA; - if (is_null($rowsA) && is_null($rowsB)) + } + if (is_null($rowsA) && is_null($rowsB)) { return array(); + } // build the join operation with nested loops $PHPJoin = 'foreach ($rowsA as $rowA) foreach ($rowsB as $rowB) if ('. Index: pear/DB_DBA/DBA_Table.php diff -u pear/DB_DBA/DBA_Table.php:1.1 pear/DB_DBA/DBA_Table.php:1.2 --- pear/DB_DBA/DBA_Table.php:1.1 Thu Jun 20 19:32:55 2002 +++ pear/DB_DBA/DBA_Table.php Fri Jun 21 01:23:32 2002 @@ -16,8 +16,10 @@ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // -// $Id: DBA_Table.php,v 1.1 2002/06/20 23:32:55 busterb Exp $ +// $Id: DBA_Table.php,v 1.2 2002/06/21 05:23:32 busterb Exp $ // +require_once 'DB_DBA/DBA.php'; + define ('DBA_TABLE_META', '__meta__'); /** @@ -26,7 +28,7 @@ * It uses a DBA class as the storage driver. * * @author Brent Cook <busterb@mail.utexas.edu> - * @version 0.0.2 + * @version 0.0.10 */ class DBA_Table { @@ -51,12 +53,10 @@ * Constructor * @param object $dba dba object to use for storage, you need this sometime */ - function DBATable ($dba = NULL) + function DBA_Table ($driver = 'simple') { - // set the internal dba object - if (!is_null($dba)) { - $this->_dba &= $dba; - } + // initialize the internal dba object + $this->_dba = DBA::create($driver); } /** @@ -67,13 +67,8 @@ * @param object $dba dba object to use for storage, you need this * @returns boolean false on error, true on success */ - function open ($tableName, $mode = 'r', $dba = NULL) + function open ($tableName, $mode = 'r') { - // set the internal dba object - if (!is_null($dba)) { - $this->_dba &= $dba; - } - if (!($this->_dba->open($tableName, $mode))) { return false; } @@ -113,39 +108,25 @@ * @param array $fieldSchema field schema for the table * @param object $dba dba object to use */ - function create ($tableName, $fieldSchema, $dba=NULL) + function create ($tableName, $fieldSchema) { // pack the fieldSchema $fieldString = $this->_packFieldSchema($fieldSchema); - if (is_null($dba)) { - if (is_object($this->_dba)) { - // close any open table, since this opens a new database with - // the same internal dba object - $r = $this->close(); - $r = $r && $this->_dba->open($tableName, 'n'); - $r = $r && $this->_dba->insert(DBA_TABLE_META, $fieldString); - $r = $r && $this->_dba->close(); - } else { - trigger_error("DBA: Could not create $tableName, no dba object". - "specified", E_USER_WARNING); - return false; - } else { - $r = $dba->open($tableName, 'n'); - $r = $r && $dba->insert(DBA_TABLE_META, $fieldString); - $r = $r && $dba->close(); - } + $r = $this->_dba->open($tableName, 'n'); + $r = $r && $this->_dba->insert(DBA_TABLE_META, $fieldString); + $r = $r && $this->_dba->close(); // return the result of the creation operations return $r; } /** - * Check whether key exists + * Check whether table exists * * @param $key string * @returns boolean */ - function exists ($tableName) + function tableExists ($tableName) { return $this->_dba->db_exists($tableName); } @@ -234,6 +215,7 @@ * * @access private * @returns integer a new key + */ function _getUniqueKey() { // find the maxKey if necessary @@ -311,8 +293,8 @@ } else { if (is_string ($value)) { // convert a 'boolean' string into a string boolean - $c_value = strval(in_array(strtolower($str) - ,array('t','true','y','yes','1')); + $c_value = strval(in_array(strtolower($str), + array('t','true','y','yes','1'))); } } break; @@ -556,6 +538,7 @@ /** * Inserts a new row in a database + */ function insertRow ($data) { if ($this->isOpen()) { @@ -611,10 +594,12 @@ while ($key) { if ($key != DBA_TABLE_META) { if (is_null($rowIDs)) { - $rows[$key] = $this->_unpackRow($this->_dba->fetch($key)); + $rows[$key] = $this->_unpackRow( + $this->_dba->fetch($key)); } else { if (in_array($key, $rowKeys)) { - $rows[$key] = $this->_unpackRow($this->_dba->fetch($key)); + $rows[$key] = $this->_unpackRow( + $this->_dba->fetch($key)); } } }
Index: pear/DB_DBA/DBA.php diff -u pear/DB_DBA/DBA.php:1.1 pear/DB_DBA/DBA.php:1.2 --- pear/DB_DBA/DBA.php:1.1 Thu Jun 20 19:32:55 2002 +++ pear/DB_DBA/DBA.php Fri Jun 21 01:23:32 2002 @@ -16,7 +16,7 @@ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // -// $Id: DBA.php,v 1.1 2002/06/20 23:32:55 busterb Exp $ +// $Id: DBA.php,v 1.2 2002/06/21 05:23:32 busterb Exp $ // class DBA { @@ -30,10 +30,10 @@ function create($driver = 'simple') { if (!function_exists('dba_open') || ($driver=='simple')) { - require_once 'DB/DBA_Simple.php'; + require_once 'DB_DBA/DBA_Simple.php'; return new DBA_Simple(); } else { - require_once 'DB/DBA_Builtin.php'; + require_once 'DB_DBA/DBA_Builtin.php'; return new DBA_Builtin($driver); } } Index: pear/DB_DBA/DBA_Relational.php diff -u pear/DB_DBA/DBA_Relational.php:1.1 pear/DB_DBA/DBA_Relational.php:1.2 --- pear/DB_DBA/DBA_Relational.php:1.1 Thu Jun 20 19:32:55 2002 +++ pear/DB_DBA/DBA_Relational.php Fri Jun 21 01:23:32 2002 @@ -16,9 +16,10 @@ // | Author: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // -// $Id: DBA_Relational.php,v 1.1 2002/06/20 23:32:55 busterb Exp $ +// $Id: DBA_Relational.php,v 1.2 2002/06/21 05:23:32 busterb Exp $ -require_once('DB_DBA/DBA_Table.php'); +require_once 'PEAR.php'; +require_once 'DB_DBA/DBA_Table.php'; /** * a relational database manager for DBM-style databases @@ -34,18 +35,22 @@ // location of table data files var $_home; + // table driver to use + var $_driver; + /** * Constructor * @param $_home */ - function DBA_Relational ($_home = '') + function DBA_Relational ($home = '', $driver = 'simple') { // add trailing slash if not present - if (substr($_home, -1) != '/') { - $this->_home = $_home.'/'; + if (substr($home, -1) != '/') { + $this->_home = $home.'/'; } else { - $this->_home = $_home; + $this->_home = $home; } + $this->_driver = $driver; } function close () @@ -56,7 +61,7 @@ } /** - * Opens a database. + * Opens a table * @param $tableName * @param $mode */ @@ -64,10 +69,9 @@ { if (!isset($this->_tables[$tableName])) { - $this->_tables[$tableName] = new DBA_Table($this->_home.$tableName, - $mode); + $this->_tables[$tableName] = new DBA_Table($this->_driver); - if (!$this->_tables[$tableName]->isOpen()) { + if (!$this->_tables[$tableName]->tableExists($tableName)) { unset($this->_tables[$tableName]); return PEAR::raiseError("Table: '$tableName' does not exist"); } @@ -76,7 +80,7 @@ if ($this->_tables[$tableName]->isOpen()) { if ((($mode == 'r') && $this->_tables[$tableName]->isReadable()) - || (($mode == 'w') && $this->_tables[$tableName]->isWritable())) { + || (($mode == 'w') && $this->_tables[$tableName]->isWritable())){ return true; } else { $this->_tables[$tableName]->close(); @@ -85,7 +89,7 @@ return $this->_tables[$tableName]->open($this->_home.$tableName, $mode); } - function formatResults($results, $fields=null) + function formatResults($results, $fields = null) { if (is_array($results) && sizeof($results)) { @@ -140,22 +144,26 @@ // check if this table object exists if (!isset($this->_tables[$tableName])) { $this->_tables[$tableName] = new DBA_Table(); - else + } else { return false; // the table object exists, so the table must exist + } // ask if the table really exists - if (!$this->_tables[$tableName]->exists($this->_home.$tableName)) - return $this->_tables[$tableName]->create($this->_home.$tableName, $schema); - else + if (!$this->_tables[$tableName]->tableExists($this->_home.$tableName)) { + return $this->_tables[$tableName]->create($this->_home.$tableName, + $schema); + } else { return false; + } } function isOpen ($tableName) { - if (isset($this->_tables[$tableName])) + if (isset($this->_tables[$tableName])) { return $this->_tables[$tableName]->isOpen(); - else + } else { return false; + } } function dropTable ($tableName) @@ -171,34 +179,38 @@ function insertRow ($tableName, $data) { - if ($this->openTable($tableName, 'w')) + if ($this->openTable($tableName, 'w')) { return $this->_tables[$tableName]->insertRow($data); - else + } else { return false; + } } function replaceRow ($tableName, $key, $data) { - if ($this->openTable($tableName, 'w')) + if ($this->openTable($tableName, 'w')) { return $this->_tables[$tableName]->replaceRow($key, $data); - else + } else { return false; + } } function deleteRow ($tableName, $key) { - if ($this->openTable($tableName, 'w')) + if ($this->openTable($tableName, 'w')) { return $this->_tables[$tableName]->deleteRow($key); - else + } else { return false; + } } function fetchRow ($tableName, $key) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->fetchRow($key); - else + } else { return false; + } } function select ($tableName, $query, $rows=null) @@ -212,34 +224,38 @@ function sort ($tableName, $fields, $order='a', $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->sort($fields, $order, $rows); - else + } else { return false; + } } function project ($tableName, $fields, $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->project($fields, $rows); - else + } else { return false; + } } function unique ($tableName, $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->unique($rows); - else + } else { return false; + } } function finalizeRows($tableName, $rows=null) { - if ($this->openTable($tableName, 'r')) + if ($this->openTable($tableName, 'r')) { return $this->_tables[$tableName]->finalizeRows($rows); - else + } else { return false; + } } function _validateTable (&$table, &$rows, &$fields, $altName) @@ -300,18 +316,23 @@ function join ($tableA, $tableB, $rawQuery) { // validate tables - if (!$this->_validateTable($tableA, $rowsA, $fieldsA, 'A')) + if (!$this->_validateTable($tableA, $rowsA, $fieldsA, 'A')) { return false; - if (!$this->_validateTable($tableB, $rowsB, $fieldsB, 'B')) + } + if (!$this->_validateTable($tableB, $rowsB, $fieldsB, 'B')) { return false; + } // check for empty tables - if (is_null($rowsA) && !is_null($rowsB)) + if (is_null($rowsA) && !is_null($rowsB)) { return $rowsB; - if (!is_null($rowsA) && is_null($rowsB)) + } + if (!is_null($rowsA) && is_null($rowsB)) { return $rowsA; - if (is_null($rowsA) && is_null($rowsB)) + } + if (is_null($rowsA) && is_null($rowsB)) { return array(); + } // build the join operation with nested loops $PHPJoin = 'foreach ($rowsA as $rowA) foreach ($rowsB as $rowB) if ('. Index: pear/DB_DBA/DBA_Table.php diff -u pear/DB_DBA/DBA_Table.php:1.1 pear/DB_DBA/DBA_Table.php:1.2 --- pear/DB_DBA/DBA_Table.php:1.1 Thu Jun 20 19:32:55 2002 +++ pear/DB_DBA/DBA_Table.php Fri Jun 21 01:23:32 2002 @@ -16,8 +16,10 @@ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // -// $Id: DBA_Table.php,v 1.1 2002/06/20 23:32:55 busterb Exp $ +// $Id: DBA_Table.php,v 1.2 2002/06/21 05:23:32 busterb Exp $ // +require_once 'DB_DBA/DBA.php'; + define ('DBA_TABLE_META', '__meta__'); /** @@ -26,7 +28,7 @@ * It uses a DBA class as the storage driver. * * @author Brent Cook <busterb@mail.utexas.edu> - * @version 0.0.2 + * @version 0.0.10 */ class DBA_Table { @@ -51,12 +53,10 @@ * Constructor * @param object $dba dba object to use for storage, you need this sometime */ - function DBATable ($dba = NULL) + function DBA_Table ($driver = 'simple') { - // set the internal dba object - if (!is_null($dba)) { - $this->_dba &= $dba; - } + // initialize the internal dba object + $this->_dba = DBA::create($driver); } /** @@ -67,13 +67,8 @@ * @param object $dba dba object to use for storage, you need this * @returns boolean false on error, true on success */ - function open ($tableName, $mode = 'r', $dba = NULL) + function open ($tableName, $mode = 'r') { - // set the internal dba object - if (!is_null($dba)) { - $this->_dba &= $dba; - } - if (!($this->_dba->open($tableName, $mode))) { return false; } @@ -113,39 +108,25 @@ * @param array $fieldSchema field schema for the table * @param object $dba dba object to use */ - function create ($tableName, $fieldSchema, $dba=NULL) + function create ($tableName, $fieldSchema) { // pack the fieldSchema $fieldString = $this->_packFieldSchema($fieldSchema); - if (is_null($dba)) { - if (is_object($this->_dba)) { - // close any open table, since this opens a new database with - // the same internal dba object - $r = $this->close(); - $r = $r && $this->_dba->open($tableName, 'n'); - $r = $r && $this->_dba->insert(DBA_TABLE_META, $fieldString); - $r = $r && $this->_dba->close(); - } else { - trigger_error("DBA: Could not create $tableName, no dba object". - "specified", E_USER_WARNING); - return false; - } else { - $r = $dba->open($tableName, 'n'); - $r = $r && $dba->insert(DBA_TABLE_META, $fieldString); - $r = $r && $dba->close(); - } + $r = $this->_dba->open($tableName, 'n'); + $r = $r && $this->_dba->insert(DBA_TABLE_META, $fieldString); + $r = $r && $this->_dba->close(); // return the result of the creation operations return $r; } /** - * Check whether key exists + * Check whether table exists * * @param $key string * @returns boolean */ - function exists ($tableName) + function tableExists ($tableName) { return $this->_dba->db_exists($tableName); } @@ -234,6 +215,7 @@ * * @access private * @returns integer a new key + */ function _getUniqueKey() { // find the maxKey if necessary @@ -311,8 +293,8 @@ } else { if (is_string ($value)) { // convert a 'boolean' string into a string boolean - $c_value = strval(in_array(strtolower($str) - ,array('t','true','y','yes','1')); + $c_value = strval(in_array(strtolower($str), + array('t','true','y','yes','1'))); } } break; @@ -556,6 +538,7 @@ /** * Inserts a new row in a database + */ function insertRow ($data) { if ($this->isOpen()) { @@ -611,10 +594,12 @@ while ($key) { if ($key != DBA_TABLE_META) { if (is_null($rowIDs)) { - $rows[$key] = $this->_unpackRow($this->_dba->fetch($key)); + $rows[$key] = $this->_unpackRow( + $this->_dba->fetch($key)); } else { if (in_array($key, $rowKeys)) { - $rows[$key] = $this->_unpackRow($this->_dba->fetch($key)); + $rows[$key] = $this->_unpackRow( + $this->_dba->fetch($key)); } } }