From 36abb3b7ea9592eabbbf2e9dd8d67dc5cfb47d03 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 7 Jun 2010 00:42:25 +0200 Subject: [PATCH] moved addChannel() to ChannelController done some code cleanup and added comments --- lib/controller/channelcontroller.php | 17 +++++++- lib/model/channel.php | 38 +++++++---------- lib/model/databaseobject.php | 62 ++++++++++++++++------------ lib/model/group.php | 2 +- lib/model/user.php | 5 ++- lib/util/exceptions.php | 5 ++- share/tools/tests.php | 5 --- 7 files changed, 75 insertions(+), 59 deletions(-) diff --git a/lib/controller/channelcontroller.php b/lib/controller/channelcontroller.php index 832055d..291bf6c 100644 --- a/lib/controller/channelcontroller.php +++ b/lib/controller/channelcontroller.php @@ -99,12 +99,27 @@ class ChannelController extends Controller { $channel = Channel::getByUcid($ucid); - if (!($channel instanceof Channel)) { + if (!($channel instanceof Channel)) { // TODO rework $channel = Channel::addChannel($ucid); } $channel->addData($this->request->get); } + + public function add($ucid) { // TODO rework + $channel = new Channel(); + $channel->ucid = $ucid; + + if (substr($channel->ucid, 0, 19) == OneWireSensor::$ucidPrefix) { + $channel->type = 'OneWireSensor'; + $channel->description = OneWireSensor::getFamilyDescription($channel); + } + else { + $channel->type = 'Channel'; + } + + $channel->save(); + } } ?> \ No newline at end of file diff --git a/lib/model/channel.php b/lib/model/channel.php index 2f96a4c..cb040d8 100644 --- a/lib/model/channel.php +++ b/lib/model/channel.php @@ -34,36 +34,20 @@ interface ChannelInterface { abstract class Channel extends DatabaseObject implements ChannelInterface { const table = 'channels'; - static public function addChannel($ucid) { // TODO rework (move to channelcontroller?) - $channel = new Channel(); - $channel->ucid = $ucid; - - if (substr($channel->ucid, 0, 19) == OneWireSensor::$ucidPrefix) { - $channel->type = 'OneWireSensor'; - $channel->description = OneWireSensor::getFamilyDescription($channel); - } - else { - $channel->type = 'Channel'; - } - - $channel->save(); - return current(Channel::getByFilter(array('id' => $channel->id))); - } - public function delete() { - $this->reset(); + $this->reset(); // delete all readings if database doesn't support foreign keys parent::delete(); } /* - * deletes all values from database + * deletes all readings from database */ public function reset($from = NULL, $to = NULL) { $this->dbh->execute('DELETE FROM data WHERE channel_id = ' . (int) $this->id) . $this->buildTimeFilter($from, $to); } /* - * add a new value to the database + * add a new reading to the database */ public function addData($data) { $sql = 'INSERT INTO data (channel_id, timestamp, value) VALUES(' . $this->dbh->escape($this) . ', ' . $this->dbh->escape($data['timestamp']) . ', ' . $this->dbh->escape($data['value']) . ')'; @@ -115,7 +99,7 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { $sql .= ' GROUP BY ' . $sqlGroupBy; } - $sql .= ' ORDER BY timestamp DESC'; // TODO optimize this grouping algorithm. array_reverse() nescessary? + $sql .= ' ORDER BY timestamp DESC'; $result = $this->dbh->query($sql); $totalCount = $result->count(); @@ -147,23 +131,26 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { $reading = $result->next(); } - return array_reverse($packages); // start with oldest ts & ends with newest ts (reverse array order due to descending order in sql statement) + return array_reverse($packages); // start with oldest ts and ends with newest ts (reverse array order due to descending order in sql statement) } /* - * wrapper for self::getByFilter() + * simple self::getByFilter() wrapper */ static public function getByUcid($ucid) { return current(self::getByFilter(array('ucid' => $ucid))); } /* - * wrapper for self::getByFilter() + * simple self::getByFilter() wrapper */ static public function getByType($type) { return current(self::getByFilter(array('type' => $type))); } + /* + * create new channel instance by given database query result + */ final static protected function factory($object) { $rc = new ReflectionClass($object['type']); if (!$rc->isSubclassOf('Channel')) { @@ -173,6 +160,9 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { return $rc->newInstanceArgs(array($object)); } + /* + * build simple timeframe filter + */ static protected function buildFilterTime($from = NULL, $to = NULL) { $sql = ''; @@ -188,7 +178,7 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { } static protected function buildFilterQuery($filters, $conjunction, $columns = array('id')) { - $sql = 'SELECT id, type FROM ' . static::table; + $sql = 'SELECT ' . static::table . '.* FROM ' . static::table; // join groups if (key_exists('group', $filters)) { diff --git a/lib/model/databaseobject.php b/lib/model/databaseobject.php index ae8b844..a95bd15 100644 --- a/lib/model/databaseobject.php +++ b/lib/model/databaseobject.php @@ -26,22 +26,13 @@ abstract class DatabaseObject { private $values = array(); static private $instances = array(); // singletons of objects - + + /* + * magic functions + */ final public function __construct($object) { $this->dbh = Database::getConnection(); - - if (key_exists('id', $object)) { // referencing existing object in database by id - $this->values['id'] = $object['id']; - $this->dirty = false; - } - else { // create new object in database - $this->values = $object; - $this->dirty = true; - } - } - - static protected function factory($object) { - return new static($object); + $this->values = $object; } public function __get($key) { @@ -53,10 +44,10 @@ abstract class DatabaseObject { } public function __set($key, $value) { // TODO untested - if ($key != 'id') { - $this->values[$key] = $value; - $this->dirty = true; - } + if ($key != 'id') { + $this->values[$key] = $value; + $this->dirty = true; + } } final public function __sleep() { @@ -67,11 +58,24 @@ abstract class DatabaseObject { final public function __wakeup() { $this->dbh = Database::getConnection(); } + + final public function __isset($key) { + return isset($this->values[$key]); + } + + static protected function factory($object) { + return new static($object); + } + /* + * insert oder update the database representation of the object + */ public function save() { if ($this->id) { // just update foreach ($this->values as $column => $value) { - $columns[] = $column . ' = ' . $this->dbh->escape($value); + if ($column != 'id') { + $columns[] = $column . ' = ' . $this->dbh->escape($value); + } } $sql = 'UPDATE ' . static::table . ' SET ' . implode(', ', $columns) . ' WHERE id = ' . (int) $this->id; @@ -85,6 +89,9 @@ abstract class DatabaseObject { $this->dirty = false; } + /* + * loads all columns from the database and caches them in $this->values + */ private function load() { $result = $this->dbh->query('SELECT * FROM ' . static::table . ' WHERE id = ' . (int) $this->id, 1)->current(); @@ -99,14 +106,17 @@ abstract class DatabaseObject { } } - + /* + * deletes database representation of this object, but leaves object members. + * by calling $this->save() you can easily reinsert the object with a new id + */ public function delete() { $this->dbh->execute('DELETE FROM ' . static::table . ' WHERE id = ' . (int) $this->id); // delete from database unset($this->values['id']); } /* - * @return array(static) Array with results + * data filtering */ static public function getByFilter($filters = array(), $conjunction = true) { $sql = static::buildFilterQuery($filters, $conjunction); @@ -119,18 +129,18 @@ abstract class DatabaseObject { $instances = array(); foreach ($result as $object) { if (!isset(self::$instances[static::table][$object['id']])) { - self::$instances[static::table][$object['id']] = static::factory($object); + self::$instances[static::table][$object['id']] = static::factory($object); // create singleton instance of database object } - $instances[$object['id']] = self::$instances[static::table][$object['id']]; + $instances[$object['id']] = self::$instances[static::table][$object['id']]; // return singleton instance of database object } return $instances; } - static protected function buildFilterQuery($filters, $conjunction, $columns = array('id')) { - return 'SELECT id FROM ' . static::table . static::buildFilterCondition($filters, $conjunction); + static protected function buildFilterQuery($filters, $conjunction) { + return 'SELECT ' . static::table . '.* FROM ' . static::table . static::buildFilterCondition($filters, $conjunction); } - + static protected function buildFilterCondition($filters, $conjunction) { $dbh = Database::getConnection(); diff --git a/lib/model/group.php b/lib/model/group.php index 544f332..1c5dc95 100644 --- a/lib/model/group.php +++ b/lib/model/group.php @@ -54,7 +54,7 @@ class Group extends DatabaseObject { } static protected function buildFilterQuery($filters, $conjunction, $columns = array('id')) { - $sql = 'SELECT id FROM ' . static::table; + $sql = 'SELECT ' . static::table . '.* FROM ' . static::table; // join groups if (key_exists('group', $filters)) { diff --git a/lib/model/user.php b/lib/model/user.php index d79487f..6669496 100644 --- a/lib/model/user.php +++ b/lib/model/user.php @@ -25,6 +25,9 @@ class User extends DatabaseObject { const table = 'users'; + /* + * simple self::getByFilter() wrapper + */ public static function getByEMail($email) { return current(self::getByFilter(array('email' => $email))); } @@ -57,7 +60,7 @@ class User extends DatabaseObject { } static protected function buildQuery($filters, $conjunction, $columns = array('id')) { - $sql = 'SELECT id FROM ' . static::table; + $sql = 'SELECT ' . static::table . '.* FROM ' . static::table; // join groups if (key_exists('group', $filters)) { $sql .= ' LEFT JOIN group_user AS rel ON rel.user_id = ' . static::table . '.id'; diff --git a/lib/util/exceptions.php b/lib/util/exceptions.php index 1d02511..6d816fe 100644 --- a/lib/util/exceptions.php +++ b/lib/util/exceptions.php @@ -36,10 +36,13 @@ class CustomException extends Exception { public function toHtml() { return $this->message . ' in ' . $this->file . ':' . $this->line; } + + public function toJson() { // TODO implement + + } } class CustomErrorException extends ErrorException { - static public function errorHandler($errno, $errstr, $errfile, $errline ) { throw new self($errstr, 0, $errno, $errfile, $errline); } diff --git a/share/tools/tests.php b/share/tools/tests.php index 2cf4a7e..0f27b14 100644 --- a/share/tools/tests.php +++ b/share/tools/tests.php @@ -38,13 +38,8 @@ foreach ($groups as $group) { echo '
';
 var_dump(Database::getConnection());
-var_dump(DatabaseObject::$objects);
 echo '
'; - - - - /*$meter = current(Channel::getByFilter(array('id' => 19))); $start = microtime(true);