CC-4345: Prepared statements - part 1

-modified Block.php to use Application_Common_Database::prepareAndExecute()
This commit is contained in:
denise 2012-09-06 11:11:09 -04:00
parent 5481191730
commit f06b3e2c36
1 changed files with 88 additions and 177 deletions

View File

@ -199,16 +199,8 @@ class Application_Model_Block implements Application_Model_LibraryEditable
WHERE pc.block_id = :block_id WHERE pc.block_id = :block_id
ORDER BY pc.position; ORDER BY pc.position;
EOT; EOT;
$con = Propel::getConnection();
$stmt = $con->prepare($sql); $rows = Application_Common_Database::prepareAndExecute($sql, array(':block_id'=>$this->id));
$stmt->bindParam(':block_id', $this->id);
if ($stmt->execute()) {
$rows = $stmt->fetchAll();
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
$offset = 0; $offset = 0;
foreach ($rows as &$row) { foreach ($rows as &$row) {
@ -332,15 +324,8 @@ EOT;
public function getStaticLength() public function getStaticLength()
{ {
$sql = "SELECT SUM(cliplength) as length FROM cc_blockcontents WHERE block_id = :block_id"; $sql = "SELECT SUM(cliplength) as length FROM cc_blockcontents WHERE block_id = :block_id";
$stmt = $this->con->prepare($sql); $result = Application_Common_Database::prepareAndExecute($sql, array(':block_id'=>$this->id), 'all', PDO::FETCH_NUM);
$stmt->bindParam(':block_id', $this->id); Logging::info($result);
if ($stmt->execute()) {
$result = $stmt->fetchAll(PDO::FETCH_NUM);
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("error: $msg");
}
return $result[0][0]; return $result[0][0];
} }
@ -653,39 +638,31 @@ EOT;
if (!is_null($fadeIn)) { if (!is_null($fadeIn)) {
$sql = "SELECT :fade_in::INTERVAL > :clip_length::INTERVAL"; $sql = "SELECT :fade_in::INTERVAL > :clip_length::INTERVAL";
$params = array(
':fade_in' => $fadeIn,
':clip_length' => $clipLength
);
$stmt = $this->con->prepare($sql); $result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
$stmt->bindParam(':fade_in', $fadeIn); if ($result) {
$stmt->bindParam(':clip_length', $clipLength); //"Fade In can't be larger than overall playlength.";
$fadeIn = $clipLength;
if ($stmt->execute()) {
if ($stmt->fetchColumn(0)) {
//"Fade In can't be larger than overall playlength.";
$fadeIn = $clipLength;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$row->setDbFadein($fadeIn); $row->setDbFadein($fadeIn);
} }
if (!is_null($fadeOut)) { if (!is_null($fadeOut)) {
$sql = "SELECT :fade_out::INTERVAL > :clip_length::INTERVAL"; $sql = "SELECT :fade_out::INTERVAL > :clip_length::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':fade_out' => $fadeOut,
$stmt->bindParam(':fade_out', $fadeOut); ':clip_length' => $clipLength
$stmt->bindParam(':clip_length', $clipLength); );
if ($stmt->execute()) { $result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->fetchColumn(0)) { if ($result) {
//Fade Out can't be larger than overall playlength."; //"Fade Out can't be larger than overall playlength.";
$fadeOut = $clipLength; $fadeOut = $clipLength;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$row->setDbFadeout($fadeOut); $row->setDbFadeout($fadeOut);
} }
@ -773,52 +750,31 @@ EOT;
$cueOut = $origLength; $cueOut = $origLength;
} }
$sql = "SELECT :cue_in::INTERVAL > :cue_out::INTERVAL"; $sql = "SELECT :cue_out::INTERVAL > :orig_length::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':cue_out' => $cueOut,
$stmt->bindParam(':cue_in', $cueIn); ':orig_length' => $origLength
$stmt->bindParam(':cue_out', $cueOut); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $errArray["error"] = "Can't set cue out to be greater than file length.";
$errArray["error"] = "Can't set cue in to be larger than cue out."; return $errArray;
return $errArray;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$sql = "SELECT :cue_out::INTERVAL > INTERVAL :orig_length::INTERVAL"; $sql = "SELECT :cue_in::INTERVAL > :cue_out::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':cue_in' => $cueIn,
$stmt->bindParam(':cue_out', $cueOut); ':cue_out' => $cueOut
$stmt->bindParam(':orig_length', $origLength); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $errArray["error"] = "Can't set cue in to be larger than cue out.";
$errArray["error"] = "Can't set cue out to be greater than file length."; return $errArray;
return $errArray;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$sql = "SELECT :cue_out::INTERVAL - :cue_in::INTERVAL"; $sql = "SELECT :cue_out::INTERVAL - :cue_in::INTERVAL";
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
$stmt = $this->con->prepare($sql); $cliplength = $result;
$stmt->bindParam(':cue_out', $cueOut);
$stmt->bindParam(':cue_in', $cueIn);
if ($stmt->execute()) {
$cliplength = $stmt->fetchColumn(0);
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
$row->setDbCuein($cueIn); $row->setDbCuein($cueIn);
$row->setDbCueout($cueOut); $row->setDbCueout($cueOut);
@ -827,89 +783,54 @@ EOT;
} elseif (!is_null($cueIn)) { } elseif (!is_null($cueIn)) {
$sql = "SELECT :cue_in::INTERVAL > :old_cue_out::INTERVAL"; $sql = "SELECT :cue_in::INTERVAL > :old_cue_out::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':cue_in' => $cueIn,
$stmt->bindParam(':cue_in', $cueIn); ':old_cue_out' => $oldCueOut
$stmt->bindParam(':old_cue_out', $oldCueOut); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $errArray["error"] = "Can't set cue in to be larger than cue out.";
$errArray["error"] = "Can't set cue in to be larger than cue out."; return $errArray;
return $errArray;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$sql = "SELECT :old_cue_out::INTERVAL - :cue_in::INTERVAL"; $sql = "SELECT :old_cue_out::INTERVAL - :cue_in::INTERVAL";
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
$stmt = $this->con->prepare($sql); $cliplength = $result;
$stmt->bindParam(':old_cue_out', $oldCueOut);
$stmt->bindParam(':cue_in', $cueIn);
if ($stmt->execute()) {
$cliplength = $stmt->fetchColumn(0);
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
$row->setDbCuein($cueIn); $row->setDbCuein($cueIn);
$row->setDBCliplength($cliplength); $row->setDBCliplength($cliplength);
} elseif (!is_null($cueOut)) { } elseif (!is_null($cueOut)) {
if ($cueOut === "") { if ($cueOut === "") {
$cueOut = $origLength; $cueOut = $origLength;
} }
$sql = "SELECT :cue_out::INTERVAL < :old_cue_in::INTERVAL"; $sql = "SELECT :cue_out::INTERVAL > :orig_length::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':cue_out' => $cueOut,
$stmt->bindParam(':cue_out', $cueOut); ':orig_length' => $origLength
$stmt->bindParam(':old_cue_in', $oldCueIn); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $errArray["error"] = "Can't set cue out to be greater than file length.";
$errArray["error"] = "Can't set cue out to be smaller than cue in."; return $errArray;
return $errArray;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$sql = "SELECT :cue_out::INTERVAL > :orig_length::INTERVAL"; $sql = "SELECT :cue_out::INTERVAL < :old_cue_in::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':cue_out' => $cueOut,
$stmt->bindParam(':cue_out', $cueOut); ':old_cue_in' => $oldCueIn
$stmt->bindParam(':orig_length', $origLength); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $errArray["error"] = "Can't set cue out to be smaller than cue in.";
$errArray["error"] = "Can't set cue out to be greater than file length."; return $errArray;
return $errArray;
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$sql = "SELECT :cue_out::INTERVAL - :old_cue_in::INTERVAL"; $sql = "SELECT :cue_out::INTERVAL - :old_cue_in::INTERVAL";
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
$stmt = $this->con->prepare($sql); $cliplength = $result;
$stmt->bindParam(':cue_out', $cueOut);
$stmt->bindParam(':old_cue_in', $oldCueIn);
if ($stmt->execute()) {
$cliplength = $stmt->fetchColumn(0);
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
$row->setDbCueout($cueOut); $row->setDbCueout($cueOut);
$row->setDBCliplength($cliplength); $row->setDBCliplength($cliplength);
@ -918,35 +839,25 @@ EOT;
$cliplength = $row->getDbCliplength(); $cliplength = $row->getDbCliplength();
$sql = "SELECT :fade_in::INTERVAL > :clip_length::INTERVAL"; $sql = "SELECT :fade_in::INTERVAL > :clip_length::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':fade_in' => $fadeIn,
$stmt->bindParam(':fade_in', $fadeIn); ':clip_length' => $cliplength
$stmt->bindParam(':clip_length', $cliplength); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $fadeIn = $cliplength;
$fadeIn = $cliplength; $row->setDbFadein($fadeIn);
$row->setDbFadein($fadeIn);
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$sql = "SELECT :fade_out::INTERVAL > :clip_length::INTERVAL"; $sql = "SELECT :fade_out::INTERVAL > :clip_length::INTERVAL";
$params = array(
$stmt = $this->con->prepare($sql); ':fade_out' => $fadeOut,
$stmt->bindParam(':fade_out', $fadeOut); ':clip_length' => $cliplength
$stmt->bindParam(':clip_length', $cliplength); );
$result = Application_Common_Database::prepareAndExecute($sql, $params, 'column');
if ($stmt->execute()) { if ($result) {
if ($stmt->fetchColumn(0)) { $fadeOut = $cliplength;
$fadeOut = $cliplength; $row->setDbFadein($fadeOut);
$row->setDbFadein($fadeOut);
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
} }
$row->save($this->con); $row->save($this->con);