generated from vjik/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabasePopulator.php
164 lines (138 loc) · 4.29 KB
/
DatabasePopulator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Vjik\Codeception\DatabasePopulator;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Driver\Db as DbDriver;
use Codeception\Lib\Driver\MySql as MySqlDriver;
use Codeception\Module\Db;
use PDO;
/**
* @internal
*/
final class DatabasePopulator
{
private DbDriver $dbDriver;
private PDO $dbh;
private Config $config;
public function __construct(Db $db, Config $config)
{
$this->dbDriver = $db->_getDriver();
$this->dbh = $db->_getDbh();
$this->config = $config;
}
public function loadDump(string ...$dumps): void
{
// Clear database
$this->dbDriver->cleanup();
// Load dumps
foreach ($dumps as $dump) {
$this->dbDriver->load($this->readDump($dump));
}
}
public function loadRows(string ...$sets): void
{
$this->beforeLoadRows();
foreach ($sets as $set) {
/**
* @psalm-suppress UnresolvableInclude
* @psalm-var array<string, list<array<string, mixed>>> $data
*/
$data = require $this->getRowsFilePath($set);
foreach ($data as $table => $rows) {
$this->insertRows($table, $rows);
}
}
$this->afterLoadRows();
}
/**
* @param array[] $rows
* @psalm-param list<array<string,mixed>> $rows
*/
private function insertRows(string $table, array $rows): void
{
$requests = [];
foreach ($rows as $row) {
$columns = array_keys($row);
$key = implode('~', $columns);
if (isset($requests[$key])) {
$requests[$key]['rows'][] = $row;
} else {
$requests[$key] = [
'columns' => $columns,
'rows' => [$row],
];
}
}
foreach ($requests as $request) {
$columns = array_map(
fn($c) => $this->dbDriver->getQuotedName($c),
$request['columns']
);
$sql = sprintf(
'INSERT INTO %s (%s) VALUES ',
$this->dbDriver->getQuotedName($table),
implode(',', $columns),
);
$insertQuery = [];
$insertData = [];
$n = 0;
foreach ($request['rows'] as $row) {
$insertQueryData = [];
/** @var mixed $value */
foreach ($row as $key => $value) {
$insertQueryData[] = ':' . $key . $n;
/** @var mixed */
$insertData[$key . $n] = $value;
}
$insertQuery[] = '(' . implode(',', $insertQueryData) . ')';
$n++;
}
$sql .= implode(', ', $insertQuery);
$this->dbh->prepare($sql)->execute($insertData);
}
}
private function beforeLoadRows(): void
{
if ($this->dbDriver instanceof MySqlDriver) {
$this->dbh->prepare('SET FOREIGN_KEY_CHECKS = 0')->execute();
}
}
private function afterLoadRows(): void
{
if ($this->dbDriver instanceof MySqlDriver) {
$this->dbh->prepare('SET FOREIGN_KEY_CHECKS = 1')->execute();
}
}
/**
* @see Db::readSqlFile
* @see Db::readSql
*
* @return string[]
*/
private function readDump(string $dump): array
{
$file = $this->getDumpFilePath($dump);
if (!file_exists($file)) {
throw new ModuleException(
Module::class,
"\nFile with dump doesn't exist.\nPlease, check path for SQL-file: $file"
);
}
$sql = file_get_contents($file);
// Remove C-style comments (except MySQL directives)
$sql = preg_replace('%/\*(?!!\d+).*?\*/%s', '', $sql);
if (empty($sql)) {
return [];
}
// Split SQL dump into lines
return preg_split('/\r\n|\n|\r/', $sql, -1, PREG_SPLIT_NO_EMPTY);
}
private function getDumpFilePath(string $dump): string
{
return $this->config->dumpsPath() . '/' . $dump . '.sql';
}
private function getRowsFilePath(string $set): string
{
return $this->config->rowsPath() . '/' . $set . '.php';
}
}