moved addChannel() to ChannelController

done some code cleanup and added comments
This commit is contained in:
Steffen Vogel 2010-06-07 00:42:25 +02:00
parent 62ff0c614d
commit 36abb3b7ea
7 changed files with 75 additions and 59 deletions

View file

@ -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();
}
}
?>

View file

@ -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)) {

View file

@ -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();

View file

@ -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)) {

View file

@ -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';

View file

@ -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);
}

View file

@ -38,13 +38,8 @@ foreach ($groups as $group) {
echo '<pre>';
var_dump(Database::getConnection());
var_dump(DatabaseObject::$objects);
echo '</pre>';
/*$meter = current(Channel::getByFilter(array('id' => 19)));
$start = microtime(true);