Nov 042014
 
Article PHP

The default PHP interpreter in most Linux distros includes de Database Abstraction Layer (DBA) extension. DBA implements the functionality required to work with dbm type databases, such as BerkeleyDB. This extension does not  implement all the functionality available in libraries specific for each of the databases types, but it may be enough for most use cases.

This post explains how to use DBA in a PHP script, to carry out the most common operations on a BDB database .

 1. Opening the database

The examples in this post will use a sample database “test.db”. To work with it, the first step is obtaining a handle with a call to dba_open():

$db = dba_open ( "test.db" , "r" , "db4" );
  • The first argument is the DB filename
  • The second argument specifies the desired access mode:
    • “r” – open the database read-only
    • “w” – open the database in read-write mode.
    • “c” – Create the database if it does not exist. Open the database in read-write mode.
    • “n” – Create the database if it does not exist, truncate (empty) the database otherwise. Open the database in read-write mode.
  • The third argument specifies the desired database handler (db4 to open a BerkeleyDB file).

BerkeleyDB database are created in BTree mode. Hash, Queue and other BDB modes are not supported by the DBA extension.

Besides, for the call to dba_open() to succeed, the requested db handler must have been compiled into the PHP interpreter. The dba_handlers() function can be used to get an array of the available handlers in the PHP interpreter were it is run:

echo "Available DBA handlers:\n";
$handlers = dba_handlers();
for ($i = 0; $i < count($handlers); $i++) {
    echo $handlers[$i] . "\n";
}

The PHP interpreter installed in our Debian wheezy system was installed from the debian repository with apt-get. Using that interpreter, the output that results from running the above code is:

Available DBA handlers:
cdb
cdb_make
db4
inifile
flatfile
qdbm

As we can see, our PHP interpreter includes the db4 handler required to open BerkeleyDB files.

2. Reading the value of a given key

A Berkeley database is basically a collection of pairs (key,value), optimized by indexing the keys. The value assigned to a given key $key can be retrieved with a call to dba_fetch():

$value = dba_fetch($key,$db);

3. Inserting new records, updating and removing records

dba_insert() can be used to insert a new record in the database:

dba_insert($key,$value,$db);

This function returns true if the insertion succeeds, and false if there is an error, as for instance if the key to be added already exists in the database.

Updating the value of a record is performed with a call to dba_replace():

dba_replace($key,value,$db);

Note: If the key passed as argument in the call to dba_replace() did not exist previously, a new record is inserted.

Finally, a record can be deleted from the database with a call to dba_delete():

dba_delete($key,$db);

4. check the existence of a key

The dba_exists() function returns true true if the key passed as argument exists in the database:

if (dba_exists($key,$db)) echo "Key " . $clave . " exists!\n";

5. Traversing the content of the database

To traverse the content of the database, the first key is retrieved with a call to dba_firstkey(), and then all the other keys are retrieved in a loop that includes a call to dba_nextkey(), until it returns false.Indeed, the value for each key retrieved can be obtained with a call to dba_fetch:

$key = dba_firstkey($db);
while ($key != false) {
    echo "Key: " . $key . ", value: " . dba_fetch($key,$db) . "\n";
    $key = dba_nextkey($db);
}

References

Index of posts related to PHP programming

 Posted by at 6:43 pm

 Leave a Reply

(required)

(required)