-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bind PostgreSQL values using strong type (#1217)
- Loading branch information
Showing
16 changed files
with
253 additions
and
61 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
74 changes: 74 additions & 0 deletions
74
src/Persistence/Sql/JsonTypeCompatibilityTypecastTrait.php
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Persistence\Sql; | ||
|
||
use Atk4\Data\Exception; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
|
||
trait JsonTypeCompatibilityTypecastTrait | ||
{ | ||
private function jsonTypeValueGetPrefixConst(): string | ||
{ | ||
return "atk4_json\ru5f8mzx4vsm8g2c9\r"; | ||
} | ||
|
||
private function jsonTypeValueEncode(string $value): string | ||
{ | ||
return $this->jsonTypeValueGetPrefixConst() . hash('crc32b', $value) . $value; | ||
} | ||
|
||
private function jsonTypeValueIsEncoded(string $value): bool | ||
{ | ||
return str_starts_with($value, $this->jsonTypeValueGetPrefixConst()); | ||
} | ||
|
||
private function jsonTypeValueDecode(string $value): string | ||
{ | ||
if (!$this->jsonTypeValueIsEncoded($value)) { | ||
throw new Exception('Unexpected unencoded json value'); | ||
} | ||
|
||
$resCrc = substr($value, strlen($this->jsonTypeValueGetPrefixConst()), 8); | ||
$res = substr($value, strlen($this->jsonTypeValueGetPrefixConst()) + 8); | ||
if ($resCrc !== hash('crc32b', $res)) { | ||
throw new Exception('Unexpected json value crc'); | ||
} | ||
|
||
if ($this->jsonTypeValueIsEncoded($res)) { | ||
throw new Exception('Unexpected double encoded json value'); | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
private function jsonTypeIsEncodeNeeded(string $type): bool | ||
{ | ||
// json values for PostgreSQL database are stored natively, but we need | ||
// to encode first to hold the json type info for PDO parameter type binding | ||
|
||
$platform = $this->getDatabasePlatform(); | ||
if ($platform instanceof PostgreSQLPlatform) { | ||
if ($type === 'json') { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param scalar $value | ||
*/ | ||
private function jsonTypeIsDecodeNeeded(string $type, $value): bool | ||
{ | ||
if ($this->jsonTypeIsEncodeNeeded($type)) { | ||
if ($this->jsonTypeValueIsEncoded($value)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/Persistence/Sql/Postgresql/InitializeSessionMiddleware.php
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Persistence\Sql\Postgresql; | ||
|
||
use Doctrine\DBAL\Driver; | ||
use Doctrine\DBAL\Driver\Connection; | ||
use Doctrine\DBAL\Driver\Middleware; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; | ||
|
||
/** | ||
* Setup "citext" server extension to be available as we use "citext" type for all bound string variables. | ||
* | ||
* Based on https://github.com/doctrine/dbal/blob/3.6.5/src/Driver/OCI8/Middleware/InitializeSession.php | ||
*/ | ||
class InitializeSessionMiddleware implements Middleware | ||
{ | ||
#[\Override] | ||
public function wrap(Driver $driver): Driver | ||
{ | ||
return new class($driver) extends AbstractDriverMiddleware { | ||
#[\Override] | ||
public function connect( | ||
#[\SensitiveParameter] | ||
array $params | ||
): Connection { | ||
$connection = parent::connect($params); | ||
|
||
if ($connection->query('SELECT to_regtype(\'citext\')')->fetchOne() === null) { | ||
// "CREATE EXTENSION IF NOT EXISTS ..." cannot be used as it requires | ||
// CREATE privilege even if the extension is already installed | ||
$connection->query('CREATE EXTENSION citext'); | ||
} | ||
|
||
return $connection; | ||
} | ||
}; | ||
} | ||
} |
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
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.