Esempio n. 1
0
 /**
  * @param bool $forceRegen
  * @return Result\SingularRead
  * @throws FREST\Exception
  */
 public function generateResult($forceRegen = FALSE)
 {
     $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'singularread');
     $otherResult = parent::generateResult($forceRegen);
     if (isset($otherResult)) {
         return $otherResult;
     }
     $fieldString = $this->generateFieldString($this->fieldSpecs);
     $tablesToReadString = $this->generateTableString($this->tableSpecs);
     $joinString = $this->generateJoinString($this->resource, $this->joinSpecs);
     /** @var Setting\Field $idFieldSetting */
     $idField = $this->resource->getIDField($idFieldSetting);
     $tableWithID = $this->resource->getTableForField($idField);
     $tableAbbrvWithID = $this->getTableAbbreviation($tableWithID);
     $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'singularread');
     $this->frest->startTimingForLabel(Type\Timing::SQL, 'singularread');
     $pdo = $this->frest->getConfig()->getPDO();
     $sql = "SELECT {$fieldString} FROM {$tablesToReadString}{$joinString} WHERE {$tableAbbrvWithID}.{$idField} = :id LIMIT 1";
     $stmt = $pdo->prepare($sql);
     $success = $stmt->execute(array(':id' => $this->resourceID));
     if (!$success) {
         throw new FREST\Exception(FREST\Exception::SQLError, 'Failed reading resource from database');
     }
     $objects = $stmt->fetchAll(\PDO::FETCH_OBJ);
     $resultsCount = count($objects);
     if ($resultsCount == 0) {
         throw new FREST\Exception(FREST\Exception::NoResults);
     }
     $this->frest->stopTimingForLabel(Type\Timing::SQL, 'singularread');
     $this->parseObjects($this->resource, $objects, $this->readSettings);
     $this->result = new Result\SingularRead($objects[0]);
     return $this->result;
 }
Esempio n. 2
0
 /**
  * @param bool $forceRegen
  * @return Result\PluralRead|Result\Error
  * @throws FREST\Exception
  */
 public function generateResult($forceRegen = FALSE)
 {
     $this->frest->startTimingForLabel(Type\Timing::PROCESSING, 'pluralread');
     $otherResult = parent::generateResult($forceRegen);
     if (isset($otherResult)) {
         return $otherResult;
     }
     $fieldString = $this->generateFieldString($this->fieldSpecs);
     $tablesToReadString = $this->generateTableString($this->tableSpecs);
     $conditionString = $this->processConditionSpecs($this->conditionSpecs, $this->queryParameterSpecs);
     $offset = $this->generateOffset($this->queryParameterSpecs);
     $limit = $this->generateLimit($this->resource, $this->queryParameterSpecs);
     $orderByString = $this->generateOrderString($this->orderSpecs);
     $joinsString = $this->generateJoinString($this->resource, $this->joinSpecs);
     $this->frest->stopTimingForLabel(Type\Timing::PROCESSING, 'pluralread');
     $this->frest->startTimingForLabel(Type\Timing::SQL, 'pluralread');
     $pdo = $this->frest->getConfig()->getPDO();
     // SQL
     $sqlParts = array();
     $countSQLParts = array();
     $sqlParts[] = "SELECT {$fieldString} FROM {$tablesToReadString}";
     $countSQLParts[] = "SELECT COUNT(0) AS Count FROM {$tablesToReadString}";
     if (strlen($joinsString)) {
         $sqlParts[] = $joinsString;
         $countSQLParts[] = $joinsString;
     }
     if (strlen($conditionString)) {
         $sqlParts[] = $conditionString;
         $countSQLParts[] = $conditionString;
     }
     if (strlen($orderByString)) {
         $sqlParts[] = $orderByString;
     }
     $sqlParts[] = "LIMIT :_offset, :_limit";
     $sql = implode(' ', $sqlParts);
     $countSQL = implode(' ', $countSQLParts);
     $resultsStmt = $pdo->prepare($sql);
     $countStmt = $pdo->prepare($countSQL);
     /** @var Spec\QueryParameter $queryParameterSpec */
     foreach ($this->queryParameterSpecs as $parameterName => $queryParameterSpec) {
         $pdoParamType = Type\Variable::pdoTypeFromVariableType($queryParameterSpec->getVariableType());
         if ($parameterName !== '_limit' && $parameterName !== '_offset') {
             $countStmt->bindValue($queryParameterSpec->getParameterName(), $queryParameterSpec->getValue(), $pdoParamType);
         }
         $resultsStmt->bindValue($queryParameterSpec->getParameterName(), $queryParameterSpec->getValue(), $pdoParamType);
     }
     //die($sql);
     if (!$resultsStmt->execute()) {
         throw new FREST\Exception(FREST\Exception::SQLError, 'Error querying database for Result');
     }
     $objects = $resultsStmt->fetchAll(\PDO::FETCH_OBJ);
     if (!$countStmt->execute()) {
         throw new FREST\Exception(FREST\Exception::SQLError, 'Error querying database for count');
     }
     $countResult = $countStmt->fetchAll(\PDO::FETCH_ASSOC);
     $count = intval($countResult[0]['Count']);
     $this->frest->stopTimingForLabel(Type\Timing::SQL, 'pluralread');
     $this->parseObjects($this->resource, $objects, $this->readSettings);
     $this->result = new Result\PluralRead($objects, $limit, $offset, $count);
     return $this->result;
 }