-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathPlatformTrait.php
133 lines (113 loc) · 4.46 KB
/
PlatformTrait.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
<?php
declare(strict_types=1);
namespace Atk4\Data\Persistence\Sql\Mssql;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Index;
trait PlatformTrait
{
#[\Override]
public function getVarcharTypeDeclarationSQL(array $column)
{
$column['length'] = ($column['length'] ?? 255) * 4;
return parent::getVarcharTypeDeclarationSQL($column);
}
// remove once https://github.com/doctrine/dbal/pull/4987 is fixed
// and also $this->markDoctrineTypeCommented('text') below
#[\Override]
public function getClobTypeDeclarationSQL(array $column)
{
$res = parent::getClobTypeDeclarationSQL($column);
return (str_starts_with($res, 'VARCHAR') ? 'N' : '') . $res;
}
// TODO test DBAL DB diff for each supported Field type
// then fix using https://github.com/doctrine/dbal/issues/5194#issuecomment-1018790220
/* protected function initializeCommentedDoctrineTypes()
{
parent::initializeCommentedDoctrineTypes();
$this->markDoctrineTypeCommented('text');
} */
#[\Override]
public function getCurrentDatabaseExpression(bool $includeSchema = false): string
{
if ($includeSchema) {
return 'CONCAT(DB_NAME(), \'.\', SCHEMA_NAME())';
}
return parent::getCurrentDatabaseExpression();
}
#[\Override]
public function getCreateIndexSQL(Index $index, $table)
{
// workaround https://github.com/doctrine/dbal/issues/5507
// no side effect on DBAL index list observed, but multiple null values cannot be inserted
// the only, very complex, solution would be using intermediate view
// SQL Server should be fixed to allow FK creation when there is an unique index
// with "WHERE xxx IS NOT NULL" as FK does not restrict NULL values anyway
return $index->hasFlag('atk4-not-null')
? AbstractPlatform::getCreateIndexSQL($index, $table)
: parent::getCreateIndexSQL($index, $table);
}
// SQL Server DBAL platform has buggy identifier escaping, fix until fixed officially, see:
// https://github.com/doctrine/dbal/pull/6353
private function unquoteSingleIdentifier(string $possiblyQuotedName): string
{
return str_starts_with($possiblyQuotedName, '[') && str_ends_with($possiblyQuotedName, ']')
? substr($possiblyQuotedName, 1, -1)
: $possiblyQuotedName;
}
#[\Override]
protected function getCreateColumnCommentSQL($tableName, $columnName, $comment)
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}
return $this->getAddExtendedPropertySQL(
'MS_Description',
$comment,
'SCHEMA',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($schemaName)),
'TABLE',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
'COLUMN',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($columnName)),
);
}
#[\Override]
protected function getAlterColumnCommentSQL($tableName, $columnName, $comment)
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}
return $this->getUpdateExtendedPropertySQL(
'MS_Description',
$comment,
'SCHEMA',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($schemaName)),
'TABLE',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
'COLUMN',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($columnName)),
);
}
#[\Override]
protected function getDropColumnCommentSQL($tableName, $columnName)
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}
return $this->getDropExtendedPropertySQL(
'MS_Description',
'SCHEMA',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($schemaName)),
'TABLE',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
'COLUMN',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($columnName)),
);
}
}