Browse Source

Implemented convenience accessors and corrected a few bugs.

pull/5/head
Etenil 15 years ago
parent
commit
305b83387b
  1. 69
      system/Storage/StorageBase.php
  2. 3
      system/Storage/StorageDriver.php
  3. 13
      system/Storage/StorageEngineBase.php
  4. 64
      system/Storage/drivers/sqlite/StorageEngineSqlite.php
  5. 3
      tests/php/Session/TestSession.php
  6. 73
      tests/php/Storage/TestStorage.php

69
system/Storage/StorageBase.php

@ -21,6 +21,7 @@ class StorageBase
{
protected $id = false;
public $children;
protected static $db = null;
/**
* Constructor.
@ -36,6 +37,33 @@ class StorageBase
}
}
/**
* Defines a common database connection for Storable objects.
*/
public static function bind($db)
{
if(StorageEngineBase::does_extend($db, "StorageEngineBase")) {
self::$db = $db;
} else {
throw new StorageException(t("Cannot bind non-StorageEngine object."));
}
}
protected static function is_bound()
{
return (self::$db !== null);
}
protected static function require_bound()
{
if(!self::is_bound()) {
throw new StorageException(t("Object not bound to StorageEngine."));
return false;
}
return true;
}
/**
* Sets the object's value based on the given array. The array's keys are
* used as member variables's names and the associated values are set to
@ -181,7 +209,7 @@ class StorageBase
/**
* Sets the object's ID.
*/
function setid($id)
public function setid($id)
{
if($this->id !== false) {
throw new StorageException(t("Attempting to set the id of an existing object."));
@ -194,10 +222,47 @@ class StorageBase
/**
* Unsets the object's ID.
*/
function clearid()
public function clearid()
{
$this->id = false;
}
/* ******* Convenience wrapper **********/
public function create()
{
$this->require_bound();
return self::$db->create($this);
}
public function save()
{
$this->require_bound();
return self::$db->save($this);
}
public function delete()
{
$this->require_bound();
return self::$db->delete($this);
}
public function drop()
{
$this->require_bound();
return self::$db->drop($this);
}
public function load($cond)
{
$this->require_bound();
return self::$db->load($this, $cond);
}
public static function select($cond)
{
self::require_bound();
return self::$db->select(get_called_class(), $cond);
}
}
?>

3
system/Storage/StorageDriver.php

@ -21,7 +21,8 @@ interface StorageDriver
public function create(&$object);
public function save(&$object);
public function delete(&$object);
public function select(&$object, array $cond);
public function load(&$object, array $cond);
public function select($objecttype, array $cond);
public function drop(&$object);
}

13
system/Storage/StorageEngineBase.php

@ -42,7 +42,14 @@ class StorageEngineBase implements StorageDriver
/**
* Loads up the data corresponding to the object in the storage.
*/
public function select(&$object, array $cond)
public function load(&$object, array $cond)
{
}
/**
* Loads objects from storage.
*/
public function select($objecttype, array $cond)
{
}
@ -71,7 +78,7 @@ class StorageEngineBase implements StorageDriver
}
}
}
/**
* Checks that object is a storable object.
*/
@ -98,7 +105,7 @@ class StorageEngineBase implements StorageDriver
if(!is_object($object)) {
return false;
}
$refl = null;
try {
$refl = new ReflectionClass($object);

64
system/Storage/drivers/sqlite/StorageEngineSqlite.php

@ -68,7 +68,10 @@ class StorageEngineSqlite extends StorageEngineBase implements StorageDriver
$this->errors();
$table = array();
while($table[] = $res->fetchArray(SQLITE3_ASSOC)) {}
while($row = $res->fetchArray(SQLITE3_ASSOC))
{
$table[] = $row;
}
return $table;
} else {
$answer = $this->db->exec($statement);
@ -176,11 +179,18 @@ class StorageEngineSqlite extends StorageEngineBase implements StorageDriver
} else {
$stmt = "UPDATE " . $this->obj_name($object) . " SET ";
$cols = "";
$vals = "";
foreach($props as $prop) {
$stmt.= $prop['name'] . '="' . $prop['val'] . '", ';
$stmt.= $prop['name'] . '=';
if(StorageEngineBase::does_extend($prop['val'], "StorageBase")) {
$stmt.= '"' . $prop['val']->id . '", ';
} else {
$stmt.= '"' . $prop['val']->getval() . '", ';
}
}
$stmt = substr($stmt, 0, -2) . ' WHERE id="' . $object->id . '";';
$stmt = substr($stmt, 0, -2) . ' WHERE id="' . $object->id . '"';
return $this->query($stmt);
}
@ -195,11 +205,11 @@ class StorageEngineSqlite extends StorageEngineBase implements StorageDriver
$stmt = "DELETE FROM " . $this->obj_name($object) . " WHERE id=\"" . $object->id . "\";";
$result = $this->query($stmt);
if($result) {
$object->clearid();
}
return $result;
}
}
@ -215,14 +225,14 @@ class StorageEngineSqlite extends StorageEngineBase implements StorageDriver
if($result) {
$object->clearid();
}
return $result;
}
/**
* Returns data relative to an object as an array.
*/
public function select(&$object, array $cond)
public function load(&$object, array $cond)
{
$stmt = "SELECT * FROM " . $this->obj_name($object);
@ -251,6 +261,46 @@ class StorageEngineSqlite extends StorageEngineBase implements StorageDriver
}
}
}
/**
* Loads a bunch of objects of a given type.
*/
public function select($objecttype, array $cond)
{
$stmt = "SELECT * FROM " . $objecttype;
if(count($cond) > 1) {
$where . " WHERE ";
foreach($cond as $col => $val) {
$stmt.= "$col=\"$val\" AND ";
}
// Stripping the extra " AND "
$stmt = substr($stmt, 0, -5) . ';';
}
$this->log($stmt);
$data = $this->query($stmt);
$objs = array();
foreach($data as $row) {
$object = new $objecttype();
// Populating the object.
$props = $object->prototype();
foreach($props as $prop) {
if(isset($row[$prop['name']])) {
$object->__set($prop['name'], $row[$prop['name']]);
}
}
$objs[] = $object;
}
return $objs;
}
}
?>

3
tests/php/Session/TestSession.php

@ -20,7 +20,10 @@ define('DB_LOGFILE', 'queries.log');
class TestSession
{
function testCreate()
{
}
}
?>

73
tests/php/Storage/TestStorage.php

@ -66,23 +66,29 @@ class TestStorage
unlink($this->db_file);
$this->sdb = new StorageEngineSqlite($this->db_file);
$this->db = new SQLite3($this->db_file);
StorageBase::bind($this->sdb);
}
function testCreate()
{
$test = new Account();
$this->sdb->create($test);
unset($test);
$numtables = $this->db->querySingle(
'SELECT count(name) as count FROM sqlite_master WHERE type="table" AND name="Account"');
ut_equals($numtables, 1);
$this->_wipe();
$test->create();
$numtables = $this->db->querySingle(
'SELECT count(name) as count FROM sqlite_master WHERE type="table" AND name="Account"');
ut_equals($numtables, 1);
}
function testPopulate()
{
$vals = array('balance' => 50, 'interest' => 0.01);
$test = new Account();
$test->populate($vals);
ut_equals($test->balance, 50);
@ -104,14 +110,29 @@ class TestStorage
'SELECT count(*) as count FROM Account '.
'WHERE balance="100" AND interest="0.025"');
ut_equals($count, 1);
$account->balance = 200;
$account->interest = 0.015;
$account->save();
$count = $this->db->querySingle(
'SELECT count(*) as count FROM Account '.
'WHERE balance="200" AND interest="0.015"');
ut_equals($count, 1);
}
function testLoad()
{
$account = new Account();
$this->sdb->select($account, array('id' => 1));
ut_equals($account->balance, 100);
ut_equals($account->interest, 0.025);
$this->sdb->load($account, array('id' => 1));
ut_equals($account->balance, 200);
ut_equals($account->interest, 0.015);
$account = null;
$account = new Account();
$account->load(array('id' => 1));
ut_equals($account->balance, 200);
ut_equals($account->interest, 0.015);
}
function testDelete()
@ -134,6 +155,26 @@ class TestStorage
ut_equals($count, 0);
ut_nassert($account->id);
$account = null;
$account = new Account();
$account->balance = 200;
$account->interest = 0.020;
$account->save();
$count = $this->db->querySingle(
'SELECT count(*) as count FROM Account '.
'WHERE balance="200" AND interest="0.020"');
ut_equals($count, 1);
$account->delete();
$count = $this->db->querySingle(
'SELECT count(*) as count FROM Account '.
'WHERE balance="200" AND interest="0.020"');
ut_equals($count, 0);
ut_nassert($account->id);
}
function testDrop()
@ -147,7 +188,7 @@ class TestStorage
$account->interest = 0.020;
$this->sdb->save($account);
ut_differs($account->id, false);
$this->sdb->drop($account);
$numtables = $this->db->querySingle(
@ -157,16 +198,22 @@ class TestStorage
ut_nassert($account->id);
}
function _testCreateLinked()
function testSelect()
{
// Wiping
$this->_wipe();
$owner = new Owner();
$owner->create($this->sdb);
$numtables = $this->db->querySingle(
'SELECT count(name) as count FROM sqlite_master '.
'WHERE type="table" AND (name="Owner" OR name="Account")');
ut_equals($numtables, 2);
// Inserting two accounts.
$acc1 = new Account(array('balance' => 100, 'interest' => 0.015));
$acc2 = new Account(array('balance' => 200, 'interest' => 0.015));
$acc1->create();
$acc1->save();
$acc2->save();
$objs = Account::select(array('interest' => 0.015));
ut_equals(count($objs), 2);
}
function __destruct()

Loading…
Cancel
Save