Recommend this page to a friend! |
Nanobase is a fast, lightweight relational database management class for PHP, providing a simple data management solution for tasks where performance, memory efficiency, portability and flexibility are the priorities.
Please note: This is a database management class that focuses on datapoints, not a replacement for a full database management system!
Nanobase uses plain text files (with a .db extension) to store data in key/value pairs. Each file contains one column of data in fixed-width format.
Example of a file with three entries:
00000001|John________________
00000002|Jane________________
00000003|Andrew-Saint-Germain
The 8-digit key on the left of each row links entries across files to form a unique record.
Data are stored in fixed-width format, using a pipe to separate the key from the value and an underscore to pad the value when needed. Fixed-width format makes the data positions in the file predictable which massively increases search performance (because we know exactly where all entries begin, we can quickly move the file pointer to any entry in the file).
Nanobase can search a typical table with 1,000,000 records across four columns in about three seconds.
Data are read and written using the SplFileObject
class from the Standard PHP Library. SplFileObject
lets Nanobase iterate its files without having to first load all the file contents into server memory. This makes memory overhead trivial and allows large amounts of data to be accessed without having to worry about memory overload.
Nanobase assets are nothing more than folders and text files, so relocating, duplicating and backing up are dead simple.
You can add a new column at any time and Nanobase will integrate it seamlessly.
Columns are not typed by default. You can add any UTF-8 character to any column.
An entry is converted to a list (array) automatically when you append an item.
Before any write operation, all columns are locked using PHP flock
to avoid any possible (even if very unlikely) collisions.
Reserved and potentially unsafe characters are prevented from writing.
// instantiate the Nanobase class and make a new table called "users" with four columns
// argument 2 in "makeColumn" sets the maximum character length for column entries
$db = new Nanobase('path/to/database', 'users');
$db->makeTable();
$db->makeColumn('userId', 8);
$db->makeColumn('firstName', 20);
$db->makeColumn('surname', 20);
$db->makeColumn('email', 50);
// add John as a user
$db->make([
'userId' => '10000001',
'email' => 'example@example.com',
'firstName' => 'John',
'surname' => 'Smith'
]);
// search for John in the "firstName" column and display the first record found
$db->search('john', ['firstName']);
$result = $db->read();
print_r($result);
Result
Array
(
[userId] => 10000001
[firstName] => John
[surname] => Smith
[email] => example@example.com
)
More detailed comments are available in the Nanobase class.
// return a short JSON report, or bool on exception
$db->throw(bool $isReport = true);
// make a new table using constructor argument 2 as the name
$db->makeTable();
// make a new column in the table with max. character length (hard limit is 100)
$db->makeColumn(string $columnName, int $capacity = null);
// make a new record with each column name and value as a key/value pair
$db->make(array $newEntries);
$db->count(
string $term = null, // phrase to search for (search for all by default)
array $columns = [], // columns to search through (search through all by default)
bool $isWhole = false, // match search phrase to entire column entry, or partial match
bool $isCase = false // case-sensitive, or case-insensitive phrase match
);
$db->search(
string $term = null, // phrase to search for (search for all by default)
array $columns = [], // columns to search through (search through all by default)
int $limit = 1, // max. number of records to find (hard limit is 100)
bool $isWhole = false, // match search phrase to entire column entry, or partial match
bool $isCase = false // case-sensitive, or case-insensitive phrase match
);
// overwrite the found column entries, else create a new entry
$db->update(string $newEntry, array $operationColumns = null);
// convert the found column entries to a list and append a new item
$db->attach(string $newItem, array $operationColumns = null);
// remove a list item from the found column entries
$db->detach(string $detachItem, array $operationColumns = null);
// display the first record found
$result = $db->read();
// display all records found
$result = $db->list();
Download src/Nanobase.php
and drop it into your project. It's that simple.
Use this PHP code for a quick demo. The first argument ('path/to/sample') should point to the folder 'sample' in this repo.
$db = new Nanobase('path/to/sample', 'cities');
$db->search('cape town');
$result = $db->read();
print_r($result);
Feel free to mail me on sheldonkennedy@gmail.com. I'll respond as soon as I can.
Classes of Sheldon Kennedy | > | How to Create and Manage a PHP Database File-based using Nanobase | > | Download .zip .tar.gz | > | Support forum (1) | > | Blog | > | Latest changes |
|
Groups | Applications | Files |
Groups |
PHP 5 | Classes using PHP 5 specific features | View top rated classes |
Databases | Database management, accessing and searching | View top rated classes |
Files and Folders | Listing, accessing and manipulating files and folders | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files | / | sample | / | cities |
File | Role | Description |
---|---|---|
city.db | Data | Auxiliary data |
digest.json | Data | Auxiliary data |
iso2.db | Data | Auxiliary data |
iso3.db | Data | Auxiliary data |
Download all files: nanobase.tar.gz nanobase.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|
Files | / | sample | / | cities |
File | Role | Description |
---|---|---|
city.db | Data | Auxiliary data |
digest.json | Data | Auxiliary data |
iso2.db | Data | Auxiliary data |
iso3.db | Data | Auxiliary data |
Download all files: nanobase.tar.gz nanobase.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|