Skip to content

Commit 629a6eb

Browse files
authored
Merge pull request #220: add public clearCache method
2 parents 6a87a69 + 6c7d4c1 commit 629a6eb

File tree

6 files changed

+83
-17
lines changed

6 files changed

+83
-17
lines changed

src/Driver/Driver.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ public function withoutCache(): static
106106

107107
$driver = clone $this;
108108
$driver->useCache = false;
109+
$driver->queryCache = [];
109110

110111
return $driver;
111112
}
112113

114+
public function clearCache(): void
115+
{
116+
$this->queryCache = [];
117+
}
118+
113119
/**
114120
* Get driver source database or file name.
115121
*
@@ -132,7 +138,7 @@ public function getTimezone(): \DateTimeZone
132138
public function getSchemaHandler(): HandlerInterface
133139
{
134140
// do not allow to carry prepared statements between schema changes
135-
$this->queryCache = [];
141+
$this->clearCache();
136142

137143
return $this->schemaHandler;
138144
}
@@ -171,7 +177,7 @@ public function isConnected(): bool
171177
public function disconnect(): void
172178
{
173179
try {
174-
$this->queryCache = [];
180+
$this->clearCache();
175181
$this->pdo = null;
176182
} catch (\Throwable $e) {
177183
// disconnect error

src/Driver/Jsoner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function toJson(mixed $value, bool $encode = true, bool $validate
3232

3333
$result = (string) $value;
3434

35-
if ($validate && !json_validate($result)) {
35+
if ($validate && !\json_validate($result)) {
3636
throw new BuilderException('Invalid JSON value.');
3737
}
3838

src/Schema/AbstractColumn.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,15 @@ public function isReadonlySchema(): bool
623623
return $this->getAttributes()['readonlySchema'] ?? false;
624624
}
625625

626+
/**
627+
* Get column comment.
628+
* An empty string will be returned if the feature is not supported by the driver.
629+
*/
630+
public function getComment(): string
631+
{
632+
return '';
633+
}
634+
626635
/**
627636
* Shortcut for AbstractColumn->type() method.
628637
*
@@ -785,13 +794,4 @@ protected function formatDatetime(
785794
default => $value,
786795
};
787796
}
788-
789-
/**
790-
* Get column comment.
791-
* An empty string will be returned if the feature is not supported by the driver.
792-
*/
793-
public function getComment(): string
794-
{
795-
return '';
796-
}
797797
}

tests/Database/Functional/Driver/Common/Driver/DriverTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Cycle\Database\Tests\Functional\Driver\Common\Driver;
66

7+
use Cycle\Database\Config\DriverConfig;
8+
use Cycle\Database\Driver\Driver;
9+
use Cycle\Database\Exception\StatementException;
710
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
811

912
abstract class DriverTest extends BaseTest
@@ -59,4 +62,62 @@ public function datetimeDataProvider(): \Traversable
5962
yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTimeImmutable {}];
6063
yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTime {}];
6164
}
65+
66+
public function testClearCache(): void
67+
{
68+
$driver = $this->mockDriver();
69+
70+
$driver->testPolluteCache();
71+
self::assertNotEmpty($driver->testGetCache());
72+
73+
$driver->clearCache();
74+
75+
self::assertEmpty($driver->testGetCache());
76+
}
77+
78+
public function testWithoutCache(): void
79+
{
80+
$driver = $this->mockDriver();
81+
$driver->testPolluteCache();
82+
self::assertNotEmpty($driver->testGetCache());
83+
84+
$new = $driver->withoutCache();
85+
86+
self::assertNotEmpty($driver->testGetCache());
87+
self::assertEmpty($new->testGetCache());
88+
}
89+
90+
private function mockDriver(): Driver
91+
{
92+
return new class extends Driver {
93+
public function __construct() {}
94+
95+
public function testPolluteCache(): void
96+
{
97+
$this->queryCache[] = ['sql' => 'SELECT * FROM table', 'params' => []];
98+
}
99+
100+
public function testGetCache(): array
101+
{
102+
return $this->queryCache;
103+
}
104+
105+
protected function mapException(\Throwable $exception, string $query): StatementException
106+
{
107+
throw new \Exception('not needed');
108+
}
109+
110+
public static function create(DriverConfig $config): \Cycle\Database\Driver\DriverInterface
111+
{
112+
throw new \Exception('not needed');
113+
}
114+
115+
public function getType(): string
116+
{
117+
throw new \Exception('not needed');
118+
}
119+
120+
public function __clone(): void {}
121+
};
122+
}
62123
}

tests/Database/Functional/Driver/Common/Schema/CommentTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Cycle\Database\Tests\Functional\Driver\Common\Schema;
66

77
// phpcs:ignore
8-
use Cycle\Database\ColumnInterface;
98
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
109
use Cycle\Database\Tests\Utils\DontGenerateAttribute;
1110

tests/generate.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use Cycle\Database\Tests\Utils\DontGenerateAttribute;
66
use Spiral\Tokenizer;
77

8-
error_reporting(E_ALL | E_STRICT);
9-
ini_set('display_errors', '1');
8+
\error_reporting(E_ALL | E_STRICT);
9+
\ini_set('display_errors', '1');
1010

1111
//Composer
12-
require_once dirname(__DIR__) . '/vendor/autoload.php';
12+
require_once \dirname(__DIR__) . '/vendor/autoload.php';
1313

1414
$tokenizer = new Tokenizer\Tokenizer(new Tokenizer\Config\TokenizerConfig([
1515
'directories' => [__DIR__ . '/Database/Functional/Driver/Common'],
@@ -67,7 +67,7 @@
6767
\str_replace('\\', '/', $class->getFileName()),
6868
);
6969

70-
$path = ltrim($path, '/');
70+
$path = \ltrim($path, '/');
7171

7272
foreach ($databases as $driver => $details) {
7373
$filename = $details['directory'] . $path;

0 commit comments

Comments
 (0)