PODB

Translators from Russian to English Wanted!

 

Accessing the Database

The Database class allows you to manage the connection to the database. The usual way to establish a connection to the database is as follows:

include_once("./PODB.php");

$params = array('path' => './podb.fs', 'mode' => 'r', 'handler' => 'db3');
$podb = PODB::connect("Berkeley", $params);

$root =& $podb->get_root();

var_dump($root);

$podb->close();

 

Database Storage Handlers

MySQL

$params = array('host' => 'localhost',
                'user' => 'root',
                'pass' => '',
                'database' => 'podb');
$podb = PODB::connect("MySQL", $params);

Berkeley

$params = array('path' => './test6.fs',
                'mode' => 'w-',
                'handler' => 'db3',
                'persistent' => true);
$podb = PODB::connect("Berkeley", $params);

SQLite

$params = array('path' => './test6.fs', 
                'perm' => O666,
                'persistent' => true);
$podb = PODB::connect("SQLite", $params);

 

Transaction access

To update the database, a transaction has to be started. Starting the transaction must be done explicitly; there is no implicit transaction when accessing the database. Only one transaction at a time can be opened on a connection.

$podb = PODB::connect("Berkeley", $params);
$podb->begin();

// update your data

// Commit the changes
$podb->commit();
$podb->close();

 

Version access

Only one version access at a time can be opened on a connection. The argument of the versionBegin method, when it is specified, must be the name of a database version.

$params = array('path' => './test2.fs', 'mode' => 'w-', 'handler' => 'db3');
$podb = PODB::connect("Berkeley", $params);
$podb->versionBegin('test'); $podb->begin();
// Working with object in version test
$podb->commit(); $podb->close();

 

$podb = PODB::connect("Berkeley", );
if (
$podb->versionExists('test')) { // Starting version access and applying changes in version // to default object storage. $podb->versionBegin('test');     $podb->versionCommit();     $podb->versionDrop(); }
$podb->close();

 

Persistent like PHP Class

Object of any class derived from Persistent base class is considered as persistent capable.

class Counter extends Persistent { 
    var  =
0;     function count() {         $this->setChanged(); // Talking storage this object is changed.         return ++;     } }

 

Root Object

The storage has one special root object. Root object is the only persistent object accessed in the special way (using get_root method). All other persistent objects are accessed in normal way: either by traversing by references from another persistent objects, either using ReferenceList or ReferenceMap objects. Unlike many other OODBMS, there can be only one root in the storage. If you need to have several named roots, you should create Persistent object with attribute RelationMap object and use it as root object.

When you create new storage get_root() returns NULL. You should create root object and store reference to it by set_root() method. Next time you are opening storage, root object can be retrieved by get_root().

$podb = PODB::connect("Berkeley", );
$root =& $podb->get_root();
$podb->close();

 

Updating an Objects

Как уже говорилось, изменение базы данных возможно только в доступе к базе внутри запущенной транзакции.

Пример реализации счетчика посещений. Класс Counter используемый в примере описан выше.

$podb = PODB::connect("Berkeley", $params);
$podb->begin();

$counter =& $podb->get_root();
if (
$counter == null || get_class($counter) != 'counter') {     $counter = new Counter();     $podb->set_root($counter); } echo "Counter value: ".$counter->count()."\n";
// Commit the changes $podb->commit(); $podb->close();

 

Manipulating Relationships

class Root extends Persistent {
    var $folders;
}

class Folder extends Persistent {
    var $name;

    function Folder($n) {
        $this->name = $n;
    }
}

$podb = PODB::connect("Berkeley", $params);
$podb->begin();

$root = new Root();
$root->foders = $podb->createRelationList();

$folder1 = new Folder("Folder1");
$folder2 = new Folder("Folder2");

$root->folders->insert($folder1); $root->folders->insert($folder2); $podb->set_root($root); $podb->commit(); $podb->close();

 

Accessing Relationship Objects

In an example shown above, we have created the small structure of the data consisting of object Root containing a little bit objects Folder. Now we shall consider a retrieving method of these objects:

$podb = PODB::connect("Berkeley", $params);

$root = $podb->get_root();

$folders = $root->folders->iterator();
$folder =& $folders->current();

while ($folder) {
    echo
"Folder: ".$folder->name." OID ".$folder->oid."\n";     $folder =& $folders->next(); } $podb->close();

 

Removing Objects

 

 

 

Copyright © 2004
Sergey Volkov


SourceForge.net Logo