| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Data\Schema; |
| 6: | |
| 7: | use Atk4\Data\Persistence; |
| 8: | use Doctrine\DBAL\Logging\SQLLogger; |
| 9: | use Doctrine\DBAL\Platforms\MySQLPlatform; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | final class TestSqlPersistence extends Persistence\Sql |
| 17: | { |
| 18: | public function __construct() {} |
| 19: | |
| 20: | #[\Override] |
| 21: | public function getConnection(): Persistence\Sql\Connection |
| 22: | { |
| 23: | \Closure::bind(function () { |
| 24: | if ($this->_connection === null) { |
| 25: | $this->_connection = Persistence::connect($_ENV['DB_DSN'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'])->_connection; |
| 26: | |
| 27: | if ($this->getDatabasePlatform() instanceof MySQLPlatform) { |
| 28: | $this->getConnection()->expr( |
| 29: | 'SET SESSION auto_increment_increment = 1, SESSION auto_increment_offset = 1' |
| 30: | )->executeStatement(); |
| 31: | } |
| 32: | |
| 33: | |
| 34: | $this->getConnection()->getConnection()->getConfiguration()->setSQLLogger( |
| 35: | |
| 36: | new class() implements SQLLogger { |
| 37: | #[\Override] |
| 38: | public function startQuery($sql, array $params = null, array $types = null): void |
| 39: | { |
| 40: | |
| 41: | |
| 42: | if (preg_match('~^(?:SAVEPOINT|RELEASE SAVEPOINT|ROLLBACK TO SAVEPOINT|SAVE TRANSACTION|ROLLBACK TRANSACTION) DOCTRINE2_SAVEPOINT_\d+;?$~', $sql)) { |
| 43: | return; |
| 44: | } |
| 45: | |
| 46: | |
| 47: | if ($params !== null && $params !== [] && array_is_list($params)) { |
| 48: | $params = array_combine(range(1, count($params)), $params); |
| 49: | } |
| 50: | |
| 51: | $test = TestCase::getTestFromBacktrace(); |
| 52: | \Closure::bind(static fn () => $test->logQuery($sql, $params ?? [], $types ?? []), null, TestCase::class)(); |
| 53: | } |
| 54: | |
| 55: | #[\Override] |
| 56: | public function stopQuery(): void {} |
| 57: | } |
| 58: | ); |
| 59: | } |
| 60: | }, $this, Persistence\Sql::class)(); |
| 61: | |
| 62: | return parent::getConnection(); |
| 63: | } |
| 64: | } |
| 65: | |