diff --git a/src/Driver/Driver.php b/src/Driver/Driver.php index 6e86a839..852239ac 100644 --- a/src/Driver/Driver.php +++ b/src/Driver/Driver.php @@ -106,10 +106,16 @@ public function withoutCache(): static $driver = clone $this; $driver->useCache = false; + $driver->queryCache = []; return $driver; } + public function clearCache(): void + { + $this->queryCache = []; + } + /** * Get driver source database or file name. * @@ -132,7 +138,7 @@ public function getTimezone(): \DateTimeZone public function getSchemaHandler(): HandlerInterface { // do not allow to carry prepared statements between schema changes - $this->queryCache = []; + $this->clearCache(); return $this->schemaHandler; } @@ -171,7 +177,7 @@ public function isConnected(): bool public function disconnect(): void { try { - $this->queryCache = []; + $this->clearCache(); $this->pdo = null; } catch (\Throwable $e) { // disconnect error diff --git a/src/Driver/Jsoner.php b/src/Driver/Jsoner.php index 7bc50eaa..fe08c02c 100644 --- a/src/Driver/Jsoner.php +++ b/src/Driver/Jsoner.php @@ -32,7 +32,7 @@ public static function toJson(mixed $value, bool $encode = true, bool $validate $result = (string) $value; - if ($validate && !json_validate($result)) { + if ($validate && !\json_validate($result)) { throw new BuilderException('Invalid JSON value.'); } diff --git a/src/Schema/AbstractColumn.php b/src/Schema/AbstractColumn.php index a9078841..f83ceb79 100644 --- a/src/Schema/AbstractColumn.php +++ b/src/Schema/AbstractColumn.php @@ -623,6 +623,15 @@ public function isReadonlySchema(): bool return $this->getAttributes()['readonlySchema'] ?? false; } + /** + * Get column comment. + * An empty string will be returned if the feature is not supported by the driver. + */ + public function getComment(): string + { + return ''; + } + /** * Shortcut for AbstractColumn->type() method. * @@ -785,13 +794,4 @@ protected function formatDatetime( default => $value, }; } - - /** - * Get column comment. - * An empty string will be returned if the feature is not supported by the driver. - */ - public function getComment(): string - { - return ''; - } } diff --git a/tests/Database/Functional/Driver/Common/Driver/DriverTest.php b/tests/Database/Functional/Driver/Common/Driver/DriverTest.php index 509a16ef..e09519b9 100644 --- a/tests/Database/Functional/Driver/Common/Driver/DriverTest.php +++ b/tests/Database/Functional/Driver/Common/Driver/DriverTest.php @@ -4,6 +4,9 @@ namespace Cycle\Database\Tests\Functional\Driver\Common\Driver; +use Cycle\Database\Config\DriverConfig; +use Cycle\Database\Driver\Driver; +use Cycle\Database\Exception\StatementException; use Cycle\Database\Tests\Functional\Driver\Common\BaseTest; abstract class DriverTest extends BaseTest @@ -59,4 +62,62 @@ public function datetimeDataProvider(): \Traversable yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTimeImmutable {}]; yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTime {}]; } + + public function testClearCache(): void + { + $driver = $this->mockDriver(); + + $driver->testPolluteCache(); + self::assertNotEmpty($driver->testGetCache()); + + $driver->clearCache(); + + self::assertEmpty($driver->testGetCache()); + } + + public function testWithoutCache(): void + { + $driver = $this->mockDriver(); + $driver->testPolluteCache(); + self::assertNotEmpty($driver->testGetCache()); + + $new = $driver->withoutCache(); + + self::assertNotEmpty($driver->testGetCache()); + self::assertEmpty($new->testGetCache()); + } + + private function mockDriver(): Driver + { + return new class extends Driver { + public function __construct() {} + + public function testPolluteCache(): void + { + $this->queryCache[] = ['sql' => 'SELECT * FROM table', 'params' => []]; + } + + public function testGetCache(): array + { + return $this->queryCache; + } + + protected function mapException(\Throwable $exception, string $query): StatementException + { + throw new \Exception('not needed'); + } + + public static function create(DriverConfig $config): \Cycle\Database\Driver\DriverInterface + { + throw new \Exception('not needed'); + } + + public function getType(): string + { + throw new \Exception('not needed'); + } + + public function __clone(): void {} + }; + } } diff --git a/tests/Database/Functional/Driver/Common/Schema/CommentTest.php b/tests/Database/Functional/Driver/Common/Schema/CommentTest.php index 963065c1..2671d3f9 100644 --- a/tests/Database/Functional/Driver/Common/Schema/CommentTest.php +++ b/tests/Database/Functional/Driver/Common/Schema/CommentTest.php @@ -5,7 +5,6 @@ namespace Cycle\Database\Tests\Functional\Driver\Common\Schema; // phpcs:ignore -use Cycle\Database\ColumnInterface; use Cycle\Database\Tests\Functional\Driver\Common\BaseTest; use Cycle\Database\Tests\Utils\DontGenerateAttribute; diff --git a/tests/generate.php b/tests/generate.php index 6661267b..eff3e9ac 100644 --- a/tests/generate.php +++ b/tests/generate.php @@ -5,11 +5,11 @@ use Cycle\Database\Tests\Utils\DontGenerateAttribute; use Spiral\Tokenizer; -error_reporting(E_ALL | E_STRICT); -ini_set('display_errors', '1'); +\error_reporting(E_ALL | E_STRICT); +\ini_set('display_errors', '1'); //Composer -require_once dirname(__DIR__) . '/vendor/autoload.php'; +require_once \dirname(__DIR__) . '/vendor/autoload.php'; $tokenizer = new Tokenizer\Tokenizer(new Tokenizer\Config\TokenizerConfig([ 'directories' => [__DIR__ . '/Database/Functional/Driver/Common'], @@ -67,7 +67,7 @@ \str_replace('\\', '/', $class->getFileName()), ); - $path = ltrim($path, '/'); + $path = \ltrim($path, '/'); foreach ($databases as $driver => $details) { $filename = $details['directory'] . $path;