From 59972d5e45e1612510aae456cc6de0e2a58c8330 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 6 Jul 2023 14:39:40 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/SQLServer.php | 8 -------- src/Traits/Attributes.php | 5 +---- src/Traits/AvailableDrivers.php | 2 +- src/Traits/Constraints.php | 29 +---------------------------- src/Traits/Transaction.php | 7 +++---- 5 files changed, 6 insertions(+), 45 deletions(-) diff --git a/src/SQLServer.php b/src/SQLServer.php index 4394cf5..ec791ec 100755 --- a/src/SQLServer.php +++ b/src/SQLServer.php @@ -251,13 +251,11 @@ final public function insertGetId(string $statement, array $params = []): bool|i } try { - // Create Connection $this->connectionPDO(); /* Validate Type of Query */ if (!empty($params)) { - // Prepare the statement with parameters $stmt = $this->PDO->prepare($statement); @@ -268,20 +266,14 @@ final public function insertGetId(string $statement, array $params = []): bool|i //Retorna El Ultimo ID return ($response && $stmt->rowCount() > 0) ? $this->PDO->lastInsertId() : false; - } else { - // Execute the query $response = $this->PDO->exec($statement); if ($response !== false) { - return $this->PDO->lastInsertId(); - } else { - return false; - } } } catch (\Exception $e) { diff --git a/src/Traits/Attributes.php b/src/Traits/Attributes.php index 7f900c4..b0f6590 100644 --- a/src/Traits/Attributes.php +++ b/src/Traits/Attributes.php @@ -6,7 +6,6 @@ trait Attributes { - /** * Set the query timeout for the PDO connection. * @@ -16,7 +15,6 @@ trait Attributes */ public function setTimeOut(int $seconds = 0) { - // Ensure the connection is established before executing the trait method $this->connectionPDO(); @@ -36,7 +34,6 @@ public function setTimeOut(int $seconds = 0) */ public function setErrorMode(string $mode = 'exception') { - // Ensure the connection is established before executing the trait method $this->connectionPDO(); @@ -110,7 +107,7 @@ public function setAnsiNulls(string $value = 'ON') { // Ensure the connection is established before executing the trait method $this->connectionPDO(); - + // Set Atribute $value = strtoupper($value); diff --git a/src/Traits/AvailableDrivers.php b/src/Traits/AvailableDrivers.php index 1a93f5f..3e9737f 100644 --- a/src/Traits/AvailableDrivers.php +++ b/src/Traits/AvailableDrivers.php @@ -11,7 +11,7 @@ trait AvailableDrivers * * @return array Returns an array of available PDO drivers. */ - public static function getAvailableDrivers() : array + public static function getAvailableDrivers(): array { return PDO::getAvailableDrivers(); } diff --git a/src/Traits/Constraints.php b/src/Traits/Constraints.php index 3f3c8aa..a6e5bf8 100644 --- a/src/Traits/Constraints.php +++ b/src/Traits/Constraints.php @@ -19,14 +19,11 @@ trait Constraints */ public function disableForeignKeys(...$tables) { - // Ensure the connection is established before executing the trait method $this->connectionPDO(); try { - if (!empty($tables)) { - // Disable foreign keys for specified tables $disableForeigns = ''; @@ -35,15 +32,12 @@ public function disableForeignKeys(...$tables) } $this->PDO->exec($disableForeigns); - } else { - // Disable foreign keys for all tables $query = "EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL';"; $success = $this->PDO->exec($query); if ($success === false) { - // If the stored procedure doesn't exist, use the alternative option $query = "SELECT 'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + ' NOCHECK CONSTRAINT ' + name FROM sys.foreign_keys @@ -52,32 +46,24 @@ public function disableForeignKeys(...$tables) $result = $this->PDO->query($query); if ($result) { - // Build an array of ALTER TABLE statements $statements = $result->fetchAll(PDO::FETCH_COLUMN); foreach ($statements as $statement) { - // Disable each foreign key constraint $success = $this->PDO->exec($statement); if ($success === false) { throw new Exception(Messages::disableForeignKeysException()); } - } - } else { - throw new Exception(Messages::disableForeignKeysException()); - } } } } catch (Exception $e) { - throw new Exception(Messages::disableForeignKeysException('Error disabling foreign keys: '.$e->getMessage())); - } return $this; @@ -96,11 +82,9 @@ public function enableForeignKeys(...$tables) { // Ensure the connection is established before executing the trait method $this->connectionPDO(); - - try { + try { if (!empty($tables)) { - // Enable foreign keys for specified tables $enableForeigns = ''; @@ -109,15 +93,12 @@ public function enableForeignKeys(...$tables) } $this->PDO->exec($enableForeigns); - } else { - // Enable foreign keys for all tables $query = "EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL';"; $success = $this->PDO->exec($query); if ($success === false) { - // If the stored procedure doesn't exist, use the alternative option $query = "SELECT 'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + ' WITH CHECK CHECK CONSTRAINT ' + name FROM sys.foreign_keys @@ -126,32 +107,24 @@ public function enableForeignKeys(...$tables) $result = $this->PDO->query($query); if ($result) { - // Build an array of ALTER TABLE statements $statements = $result->fetchAll(PDO::FETCH_COLUMN); foreach ($statements as $statement) { - // Enable each foreign key constraint $success = $this->PDO->exec($statement); if ($success === false) { throw new Exception(Messages::enableForeignKeysException()); } - } - } else { - throw new Exception(Messages::enableForeignKeysException()); - } } } } catch (Exception $e) { - throw new Exception(Messages::enableForeignKeysException('Error enabling foreign keys: '.$e->getMessage())); - } return $this; diff --git a/src/Traits/Transaction.php b/src/Traits/Transaction.php index 6c7f152..fe45f50 100644 --- a/src/Traits/Transaction.php +++ b/src/Traits/Transaction.php @@ -2,8 +2,6 @@ namespace Rmunate\SqlServerLite\Traits; -use PDO; - trait Transaction { /** @@ -15,6 +13,7 @@ public function beginTransaction() { $this->connectionPDO(); $this->PDO->beginTransaction(); + return $this; } @@ -26,6 +25,7 @@ public function beginTransaction() public function commit() { $this->PDO->commit(); + return $this; } @@ -37,8 +37,7 @@ public function commit() public function rollback() { $this->PDO->rollback(); + return $this; } - - }