Example #1
0
 /**
  * @param bool $forceRegen
  * @return Result\Update
  * @throws FREST\Exception
  */
 public function generateResult($forceRegen = FALSE)
 {
     $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'update');
     $otherResult = parent::generateResult($forceRegen);
     if (isset($otherResult)) {
         return $otherResult;
     }
     $pdo = $this->frest->getConfig()->getPDO();
     $isPerformingTransaction = FALSE;
     if (count($this->tableUpdateSpecs) > 1) {
         $pdo->beginTransaction();
         $isPerformingTransaction = TRUE;
     }
     /** @var Setting\Field $idFieldSetting */
     $this->resource->getIDField($idFieldSetting);
     // populates
     $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'update');
     $i = 0;
     /** @var Spec\TableUpdate $tableUpdateSpec */
     foreach ($this->tableUpdateSpecs as $tableUpdateSpec) {
         $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'update');
         $table = $tableUpdateSpec->getTable();
         $queryParameterSpecs = $tableUpdateSpec->getQueryParameterSpecs();
         $idFieldName = $this->resource->getIDFieldForTable($table);
         $assignmentStringList = $this->generateAssignmentStringList($queryParameterSpecs);
         $assignmentString = implode(',', $assignmentStringList);
         $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'update');
         $this->frest->startTimingForLabel(Type\Timing::SQL, 'update');
         $sql = "UPDATE {$table} SET {$assignmentString} WHERE {$idFieldName} = :_id";
         $updateStmt = $pdo->prepare($sql);
         $updateStmt->bindValue(':_id', $this->resourceID, Type\Variable::pdoTypeFromVariableType($idFieldSetting->getVariableType()));
         /** @var Spec\QueryParameter $queryParameterSpec */
         foreach ($queryParameterSpecs as $queryParameterSpec) {
             $updateStmt->bindValue($queryParameterSpec->getParameterName(), $queryParameterSpec->getValue(), Type\Variable::pdoTypeFromVariableType($queryParameterSpec->getVariableType()));
         }
         if (!$updateStmt->execute()) {
             if ($isPerformingTransaction) {
                 $pdo->rollBack();
             }
             throw new FREST\Exception(FREST\Exception::SQLError, 'Error updating database');
         }
         $this->frest->stopTimingForLabel(Type\Timing::SQL, 'update');
         $i++;
     }
     $this->frest->startTimingForLabel(Type\Timing::SQL, 'update');
     if ($isPerformingTransaction) {
         $pdo->commit();
     }
     $this->frest->stopTimingForLabel(Type\Timing::SQL, 'update');
     $this->result = new Result\Update();
     return $this->result;
 }
Example #2
0
 /**
  * @param bool $forceRegen
  * @return Result\Delete
  * @throws FREST\Exception
  */
 public function generateResult($forceRegen = FALSE)
 {
     $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'delete');
     $otherResult = parent::generateResult($forceRegen);
     if (isset($otherResult)) {
         return $otherResult;
     }
     $pdo = $this->frest->getConfig()->getPDO();
     /** @var Setting\Field $idFieldSetting */
     $this->resource->getIDField($idFieldSetting);
     $isPerformingTransaction = FALSE;
     if (count($this->tableDeleteSpecs) > 1) {
         $pdo->beginTransaction();
         $isPerformingTransaction = TRUE;
     }
     $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'delete');
     /** @var Spec\TableDelete $tableDeleteSpec */
     foreach ($this->tableDeleteSpecs as $tableDeleteSpec) {
         $table = $tableDeleteSpec->getTable();
         $idFieldName = $this->resource->getIDFieldForTable($table);
         $this->frest->startTimingForLabel(Type\Timing::SQL, 'delete');
         $sql = "DELETE FROM {$table} WHERE {$idFieldName} = :_id";
         $deleteStmt = $pdo->prepare($sql);
         $deleteStmt->bindValue(':_id', $this->resourceID, Type\Variable::pdoTypeFromVariableType($idFieldSetting->getVariableType()));
         if (!$deleteStmt->execute()) {
             if ($isPerformingTransaction) {
                 $pdo->rollBack();
             }
             throw new FREST\Exception(FREST\Exception::SQLError, 'Error deleting from database');
         }
         $this->frest->stopTimingForLabel(Type\Timing::SQL, 'delete');
     }
     $this->frest->startTimingForLabel(Type\Timing::SQL, 'delete');
     if ($isPerformingTransaction) {
         $pdo->commit();
     }
     $this->frest->stopTimingForLabel(Type\Timing::SQL, 'delete');
     $this->result = new Result\Delete();
     return $this->result;
 }
Example #3
0
 /**
  * @param bool $forceRegen
  * @return Result\Create
  * @throws FREST\Exception
  */
 public function generateResult($forceRegen = FALSE)
 {
     $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'create');
     $otherResult = parent::generateResult($forceRegen);
     if (isset($otherResult)) {
         return $otherResult;
     }
     $pdo = $this->frest->getConfig()->getPDO();
     $isPerformingTransaction = FALSE;
     if (count($this->tableCreateSpecs) > 1) {
         $pdo->beginTransaction();
         $isPerformingTransaction = TRUE;
     }
     $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'create');
     $i = 0;
     /** @var \FREST\Spec\TableCreate $tableCreateSpec */
     foreach ($this->tableCreateSpecs as $tableCreateSpec) {
         $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'create');
         $table = $tableCreateSpec->getTable();
         $queryParameterSpecs = $tableCreateSpec->getQueryParameterSpecs();
         if ($i > 0) {
             if (!isset($createdResourceID)) {
                 throw new FREST\Exception(FREST\Exception::SQLError, 'No ID generated or set for the created resource');
             }
             // TODO: potential multiple table ID problems
             $alias = 'id';
             $idFieldName = $this->resource->getIDFieldForTable($table);
             $idQueryParameterSpec = new Spec\QueryParameter($idFieldName, ':' . $alias, $createdResourceID, Type\Variable::INT);
             // put id spec at beginning of spec list
             $queryParameterSpecs = array($alias => $idQueryParameterSpec) + $tableCreateSpec;
         }
         $fieldStringList = $this->generateFieldStringList($queryParameterSpecs);
         $parameterStringList = $this->generateParameterStringList($queryParameterSpecs);
         $fieldsString = implode(',', $fieldStringList);
         $parametersString = implode(',', $parameterStringList);
         $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'create');
         $this->frest->startTimingForLabel(Type\Timing::SQL, 'create');
         $sql = "INSERT INTO {$table} ({$fieldsString}) VALUES ({$parametersString})";
         $createStmt = $pdo->prepare($sql);
         /** @var Spec\QueryParameter $queryParameterSpec */
         foreach ($queryParameterSpecs as $queryParameterSpec) {
             $createStmt->bindValue($queryParameterSpec->getParameterName(), $queryParameterSpec->getValue(), Type\Variable::pdoTypeFromVariableType($queryParameterSpec->getVariableType()));
         }
         if (!$createStmt->execute()) {
             if ($isPerformingTransaction) {
                 $pdo->rollBack();
             }
             throw new FREST\Exception(FREST\Exception::SQLError, 'Error inserting into database');
         }
         if ($i == 0) {
             $createdResourceID = isset($this->resourceID) ? $this->resourceID : $pdo->lastInsertID();
         }
         $this->frest->stopTimingForLabel(Type\Timing::SQL, 'create');
         $i++;
     }
     $this->frest->startTimingForLabel(Type\Timing::SQL, 'create');
     if ($isPerformingTransaction) {
         $pdo->commit();
     }
     $this->frest->stopTimingForLabel(Type\Timing::SQL, 'create');
     $this->frest->startTimingForLabel(Type\Timing::POST_PROCESSING, 'create');
     if (!isset($createdResourceID)) {
         throw new FREST\Exception(FREST\Exception::SQLError, 'No ID generated or set for the created resource');
     }
     $this->result = new Result\Create($createdResourceID);
     $this->frest->stopTimingForLabel(Type\Timing::POST_PROCESSING, 'create');
     return $this->result;
 }