-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Composite PHP
committed
Dec 23, 2023
1 parent
1ae4801
commit ba50060
Showing
14 changed files
with
204 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composite\DB\Helpers; | ||
|
||
use Composite\DB\Exceptions\DbException; | ||
use Doctrine\DBAL\Driver; | ||
|
||
trait DatabaseSpecificTrait | ||
{ | ||
private ?bool $isPostgreSQL = null; | ||
private ?bool $isMySQL = null; | ||
private ?bool $isSQLite = null; | ||
|
||
private function identifyPlatform(): void | ||
{ | ||
if ($this->isPostgreSQL !== null) { | ||
return; | ||
} | ||
$driver = $this->getConnection()->getDriver(); | ||
if ($driver instanceof Driver\AbstractPostgreSQLDriver) { | ||
$this->isPostgreSQL = true; | ||
$this->isMySQL = $this->isSQLite = false; | ||
} elseif ($driver instanceof Driver\AbstractSQLiteDriver) { | ||
$this->isSQLite = true; | ||
$this->isPostgreSQL = $this->isMySQL = false; | ||
} elseif ($driver instanceof Driver\AbstractMySQLDriver) { | ||
$this->isMySQL = true; | ||
$this->isPostgreSQL = $this->isSQLite = false; | ||
} else { | ||
// @codeCoverageIgnoreStart | ||
throw new DbException('Unsupported driver ' . $driver::class); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $data | ||
* @return array<string, mixed> | ||
*/ | ||
private function prepareDataForSql(array $data): array | ||
{ | ||
$this->identifyPlatform(); | ||
foreach ($data as $columnName => $value) { | ||
if (is_bool($value) && !$this->isPostgreSQL) { | ||
$data[$columnName] = $value ? 1 : 0; | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
protected function escapeIdentifier(string $key): string | ||
{ | ||
$this->identifyPlatform(); | ||
if ($this->isMySQL) { | ||
return implode('.', array_map(fn ($part) => "`$part`", explode('.', $key))); | ||
} else { | ||
return '"' . $key . '"'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.