cvs: pear /DBA_Relational Relational.php Table.php

From: Date: Tue, 14 Jan 2003 06:30:55 +0000
Subject: cvs: pear /DBA_Relational Relational.php Table.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8234@lists.php.net to get a copy of this message
busterb Tue Jan 14 01:30:55 2003 EDT Modified files: /pear/DBA_Relational Relational.php Table.php Log: moved new select() back to Table class, revert DBA_Relational::select()

Index: pear/DBA_Relational/Relational.php diff -u pear/DBA_Relational/Relational.php:1.7 pear/DBA_Relational/Relational.php:1.8 --- pear/DBA_Relational/Relational.php:1.7 Tue Jan 14 01:10:32 2003 +++ pear/DBA_Relational/Relational.php Tue Jan 14 01:30:55 2003 @@ -21,7 +21,7 @@ // | Jacob Lee <jacobswell4u@yahoo.com> | // +----------------------------------------------------------------------+ // -// $Id: Relational.php,v 1.7 2003/01/14 06:10:32 busterb Exp $ +// $Id: Relational.php,v 1.8 2003/01/14 06:30:55 busterb Exp $ require_once 'PEAR.php'; require_once 'DBA/Table.php'; @@ -62,20 +62,6 @@ */ var $_driver; - /** - * Table of functions recognized in select/join parsing - * @access private - * @var array - */ - var $_functions; - - /** - * Table of operators recognized in select/join parsing - * @access private - * @var array - */ - var $_operators; - // }}} // {{{ DBA_Relational($home = '', $driver = 'file') @@ -98,12 +84,6 @@ $this->_driver = $driver; - // we store symbols this way to take advantage of PHP's hash table - // implementation of associative arrays - $this->_functions = array_flip(get_class_methods('DBA_Functions')); - $this->_operators = array_flip(array('=','<','>','!','(',')','&','|', - '*','/','+','-','%',',')); - // create the _tables table. this keeps track of the tables to be used // in DBA_Relational as well as which driver to use for each. if (!$this->tableExists('_tables')) { @@ -519,65 +499,6 @@ } // }}} - // {{{ function _parsePHPQuery($rawQuery) - function _parsePHPQuery($rawQuery) - { - $inQuote = false; - $PHPQuery = ''; - $token = ''; - - for($i=0; $i < strlen($rawQuery); $i++) { - $c = $rawQuery{$i}; - if ($c == "'" || $c == "\"") { - if (!$inQuote) { - $inQuote = !$inQuote; - $quote = $c; - } elseif ($c == $quote) { - $inQuote = !$inQuote; - $quote = ''; - } - } elseif (isset($this->_operators[$c]) || $c == ' ') { - if (!$inQuote && strlen($token)) { - $PHPQuery .= $this->_cookIdentToken($token); - $PHPQuery .= $c; - $token = ''; - } elseif ($inQuote) { - $token .= $c; - } else { - $PHPQuery .= $c; - } - } else { - $token .= $c; - } - } - - if (strlen($token)) { - $PHPQuery .= $this->_cookIdentToken($token); - } - return $PHPQuery; - } - // }}} - - // {{{ function _cookIdentToken($token) - function _cookIdentToken($token) - { - // quoted string - if ($token{0} == "'" && $token{0} == "\"") { - $cookedToken .= $token; - } elseif ($token == 'null' || $token == 'and' || $token == 'or' || - $token == 'false' || $token == 'true' || function_exists($token)) { - $cookedToken .= $token; - } elseif (isset($this->_functions[$token])) { - $cookedToken .= 'DBA_Function::'.$token; - } elseif (!is_numeric($token)) { - $cookedToken .= '$row[\''.$token.'\']'; - } else { - $cookedToken .= $token; - } - return $cookedToken; - } - // }}} - // {{{ select($tableName, $query, $rows=null) /** * Performs a select on a table. This means that a subset of rows in a @@ -591,28 +512,15 @@ * @param string $query query expression for performing the select * @return mixed PEAR_Error on failure, the row array on success */ - function select($table, $query='*', $rows=null) + function select($tableName, $query='*', $rows=null) { - $result = $this->_openTable($table,'r'); + $result = $this->_openTable($tableName,'r'); if (PEAR::isError($result)) { return $result; } - $rows = $this->_tables[$table]->getRows(); - - if ($query == '*') { - return $rows; - } - - $PHPQuery = $this->_parsePHPQuery($query); - - $PHPSelect = 'foreach ($rows as $key=>$row) if ('. - $PHPQuery.') $results[$key] = $row;'; - echo("$PHPSelect\n"); - - $results = null; - eval ($PHPSelect); - return $results; + // use the table's select query parser + return $this->_tables[$tableName]->select($query, $rows); } // }}} @@ -628,6 +536,8 @@ */ function join($tableA, $tableB, $rawQuery) { + return $this->raiseError('TODO: merge new join()'); +/* // validate tables if (!$this->_validateTable($tableA, $rowsA, $fieldsA, 'A')) { return $this->raiseError("$tableA not in query"); @@ -659,6 +569,7 @@ eval ($PHPJoin); return $results; +*/ } // }}} } Index: pear/DBA_Relational/Table.php diff -u pear/DBA_Relational/Table.php:1.12 pear/DBA_Relational/Table.php:1.13 --- pear/DBA_Relational/Table.php:1.12 Wed Jan 8 17:01:05 2003 +++ pear/DBA_Relational/Table.php Tue Jan 14 01:30:55 2003 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Table.php,v 1.12 2003/01/08 22:01:05 busterb Exp $ +// $Id: Table.php,v 1.13 2003/01/14 06:30:55 busterb Exp $ // require_once 'PEAR.php'; require_once 'DBA.php'; @@ -124,14 +124,32 @@ /** * Name of the DBA driver to use + * @var string + * @access private */ var $_driver; /** * Name of the Table + * @var string + * @access private */ var $_tableName; + /** + * Table of functions recognized in select/join parsing + * @var array + * @access private + */ + var $_functions; + + /** + * Table of operators recognized in select/join parsing + * @var array + * @access private + */ + var $_operators; + // }}} // {{{ DBA_Table($driver = 'file') @@ -147,6 +165,12 @@ // initialize the internal dba object $this->_dba =& DBA::create($driver); $this->_driver = $driver; + + // store symbols this way to take advantage of PHP's hash table + // implementation of associative arrays + $this->_functions = array_flip(get_class_methods('DBA_Functions')); + $this->_operators = array_flip(array('=','<','>','!','(',')','&','|', + '*','/','+','-','%',',')); } // }}} @@ -993,72 +1017,63 @@ } // }}} - // {{{ _cookQuery($query) - /** - * Adds spaces around special symbols so that explode will separate them - * properly from other tokens. Replace spaces within strings with pipe - * characters so that explode will not separate string tokens. - * - * @access private - * @param string $string - * @return string the cooked query - */ - function _cookQuery($query) + // {{{ function _parsePHPQuery($rawQuery) + function _parsePHPQuery($rawQuery) { - foreach (array(',','(',')','==','!=','>','<','<=','>=') as $symbol) { - $query = str_replace($symbol, " $symbol ", $query); - } - - $inString = false; - $cookedQuery = ''; - for ($i=0; $i < strlen($query); ++$i) { - $chr = $query{$i}; - if ($chr =='\'') { - $inString = !$inString; - $cookedQuery .= $chr; - } elseif ($inString && ($chr == ' ')) { - $cookedQuery .= DBA_FIELD_SEPARATOR; + $inQuote = false; + $PHPQuery = ''; + $token = ''; + + for($i=0; $i < strlen($rawQuery); $i++) { + $c = $rawQuery{$i}; + if ($c == "'" || $c == '"') { + if (!$inQuote) { + $inQuote = !$inQuote; + $quote = $c; + } elseif ($c == $quote) { + $inQuote = !$inQuote; + $quote = ''; + } + } elseif (isset($this->_operators[$c]) || $c == ' ') { + if (!$inQuote && strlen($token)) { + $PHPQuery .= $this->_cookIdentToken($token); + $PHPQuery .= $c; + $token = ''; + } elseif ($inQuote) { + $token .= $c; + } else { + $PHPQuery .= $c; + } } else { - $cookedQuery .= $chr; + $token .= $c; } } - return $cookedQuery; + + if (strlen($token)) { + $PHPQuery .= $this->_cookIdentToken($token); + } + return $PHPQuery; } // }}} - // {{{ _parsePHPQuery($rawQuery, $fieldTokens) - /** - * Converts a query expression into PHP code for executing a select. - * - * @access private - * @param string $rawQuery the incoming query - * @param array $fieldTokens list of tokens that should be treated as - * field names - * @return string PHP code for performing a select - */ - function _parsePHPQuery($rawQuery, $fieldTokens) - { - // add spaces around symbols for explode to work properly - $cookedQuery = $this->_cookQuery($rawQuery); - - // begin building the php query for a row - $phpQuery = ''; - - // scan the tokens in the raw query to build a new query - // if the token is a field name, use it as a key in $row[] - $tokens = explode(' ', $cookedQuery); - foreach ($tokens as $token) { - // is this token a field name? - if (in_array($token, $fieldTokens)) { - $phpQuery .= "\$row['$token']"; - } elseif ($token != '||') { - // restore spaces in a string token - $phpQuery .= str_replace(DBA_FIELD_SEPARATOR, ' ', $token); - } else { - $phpQuery .= $token; - } + // {{{ function _cookIdentToken($token) + function _cookIdentToken($token) + { + // quoted string + if ($token{0} == "'" && $token{0} == '"') { + $cookedToken .= $token; + } elseif ($token == 'null' || $token == 'and' || $token == 'or' || + $token == 'false' || $token == 'true' || + function_exists($token)) { + $cookedToken .= $token; + } elseif (isset($this->_functions[$token])) { + $cookedToken .= 'DBA_Function::'.$token; + } elseif (!is_numeric($token)) { + $cookedToken .= '$row[\''.$token.'\']'; + } else { + $cookedToken .= $token; } - return $phpQuery; + return $cookedToken; } // }}} @@ -1079,27 +1094,27 @@ { if ($this->_dba->isOpen()) { - // get a list of valid field names - $fieldTokens = array_keys($this->_schema); - // if we haven't passed any rows to select from, use the whole table if ($rows==null) $rows = $this->getRows(); + $rows = $this->getRows(); + // handle the special case of requesting all rows - if ($rawQuery == '*') + if ($rawQuery == '*') { return $rows; + } // convert the query into a php statement + $PHPQuery = $this->_parsePHPQuery($rawQuery); $PHPSelect = 'foreach ($rows as $key=>$row) if ('. - $this->_parsePHPQuery($rawQuery, $fieldTokens). - ') $results[$key] = $row;'; + $PHPQuery.') $results[$key] = $row;'; // perform the select $results = null; eval ($PHPSelect); - return $results; + } else { return $this->raiseError('table not open'); }
« previous php.pear.cvs (#8234) next »