CC-4348: Prepared statements - part 4

-fixed Show.php
This commit is contained in:
Martin Konecny 2012-09-04 13:12:35 -04:00
parent 8e40137b7d
commit a731fc147b
4 changed files with 95 additions and 38 deletions

View file

@ -382,7 +382,7 @@ class ApiController extends Zend_Controller_Action
$this->view->is_recording = false; $this->view->is_recording = false;
$this->view->server_timezone = Application_Model_Preference::GetTimezone(); $this->view->server_timezone = Application_Model_Preference::GetTimezone();
$rows = Application_Model_Show::GetCurrentShow($today_timestamp); $rows = Application_Model_Show::getCurrentShow($today_timestamp);
Application_Model_Show::convertToLocalTimeZone($rows, array("starts", "ends", "start_timestamp", "end_timestamp")); Application_Model_Show::convertToLocalTimeZone($rows, array("starts", "ends", "start_timestamp", "end_timestamp"));
if (count($rows) > 0) { if (count($rows) > 0) {
@ -928,7 +928,7 @@ class ApiController extends Zend_Controller_Action
} }
} elseif ($djtype == "dj") { } elseif ($djtype == "dj") {
//check against show dj auth //check against show dj auth
$showInfo = Application_Model_Show::GetCurrentShow(); $showInfo = Application_Model_Show::getCurrentShow();
// there is current playing show // there is current playing show
if (isset($showInfo[0]['id'])) { if (isset($showInfo[0]['id'])) {
$current_show_id = $showInfo[0]['id']; $current_show_id = $showInfo[0]['id'];

View file

@ -24,7 +24,7 @@ class DashboardController extends Zend_Controller_Action
$userInfo = Zend_Auth::getInstance()->getStorage()->read(); $userInfo = Zend_Auth::getInstance()->getStorage()->read();
$user = new Application_Model_User($userInfo->id); $user = new Application_Model_User($userInfo->id);
$show = Application_Model_Show::GetCurrentShow(); $show = Application_Model_Show::getCurrentShow();
$show_id = isset($show['id'])?$show['id']:0; $show_id = isset($show['id'])?$show['id']:0;
@ -51,7 +51,7 @@ class DashboardController extends Zend_Controller_Action
$userInfo = Zend_Auth::getInstance()->getStorage()->read(); $userInfo = Zend_Auth::getInstance()->getStorage()->read();
$user = new Application_Model_User($userInfo->id); $user = new Application_Model_User($userInfo->id);
$show = Application_Model_Show::GetCurrentShow(); $show = Application_Model_Show::getCurrentShow();
$show_id = isset($show[0]['id'])?$show[0]['id']:0; $show_id = isset($show[0]['id'])?$show[0]['id']:0;
$source_connected = Application_Model_Preference::GetSourceStatus($sourcename); $source_connected = Application_Model_Preference::GetSourceStatus($sourcename);

View file

@ -121,7 +121,7 @@ class ScheduleController extends Zend_Controller_Action
public function getCurrentShowAction() public function getCurrentShowAction()
{ {
$currentShow = Application_Model_Show::GetCurrentShow(); $currentShow = Application_Model_Show::getCurrentShow();
if (!empty($currentShow)) { if (!empty($currentShow)) {
$this->view->si_id = $currentShow[0]["instance_id"]; $this->view->si_id = $currentShow[0]["instance_id"];
$this->view->current_show = true; $this->view->current_show = true;
@ -346,7 +346,7 @@ class ScheduleController extends Zend_Controller_Action
public function getCurrentPlaylistAction() public function getCurrentPlaylistAction()
{ {
$range = Application_Model_Schedule::GetPlayOrderRange(); $range = Application_Model_Schedule::GetPlayOrderRange();
$show = Application_Model_Show::GetCurrentShow(); $show = Application_Model_Show::getCurrentShow();
/* Convert all UTC times to localtime before sending back to user. */ /* Convert all UTC times to localtime before sending back to user. */
if (isset($range["previous"])) { if (isset($range["previous"])) {

View file

@ -1788,7 +1788,7 @@ class Application_Model_Show
* @param String $timeNow - current time (in UTC) * @param String $timeNow - current time (in UTC)
* @return array - show being played right now * @return array - show being played right now
*/ */
public static function GetCurrentShow($timeNow=null) public static function getCurrentShow($timeNow=null)
{ {
global $CC_CONFIG; global $CC_CONFIG;
$con = Propel::getConnection(); $con = Propel::getConnection();
@ -1797,16 +1797,34 @@ class Application_Model_Show
$timeNow = $date->getUtcTimestamp(); $timeNow = $date->getUtcTimestamp();
} }
//TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin
$sql = "SELECT si.starts as start_timestamp, si.ends as end_timestamp, s.name," $sql = <<<SQL
." s.id, si.id as instance_id, si.record, s.url, starts, ends" SELECT si.starts AS start_timestamp,
." FROM $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s" si.ends AS end_timestamp,
." WHERE si.show_id = s.id" s.name,
." AND si.starts <= TIMESTAMP '$timeNow'" s.id,
." AND si.ends > TIMESTAMP '$timeNow'" si.id AS instance_id,
." AND modified_instance != TRUE"; si.record,
s.url,
starts,
ends
FROM cc_show_instances si,
cc_show s
WHERE si.show_id = s.id
AND si.starts <= TIMESTAMP ':timeNow1'
AND si.ends > TIMESTAMP ':timeNow2'
AND modified_instance != TRUE"
SQL;
// Convert back to local timezone $stmt = $con->prepare($sql);
$rows = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC); $stmt->bindParam(':timeNow1', $timeNow);
$stmt->bindParam(':timeNow2', $timeNow);
if ($stmt->execute()) {
$rows = $stmt->fetchAll();
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
return $rows; return $rows;
} }
@ -1819,17 +1837,38 @@ class Application_Model_Show
{ {
global $CC_CONFIG; global $CC_CONFIG;
$con = Propel::getConnection(); $con = Propel::getConnection();
//
//TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin
$sql = "SELECT si.starts as start_timestamp, si.ends as end_timestamp, s.name," $sql = <<<SQL
." s.id, si.id as instance_id, si.record, s.url, starts, ends" SELECT si.starts AS start_timestamp,
." FROM $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s" si.ends AS end_timestamp,
." WHERE si.show_id = s.id" s.name,
." AND si.starts > TIMESTAMP '$p_timeNow' - INTERVAL '2 days'" s.id,
." AND si.ends < TIMESTAMP '$p_timeNow' + INTERVAL '2 days'" si.id AS instance_id,
." AND modified_instance != TRUE" si.record,
." ORDER BY si.starts"; s.url,
starts,
ends
FROM cc_show_instances si,
cc_show s
WHERE si.show_id = s.id
AND si.starts > TIMESTAMP ':timeNow1' - INTERVAL '2 days'
AND si.ends < TIMESTAMP ':timeNow2' + INTERVAL '2 days'
AND modified_instance != TRUE
ORDER BY si.starts
SQL;
$stmt = $con->prepare($sql);
$stmt->bindParam(':timeNow1', $p_timeNow);
$stmt->bindParam(':timeNow2', $p_timeNow);
if ($stmt->execute()) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
$rows = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$numberOfRows = count($rows); $numberOfRows = count($rows);
$results['previousShow'] = array(); $results['previousShow'] = array();
@ -1923,7 +1962,7 @@ class Application_Model_Show
* @param String $timeEnd - interval end time (in UTC) * @param String $timeEnd - interval end time (in UTC)
* @return array - the next $limit number of shows within the time interval * @return array - the next $limit number of shows within the time interval
*/ */
public static function getNextShows($timeStart, $limit = "0", $timeEnd = "") public static function getNextShows($timeStart, $limit = "ALL", $timeEnd = "")
{ {
global $CC_CONFIG; global $CC_CONFIG;
$con = Propel::getConnection(); $con = Propel::getConnection();
@ -1937,21 +1976,38 @@ class Application_Model_Show
} }
//TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin
$sql = "SELECT si.starts as start_timestamp, si.ends as end_timestamp, s.name, s.id, si.id as instance_id, si.record, s.url, starts, ends FROM" $sql = <<<SQL
." $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s" SELECT si.starts AS start_timestamp,
." WHERE si.show_id = s.id" si.ends AS end_timestamp,
." AND si.starts >= TIMESTAMP '$timeStart'" s.name,
." AND si.starts < TIMESTAMP $timeEnd" s.id,
." AND modified_instance != TRUE" si.id AS instance_id,
." ORDER BY si.starts"; si.record,
s.url,
starts,
ends
FROM cc_show_instances,
cc_show
WHERE si.show_id = s.id"
AND si.starts >= TIMESTAMP ':timeStart'
AND si.starts < TIMESTAMP ':timeEnd'
AND modified_instance != TRUE
ORDER BY si.starts
LIMIT :lim
SQL;
// defaults to retrieve all shows within the interval if $limit not set $stmt = $con->prepare($sql);
if ($limit != "0") { $stmt->bindParam(':timeStart', $timeStart);
$sql = $sql . " LIMIT $limit"; $stmt->bindParam(':timeEnd', $timeEnd);
$stmt->bindParam(':lim', $limit);
if ($stmt->execute()) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$rows = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC);
return $rows; return $rows;
} }
@ -1981,6 +2037,7 @@ class Application_Model_Show
global $CC_CONFIG; global $CC_CONFIG;
$con = Propel::getConnection(); $con = Propel::getConnection();
//Not using prepared statement here since not using an variable input.
$sql = "SELECT column_name, character_maximum_length FROM information_schema.columns" $sql = "SELECT column_name, character_maximum_length FROM information_schema.columns"
." WHERE table_name = 'cc_show' AND character_maximum_length > 0"; ." WHERE table_name = 'cc_show' AND character_maximum_length > 0";
$result = $con->query($sql)->fetchAll(); $result = $con->query($sql)->fetchAll();