-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
SqlGenerator.php
87 lines (74 loc) · 2.4 KB
/
SqlGenerator.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
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Generator;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\SqlFormatter\NullHighlighter;
use Doctrine\SqlFormatter\SqlFormatter;
use function array_unshift;
use function count;
use function get_class;
use function implode;
use function sprintf;
use function stripos;
use function strlen;
use function var_export;
/**
* The SqlGenerator class is responsible for generating the body of the up() and down() methods for a migration
* from an array of SQL queries.
*
* @internal
*/
class SqlGenerator
{
public function __construct(
private readonly Configuration $configuration,
private readonly AbstractPlatform $platform,
) {
}
/** @param string[] $sql */
public function generate(
array $sql,
bool $formatted = false,
int $lineLength = 120,
bool $checkDbPlatform = true,
): string {
$code = [];
$storageConfiguration = $this->configuration->getMetadataStorageConfiguration();
foreach ($sql as $query) {
if (
$storageConfiguration instanceof TableMetadataStorageConfiguration
&& stripos($query, $storageConfiguration->getTableName()) !== false
) {
continue;
}
if ($formatted) {
$maxLength = $lineLength - 18 - 8; // max - php code length - indentation
if (strlen($query) > $maxLength) {
$query = (new SqlFormatter(new NullHighlighter()))->format($query);
}
}
$code[] = sprintf('$this->addSql(%s);', var_export($query, true));
}
if (count($code) !== 0 && $checkDbPlatform && $this->configuration->isDatabasePlatformChecked()) {
$currentPlatform = '\\' . get_class($this->platform);
array_unshift(
$code,
sprintf(
<<<'PHP'
$this->abortIf(
!$this->connection->getDatabasePlatform() instanceof %s,
"Migration can only be executed safely on '%s'."
);
PHP
,
$currentPlatform,
$currentPlatform,
),
'',
);
}
return implode("\n", $code);
}
}