...

View Full Version : Feedback on my script (text database)



Michiel
12-05-2003, 02:18 PM
Hi guys,

I've been working on a simple text database that I can use when I want to store (and edit) some data, where a MySql would be a bit over the top. Currently the script is in the development phase, but before I continue I'd like to ask you to take a look at the script. It would be very usefull to receive your feedback on the script: where do I go wrong in terms of security, speed, efficiency, style etc.?? What can be improved?

Before reviewing the script, please note that this is the very first version of the script. I have not added an error-reporting system and there may be some inconsistant variable names.

Thanx in advance for your feedback!

Cheers, Michiel



class TextDatabase {

var $_aConfig = array();
var $_sDatabase;
var $_aTable = array();
var $_aResult = array();

function MilesTextDatabase($config_file) {
//$this->_aConfig = parse_ini_file($config_file);
// Just for now I've added the configuration directly
// into the script.
$this->_aConfig['path'] = 'db/';
$this->_aConfig['extension'] = '.db.php';
$this->_aConfig['delimiter'] = '#';
}

function Connect($database) {
$this->_sDatabase = $database;
}

function Select($table, $columns, $condition) {
// Currently the script can only select on one condition!
$_aTable = $this->_ReadTable($table);
$condition = explode(' ', $condition);
list($field, $parameter, $value) = $condition;
$columns = explode(',', $columns);
$_aRows = $this->_SelectRows($_aTable, $field, $parameter, $value);
$_aResult = $this->_FetchArray($_aTable, $_aRows, $columns);
return $_aResult;
}

function Insert($table, $columns, $values) {
$columns = explode(',', $columns);
$values = explode(',', $values);
$_aTable = $this->_ReadTable($table);
$row = count($_aTable) + 1;
$_aTable = $this->_Insert($_aTable, $row, $columns, $values);
$this->_SaveTable($table, $_aTable);
}

function Update($table, $columns, $values, $condition) {
$_aTable = $this->_ReadTable($table);
$condition = explode(' ', $condition);
list($field, $parameter, $value) = $condition;
$_aRows = $this->_SelectRows($_aTable, $field, $parameter, $value);

$columns = explode(',', $columns);
$values = explode(',', $values);
$_aTable = $this->_ReadTable($table);
foreach($_aRows as $row) {
$_aTable = $this->_Insert($_aTable, $row, $columns, $values);
}
$this->_SaveTable($table, $_aTable);
}

function Delete($table, $condition) {
$_aTable = $this->_ReadTable($table);
$condition = explode(' ', $condition);
list($field, $parameter, $value) = $condition;
$_aRows = $this->_SelectRows($_aTable, $field, $parameter, $value);
foreach($_aRows as $row) {
unset($_aTable[$row]);
}
$_aTable = array_values($_aTable);
$this->_SaveTable($table, $_aTable);
}

function CreateDatabase($database) {
$_sDir = $this->_aConfig['path'].$database;
if(is_dir($_sDir)) {
return false;
}
else {
mkdir($_sDir);
return true;
}
}

function CreateTable($table, $columns) {
$columns = explode(',', $columns);
$_sDatafile = $this->_aConfig['path'].$this->_sDatabase.'/'.$table.$this->_aConfig['extension'];
if(file_exists($_sDatafile)) return false;
else {
$write = "NO ACCESS\r\n";
foreach($columns as $value) {
$newline .= trim($value).$this->_aConfig['delimiter'];
}
$newline = rtrim($newline, $this->_aConfig['delimiter']);
$write .= $newline;
$write = rtrim($write);
$handle = fopen($_sDatafile, 'w');
fwrite($handle, $write);
fclose($handle);
return true;
}
}

function DropDatabase() {
$handle = opendir($this->_aConfig['path'].$this->_sDatabase);
while(false !== ($file = readdir($handle))) {
if($file != '.' && $file != '..') {
$this->DropTable(rtrim($file, $this->_aConfig['extension']));
}
}
rmdir($this->_aConfig['path'].$this->_sDatabase);
}

function DropTable($table) {
$_sDatafile = $this->_aConfig['path'].$this->_sDatabase.'/'.$table.$this->_aConfig['extension'];
unlink($_sDatafile);
}

function Dump($table) {
print "<pre>";
print_r($_aTable = $this->_ReadTable($table));
print "</pre>";
}

function _ReadTable($table) {
$_sDatafile = $this->_aConfig['path'].$this->_sDatabase.'/'.$table.$this->_aConfig['extension'];
if(!file_exists($_sDatafile)) {
print 'Error';
}
else {
$_aRows = file($_sDatafile);
$_aColumnNames = explode($this->_aConfig['delimiter'], trim($_aRows[1]));
for($i = 2; $i < count($_aRows); $i++) {
$_aRow = explode($this->_aConfig['delimiter'], trim($_aRows[$i]));
$ii = 0;
foreach($_aColumnNames as $_sValue) {
$_sValue = strtolower($_sValue);
$_sValue = trim($_sValue);
$_aTable[$i-2][$_sValue] = trim($_aRow[$ii]);
$ii++;
}
}
return $_aTable;
}
}

function _SaveTable($table, $_aTable) {

$_aColumnNames = array_keys($_aTable[0]);
$write = "NO ACCES\r\n";
foreach($_aColumnNames as $value) {
$write .= $value.$this->_aConfig['delimiter'];
}

$write = trim($write, $this->_aConfig['delimiter']);
$write .= "\r\n";

foreach($_aTable as $value) {
$newline = "";
foreach($value as $vvalue) {
$newline .= trim($vvalue).$this->_aConfig['delimiter'];
}
$newline = trim($newline, $this->_aConfig['delimiter']);
$newline .= "\r\n";
$write .= $newline;

}

$write = rtrim($write, "\r\n");
$_sDatafile = $this->_aConfig['path'].$this->_sDatabase.'/'.$table.$this->_aConfig['extension'];

$handle = fopen($_sDatafile, 'w');
fwrite($handle, $write);
fclose($handle);
}

function _SelectRows($array, $field, $parameter, $value) {
foreach ($array as $key=>$info) {
eval('if($info[$field] '.$parameter.' $value) {$matches[] = $key;}');
}
return $matches;
}

function _FetchArray($array, $rows, $columns) {
$i = 0;
foreach($rows as $row) {
$i++;
foreach($columns as $column) {
$column = trim($column);
$result[$i][$column] = $array[$row][$column];
}
}
return $result;
}

function _Insert($_aTable, $row, $columns, $values) {
foreach($columns as $key=>$value) {
$value = trim($value); // temporary fix!
$_aTable[$row][$value] = $values[$key];
}
return $_aTable;
}
}

// initiate new object
$object = new TextDatabase('config.ini.php');
// create a database named 'test'
$object->CreateDatabase('test');
// connect to the database 'test'
$object->Connect('test');
// create a new table (members) in the current database (test)
$object->CreateTable('members', 'name, age, country');
// select name and age of all the people older than 18.
$object->Select('members', 'name, age', 'age < 18'));
// insert a new member
$object->Insert('members', 'name, age, country', 'john, 22, The Netherlands');
// Delete all the people from the table that are exactly 19 years old.
$object->Delete('members', 'leeftijd == 19');
// Dump the table memers in the current database
$object->Dump('members');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum