Skip to content

andkom/php-berkeley-db

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Berkeley DB

Simple Berkeley DB wrapper for PHP.

Installation

Download Berkeley DB source:

wget 'http://download.oracle.com/berkeley-db/db-x.x.x.tar.gz'
tar -xzvf db-x.x.x.tar.gz

Build Berkeley DB libraries and headers:

cd db-x.x.x/build_unix/
../dist/configure --enable-cxx
make
make install

Install php_db4 extension for "phpdb4" adapter (better):

Build db4 extension:

cd ../lang/php4_db
phpize
./configure --with-db4=/usr/local/BerkeleyDB.x.x
make
make install

Add db4 extension to php.ini:

extension=db4.so

Or install db4 DBA handler for "dba" adapter:

Build PHP with db4 support:

./configure --with-db4=/usr/local/BerkeleyDB.x.x
make
sudo make install

Install composer package:

composer install andkom/php-berkeley-db

Usage

Create instance:

use AndKom\BerkeleyDb\Adapter\AdapterFactory;

$adapter = AdapterFactory::create(); // use first available adapter
$adapter = AdapterFactory::create("phpdb4"); // use phpdb4 adapter
$adapter = AdapterFactory::create("dba"); // use dba adapter

Open database for reading:

$adapter->open("filename", "r");

Open database with optional database name:

$adapter->open("filename", "r", "main");

Open database for writing:

$adapter->open("filename", "w");

Close database:

$adapter->close();

Sync changes to database:

$adapter->sync();

Read key value:

$value = $adapter->get("key");

Write key value:

$adapter->put("key", "value");

Delete key:

$adapter->delete("key");

Returns first key and moves cursor to the next position:

$adapter->firstKey();

Read next key and moves cursor to the next position:

$adapter->nextKey();

Read all database keys:

foreach ($adapter->read() as $key => $value) {
    ...
}

Laravel Service Provider

Publish configuration to app config:

./artisan vendor:publish --provider=AndKom\\BerkeleyDb\\ServiceProvider

Edit app/config/berkeleydb.php:

<?php
 
return [
    'adapter' => 'phpdb4',
 
    'filename' => database_path('database.dat'),
 
    'database' => null,
 
    'mode' => 'w',
];

Use BDB facade to access database methods:

$value = BDB::get("key");