Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Jul 6, 2023
1 parent b02eb87 commit 59972d5
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 45 deletions.
8 changes: 0 additions & 8 deletions src/SQLServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions src/Traits/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

trait Attributes
{

/**
* Set the query timeout for the PDO connection.
*
Expand All @@ -16,7 +15,6 @@ trait Attributes
*/
public function setTimeOut(int $seconds = 0)
{

// Ensure the connection is established before executing the trait method
$this->connectionPDO();

Expand All @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/AvailableDrivers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
29 changes: 1 addition & 28 deletions src/Traits/Constraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 = '';

Expand All @@ -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
Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/Traits/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rmunate\SqlServerLite\Traits;

use PDO;

trait Transaction
{
/**
Expand All @@ -15,6 +13,7 @@ public function beginTransaction()
{
$this->connectionPDO();
$this->PDO->beginTransaction();

return $this;
}

Expand All @@ -26,6 +25,7 @@ public function beginTransaction()
public function commit()
{
$this->PDO->commit();

return $this;
}

Expand All @@ -37,8 +37,7 @@ public function commit()
public function rollback()
{
$this->PDO->rollback();

return $this;
}


}

0 comments on commit 59972d5

Please sign in to comment.