Skip to content

Commit

Permalink
Merge branch 'bugfix/integration-tests' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
StulE-ru committed Aug 30, 2023
2 parents 3f8ed6e + 3088f40 commit b7aa984
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
82 changes: 82 additions & 0 deletions src/Drivers/Actions/PostgreSQL/AlterTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace ModernPDO\Drivers\Actions\PostgreSQL;

use ModernPDO\Actions\AlterTable as BaseAlterTable;

/**
* Class for updating tables.
*/
class AlterTable extends BaseAlterTable
{
/**
* Updates table.
*/
public function execute(): bool
{
// !!! ATTENSION !!!
// PostgreSQL does not support queries with multiple rename
// So we will run them separately in transaction

if (
$this->newName === ''
&& empty($this->addFields)
&& empty($this->renameFields)
&& empty($this->dropFields)
) {
return false;
}

// Store values
$newName = $this->newName;
$addFields = $this->addFields;
$renameFields = $this->renameFields;
$dropFields = $this->dropFields;

// Clear action values
$this->newName = '';
$this->addFields = [];
$this->renameFields = [];
$this->dropFields = [];

// Execute

$transaction = $this->mpdo->transaction();

$transaction->begin();

// Rename fields

foreach ($renameFields as $key => $value) {
$this->renameFields = [$key => $value];

if (!parent::execute()) {
$transaction->rollBack();

return false;
}
}

$this->renameFields = [];

$this->newName = $newName;
$this->addFields = $addFields;
$this->dropFields = $dropFields;

if (!parent::execute() && !empty($renameFields)) {
$transaction->rollBack();

return false;
}

$transaction->commit();

// Restore action values
$this->newName = $newName;
$this->addFields = $addFields;
$this->renameFields = $renameFields;
$this->dropFields = $dropFields;

return true;
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Escapers/PostgreSQLEscaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public function stringValue(string $value): string
*/
public function boolValue(bool $value): string
{
return '\'' . parent::boolValue($value) . '\'';
return $value ? 'TRUE::int::bit(1)' : 'FALSE::int::bit(1)';
}
}
21 changes: 21 additions & 0 deletions src/Drivers/Factories/PostgreSQLFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace ModernPDO\Drivers\Factories;

use ModernPDO\Actions\AlterTable;
use ModernPDO\Drivers\Actions\PostgreSQL\AlterTable as PostgreSQLAlterTable;
use ModernPDO\Factory;

/**
* Factory for customize PostgreSQL.
*/
class PostgreSQLFactory extends Factory
{
/**
* Returns AlterTable object.
*/
public function alterTable(string $table): AlterTable
{
return new PostgreSQLAlterTable($this->mpdo, $table);
}
}
3 changes: 2 additions & 1 deletion src/Drivers/PostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ModernPDO\Drivers;

use ModernPDO\Drivers\Escapers\PostgreSQLEscaper;
use ModernPDO\Drivers\Factories\PostgreSQLFactory;
use ModernPDO\ModernPDO;

/**
Expand Down Expand Up @@ -37,6 +38,6 @@ public function __construct(
$password,
);

parent::__construct($pdo, new PostgreSQLEscaper($pdo));
parent::__construct($pdo, new PostgreSQLEscaper($pdo), new PostgreSQLFactory($pdo, $this));
}
}

0 comments on commit b7aa984

Please sign in to comment.