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->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"));
if (count($rows) > 0) {
@ -928,7 +928,7 @@ class ApiController extends Zend_Controller_Action
}
} elseif ($djtype == "dj") {
//check against show dj auth
$showInfo = Application_Model_Show::GetCurrentShow();
$showInfo = Application_Model_Show::getCurrentShow();
// there is current playing show
if (isset($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();
$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;
@ -51,7 +51,7 @@ class DashboardController extends Zend_Controller_Action
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
$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;
$source_connected = Application_Model_Preference::GetSourceStatus($sourcename);

View File

@ -121,7 +121,7 @@ class ScheduleController extends Zend_Controller_Action
public function getCurrentShowAction()
{
$currentShow = Application_Model_Show::GetCurrentShow();
$currentShow = Application_Model_Show::getCurrentShow();
if (!empty($currentShow)) {
$this->view->si_id = $currentShow[0]["instance_id"];
$this->view->current_show = true;
@ -346,7 +346,7 @@ class ScheduleController extends Zend_Controller_Action
public function getCurrentPlaylistAction()
{
$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. */
if (isset($range["previous"])) {

View File

@ -1788,7 +1788,7 @@ class Application_Model_Show
* @param String $timeNow - current time (in UTC)
* @return array - show being played right now
*/
public static function GetCurrentShow($timeNow=null)
public static function getCurrentShow($timeNow=null)
{
global $CC_CONFIG;
$con = Propel::getConnection();
@ -1797,16 +1797,34 @@ class Application_Model_Show
$timeNow = $date->getUtcTimestamp();
}
//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 $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s"
." WHERE si.show_id = s.id"
." AND si.starts <= TIMESTAMP '$timeNow'"
." AND si.ends > TIMESTAMP '$timeNow'"
." AND modified_instance != TRUE";
$sql = <<<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 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
$rows = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$stmt = $con->prepare($sql);
$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;
}
@ -1819,17 +1837,38 @@ class Application_Model_Show
{
global $CC_CONFIG;
$con = Propel::getConnection();
//
//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 $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s"
." WHERE si.show_id = s.id"
." AND si.starts > TIMESTAMP '$p_timeNow' - INTERVAL '2 days'"
." AND si.ends < TIMESTAMP '$p_timeNow' + INTERVAL '2 days'"
." AND modified_instance != TRUE"
." ORDER BY si.starts";
$sql = <<<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 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);
$results['previousShow'] = array();
@ -1923,7 +1962,7 @@ class Application_Model_Show
* @param String $timeEnd - interval end time (in UTC)
* @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;
$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
$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"
." $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s"
." WHERE si.show_id = s.id"
." AND si.starts >= TIMESTAMP '$timeStart'"
." AND si.starts < TIMESTAMP $timeEnd"
." AND modified_instance != TRUE"
." ORDER BY si.starts";
$sql = <<<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 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
if ($limit != "0") {
$sql = $sql . " LIMIT $limit";
$stmt = $con->prepare($sql);
$stmt->bindParam(':timeStart', $timeStart);
$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;
}
@ -1981,6 +2037,7 @@ class Application_Model_Show
global $CC_CONFIG;
$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"
." WHERE table_name = 'cc_show' AND character_maximum_length > 0";
$result = $con->query($sql)->fetchAll();