From cc7d33eed6b151ad716fe3e144270b45aec6752b Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 8 Jun 2010 15:41:02 +0200 Subject: [PATCH] fixed sql group by statement added some exception throwing in invalid arguments --- backend/lib/model/channel.php | 13 +++++++++---- backend/lib/model/channel/meter.php | 10 +++++----- backend/lib/model/db/pgsql.php | 2 +- backend/lib/model/group.php | 10 ++++++++-- backend/lib/model/user.php | 16 ++++++++++++++-- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/backend/lib/model/channel.php b/backend/lib/model/channel.php index c470c76..bec3ecc 100644 --- a/backend/lib/model/channel.php +++ b/backend/lib/model/channel.php @@ -61,7 +61,7 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { * @param $groupBy determines how readings are grouped. Possible values are: year, month, day, hour, minute or an integer for the desired size of the returned array */ public function getData($from = NULL, $to = NULL, $groupBy = NULL) { - $ts = 'FROM_UNIXTIME(timestamp)'; // just for saving space + $ts = 'FROM_UNIXTIME(timestamp/1000)'; // just for saving space switch ($groupBy) { case 'year': $sqlGroupBy = 'YEAR(' . $ts . ')'; @@ -100,7 +100,6 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { } $sql .= ' ORDER BY timestamp DESC'; - $result = $this->dbh->query($sql); $totalCount = $result->count(); @@ -138,14 +137,20 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { * simple self::getByFilter() wrapper */ static public function getByUcid($ucid) { - return current(self::getByFilter(array('ucid' => $ucid))); + $channel = self::getByFilter(array('ucid' => $ucid)); + + if (current($channel) === false) { + throw new InvalidArgumentException('No such channel!'); + } + + return $channel; } /* * simple self::getByFilter() wrapper */ static public function getByType($type) { - return current(self::getByFilter(array('type' => $type))); + return self::getByFilter(array('type' => $type)); } /* diff --git a/backend/lib/model/channel/meter.php b/backend/lib/model/channel/meter.php index a3f988f..e8ed187 100644 --- a/backend/lib/model/channel/meter.php +++ b/backend/lib/model/channel/meter.php @@ -30,7 +30,7 @@ abstract class Meter extends Channel { $result = $this->dbh->query($sql)->rewind(); - return $result['count'] / $this->resolution; + return $result['count'] / $this->resolution / 1000; // returns Wh } public function getMin($from = NULL, $to = NULL) { @@ -57,8 +57,8 @@ abstract class Meter extends Channel { return $min; } - public function getAverage($from, $to) { - return $this->getConsumption($from, $to) / ($to - $from) * 1000; + public function getAverage($from = NULL, $to = NULL) { // TODO calculate timeinterval if no params were given + return $this->getConsumption($from, $to) / ($to - $from) / 1000; // return W } /* @@ -79,9 +79,9 @@ abstract class Meter extends Channel { $delta = $pulses[$i]['timestamp'] - $pulses[$i-1]['timestamp']; $pulses[$i]['timestamp'] -= $delta/2; - $pulses[$i]['value'] *= 3600000/(($this->resolution / 1000) * $delta); + $pulses[$i]['value'] *= 3600000/(($this->resolution / 1000) * $delta); // TODO untested } - return $pulses; + return $pulses; // returns W } } \ No newline at end of file diff --git a/backend/lib/model/db/pgsql.php b/backend/lib/model/db/pgsql.php index 8695a9e..7bf5fae 100644 --- a/backend/lib/model/db/pgsql.php +++ b/backend/lib/model/db/pgsql.php @@ -145,7 +145,7 @@ class PgSql extends Database { } public function getLastInsertId() { - throw new Exception('PgSql::getLastInsertId() hasnt implemented yet!'); // TODO find solution + throw new Exception('PgSql::getLastInsertId() hasnt implemented yet!'); // TODO find solution, use PDO? } } diff --git a/backend/lib/model/group.php b/backend/lib/model/group.php index 5e8f693..be5efcb 100644 --- a/backend/lib/model/group.php +++ b/backend/lib/model/group.php @@ -37,8 +37,14 @@ class Group extends DatabaseObject { return $groups; } - public static function getByUgid($uuid) { - return current(self::getByFilter(array('ugid' => $ugid))); + public static function getByUgid($ugid) { + $group = self::getByFilter(array('ugid' => $ugid)); + + if (current($group) === false) { + throw new InvalidArgumentException('No such group!'); + } + + return $group; } public function getUsers($recursive = false) { diff --git a/backend/lib/model/user.php b/backend/lib/model/user.php index efae38b..95ef60a 100644 --- a/backend/lib/model/user.php +++ b/backend/lib/model/user.php @@ -29,11 +29,23 @@ class User extends DatabaseObject { * simple self::getByFilter() wrapper */ public static function getByUuid($uuid) { - return current(self::getByFilter(array('uuid' => $uuid))); + $user = self::getByFilter(array('uuid' => $uuid)); + + if (current($user) === false) { + throw new InvalidArgumentException('No such user!'); + } + + return $user; } public static function getByEMail($email) { - return current(self::getByFilter(array('email' => $email))); + $user = self::getByFilter(array('email' => $email)); + + if (current($user) === false) { + throw new InvalidArgumentException('No such user!'); + } + + return $user; } public function getChannels($recursive = false) {