From f60ec39d92217a4f8972f311203b216820446590 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 6 Jan 2024 21:11:52 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Response/SQLServerRow.php | 4 +-- src/Traits/Execute.php | 38 --------------------------- src/Traits/Methods.php | 4 ++- src/Validator/StatementsValidator.php | 2 +- 4 files changed, 6 insertions(+), 42 deletions(-) diff --git a/src/Response/SQLServerRow.php b/src/Response/SQLServerRow.php index a1fbda5..c5a2dd1 100644 --- a/src/Response/SQLServerRow.php +++ b/src/Response/SQLServerRow.php @@ -7,10 +7,10 @@ class SQLServerRow /** * @param array $values */ - public function __construct(array $values) { + public function __construct(array $values) + { foreach ($values as $key => $value) { $this->{$key} = $value; } } - } diff --git a/src/Traits/Execute.php b/src/Traits/Execute.php index 8b0f48a..0310dab 100644 --- a/src/Traits/Execute.php +++ b/src/Traits/Execute.php @@ -35,7 +35,6 @@ private function execGeneral() private function execSelect() { try { - $PDO = $this->connection->prepare($this->statement); StatementsValidator::isValidParams($this->params); @@ -52,9 +51,7 @@ private function execSelect() $rows = $PDO->fetchAll(PDO::FETCH_ASSOC); $this->response = $rows; - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Select Query: '.$e->getMessage()); } } @@ -67,7 +64,6 @@ private function execSelect() private function execUpdate() { try { - $this->inactivateCheckConstraint(); $PDO = $this->connection->prepare($this->statement); @@ -87,9 +83,7 @@ private function execUpdate() $this->response = $response && $PDO->rowCount() > 0; $this->activateCheckConstraint(); - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Update Query: '.$e->getMessage()); } } @@ -102,11 +96,9 @@ private function execUpdate() private function execInsert() { try { - $this->inactivateCheckConstraint(); if (!empty($this->params)) { - $PDO = $this->connection->prepare($this->statement); if (Utilities::hasSubArrays($this->params)) { @@ -131,18 +123,14 @@ private function execInsert() /** @phpstan-ignore-next-line */ $this->response = $response && $PDO->rowCount() > 0; - } else { - $response = $this->connection->exec($this->statement); $this->response = $response > 0; } $this->activateCheckConstraint(); - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Insert Query: '.$e->getMessage()); } } @@ -155,15 +143,12 @@ private function execInsert() private function execInsertGetId() { try { - $this->inactivateCheckConstraint(); if (!empty($this->params)) { - $PDO = $this->connection->prepare($this->statement); if (Utilities::hasSubArrays($this->params)) { - $ids = []; foreach ($this->params as $key => $param) { @@ -179,9 +164,7 @@ private function execInsertGetId() } $this->response = $ids; - } else { - foreach ($this->params as $key => $value) { if (strpos($this->statement, ":$key") !== false) { $PDO->bindParam($key, $this->params[$key]); @@ -192,18 +175,14 @@ private function execInsertGetId() $this->response = ($response && $PDO->rowCount() > 0) ? $this->connection->lastInsertId() : null; } - } else { - $response = $this->connection->exec($this->statement); $this->response = $response > 0; } $this->activateCheckConstraint(); - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Insert Query: '.$e->getMessage()); } } @@ -216,7 +195,6 @@ private function execInsertGetId() private function execDelete() { try { - $this->inactivateCheckConstraint(); if (!empty($this->params)) { @@ -231,18 +209,14 @@ private function execDelete() $PDO->execute(); $this->response = $PDO->rowCount() > 0; - } else { - $PDO = $this->connection->exec($this->statement); $this->response = $PDO !== false; } $this->activateCheckConstraint(); - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Delete Query: '.$e->getMessage()); } } @@ -255,13 +229,11 @@ private function execDelete() private function execProcedure() { try { - $PDO = $this->connection->prepare($this->statement); StatementsValidator::isValidParams($this->params); if (!empty($this->params)) { - foreach ($this->params as $key => $value) { if (strpos($this->statement, ":$key") !== false) { $PDO->bindParam($key, $this->params[$key]); @@ -273,9 +245,7 @@ private function execProcedure() $rows = $PDO->fetchAll(PDO::FETCH_ASSOC); $this->response = $rows; - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Store Procedure Query: '.$e->getMessage()); } } @@ -288,7 +258,6 @@ private function execProcedure() private function execTransactionalProcedure() { try { - $PDO = $this->connection->prepare($this->statement); StatementsValidator::isValidParams($this->params); @@ -305,9 +274,7 @@ private function execTransactionalProcedure() $PDO->closeCursor(); $this->response = $result; - } catch (\Exception $e) { - throw SQLServerException::create('Error executing SQL Transactional Store Procedure Query: '.$e->getMessage()); } } @@ -344,23 +311,18 @@ private function inactivateCheckConstraint() private function activateCheckConstraint() { if ($this->constraints) { - if (!empty($this->constraintsTables)) { - foreach ($this->constraintsTables as $key => $table) { $stmt = "ALTER TABLE {$table} CHECK CONSTRAINT ALL;"; $this->connection->exec($stmt); } - } else { - $nameTable = Utilities::getNameTable($this->statement); if (!empty($nameTable)) { $stmt = "ALTER TABLE {$nameTable} CHECK CONSTRAINT ALL;"; $this->connection->exec($stmt); } - } } } diff --git a/src/Traits/Methods.php b/src/Traits/Methods.php index e717262..067f13e 100644 --- a/src/Traits/Methods.php +++ b/src/Traits/Methods.php @@ -3,8 +3,8 @@ namespace Rmunate\SqlServerLite\Traits; use Closure; -use Rmunate\SqlServerLite\Response\SQLServerRow; use Rmunate\SqlServerLite\Response\SQLServerResponse; +use Rmunate\SqlServerLite\Response\SQLServerRow; trait Methods { @@ -55,6 +55,7 @@ public function first() if (!empty($this->response)) { $first = reset($this->response); + return new SQLServerRow($first); } @@ -72,6 +73,7 @@ public function last() if (!empty($this->response)) { $first = end($this->response); + return new SQLServerRow($first); } diff --git a/src/Validator/StatementsValidator.php b/src/Validator/StatementsValidator.php index 11448bf..217576e 100644 --- a/src/Validator/StatementsValidator.php +++ b/src/Validator/StatementsValidator.php @@ -28,7 +28,7 @@ public static function withoutComments(string $statement) $statement = preg_replace('/\/\*.*?\*\//s', '', $statement); // Check for forbidden comment strings - $forbiddenCommentPatterns = ["/*", "*/", "//", "--", "<-", "->"]; + $forbiddenCommentPatterns = ['/*', '*/', '//', '--', '<-', '->']; foreach ($forbiddenCommentPatterns as $commentPattern) { if (stripos($statement, $commentPattern)) { throw SQLServerException::create("The statement contains comments ($commentPattern); please remove them to proceed.");