1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Support; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Yiisoft\Cache\Cache; |
9
|
|
|
use Yiisoft\Cache\CacheInterface; |
10
|
|
|
use Yiisoft\Cache\File\FileCache; |
11
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
12
|
|
|
use Yiisoft\Db\Cache\SchemaCache; |
13
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
14
|
|
|
use Yiisoft\Db\Exception\Exception; |
15
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
16
|
|
|
use Yiisoft\Log\Logger; |
17
|
|
|
use Yiisoft\Profiler\Profiler; |
18
|
|
|
use Yiisoft\Profiler\ProfilerInterface; |
19
|
|
|
|
20
|
|
|
use function explode; |
21
|
|
|
use function file_get_contents; |
22
|
|
|
use function preg_replace; |
23
|
|
|
use function str_replace; |
24
|
|
|
use function trim; |
25
|
|
|
|
26
|
|
|
final class DbHelper |
27
|
|
|
{ |
28
|
|
|
public static function getCache(): CacheInterface |
29
|
|
|
{ |
30
|
|
|
return new Cache(new FileCache(__DIR__ . '/runtime/cache')); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function getLogger(): LoggerInterface |
34
|
|
|
{ |
35
|
|
|
return new Logger(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getQueryCache(): QueryCache |
39
|
|
|
{ |
40
|
|
|
return new QueryCache(self::getCache()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function getProfiler(): ProfilerInterface |
44
|
|
|
{ |
45
|
|
|
return new Profiler(self::getLogger()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function getSchemaCache(): SchemaCache |
49
|
|
|
{ |
50
|
|
|
return new SchemaCache(self::getCache()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Loads the fixture into the database. |
55
|
|
|
* |
56
|
|
|
* @throws Exception |
57
|
|
|
* @throws InvalidConfigException |
58
|
|
|
*/ |
59
|
|
|
public static function loadFixture(ConnectionPDOInterface $db, string $fixture): void |
60
|
|
|
{ |
61
|
|
|
// flush cache to new import data to dbms. |
62
|
|
|
self::getCache()->psr()->clear(); |
63
|
|
|
|
64
|
|
|
$db->open(); |
65
|
|
|
|
66
|
|
|
if ($db->getName() === 'oci') { |
67
|
|
|
[$drops, $creates] = explode('/* STATEMENTS */', file_get_contents($fixture), 2); |
68
|
|
|
[$statements, $triggers, $data] = explode('/* TRIGGERS */', $creates, 3); |
69
|
|
|
$lines = array_merge( |
70
|
|
|
explode('--', $drops), |
71
|
|
|
explode(';', $statements), |
72
|
|
|
explode('/', $triggers), |
73
|
|
|
explode(';', $data) |
74
|
|
|
); |
75
|
|
|
} else { |
76
|
|
|
$lines = explode(';', file_get_contents($fixture)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
foreach ($lines as $line) { |
80
|
|
|
if (trim($line) !== '') { |
81
|
|
|
$db->getPDO()?->exec($line); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Adjust dbms specific escaping. |
88
|
|
|
* |
89
|
|
|
* @param string $sql string SQL statement to adjust. |
90
|
|
|
* @param string $driverName string DBMS name. |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public static function replaceQuotes(string $sql, string $driverName): string |
95
|
|
|
{ |
96
|
|
|
return match ($driverName) { |
97
|
|
|
'mysql', 'sqlite' => str_replace(['[[', ']]'], '`', $sql), |
98
|
|
|
'oci' => str_replace(['[[', ']]'], '"', $sql), |
99
|
|
|
'pgsql' => str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?<!(\[))]])/', '"', $sql)), |
100
|
|
|
'db', 'sqlsrv' => str_replace(['[[', ']]'], ['[', ']'], $sql), |
101
|
|
|
default => $sql, |
102
|
|
|
}; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|