-
-
Notifications
You must be signed in to change notification settings - Fork 994
Expand file tree
/
Copy pathplatform.php
More file actions
205 lines (183 loc) · 4.9 KB
/
platform.php
File metadata and controls
205 lines (183 loc) · 4.9 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\middleware\postgresql;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BigIntType;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\SmallIntType;
use Doctrine\DBAL\Types\Type;
/**
* PostgreSQL specific schema restrictions for BC.
*
* Doctrine is using SERIAL which auto creates the sequences with
* a name different from the one our driver is using. So in order
* to stay compatible with the existing DB we have to change its
* naming and not ours.
*/
class platform extends PostgreSQLPlatform
{
/**
* {@inheritdoc}
*/
public function getIdentitySequenceName($tableName, $columnName): string
{
return $tableName . '_seq';
}
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $column): string
{
return 'INT';
}
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $column): string
{
return 'BIGINT';
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $column): string
{
return 'SMALLINT';
}
/**
* {@inheritDoc}
*/
public function getDefaultValueDeclarationSQL($column): string
{
if ($this->isSerialColumn($column))
{
return ' DEFAULT {{placeholder_sequence}}';
}
return AbstractPlatform::getDefaultValueDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getAlterTableSQL(TableDiff $diff)
{
$sql = parent::getAlterTableSQL($diff);
$table_name = $diff->getOldTable()->getName();
$columns = $diff->getAddedColumns();
$post_sql = $sequence_sql = [];
foreach ($columns as $column)
{
$column_name = $column->getName();
if (!empty($column->getAutoincrement()))
{
$sequence = new Sequence($this->getIdentitySequenceName($table_name, $column_name));
$sequence_sql[] = $this->getCreateSequenceSQL($sequence);
$post_sql[] = 'ALTER SEQUENCE ' . $sequence->getName() . ' OWNED BY ' . $table_name . '.' . $column_name;
}
}
$sql = array_merge($sequence_sql, $sql, $post_sql);
foreach ($sql as $i => $query)
{
$sql[$i] = str_replace('{{placeholder_sequence}}', "nextval('{$table_name}_seq')", $query);
}
return $sql;
}
/**
* {@inheritDoc}
*/
public function supportsIdentityColumns(): bool
{
return false;
}
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($name, array $columns, array $options = []): array
{
$sql = [];
$post_sql = [];
foreach ($columns as $column_name => $column)
{
if (!empty($column['autoincrement']))
{
$sequence = new Sequence($this->getIdentitySequenceName($name, $column_name));
$sql[] = $this->getCreateSequenceSQL($sequence);
$post_sql[] = 'ALTER SEQUENCE '.$sequence->getName().' OWNED BY '.$name.'.'.$column_name;
}
}
$sql = array_merge($sql, parent::_getCreateTableSQL($name, $columns, $options), $post_sql);
foreach ($sql as $i => $query)
{
$sql[$i] = str_replace('{{placeholder_sequence}}', "nextval('{$name}_seq')", $query);
}
return $sql;
}
/**
* Return if column is a "serial" column, i.e. type supporting auto-increment
*
* @param array $column Column data
* @return bool
*/
private function isSerialColumn(array $column): bool
{
return isset($column['type'], $column['autoincrement'])
&& $column['autoincrement'] === true
&& $this->isNumericType($column['type']);
}
/**
* Return if supplied type is of numeric type
*
* @param Type $type
* @return bool
*/
private function isNumericType(Type $type): bool
{
return $type instanceof IntegerType || $type instanceof BigIntType || $type instanceof SmallIntType;
}
/**
* {@inheritDoc}
*/
public function getListSequencesSQL($database): string
{
return "SELECT sequence_name AS relname,
sequence_schema AS schemaname,
1 AS min_value,
1 AS increment_by
FROM information_schema.sequences
WHERE sequence_schema NOT LIKE 'pg\_%'
AND sequence_schema <> 'information_schema'";
}
/**
* {@inheritDoc}
*/
public function getDropIndexSQL($index, $table = null): string
{
// If we have a primary or a unique index, we need to drop the constraint
// instead of the index itself or postgreSQL will reject the query.
if (is_string($index) && $table !== null && $index === $this->tableName($table) . '_pkey')
{
return $this->getDropConstraintSQL($index, $this->tableName($table));
}
return parent::getDropIndexSQL($index, $table);
}
/**
* {@inheritDoc}
*/
private function tableName($table)
{
return $table instanceof Table ? $table->getName() : (string) $table;
}
}