Implement the SQLite extension for doctrine via a middleware instead of an deprecated event listener

This commit is contained in:
Jan Böhmer 2023-12-05 23:50:07 +01:00
parent 641b47b189
commit dd0f8ec97c
6 changed files with 137 additions and 21 deletions

View file

@ -1,11 +1,8 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
@ -20,29 +17,31 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Doctrine;
declare(strict_types=1);
namespace App\Doctrine\Middleware;
use App\Exceptions\InvalidRegexException;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Doctrine\DBAL\Platforms\SqlitePlatform;
/**
* This subscriber is used to add the regexp operator to the SQLite platform.
* This middleware is used to add the regexp operator to the SQLite platform.
* As a PHP callback is called for every entry to compare it is most likely much slower than using regex on MySQL.
* But as regex is not often used, this should be fine for most use cases, also it is almost impossible to implement a better solution.
*/
#[AsDoctrineListener(Events::postConnect)]
class SQLiteRegexExtension
class SQLiteRegexExtensionMiddlewareDriver extends AbstractDriverMiddleware
{
public function postConnect(ConnectionEventArgs $eventArgs): void
public function connect(#[\SensitiveParameter] array $params): Connection
{
$connection = $eventArgs->getConnection();
//Do connect process first
$connection = parent::connect($params); // TODO: Change the autogenerated stub
//We only execute this on SQLite databases
if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {
//Then add the functions if we are on SQLite
if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$native_connection = $connection->getNativeConnection();
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
@ -52,6 +51,9 @@ class SQLiteRegexExtension
$native_connection->sqliteCreateFunction('FIELD2', self::field2(...), 2, \PDO::SQLITE_DETERMINISTIC);
}
}
return $connection;
}
/**
@ -107,4 +109,4 @@ class SQLiteRegexExtension
return $index + 1;
}
}
}

View file

@ -0,0 +1,35 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Doctrine\Middleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
class SQLiteRegexExtensionMiddlewareWrapper implements Middleware
{
public function wrap(Driver $driver): Driver
{
return new SQLiteRegexExtensionMiddlewareDriver($driver);
}
}

View file

@ -20,7 +20,7 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Doctrine\SetSQLMode;
namespace App\Doctrine\Middleware;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;

View file

@ -20,7 +20,7 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Doctrine\SetSQLMode;
namespace App\Doctrine\Middleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
@ -30,7 +30,6 @@ use Doctrine\DBAL\Driver\Middleware;
*/
class SetSQLModeMiddlewareWrapper implements Middleware
{
public function wrap(Driver $driver): Driver
{
return new SetSQLModeMiddlewareDriver($driver);