Re: Off-line databases
| From: | James Cameron | Date: | Thu, 14 Nov 2002 21:44:00 +0000 |
| Subject: | Re: Off-line databases | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-2630@lists.php.net to get a copy of this message | ||
On Thu, Nov 14, 2002 at 01:21:17PM +0200, Andrew Smith wrote:
> What are some ideas for a database to house data on a client machine? I
> don't want to expect uses of my app to install MySQL or some other server.
> The installation should be as simple as possible (ie none!).
One of the constant design problems with using any data storage method
is the translation between the data structures used by the program, and
the data structures in the storage.
For example, if you decide to use a plain text file, you often have to
write a complex function that will translate your in-memory variables
and arrays into text before writing the file. Reading involves the
translation and validation of text back into those variables and arrays.
If you use a key/value database, like dba_* functions, then you are
constrained by that mode of access, and you have no easy access except
through the program. A black box approach.
If you use an SQL engine, you have to translate everything into tables
and rows, which forces particular designs onto the application. You
also have to make sure the SQL engine is available.
Many of my PHP programs use serialize() and unserialize(). These
functions do the translation for you. If many variables and arrays
need to be stored, then they can be assembled into a big array.
# store array into file
function array_to_file ($file, $array) {
$fp = fopen($file, 'w');
fwrite($fp, serialize($array));
fclose($fp);
}
$array['version'] = $version;
$array['username'] = $username;
$array['another'] = $anotherarray;
array_to_file('file.dat', $array);
If I need indexed access to data sets, I use the filesystem's file and
directory functions. I create one file per entity, in serialize()
format.
Here is some example code from my internet cafe prepaid accounting
system, ppa. <http://quozl.netrek.org/ppa> This
function is the fully
fleshed out version of a per-entity write, with locking, error checking,
and single-point commit (rename). After that is the reader.
# write an account from array to file, with interlock
function account_write ($account) {
# obtain interlock
$lock = fopen(PATH_LOCK, 'a+');
if (!flock($lock, 2)) {
echo 'Failed to lock database';
fclose($lock);
return 0;
}
# open the file
if (!($fp = fopen(PATH_NEW, 'w'))) {
echo 'Failed to open database';
fclose($lock);
return 0;
}
# write the array to the file
if (!fwrite($fp,serialize($account))) {
echo 'Failed to write to database';
fclose($fp);
fclose($lock);
return 0;
}
# close the file
if (!fclose($fp)) {
echo 'Failed to close database';
fclose($lock);
return 0;
}
# derive the file name
$file = account_to_file($account['name']);
# rename the new file into place
if (!rename(PATH_NEW, $file)) {
echo 'Failed to move database file into place';
fclose($lock);
return 0;
}
# release the lock and return success
fclose($lock);
return 1;
}
# read an account from file into array
function account_read ($name) {
# derive the file name
$file = account_to_file($name);
# return zero if no file
if (!is_file($file)) { return 0; }
# open the file
if (!($fp = fopen($file, 'r'))) {
echo 'Failed to open database';
return 0;
}
# read the file
$account = unserialize(fread($fp, filesize($file)));
# close the file
fclose($fp);
return $account;
}
--
James Cameron mailto:quozl@us.netrek.org http://quozl.netrek.org/