Thursday, October 27, 2011

So a few weeks ago, I got a bit frustrated with MySQL. It just wasn't performing how it should with a small amount of data. It didn't help that I was just storing text that referenced several different data types that were stored on the server.

So I thought to myself, "Self: wouldn't it be cool if there was a database out there that did [fill in the blank].". This is what I came up with.

What it is

This is a data storage system that shuns traditional SQL syntax and setup frustration. This is ideal for small websites with not a whole lot of data.

It's a PHP library that stores data in flat files. But more on that later

What it isn't

It isn't meant for massive amounts of data. All features as of yet are experimental.

How it works

Instead of being a relational database, where there are tables that can be related to each other through data relationships and keys and other stuff, the data is stored in flat files in a file structure determined by the data type being stored. The data is indexed in a file called mft.pdb (Master File Table).  This file also stores metadata about the various files. How the different mime types are handled and stored in the MFT and how the attributes are named are stored in a file called data_defs.pdb.

An example of data_defs.pdb:

Each mime type has it's own line. The only things that are required to remain the same on every line are: [data_type]::source::type. Why double the data type? I don't know. It's a work-around for a problem I found. Anything after the first three fields is fair game to be changed.

An example of mft.pdb:
1234.txt::text/1234.txt::text::Blog Lady 1234
1234.txt::text/1234.txt::text::Blog Post 1234
blue.jpg::image/blue.jpg::image::image/blue_thumb.jpg::Blue::This is a lady and a bridge.::example
chad.jpg::image/chad.jpg::image::image/chad_thumb.jpg::Chad::Chad is about to be in a world of hurt::example
lady.jpg::image/lady.jpg::image::image/lady_thumb.jpeg::Lady and the Tree::This is a lady and a tree.::example
wind.jpg::image/wind.jpg::image::image/wind_thumb.jpg::Wind is Hilarious::The wind told a joke. The lady laughed::example

Getting Started

Copy all from the zip file below to the root of your site directory. "include('phpdb_functions.php');" in the pages you wish to use PHPDB. Setup complete.

Adding A Text File

There are many data types that are handled the same as just plain text. Html, plain text and where the mime type can't be determined are a few examples.

An example of an add of a simple text file: db_add('text/1234.txt', 'Blog Post 1234');
echo $debug; 

"1234.txt::text/1234.txt::text::Blog Post 1234" is added to mft.pdb. The $debug echo is optional, but in case things go awry, it's there.

Adding an Image File

There is an optional $params variable that can be passed to the db_add function where you can add MFT field values specified in the data_defs.pdb in the form of an array.
For example: $params['title'] = 'Blue';
$params['gallery'] = 'example';
$params['description'] = 'This is a lady and a bridge.';
$params['thumb'] = 'image/blue_thumb.jpg';
db_add('image/blue.jpg', 'Blue', $params);
echo $debug;

This adds a line "blue.jpg::image/blue.jpg::image::image/blue_thumb.jpg::Blue::This is a lady and a bridge.::example" to the MFT.


Select Single: $image = db_select_one('blue', 'image');
echo "<a href='".$image['source']."'><img src='".$image['thumb']."'></a><br>".$image['title']."";

Select Multi: $images = db_select_many("example", 'image');

foreach($images as $image){
echo "<div style='display:block; float:left;text-align:center;'>
        <a href='".$image['source']."'><img src='".$image['thumb']."'></a><br>




Post a Comment

Subscribe to RSS Feed Follow me on Twitter!