Click to See Complete Forum and Search --> : Flat File DB Class


ShawnK
06-27-2007, 03:18 AM
So I'm working on a class that will allow you to use flatfiles(aka textfiles) for databases....this is what i have so far...

currently you can create an empty file, or a file with just fields, or a file with fields and data AND you can delete files...it doesn't sound like a lot i just want to check this so far before I continue with reading and searching.

EDIT: i added the ability to get all data from a file, it returns an assoc array


<?php

class kurtz_ffdb
{
private $sep = "|"; // the seperator between the field and data
private $db_ext;
private $data_dir;
private $handle;
private $output;

// params:
// $dataDir - the directory where files are stored
// $dbExt - the extension that all db files will have
public function __construct($dataDir='data/', $dbExt='.ffdb')
{
if(!is_dir($dataDir)){ $this->error("Data Directory Not Found!"); }
$this->db_ext = $dbExt;
$this->data_dir = $dataDir;
}

// params:
// $name - the name of the new db file
// $fields - array - the name of every field you wish to create
// $data - array - the data corresponding to above fields
public function create($name, $fields=array(), $data=array())
{
if($this->data_file_exists($name))
{
$this->error("Cannot Create File, (".$name.$this->db_ext.") Already Exists!");
}
else
{
$this->handle = fopen($this->data_dir.$name.$this->db_ext, "w+") or $this->error("Data File Couldn't Be Created!");
if(!empty($fields) && is_array($fields))
{
if(!empty($data) && is_array($data))
{
if(count($fields) !== count($data)){ $this->error("Invalid Data/Field Array!"); }
else
{
$write_data = array_combine($fields, $data);
foreach($write_data as $key => $value)
{
fwrite($this->handle, $key.$this->sep.$value."\r\n") or $this->error("Cannot Write To File!");;
}
}
}
else
{
fwrite($this->handle, implode($this->sep."\r\n", $fields).$this->sep) or $this->error("Cannot Write To File!");
}
}
}
unset($this->handle);
}

// params:
// $name - the name of field to delete
public function del($name)
{
if(!$this->data_file_exists($name)){ $this->error("File Not Found!"); }
unlink($this->data_dir.$name.$this->db_ext) or $this->error("File Could Not Be Deleted!");
}

// params:
// $name - the name of the data file to get info from
public function get_all($name)
{
$this->handle = fopen($this->data_dir.$name.$this->db_ext, "r");
$read_data = fread($this->handle, filesize($this->data_dir.$name.$this->db_ext));
$rows = explode("\r\n", $read_data);
foreach($rows as $row)
{
$split = explode($this->sep, $row);
$fields[] = $split[0];
$data[] = $split[1];
}
$this->output = array_combine($fields, $data);
$last_row = count($this->output);
unset($this->output[$last_row]);
return $this->output;
}

// INTERNAL FUNC
// params:
// $name - name of data file to check existance
private function data_file_exists($name)
{
if(file_exists($this->data_dir.$name.$this->db_ext)){ return true; }
else{ return false; }
}

// INTERNAL FUNC
// params:
// $error_txt - the text to display in error
// $error_prefix - the prefix to the error
private function error($error_txt, $error_prefix="<b>Error: </b>")
{
die($error_prefix.$error_txt);
}
}

$ffdb = new kurtz_ffdb();
$db_fields = array("id", "name", "email");
$db_data = array("111", "shawn", "shawnkurtz@gmail.com");
$users = $ffdb->get_all("users");
foreach($users as $key => $value)
{
echo ucfirst($key).": ". $value ."<br />";
}
?>

MarkR
06-30-2007, 08:18 PM
Hmm, a few questions:

- Why bother at all, there are plenty of existing ways of storing data
- What scheme do you have to escape your separator character appearing in data?
- What about newlines in data?
- Have you considered race conditions for update when several processes are running at once? Do you understand how to use file locking?

Database servers have already figured out answers to all these problems. Also, using named columns allows for easier refactoring.

Not to mention the fact that databases allow you to find things faster by using indexes. And their code is (probably) better tested.

Mark

Weedpacket
06-30-2007, 11:53 PM
SQLite

ShawnK
07-01-2007, 01:59 AM
the simple answer to all of the above it boredom...i just got bored and started on it....

rulian
07-25-2007, 01:26 PM
believe me dont try it, I was hashing out the exact same thing, but for actual use, and file reading with PHP simply isnt a practical way of storing information that you need to accesss.

specially sing SQLite and even MYSQL can do it, and very well.
MYSQL read and dumped a 1 million row file in surprisingly speedy fashion with
SELECT ..... INTO OUTFILE command

It's fun for a while, but when you actually need it to do something powerful, it doesnt compare