cvs: pear /DBA DBA_Relational.php DBA_Simple.php Sql_lex.php Sql_parse.php /DBA/tests employment.sql test_sql_dba.php
| From: | Brent Cook | Date: | Wed, 17 Jul 2002 07:30:11 +0000 |
| Subject: | cvs: pear /DBA DBA_Relational.php DBA_Simple.php Sql_lex.php Sql_parse.php /DBA/tests employment.sql test_sql_dba.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5144@lists.php.net to get a copy of this message | ||
busterb Wed Jul 17 03:30:11 2002 EDT
Added files:
/pear/DBA/tests employment.sql
Modified files:
/pear/DBA DBA_Relational.php DBA_Simple.php Sql_lex.php
Sql_parse.php
/pear/DBA/tests test_sql_dba.php
Log:
adjustments to add sql inserts
Index: pear/DBA/DBA_Relational.php diff -u pear/DBA/DBA_Relational.php:1.34 pear/DBA/DBA_Relational.php:1.35 --- pear/DBA/DBA_Relational.php:1.34 Mon Jul 15 18:00:04 2002 +++ pear/DBA/DBA_Relational.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Relational.php,v 1.34 2002/07/15 22:00:04 busterb Exp $ +// $Id: DBA_Relational.php,v 1.35 2002/07/17 07:30:10 busterb Exp $ require_once 'PEAR.php'; require_once 'DB/DBA/DBA_Table.php'; @@ -113,7 +113,7 @@ */ function _DBA_Relational() { - $this->close(); +// $this->close(); } /** Index: pear/DBA/DBA_Simple.php diff -u pear/DBA/DBA_Simple.php:1.23 pear/DBA/DBA_Simple.php:1.24 --- pear/DBA/DBA_Simple.php:1.23 Wed Jul 17 00:29:19 2002 +++ pear/DBA/DBA_Simple.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Simple.php,v 1.23 2002/07/17 04:29:19 busterb Exp $ +// $Id: DBA_Simple.php,v 1.24 2002/07/17 07:30:10 busterb Exp $ require_once 'PEAR.php'; @@ -492,7 +492,7 @@ $loc = ftell($this->_datFP); // write to the data file - $size = $vsize; // make size 5% larger + $size = $vsize + 10; // make size 5% larger // add a useless "\n" to new values. This makes the data file // readable in any text editor. Useful when things go wrong :P Index: pear/DBA/Sql_lex.php diff -u pear/DBA/Sql_lex.php:1.11 pear/DBA/Sql_lex.php:1.12 --- pear/DBA/Sql_lex.php:1.11 Mon Jul 15 18:04:33 2002 +++ pear/DBA/Sql_lex.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Sql_lex.php,v 1.11 2002/07/15 22:04:33 busterb Exp $ +// $Id: Sql_lex.php,v 1.12 2002/07/17 07:30:10 busterb Exp $ // include 'DB/DBA/phptype.php'; @@ -49,7 +49,8 @@ var $tokStart = 0; var $tokLen = 0; var $tokText = ''; - var $lineno = 0; + var $lineNo = 0; + var $lineBegin = 0; var $string; // }}} @@ -84,6 +85,13 @@ } // }}} + function getCurrLine() { + $end = 0; + while ($this->string[$this->lineBegin+$end] != "\n") + ++$end; + return trim(substr($this->string, $this->lineBegin, $end)); + } + // {{{ lex($this->string) function lex() { @@ -103,7 +111,8 @@ $c = $this->get(); while (($c == ' ') || ($c == "\t") || ($c == "\n")) { if ($c == "\n") { - ++$this->lineno; + ++$this->lineNo; + $this->lineBegin = $this->tokPtr; } $c = $this->skip(); $this->tokLen = 1; Index: pear/DBA/Sql_parse.php diff -u pear/DBA/Sql_parse.php:1.12 pear/DBA/Sql_parse.php:1.13 --- pear/DBA/Sql_parse.php:1.12 Tue Jul 16 21:57:11 2002 +++ pear/DBA/Sql_parse.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Sql_parse.php,v 1.12 2002/07/17 01:57:11 busterb Exp $ +// $Id: Sql_parse.php,v 1.13 2002/07/17 07:30:10 busterb Exp $ // require_once 'PEAR.php'; @@ -95,6 +95,7 @@ define('SQL_SET_FUNCT',157); define('SQL_CONSTRAINT',158); define('SQL_DEFAULT',159); +define('SQL_NOTNULL',160); // types define('SQL_FLOAT',171); define('SQL_FIXED',172); @@ -215,7 +216,7 @@ $types = array(); while ($this->token != ')') { $this->getTok(); - if ($this->isVal()) { + if ($this->isVal() || ($this->token == TOK_IDENT)) { $values[] = $this->lexer->tokText; $types[] = $this->token; } elseif ($this->token == ')') { @@ -232,8 +233,16 @@ // {{{ raiseError($message) function raiseError($message) { + $end = 0; + while (($this->lexer->string[$this->lexer->lineBegin+$end] != "\n") && + ($this->lexer->string[$this->lexer->lineBegin+$end])) + ++$end; $message = 'Parse error: '.$message.' on line '. - ($this->lexer->lineno+1).' at '. $this->lexer->tokText; + ($this->lexer->lineNo+1)."\n"; + $message .= substr($this->lexer->string, $this->lexer->lineBegin, $end)."\n"; + $message .= str_repeat(' ', ($this->lexer->tokPtr - + $this->lexer->lineBegin))."^\n"; + return PEAR::raiseError($message); } // }}} @@ -318,8 +327,11 @@ return $this->raiseError('Expected )'); } break; - case (SQL_NULL): case (SQL_AUTOINCREMENT): - $haveValue = false; + case (SQL_AUTOINCREMENT): + $value = true; + break; + case (SQL_NULL): + $haveValue = false; break; default: return $this->raiseError('Unexpected token ' @@ -408,13 +420,12 @@ return $this->raiseError('Expected 1 parameter'); } if ($types[0] != TOK_INT_VAL) { - print_r($types); return $this->raiseError('Expected an integer'); } $fields[$i][SQL_SIZE] = $values[0]; break; case SQL_ENUM: case SQL_SET: - if (!$results) { + if (!sizeof($values)) { return $this->raiseError('Expected a domain'); } $fields[$i][SQL_DOMAIN][] = $values; @@ -463,7 +474,7 @@ $this->getTok(); switch ($this->token) { case TOK_END_OF_INPUT: - return $this->raiseError('End of input'); + return; case SQL_CREATE: $this->getTok(); switch ($this->token) { @@ -486,7 +497,7 @@ case SQL_INSERT: $this->getTok(); if ($this->token == SQL_INTO) { - $tree[SQL_COMMAND] = SQL_CREATE_TABLE; + $tree[SQL_COMMAND] = SQL_INSERT; $this->getTok(); if ($this->token == TOK_IDENT) { $tree[SQL_NAME] = $this->lexer->tokText; @@ -533,7 +544,7 @@ if ($this->token == ';') { return $tree; } else { - return $this->raiseError('Missing ;'); + return $this->raiseError('Expected ;'); } } // }}} Index: pear/DBA/tests/test_sql_dba.php diff -u pear/DBA/tests/test_sql_dba.php:1.1 pear/DBA/tests/test_sql_dba.php:1.2 --- pear/DBA/tests/test_sql_dba.php:1.1 Mon Jul 15 02:49:56 2002 +++ pear/DBA/tests/test_sql_dba.php Wed Jul 17 03:30:11 2002 @@ -1,61 +1,17 @@ <?php - require_once 'PEAR.php'; -require_once '../Sql_parse.php'; require_once 'DB/DBA/DBA_Relational.php'; +require_once 'DB/DBA/DBA_Sql.php'; +require_once 'DB/DBA/Sql_parse.php'; -$sqlfile = implode('', file('tables.sql')); - -$parser = new Sql_Parser($sqlfile); $db = new DBA_Relational('./', 'simple'); +$results = executeSql($db, 'employment.sql'); -$tokenMap = array( - SQL_SIZE=>DBA_SIZE, - SQL_DEFAULT=>DBA_DEFAULT, - SQL_NOTNULL=>DBA_NOTNULL, - SQL_DOMAIN=>DBA_DOMAIN, - SQL_INT=>DBA_INTEGER, - SQL_FIXED=>DBA_NUMERIC, - SQL_FLOAT=>DBA_FLOAT, - SQL_CHAR=>DBA_CHAR, - SQL_VARCHAR=>DBA_VARCHAR, - SQL_TEXT=>DBA_TEXT, - SQL_BOOL=>DBA_BOOLEAN, - SQL_ENUM=>DBA_ENUM, - SQL_SET=>DBA_SET, - SQL_TIMESTAMP=>DBA_TIMESTAMP, - SQL_DATE=>DBA_TIMESTAMP, - SQL_TIME=>DBA_TIMESTAMP -); - -while (1) { - $tree = $parser->parse(); - if (PEAR::isError($tree)) { - break; - } else { - switch ($tree[SQL_COMMAND]) { - case SQL_CREATE_TABLE: - $name = $tree[SQL_NAME]; - $schema = array(); - foreach ($tree[SQL_FIELDS] as $field) { - $fieldName = $field[SQL_NAME]; - $schema[$fieldName][DBA_TYPE] = $tokenMap[$field[SQL_TYPE]]; - if ($field[SQL_SIZE]) - $schema[$fieldName][DBA_SIZE] = $field[SQL_SIZE]; - if ($field[SQL_DEFAULT]) - $schema[$fieldName][DBA_DEFAULT] = $field[SQL_DEFAULT]; - if ($field[SQL_NOTNULL]) - $schema[$fieldName][DBA_NOTNULL] = $field[SQL_NOTNULL]; - if ($field[SQL_DOMAIN]) - $schema[$fieldName][DBA_DOMAIN] = $field[SQL_DOMAIN]; - } - if ($result = PEAR::isError($db->createTable($name, $schema))) { - echo $result."\n"; - } else { - echo "Created $name successfully!\n"; - } - break; - } - } +if (PEAR::isError($results)) { + echo $results->getMessage(); +} else { + echo $results; } +//$db->close(); + ?> Index: pear/DBA/tests/employment.sql +++ pear/DBA/tests/employment.sql CREATE TABLE parttime_employment ( category enum('template','active','inactive'), department_id int, jobtitle varchar(40), available_hours varchar(40), available_dates varchar(40), pay varchar(40), description text, required_qualifications text, preferred_qualifications text, instructions text ); create table departments ( id int auto_increment default 0, title varchar(30) ); insert into departments (title) values ('Accounting Office'); insert into departments (title) values ('Administrative Office'); insert into departments (title) values ('Audio/Visual'); insert into departments (title) values ('Building Management'); insert into departments (title) values ('Building Services'); insert into departments (title) values ('Cactus Cafe'); insert into departments (title) values ('Cash Operations'); insert into departments (title) values ('Commons Coffee Company'); insert into departments (title) values ('Data Processing'); insert into departments (title) values ('Housekeeping'); insert into departments (title) values ('Human Resources'); insert into departments (title) values ('Informal Classes'); insert into departments (title) values ('Information Desk'); insert into departments (title) values ('Jester Center Campus Store'); insert into departments (title) values ('Maintenance'); insert into departments (title) values ('Programming Office'); insert into departments (title) values ('Reservation Office'); insert into departments (title) values ('Union Campus Store'); insert into departments (title) values ('Union Underground');
Index: pear/DBA/DBA_Relational.php diff -u pear/DBA/DBA_Relational.php:1.34 pear/DBA/DBA_Relational.php:1.35 --- pear/DBA/DBA_Relational.php:1.34 Mon Jul 15 18:00:04 2002 +++ pear/DBA/DBA_Relational.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Relational.php,v 1.34 2002/07/15 22:00:04 busterb Exp $ +// $Id: DBA_Relational.php,v 1.35 2002/07/17 07:30:10 busterb Exp $ require_once 'PEAR.php'; require_once 'DB/DBA/DBA_Table.php'; @@ -113,7 +113,7 @@ */ function _DBA_Relational() { - $this->close(); +// $this->close(); } /** Index: pear/DBA/DBA_Simple.php diff -u pear/DBA/DBA_Simple.php:1.23 pear/DBA/DBA_Simple.php:1.24 --- pear/DBA/DBA_Simple.php:1.23 Wed Jul 17 00:29:19 2002 +++ pear/DBA/DBA_Simple.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA_Simple.php,v 1.23 2002/07/17 04:29:19 busterb Exp $ +// $Id: DBA_Simple.php,v 1.24 2002/07/17 07:30:10 busterb Exp $ require_once 'PEAR.php'; @@ -492,7 +492,7 @@ $loc = ftell($this->_datFP); // write to the data file - $size = $vsize; // make size 5% larger + $size = $vsize + 10; // make size 5% larger // add a useless "\n" to new values. This makes the data file // readable in any text editor. Useful when things go wrong :P Index: pear/DBA/Sql_lex.php diff -u pear/DBA/Sql_lex.php:1.11 pear/DBA/Sql_lex.php:1.12 --- pear/DBA/Sql_lex.php:1.11 Mon Jul 15 18:04:33 2002 +++ pear/DBA/Sql_lex.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Sql_lex.php,v 1.11 2002/07/15 22:04:33 busterb Exp $ +// $Id: Sql_lex.php,v 1.12 2002/07/17 07:30:10 busterb Exp $ // include 'DB/DBA/phptype.php'; @@ -49,7 +49,8 @@ var $tokStart = 0; var $tokLen = 0; var $tokText = ''; - var $lineno = 0; + var $lineNo = 0; + var $lineBegin = 0; var $string; // }}} @@ -84,6 +85,13 @@ } // }}} + function getCurrLine() { + $end = 0; + while ($this->string[$this->lineBegin+$end] != "\n") + ++$end; + return trim(substr($this->string, $this->lineBegin, $end)); + } + // {{{ lex($this->string) function lex() { @@ -103,7 +111,8 @@ $c = $this->get(); while (($c == ' ') || ($c == "\t") || ($c == "\n")) { if ($c == "\n") { - ++$this->lineno; + ++$this->lineNo; + $this->lineBegin = $this->tokPtr; } $c = $this->skip(); $this->tokLen = 1; Index: pear/DBA/Sql_parse.php diff -u pear/DBA/Sql_parse.php:1.12 pear/DBA/Sql_parse.php:1.13 --- pear/DBA/Sql_parse.php:1.12 Tue Jul 16 21:57:11 2002 +++ pear/DBA/Sql_parse.php Wed Jul 17 03:30:10 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Sql_parse.php,v 1.12 2002/07/17 01:57:11 busterb Exp $ +// $Id: Sql_parse.php,v 1.13 2002/07/17 07:30:10 busterb Exp $ // require_once 'PEAR.php'; @@ -95,6 +95,7 @@ define('SQL_SET_FUNCT',157); define('SQL_CONSTRAINT',158); define('SQL_DEFAULT',159); +define('SQL_NOTNULL',160); // types define('SQL_FLOAT',171); define('SQL_FIXED',172); @@ -215,7 +216,7 @@ $types = array(); while ($this->token != ')') { $this->getTok(); - if ($this->isVal()) { + if ($this->isVal() || ($this->token == TOK_IDENT)) { $values[] = $this->lexer->tokText; $types[] = $this->token; } elseif ($this->token == ')') { @@ -232,8 +233,16 @@ // {{{ raiseError($message) function raiseError($message) { + $end = 0; + while (($this->lexer->string[$this->lexer->lineBegin+$end] != "\n") && + ($this->lexer->string[$this->lexer->lineBegin+$end])) + ++$end; $message = 'Parse error: '.$message.' on line '. - ($this->lexer->lineno+1).' at '. $this->lexer->tokText; + ($this->lexer->lineNo+1)."\n"; + $message .= substr($this->lexer->string, $this->lexer->lineBegin, $end)."\n"; + $message .= str_repeat(' ', ($this->lexer->tokPtr - + $this->lexer->lineBegin))."^\n"; + return PEAR::raiseError($message); } // }}} @@ -318,8 +327,11 @@ return $this->raiseError('Expected )'); } break; - case (SQL_NULL): case (SQL_AUTOINCREMENT): - $haveValue = false; + case (SQL_AUTOINCREMENT): + $value = true; + break; + case (SQL_NULL): + $haveValue = false; break; default: return $this->raiseError('Unexpected token ' @@ -408,13 +420,12 @@ return $this->raiseError('Expected 1 parameter'); } if ($types[0] != TOK_INT_VAL) { - print_r($types); return $this->raiseError('Expected an integer'); } $fields[$i][SQL_SIZE] = $values[0]; break; case SQL_ENUM: case SQL_SET: - if (!$results) { + if (!sizeof($values)) { return $this->raiseError('Expected a domain'); } $fields[$i][SQL_DOMAIN][] = $values; @@ -463,7 +474,7 @@ $this->getTok(); switch ($this->token) { case TOK_END_OF_INPUT: - return $this->raiseError('End of input'); + return; case SQL_CREATE: $this->getTok(); switch ($this->token) { @@ -486,7 +497,7 @@ case SQL_INSERT: $this->getTok(); if ($this->token == SQL_INTO) { - $tree[SQL_COMMAND] = SQL_CREATE_TABLE; + $tree[SQL_COMMAND] = SQL_INSERT; $this->getTok(); if ($this->token == TOK_IDENT) { $tree[SQL_NAME] = $this->lexer->tokText; @@ -533,7 +544,7 @@ if ($this->token == ';') { return $tree; } else { - return $this->raiseError('Missing ;'); + return $this->raiseError('Expected ;'); } } // }}} Index: pear/DBA/tests/test_sql_dba.php diff -u pear/DBA/tests/test_sql_dba.php:1.1 pear/DBA/tests/test_sql_dba.php:1.2 --- pear/DBA/tests/test_sql_dba.php:1.1 Mon Jul 15 02:49:56 2002 +++ pear/DBA/tests/test_sql_dba.php Wed Jul 17 03:30:11 2002 @@ -1,61 +1,17 @@ <?php - require_once 'PEAR.php'; -require_once '../Sql_parse.php'; require_once 'DB/DBA/DBA_Relational.php'; +require_once 'DB/DBA/DBA_Sql.php'; +require_once 'DB/DBA/Sql_parse.php'; -$sqlfile = implode('', file('tables.sql')); - -$parser = new Sql_Parser($sqlfile); $db = new DBA_Relational('./', 'simple'); +$results = executeSql($db, 'employment.sql'); -$tokenMap = array( - SQL_SIZE=>DBA_SIZE, - SQL_DEFAULT=>DBA_DEFAULT, - SQL_NOTNULL=>DBA_NOTNULL, - SQL_DOMAIN=>DBA_DOMAIN, - SQL_INT=>DBA_INTEGER, - SQL_FIXED=>DBA_NUMERIC, - SQL_FLOAT=>DBA_FLOAT, - SQL_CHAR=>DBA_CHAR, - SQL_VARCHAR=>DBA_VARCHAR, - SQL_TEXT=>DBA_TEXT, - SQL_BOOL=>DBA_BOOLEAN, - SQL_ENUM=>DBA_ENUM, - SQL_SET=>DBA_SET, - SQL_TIMESTAMP=>DBA_TIMESTAMP, - SQL_DATE=>DBA_TIMESTAMP, - SQL_TIME=>DBA_TIMESTAMP -); - -while (1) { - $tree = $parser->parse(); - if (PEAR::isError($tree)) { - break; - } else { - switch ($tree[SQL_COMMAND]) { - case SQL_CREATE_TABLE: - $name = $tree[SQL_NAME]; - $schema = array(); - foreach ($tree[SQL_FIELDS] as $field) { - $fieldName = $field[SQL_NAME]; - $schema[$fieldName][DBA_TYPE] = $tokenMap[$field[SQL_TYPE]]; - if ($field[SQL_SIZE]) - $schema[$fieldName][DBA_SIZE] = $field[SQL_SIZE]; - if ($field[SQL_DEFAULT]) - $schema[$fieldName][DBA_DEFAULT] = $field[SQL_DEFAULT]; - if ($field[SQL_NOTNULL]) - $schema[$fieldName][DBA_NOTNULL] = $field[SQL_NOTNULL]; - if ($field[SQL_DOMAIN]) - $schema[$fieldName][DBA_DOMAIN] = $field[SQL_DOMAIN]; - } - if ($result = PEAR::isError($db->createTable($name, $schema))) { - echo $result."\n"; - } else { - echo "Created $name successfully!\n"; - } - break; - } - } +if (PEAR::isError($results)) { + echo $results->getMessage(); +} else { + echo $results; } +//$db->close(); + ?> Index: pear/DBA/tests/employment.sql +++ pear/DBA/tests/employment.sql CREATE TABLE parttime_employment ( category enum('template','active','inactive'), department_id int, jobtitle varchar(40), available_hours varchar(40), available_dates varchar(40), pay varchar(40), description text, required_qualifications text, preferred_qualifications text, instructions text ); create table departments ( id int auto_increment default 0, title varchar(30) ); insert into departments (title) values ('Accounting Office'); insert into departments (title) values ('Administrative Office'); insert into departments (title) values ('Audio/Visual'); insert into departments (title) values ('Building Management'); insert into departments (title) values ('Building Services'); insert into departments (title) values ('Cactus Cafe'); insert into departments (title) values ('Cash Operations'); insert into departments (title) values ('Commons Coffee Company'); insert into departments (title) values ('Data Processing'); insert into departments (title) values ('Housekeeping'); insert into departments (title) values ('Human Resources'); insert into departments (title) values ('Informal Classes'); insert into departments (title) values ('Information Desk'); insert into departments (title) values ('Jester Center Campus Store'); insert into departments (title) values ('Maintenance'); insert into departments (title) values ('Programming Office'); insert into departments (title) values ('Reservation Office'); insert into departments (title) values ('Union Campus Store'); insert into departments (title) values ('Union Underground');