r1012 - in branches/dev/grid/tests/data: . txtdb
Author: paul.bakaus
Date: Tue Nov 25 06:55:32 2008
New Revision: 1012
Added:
branches/dev/grid/tests/data/
branches/dev/grid/tests/data/emails-json.php
branches/dev/grid/tests/data/emails-xml.php
branches/dev/grid/tests/data/emails.php
branches/dev/grid/tests/data/employees-json.php
branches/dev/grid/tests/data/txtdb/
branches/dev/grid/tests/data/txtdb/addressbook.txt
branches/dev/grid/tests/data/txtdb/const.php
branches/dev/grid/tests/data/txtdb/database.php
branches/dev/grid/tests/data/txtdb/employees.txt
branches/dev/grid/tests/data/txtdb/expression.php
branches/dev/grid/tests/data/txtdb/init.php
branches/dev/grid/tests/data/txtdb/resultset.php
branches/dev/grid/tests/data/txtdb/sql.php
branches/dev/grid/tests/data/txtdb/stringparser.php
branches/dev/grid/tests/data/txtdb/txt-db-api.php
branches/dev/grid/tests/data/txtdb/util.php
Log:
grid: readded test data for the grid
Added: branches/dev/grid/tests/data/emails-json.php
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/emails-json.php Tue Nov 25 06:55:32 2008
@@ -0,0 +1,5 @@
+<?php
+include("emails.php");
+header("Content-Type: text/javascript");
+echo json_encode($result);
+?>
\ No newline at end of file
Added: branches/dev/grid/tests/data/emails-xml.php
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/emails-xml.php Tue Nov 25 06:55:32 2008
@@ -0,0 +1,18 @@
+<?php
+include("emails.php");
+header("Content-Type: text/xml");
+$xml = new XMLWriter();
+$xml->openMemory();
+$xml->startDocument("1.0", "UTF-8");
+$xml->startElement("addressbook");
+$xml->writeAttribute("total", $result["totalRecords"]);
+foreach($result["records"] as $record) {
+ $xml->startElement("address");
+ $xml->writeElement("name", $record[0]);
+ $xml->writeElement("email", $record[1]);
+ $xml->endElement();
+}
+$xml->endElement();
+$xml->endDocument();
+echo $xml->flush();
+?>
\ No newline at end of file
Added: branches/dev/grid/tests/data/emails.php
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/emails.php Tue Nov 25 06:55:32 2008
@@ -0,0 +1,27 @@
+<?php
+include("txtdb/txt-db-api.php");
+
+$db = new Database(ROOT_DATABASE);
+
+$offset = $_REQUEST["start"];
+$limit = $_REQUEST["limit"];
+$sort = $_REQUEST["sortColumn"];
+$sortDirection = $_REQUEST["sortDirection"];
+
+$totalSet = $db->executeQuery("select * from addressbook");
+// do not try at home, sql-injection vulnerable
+$resultSet = $db->executeQuery("select * from addressbook order by $sort
$sortDirection limit $offset, $limit");
+
+$records = array();
+while($resultSet->next()) {
+ array_push($records, array($resultSet->getCurrentValueByName("name"),
$resultSet->getCurrentValueByName("email")));
+}
+$result = array(
+ "totalRecords" => $totalSet->getRowCount(),
+ "columns" => array(
+ array("id" => "name", "label" => "Name", "width" => 125),
+ array("id" => "email", "label" => "Address", "width" => 250)
+ ),
+ "records" => $records
+);
+?>
\ No newline at end of file
Added: branches/dev/grid/tests/data/employees-json.php
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/employees-json.php Tue Nov 25 06:55:32 2008
@@ -0,0 +1,43 @@
+<?php
+include("txtdb/txt-db-api.php");
+
+$db = new Database(ROOT_DATABASE);
+
+$offset = $_REQUEST["start"];
+$limit = $_REQUEST["limit"];
+$sort = $_REQUEST["sortColumn"];
+$sortDirection = $_REQUEST["sortDirection"];
+
+$totalSet = $db->executeQuery("select * from employees");
+// do not try at home, sql-injection vulnerable
+$resultSet = $db->executeQuery("select * from employees order by $sort
$sortDirection limit $offset, $limit");
+
+$records = array();
+while($resultSet->next()) {
+ array_push($records, array(
+ $resultSet->getCurrentValueByName("id"),
+ $resultSet->getCurrentValueByName("firstname"),
+ $resultSet->getCurrentValueByName("lastname"),
+ $resultSet->getCurrentValueByName("salary"),
+ $resultSet->getCurrentValueByName("city"),
+ $resultSet->getCurrentValueByName("department"),
+ $resultSet->getCurrentValueByName("firstworkday")
+ ));
+}
+
+$result = array(
+ "totalRecords" => $totalSet->getRowCount(),
+ "columns" => array(
+ array("id" => "id", "label" => "ID", "width" => 75),
+ array("id" => "firstname", "label" => "Firstname", "width" => 125),
+ array("id" => "lastname", "label" => "Lastname", "width" => 125),
+ array("id" => "salary", "label" => "Salary", "width" => 125),
+ array("id" => "city", "label" => "City", "width" => 150),
+ array("id" => "department", "label" => "Department", "width" => 150),
+ array("id" => "firstworkday", "label" => "Employed since", "width" =>
125)
+ ),
+ "records" => $records
+);
+header("Content-Type: text/javascript");
+echo json_encode($result);
+?>
\ No newline at end of file
Added: branches/dev/grid/tests/data/txtdb/addressbook.txt
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/txtdb/addressbook.txt Tue Nov 25 06:55:32
2008
@@ -0,0 +1,13 @@
+id#name#email
+inc#str#str
+##
+1#Peter Pan#peter@pan.de
+2#Molly#molly@yahoo.com
+3#Forneria Marconi#live@japan.jp
+4#Master Sync#205bw@samsung.com
+5#Dr. Tech de Log#g15@logitech.com
+6#Don Corleone#don@vegas.com
+7#Mc Chick#info@donalds.org
+8#Donnie Darko#dd@timeshift.info
+9#Quake The Net#webmaster@quakenet.org
+10#Dr. Write#write@writable.com
\ No newline at end of file
Added: branches/dev/grid/tests/data/txtdb/const.php
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/txtdb/const.php Tue Nov 25 06:55:32 2008
@@ -0,0 +1,112 @@
+<?php
+/**********************************************************************
+ Php Textfile DB API
+ Copyright 2005 by c-worker.ch
+ http://www.c-worker.ch
+***********************************************************************/
+/**********************************************************************
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+Redistributions of source code must retain the above copyright notice, this
+list of conditions and the following disclaimer.
+Redistributions in binary form must reproduce the above copyright notice,
+this list of conditions and the following disclaimer in the documentation
+and/or other materials provided with the distribution.
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+***********************************************************************/
+
+/***********************************
+ User Settings
+************************************/
+
+$DEBUG=0; // 0=Debug disabled, 1=Debug enabled
+$LIKE_CASE_SENSITIVE=0; // 0=LIKE is case insensitive, 1=LIKE is case
sensitive
+$ORDER_CASE_SENSITIVE=0; // 0=ORDER BY is case insensitive, 1=ORDER BY is
case sensitive
+$ASSUMED_RECORD_SIZE=30; // Set this to the average size of one record,
if in doubt
+ // leave the default value. DON'T set it to
<1! int's only!
+$PRINT_ERRORS=1; // 0 = Errors are NOT displayed, 1 = Errors are
displayed
+$PRINT_WARNINGS=0; // 0 = Warnings are NOT displayed, 1 = Warnings are
displayed
+
+
+
+/***********************************
+ Constants
+************************************/
+// Don't change them, expect you know
+// what you do!
+
+// Define User Settings
+define("LIKE_CASE_SENSITIVE",$LIKE_CASE_SENSITIVE);
+define("ORDER_CASE_SENSITIVE",$ORDER_CASE_SENSITIVE);
+define("TXTDBAPI_DEBUG",$DEBUG);
+
+// Even more Debug Infos ?
+define("TXTDBAPI_VERBOSE_DEBUG",0);
+
+// This constant doesn't limit the max size of a record it's only the
assmued size
+// of a record when a table is read for appending. If not a whole record is
+// contained in ASSUMED_RECORD_SIZE bytes, the the number of bytes read
+// is increased until a whole record was read. Choosing this value wisely
may
+// result in a better INSERT performance
+define("ASSUMED_RECORD_SIZE",$ASSUMED_RECORD_SIZE);
+
+define("PRINT_ERRORS",$PRINT_ERRORS);
+define("PRINT_WARNINGS",$PRINT_WARNINGS);
+
+// Version
+define("TXTDBAPI_VERSION","0.3.1-Beta-01");
+
+// General
+define("NOT_FOUND",-1);
+
+// File parsing
+define("COLUMN_SEPARATOR_CHAR",'#'); // Char which seperates the columns
in the table file
+ // This MUST be a sigle character and NOR a string
+define("TABLE_FILE_ESCAPE_CHAR",'%'); // Char to Escape
COLUMN_SEPARATOR_CHAR in the Table Files
+define("TABLE_FILE_OPEN_MODE",'b'); // "b" or ""
+
+// Timeouts
+define("OPEN_TIMEOUT",10); // Timeout in seconds to try opening a still
locked Table
+define("LOCK_TIMEOUT",10); // Timeout in seconds to try locking a still
locked Table
+define("LOCKFILE_TIMEOUT",30); // Timeout for the maximum time a lockfile
can exist
+
+// Predefined Databases
+define("ROOT_DATABASE","");
+
+// Order Types
+define("ORDER_ASC",1);
+define("ORDER_DESC",2);
+
+// Join Types
+define("JOIN_INNER",1);
+define("JOIN_LEFT",2);
+define("JOIN_RIGHT",3);
+
+// Row Flags
+define("FLAG_KEEP",0x1);
+
+// Column Types
+define("COL_TYPE_INC","inc");
+define("COL_TYPE_INT","int");
+define("COL_TYPE_STRING","str");
+
+// Column Function Types
+define("COL_FUNC_TYPE_SINGLEREC",1);
+define("COL_FUNC_TYPE_GROUPING",2);
+
+// File Extensions
+define("TABLE_FILE_EXT",".txt");
+define("LOCK_FILE_EXT",".lock");
+
+
+?>
Added: branches/dev/grid/tests/data/txtdb/database.php
==============================================================================
--- (empty file)
+++ branches/dev/grid/tests/data/txtdb/database.php Tue Nov 25 06:55:32 2008
@@ -0,0 +1,1136 @@
+<?php
+/**********************************************************************
+ Php Textfile DB API
+ Copyright 2005 by c-worker.ch
+ http://www.c-worker.ch
+***********************************************************************/
+/**********************************************************************
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+Redistributions of source code must retain the above copyright notice, this
+list of conditions and the following disclaimer.
+Redistributions in binary form must reproduce the above copyright notice,
+this list of conditions and the following disclaimer in the documentation
+and/or other materials provided with the distribution.
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+***********************************************************************/
+
+include_once(API_HOME_DIR . "const.php");
+include_once(API_HOME_DIR . "util.php");
+include_once(API_HOME_DIR . "resultset.php");
+include_once(API_HOME_DIR . "sql.php");
+include_once(API_HOME_DIR . "expression.php");
+
+/**********************************************************************
+ Database
+***********************************************************************/
+
+// Represents a Database and has functions to execute Sql-Queries on it
+class Database {
+ var $dbFolder;
+ var $lastInsertId=0;
+
+ var $lastErrorMsgs=array();
+
+ /***********************************
+ Constructor
+ ************************************/
+
+ function Database($dbFolder="defaultDb/") {
+ $this->dbFolder= DB_DIR . $dbFolder;
+ if(last_char($this->dbFolder) != "/")
+ $this->dbFolder .= "/";
+ }
+
+
+ /***********************************
+ Insert Id Functions
+ ************************************/
+ function updateLastInsertId($resultSet) {
+ $resultSet->end();
+ for($i=0;$i<count($resultSet->colTypes);++$i) {
+ if($resultSet->colTypes[$i]==COL_TYPE_INC) {
+ $this->lastInsertId=$resultSet->getCurrentValueByNr($i);
+ debug_print("Setting lastInsertId to " . $this->lastInsertId );
+ return;
+ }
+ }
+ // if no inc column exists set lastInsertId to 0
+ $this->lastInsertId=0;
+ debug_print("Setting lastInsertId to " . $this->lastInsertId );
+ }
+
+ function getLastInsertId() {
+ return $this->lastInsertId;
+ }
+
+ /***********************************
+ Table open/close Functions
+ ************************************/
+
+ // Does open a Table for writing
+ function openTableWrite($tableName) {
+ debug_print("openTableWrite
");
+ $filename=$this->dbFolder . $tableName . TABLE_FILE_EXT;
+
+ $fp=fopen ($filename, "r+".TABLE_FILE_OPEN_MODE);
+ if (!$fp) return 0;
+
+ while(!flock($fp, LOCK_EX)) {
+ usleep(50);
+ }
+
+ return $fp;
+ }
+
+ // same rules as for openTableWrite()
+ // does open a table for appending
+ function openTableAppend($tableName) {
+ debug_print("openTableAppend
");
+ $filename=$this->dbFolder . $tableName . TABLE_FILE_EXT;
+
+ $fp=fopen ($filename, "a".TABLE_FILE_OPEN_MODE);
+ if (!$fp) return 0;
+
+ while(!flock($fp, LOCK_EX)) {
+ usleep(50);
+ }
+
+ return $fp;
+ }
+
+ // Opens a Table for reading
+ // This function is used by SELECT
+ // returns a FilePointer or false
+ function openTableRead($tableName) {
+ debug_print("openTableRead
");
+ $filename=$this->dbFolder . $tableName . TABLE_FILE_EXT;
+ debug_print("Table $tableName opened (READ)
");
+
+ if(!file_exists($filename)) return 0;
+
+ $fp=fopen ($filename, "r".TABLE_FILE_OPEN_MODE);
+ if (!$fp) return 0;
+
+ while(!flock($fp, LOCK_SH)) {
+ usleep(50);
+ }
+
+ return $fp;
+ }
+
+ // Closes a Table
+ function closeTable($fp) {
+ debug_print("Table $fp closed
");
+ while(!flock($fp, LOCK_UN)) {
+ usleep(50);
+ }
+ return fclose($fp);
+ }
+
+
+ /***********************************
+ Table read/write Functions
+ ************************************/
+
+ // Reads a Table into a ResultSet
+ // Returns a ResultSet or null (the function opens an closes the file
itself)
+ function readTable($tableName) {
+ debug_print("readTable
");
+ $parser= new ResultSetParser();
+ if(!($fd=$this->openTableRead($tableName))) {
+ print_error_msg("readTable(): Cannot open Table $tableName");
+ return null;
+ }
+ $rs=$parser->parseResultSetFromFile($fd);
+ $this->closeTable($fd);
+ return $rs;
+ }
+
+ // Reads a Table into a ResultSet
+ // But only the column names and types + the last record
+ // thats usefull for appending record
+ function readTableForAppend($tableName) {
+ debug_print("readTableForAppend
");
+ $parser= new ResultSetParser();
+ if(!($fd=$this->openTableRead($tableName))) {
+ print_error_msg("readTableForAppend(): Cannot open Table
$tableName");
+ return null;
+ }
+ $rs=$parser->parseResultSetFromFileForAppend($fd);
+ $this->closeTable($fd);
+ return $rs;
+ }
+
+
+ // writes the table by using the FilePointer $fd
+ // $fd has to be opened an closed by the caller
+ function writeTable($fd, $resultSet) {
+ debug_print("writeTable
");
+ $parser= new ResultSetParser();
+ return $parser->parseResultSetIntoFile($fd, $resultSet);
+ }
+
+ // appends to the table by using the FilePointer $fd
+ // $fd has to be opened an closed by the caller
+ function appendToTable($fd, $resultSet,$recordCount) {
+ debug_print("appendToTable
");
+ $parser= new ResultSetParser();
+ while($resultSet->getRowCount()>$recordCount) {
+ $resultSet->reset();
+ $resultSet->next();
+ $resultSet->deleteCurrentRow();
+ }
+ return $parser->parseResultSetIntoFileAppend($fd, $resultSet);
+ }
+
+
+
+
+ /***********************************
+ Query dispatcher
+ ************************************/
+
+ // $sql_query_str is an unparsed SQL Query String
+ // Return Values:
+ // SELECT Queries: Returns a ResultSet Object or false
+ // CREATE TABLE: Returns true or false
+ // All other types: Returns the number of rows affected
+ function executeQuery($sql_query_str) {
+ set_error_handler("txtdbapi_error_handler");
+ txtdbapi_clear_errors();
+
+ debug_printb("[executeQuery] Query: $sql_query_str
");
+
+ // Parse Query
+ $start=getmicrotime();
+ $sqlParser=new SqlParser($sql_query_str);
+ $sqlQuery=$sqlParser->parseSqlQuery();
+ debug_print("parseSqlQuery: " . (getmicrotime() - $start) . " seconds
elapsed
");
+
+
+
+ // free $sqlParser
+ unset($sqlParser);
+ $sqlParser="";
+
+ // Test Query
+ if((!$sqlQuery) || (!$sqlQuery->test())) {
+ restore_error_handler();
+ return false;
+ }
+
+ $start=getmicrotime();
+
+ debug_printb("[executeQuery] Parsed Query:
");
+ if(TXTDBAPI_DEBUG) {
+ $sqlQuery->dump();
+ }
+
+
+ // Dispatch
+ switch($sqlQuery->type) {
+ case "SELECT":
+ $rc=$this->executeSelectQuery($sqlQuery);
+ break;
+ case "INSERT":
+ $rc=$this->executeInsertQuery($sqlQuery);
+ break;
+ case "DELETE":
+ $rc=$this->executeDeleteQuery($sqlQuery);
+ break;
+ case "UPDATE":
+ $rc=$this->executeUpdateQuery($sqlQuery);
+ break;
+ case "CREATE TABLE":
+ $rc=$this->executeCreateTableQuery($sqlQuery);
+ break;
+ case "DROP TABLE":
+ $rc=$this->executeDropTableQuery($sqlQuery);
+ break;
+ case "CREATE DATABASE":
+ $rc=$this->executeCreateDatabaseQuery($sqlQuery);
+ break;
+ case "DROP DATABASE":
+ $rc=$this->executeDropDatabaseQuery($sqlQuery);
+ break;
+ case "LIST TABLES":
+ $rc=$this->executeListTablesQuery($sqlQuery);
+ break;
+ default:
+ print_error_msg("Invalid or unsupported Query Type: " .
$sqlQuery->type);
+ restore_error_handler();
+ return false;
+ }
+
+ if(is_false($rc)) {
+ print_error_msg("Query '" . $sql_query_str . "' failed");
+ }
+
+ debug_printb("[executeQuery] Query execution done: " . (getmicrotime() -
$start) . " seconds elapsed
");
+ restore_error_handler();
+ return $rc;
+ }
+
+
+ /***********************************
+ Delete Query
+ ************************************/
+
+ function executeDeleteQuery(&$sqlQuery) {
+
+ // Read Table
+ $rs=$this->readTable($sqlQuery->tables[0]);
+ if(!$rs) {
+ print_error_msg("Reading the Table " . $sqlQuery->tables[0] . "
failed");
+ return false;
+ }
+
+ $rowsAffected=0;
+
+ if(!$sqlQuery->where_expr || $sqlQuery->where_expr=="") {
+ $rowsAffected=$rs->getRowCount();
+ $rs->deleteAllRows();
+ } else {
+ // set row ids
+ $rId=-1;
+ $rs->reset();
+ while($rs->next())
+ $rs->setCurrentRowId(++$rId);
+ $rs->reset();
+
+ // calc current column count
+ $colCount=count($rs->colNames);
+
+ // generate additional columns from the WHERE-expression
+ $rs->generateAdditionalColumnsFromWhereExpr($sqlQuery->where_expr);
+
+ // execute the new single-rec functions
+ $rs->executeSingleRecFuncs();
+
+ // apply WHERE Expression
+ $ep=new ExpressionParser();
+ $et=$ep->getExpressionTree($sqlQuery->where_expr);
+ $rsFiltered=$et->getFilteredResultSet($rs);
+
+ if(!$rsFiltered)
+ return 0;
+
+ // Delete rows..
+ $rsFiltered->reset();
+ while($rsFiltered->next()) {
+