/** * @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; }
/** * @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; }
/** * @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; }
/** * @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; }