prepared sql statements

cast of min/max/avg values to float/integer
This commit is contained in:
Steffen Vogel 2010-10-27 22:46:50 +02:00
parent 9b3d1128ec
commit 4a8e2b0d9e
2 changed files with 8 additions and 8 deletions

View file

@ -76,10 +76,8 @@ abstract class Interpreter implements InterpreterInterface {
*/
protected function getData($tuples = NULL, $groupBy = NULL) {
// prepare sql
$parameters = array(':id' => $this->channel->getId());
$sql['from'] = ' FROM data';
$sql['where'] = ' WHERE channel_id = :id' . self::buildDateTimeFilterSQL($this->from, $this->to);
$sql['where'] = ' WHERE channel_id = ?' . self::buildDateTimeFilterSQL($this->from, $this->to);
$sql['orderBy'] = ' ORDER BY timestamp ASC';
if ($groupBy && $sql['groupFields'] = self::buildGroupBySQL($groupBy)) {
@ -94,10 +92,10 @@ abstract class Interpreter implements InterpreterInterface {
}
// get total row count for grouping
$rowCount = $this->conn->fetchColumn($sql['rowCount'], $parameters, 0);
$rowCount = $this->conn->fetchColumn($sql['rowCount'], array($this->channel->getId()), 0);
// query for data
$stmt = $this->conn->executeQuery('SELECT ' . $sql['fields'] . $sql['from'] . $sql['where'] . $sql['groupBy'] . $sql['orderBy'], $parameters);
$stmt = $this->conn->executeQuery('SELECT ' . $sql['fields'] . $sql['from'] . $sql['where'] . $sql['groupBy'] . $sql['orderBy'], array($this->channel->getId()));
// return iterators
if ($sql['groupBy'] || is_null($tuples) || $rowCount < $tuples) {

View file

@ -58,7 +58,8 @@ class SensorInterpreter extends Interpreter {
* @return array (0 => timestamp, 1 => value)
*/
public function getMin() {
return array_map('floatval', $this->conn->fetchAssoc('SELECT value, timestamp FROM data WHERE channel_id = ' . (int) $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to) . ' ORDER BY value ASC LIMIT 1'));
$min = $this->conn->fetchAssoc('SELECT value, timestamp FROM data WHERE channel_id = ?' . parent::buildDateTimeFilterSQL($this->from, $this->to) . ' ORDER BY value ASC LIMIT 1', array($this->channel->getId()));
return ($min) ? array_map('floatval', $min) : NULL;
}
/**
@ -67,7 +68,8 @@ class SensorInterpreter extends Interpreter {
* @return array (0 => timestamp, 1 => value)
*/
public function getMax() {
return array_map('floatval', $this->conn->fetchAssoc('SELECT value, timestamp FROM data WHERE channel_id = ' . (int) $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to) . ' ORDER BY value DESC LIMIT 1'));
$max = $this->conn->fetchAssoc('SELECT value, timestamp FROM data WHERE channel_id = ?' . parent::buildDateTimeFilterSQL($this->from, $this->to) . ' ORDER BY value DESC LIMIT 1', array($this->channel->getId()));
return ($max) ? array_map('floatval', $max) : NULL;
}
/**
@ -76,7 +78,7 @@ class SensorInterpreter extends Interpreter {
* @return float
*/
public function getAverage() {
return (float) $this->conn->fetchColumn('SELECT AVG(value) FROM data WHERE channel_id = ' . (int) $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to));
return (float) $this->conn->fetchColumn('SELECT AVG(value) FROM data WHERE channel_id = ?' . parent::buildDateTimeFilterSQL($this->from, $this->to), array($this->channel->getId()), 0);
}
/**