Changed propel to prepared statements

This commit is contained in:
denise 2013-05-22 16:10:10 -04:00 committed by Martin Konecny
parent 792366e1f5
commit 0107f36206
2 changed files with 237 additions and 116 deletions

View file

@ -388,7 +388,7 @@ class Application_Model_Scheduler
return $dt;
}
private function findNextStartTime($DT, $instance)
private function findNextStartTime($DT, $instanceId)
{
$sEpoch = $DT->format("U.u");
$nEpoch = $this->epochNow;
@ -410,7 +410,7 @@ class Application_Model_Scheduler
->setDbCueIn('00:00:00')
->setDbCueOut('00:00:00')
->setDbPlayoutStatus(-1)
->setDbInstanceId($instance->getDbId())
->setDbInstanceId($instanceId)
->save($this->con);
} else {
$nextDT = $DT;
@ -521,7 +521,12 @@ class Application_Model_Scheduler
$linked = false;
$dropIndex_sql = "DROP INDEX cc_schedule_instance_id_idx";
Application_Common_Database::prepareAndExecute(
$dropIndex_sql, array(), Application_Common_Database::EXECUTE);
foreach ($scheduleItems as $schedule) {
Logging::info($schedule);
$id = intval($schedule["id"]);
/* Find out if the show where the cursor position (where an item will
@ -531,12 +536,18 @@ class Application_Model_Scheduler
* of inserted items
*/
if ($id != 0) {
$ccSchedule = CcScheduleQuery::create()->findPk($id);
$ccShowInstance = CcShowInstancesQuery::create()->findPk($ccSchedule->getDbInstanceId());
$ccShow = $ccShowInstance->getCcShow();
$linked = $ccShow->isLinked();
$schedule_sql = "SELECT * FROM cc_schedule WHERE id = ".$id;
$ccSchedule = Application_Common_Database::prepareAndExecute(
$schedule_sql, array(), Application_Common_Database::SINGLE);
$show_sql = "SELECT * FROM cc_show WHERE id IN (".
"SELECT show_id FROM cc_show_instances WHERE id = ".$ccSchedule["instance_id"].")";
$ccShow = Application_Common_Database::prepareAndExecute(
$show_sql, array(), Application_Common_Database::SINGLE);
$linked = $ccShow["linked"];
if ($linked) {
$unique = $ccShow->getDbId() . $ccSchedule->getDbPosition();
$unique = $ccShow["id"] . $ccSchedule["position"];
if (!in_array($unique, $temp)) {
$temp[] = $unique;
} else {
@ -544,11 +555,14 @@ class Application_Model_Scheduler
}
}
} else {
$ccShowInstance = CcShowInstancesQuery::create()->findPk($schedule["instance"]);
$ccShow = $ccShowInstance->getccShow();
$linked = $ccShow->isLinked();
$show_sql = "SELECT * FROM cc_show WHERE id IN (".
"SELECT show_id FROM cc_show_instances WHERE id = ".$schedule["instance"].")";
$ccShow = Application_Common_Database::prepareAndExecute(
$show_sql, array(), Application_Common_Database::SINGLE);
$linked = $ccShow["linked"];
if ($linked) {
$unique = $ccShow->getDbId() . "a";
$unique = $ccShow["id"] . "a";
if (!in_array($unique, $temp)) {
$temp[] = $unique;
} else {
@ -562,39 +576,47 @@ class Application_Model_Scheduler
* to that show
*/
if ($linked) {
$instances = $ccShow->getCcShowInstancess();
$instance_sql = "SELECT * FROM cc_show_instances ".
"WHERE show_id = ".$ccShow["id"];
$instances = Application_Common_Database::prepareAndExecute(
$instance_sql);
} else {
$instances = array($ccShowInstance);
$instance_sql = "SELECT * FROM cc_show_instances ".
"WHERE id = ".$schedule["instance"];
$instances = Application_Common_Database::prepareAndExecute(
$instance_sql);
}
foreach($instances as &$instance) {
$instanceId = $instance->getDbId();
$instanceId = $instance["id"];
if ($id !== 0) {
/* We use the selected cursor's position to find the same
* positions in every other linked instance
*/
$pos = $ccSchedule->getDbPosition();
$pos = $ccSchedule["position"];
$linkCcSchedule = CcScheduleQuery::create()
->filterByDbInstanceId($instanceId)
->filterByDbPosition($pos)
->filterByDbPlayoutStatus(-1, Criteria::NOT_EQUAL)
->findOne();
$linkedItem_sql = "SELECT ends FROM cc_schedule ".
"WHERE instance_id = {$instanceId} ".
"AND position = {$pos} ".
"AND playout_status IS NOT -1";
$linkedItemEnds = Application_Common_Database::prepareAndExecute(
$linkedItem_sql, array(), Application_Common_Database::COLUMN);
$schedItemEndDT = $linkCcSchedule->getDbEnds(null);
$nextStartDT = $this->findNextStartTime($schedItemEndDT, $instance);
$nextStartDT = $this->findNextStartTime(
new DateTime($linkedItemEnds, new DateTimeZone("UTC")),
$instanceId);
$pos++;
}
//selected empty row to add after
else {
$showStartDT = $instance->getDbStarts(null);
$nextStartDT = $this->findNextStartTime($showStartDT, $instance);
$showStartDT = new DateTime($instance["starts"], new DateTimeZone("UTC"));
$nextStartDT = $this->findNextStartTime($showStartDT, $instanceId);
//show is empty so start position counter at 0
$pos = 0;
}
if (!in_array($instance->getDbId(), $affectedShowInstances)) {
if (!in_array($instanceId, $affectedShowInstances)) {
$affectedShowInstances[] = $instanceId;
}
@ -621,39 +643,50 @@ class Application_Model_Scheduler
}
}
$doInsert = false;
$doUpdate = false;
$values = array();
foreach ($filesToInsert as &$file) {
//item existed previously and is being moved.
//need to keep same id for resources if we want REST.
if (isset($file['sched_id'])) {
$sched = CcScheduleQuery::create()->findPk($file["sched_id"]);
$doUpdate = true;
$movedItem_sql = "SELECT * FROM cc_schedule ".
"WHERE id = ".$file["sched_id"];
$sched = Application_Common_Database::prepareAndExecute(
$movedItem_sql, array(), Application_Common_Database::SINGLE);
/* We need to keep a record of the original positon a track
* is being moved from so we can use it to retrieve the correct
* items in linked instances
*/
if (!isset($originalPosition)) {
$originalPosition = $sched->getDbPosition();
$originalPosition = $sched["position"];
}
/* If we are moving an item in a linked show we need to get
* the relative item to move in each instance. We know what the
* relative item is by its position
*/
if ($linked && $moveAction) {
$sched = CcScheduleQuery::create()
->filterByDbInstanceId($instanceId)
->filterByDbPosition($originalPosition)
->findOne();
}
$excludeIds[] = intval($sched->getDbId());
if ($linked) {
$movedItem_sql = "SELECT * FROM cc_schedule ".
"WHERE position = {$originalPosition} ".
"AND instance_id = {$instanceId}";
$file["cliplength"] = $sched->getDbClipLength();
$file["cuein"] = $sched->getDbCueIn();
$file["cueout"] = $sched->getDbCueOut();
$file["fadein"] = $sched->getDbFadeIn();
$file["fadeout"] = $sched->getDbFadeOut();
$sched = Application_Common_Database::prepareAndExecute(
$movedItem_sql, array(), Application_Common_Database::SINGLE);
}
$excludeIds[] = intval($sched["id"]);
$file["cliplength"] = $sched["clip_length"];
$file["cuein"] = $sched["cue_in"];
$file["cueout"] = $sched["cue_out"];
$file["fadein"] = $sched["fade_in"];
$file["fadeout"] = $sched["fade_out"];
} else {
$sched = new CcSchedule();
//$sched = new CcSchedule();
$doInsert = true;
}
$endTimeDT = $this->findEndTime($nextStartDT, $file['cliplength']);
@ -662,30 +695,47 @@ class Application_Model_Scheduler
$file['fadein'] = Application_Common_DateHelper::secondsToPlaylistTime($file['fadein']);
$file['fadeout'] = Application_Common_DateHelper::secondsToPlaylistTime($file['fadeout']);
$sched->setDbStarts($nextStartDT)
->setDbEnds($endTimeDT)
->setDbCueIn($file['cuein'])
->setDbCueOut($file['cueout'])
->setDbFadeIn($file['fadein'])
->setDbFadeOut($file['fadeout'])
->setDbClipLength($file['cliplength'])
->setDbPosition($pos);
if (!$moveAction) {
$sched->setDbInstanceId($instanceId);
}
switch ($file["type"]) {
case 0:
$sched->setDbFileId($file['id']);
$fileId = $file["id"];
$streamId = "null";
break;
case 1:
$sched->setDbStreamId($file['id']);
$streamId = $file["id"];
$fileId = "null";
break;
default: break;
}
$sched->save($this->con);
if ($doInsert) {
$values[] = "(".
"'{$nextStartDT->format("Y-m-d H:i:s")}', ".
"'{$endTimeDT->format("Y-m-d H:i:s")}', ".
"'{$file["cuein"]}', ".
"'{$file["cueout"]}', ".
"'{$file["fadein"]}', ".
"'{$file["fadeout"]}', ".
"'{$file["cliplength"]}', ".
"{$pos}, ".
"{$instanceId}, ".
"{$fileId}, ".
"{$streamId})";
} elseif ($doUpdate) {
$update_sql = "UPDATE cc_schedule SET ".
"starts = '{$nextStartDT->format("Y-m-d H:i:s")}', ".
"ends = '{$endTimeDT->format("Y-m-d H:i:s")}', ".
"cue_in = '{$file["cuein"]}', ".
"cue_out = '{$file["cueout"]}', ".
"fade_in = '{$file["fadein"]}', ".
"fade_out = '{$file["fadeout"]}', ".
"clip_length = '{$file["cliplength"]}', ".
"position = {$pos} ".
"WHERE id = {$sched["id"]}";
Application_Common_Database::prepareAndExecute(
$update_sql, array(), Application_Common_Database::EXECUTE);
}
$nextStartDT = $endTimeDT;
$pos++;
@ -694,9 +744,18 @@ class Application_Model_Scheduler
* after the insert location, we need to exclude the
* schedule item we just inserted because it has correct
* start and end times*/
$excludeIds[] = $sched->getDbId();
//$excludeIds[] = $lastInsertId;
}//all files have been inserted/moved
if ($doInsert) {
$insert_sql = "INSERT INTO cc_schedule ".
"(starts, ends, cue_in, cue_out, fade_in, fade_out, ".
"clip_length, position, instance_id, file_id, stream_id) VALUES ".
implode($values, ",");
Application_Common_Database::prepareAndExecute(
$insert_sql, array(), Application_Common_Database::EXECUTE);
}
// update is_scheduled flag for each cc_file
$fileIds = array();
@ -718,22 +777,30 @@ class Application_Model_Scheduler
}
if ($adjustSched === true) {
$followingSchedItems = CcScheduleQuery::create()
->filterByDBStarts($initalStartDT->format("Y-m-d H:i:s.u"), Criteria::GREATER_EQUAL)
->filterByDbInstanceId($instance->getDbId())
->filterByDbId($excludeIds, Criteria::NOT_IN)
->orderByDbStarts()
->find($this->con);
$followingItems_sql = "SELECT * FROM cc_schedule ".
"WHERE starts >= '{$initalStartDT->format("Y-m-d H:i:s.u")}' ".
"AND instance_id = {$instanceId} ";
if (count($excludeIds) > 0) {
$followingItems_sql .= "AND id NOT IN (". implode($excludeIds, ",").") ";
}
$followingItems_sql .= "ORDER BY starts";
$followingSchedItems = Application_Common_Database::prepareAndExecute(
$followingItems_sql);
$pstart = microtime(true);
//recalculate the start/end times after the inserted items.
foreach ($followingSchedItems as $item) {
$endTimeDT = $this->findEndTime($nextStartDT, $item->getDbClipLength());
$item->setDbStarts($nextStartDT);
$item->setDbEnds($endTimeDT);
$item->setDbPosition($pos);
$item->save($this->con);
$endTimeDT = $this->findEndTime($nextStartDT, $item["clip_length"]);
$update_sql = "UPDATE cc_schedule SET ".
"starts = '{$nextStartDT->format("Y-m-d H:i:s")}', ".
"ends = '{$endTimeDT->format("Y-m-d H:i:s")}', ".
"position = {$pos} ".
"WHERE id = {$item["id"]}";
Application_Common_Database::prepareAndExecute(
$update_sql, array(), Application_Common_Database::EXECUTE);
$nextStartDT = $endTimeDT;
$pos++;
}
@ -742,12 +809,16 @@ class Application_Model_Scheduler
Logging::debug("adjusting all following items.");
Logging::debug(floatval($pend) - floatval($pstart));
$this->calculateCrossfades($instance->getDbId());
$this->calculateCrossfades($instanceId);
}
}//for each instance
}//for each schedule location
$createIndex_sql = "CREATE INDEX cc_schedule_instance_id_idx ".
"ON cc_schedule USING btree(instance_id)";
Application_Common_Database::prepareAndExecute(
$createIndex_sql, array(), Application_Common_Database::EXECUTE);
$endProfile = microtime(true);
Logging::debug("finished adding scheduled items.");
Logging::debug(floatval($endProfile) - floatval($startProfile));
@ -783,6 +854,11 @@ class Application_Model_Scheduler
}
}
private function updateMovedItem()
{
}
private function getInstances($instanceId)
{
$ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);