Skip to content

Commit 8924cb0

Browse files
committed
refactor: rename parameter name in _alterTable()
1 parent f34fc58 commit 8924cb0

5 files changed

Lines changed: 71 additions & 60 deletions

File tree

system/Database/MySQLi/Forge.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,35 +128,37 @@ protected function _createTableAttributes(array $attributes): string
128128
/**
129129
* ALTER TABLE
130130
*
131-
* @param string $alterType ALTER type
132-
* @param string $table Table name
133-
* @param array|string $field Column definition
131+
* @param string $alterType ALTER type
132+
* @param string $table Table name
133+
* @param array|string $processedFields Processed column definitions
134+
* or column names to DROP
134135
*
135-
* @return string|string[]
136+
* @return list<string>|string SQL string
137+
* @phpstan-return ($alterType is 'DROP' ? string : list<string>)
136138
*/
137-
protected function _alterTable(string $alterType, string $table, $field)
139+
protected function _alterTable(string $alterType, string $table, $processedFields)
138140
{
139141
if ($alterType === 'DROP') {
140-
return parent::_alterTable($alterType, $table, $field);
142+
return parent::_alterTable($alterType, $table, $processedFields);
141143
}
142144

143145
$sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
144146

145-
foreach ($field as $i => $data) {
147+
foreach ($processedFields as $i => $data) {
146148
if ($data['_literal'] !== false) {
147-
$field[$i] = ($alterType === 'ADD') ? "\n\tADD " . $data['_literal'] : "\n\tMODIFY " . $data['_literal'];
149+
$processedFields[$i] = ($alterType === 'ADD') ? "\n\tADD " . $data['_literal'] : "\n\tMODIFY " . $data['_literal'];
148150
} else {
149151
if ($alterType === 'ADD') {
150-
$field[$i]['_literal'] = "\n\tADD ";
152+
$processedFields[$i]['_literal'] = "\n\tADD ";
151153
} else {
152-
$field[$i]['_literal'] = empty($data['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
154+
$processedFields[$i]['_literal'] = empty($data['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
153155
}
154156

155-
$field[$i] = $field[$i]['_literal'] . $this->_processColumn($field[$i]);
157+
$processedFields[$i] = $processedFields[$i]['_literal'] . $this->_processColumn($processedFields[$i]);
156158
}
157159
}
158160

159-
return [$sql . implode(',', $field)];
161+
return [$sql . implode(',', $processedFields)];
160162
}
161163

162164
/**

system/Database/OCI8/Forge.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,72 +93,75 @@ class Forge extends BaseForge
9393
/**
9494
* ALTER TABLE
9595
*
96-
* @param string $alterType ALTER type
97-
* @param string $table Table name
98-
* @param array|string $field Column definition
96+
* @param string $alterType ALTER type
97+
* @param string $table Table name
98+
* @param array|string $processedFields Processed column definitions
99+
* or column names to DROP
99100
*
100-
* @return string|string[]
101+
* @return list<string>|string SQL string
102+
* @phpstan-return ($alterType is 'DROP' ? string : list<string>)
101103
*/
102-
protected function _alterTable(string $alterType, string $table, $field)
104+
protected function _alterTable(string $alterType, string $table, $processedFields)
103105
{
104106
$sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
105107

106108
if ($alterType === 'DROP') {
107-
$fields = array_map(fn ($field) => $this->db->escapeIdentifiers(trim($field)), is_string($field) ? explode(',', $field) : $field);
109+
$fields = array_map(fn ($field) => $this->db->escapeIdentifiers(trim($processedFields)), is_string($processedFields) ? explode(',', $processedFields) : $processedFields);
108110

109111
return $sql . ' DROP (' . implode(',', $fields) . ') CASCADE CONSTRAINT INVALIDATE';
110112
}
113+
111114
if ($alterType === 'CHANGE') {
112115
$alterType = 'MODIFY';
113116
}
114117

115118
$nullableMap = array_column($this->db->getFieldData($table), 'nullable', 'name');
116119
$sqls = [];
117120

118-
for ($i = 0, $c = count($field); $i < $c; $i++) {
121+
for ($i = 0, $c = count($processedFields); $i < $c; $i++) {
119122
if ($alterType === 'MODIFY') {
120123
// If a null constraint is added to a column with a null constraint,
121124
// ORA-01451 will occur,
122125
// so add null constraint is used only when it is different from the current null constraint.
123126
// If a not null constraint is added to a column with a not null constraint,
124127
// ORA-01442 will occur.
125-
$wantToAddNull = strpos($field[$i]['null'], ' NOT') === false;
126-
$currentNullable = $nullableMap[$field[$i]['name']];
128+
$wantToAddNull = strpos($processedFields[$i]['null'], ' NOT') === false;
129+
$currentNullable = $nullableMap[$processedFields[$i]['name']];
127130

128131
if ($wantToAddNull === true && $currentNullable === true) {
129-
$field[$i]['null'] = '';
130-
} elseif ($field[$i]['null'] === '' && $currentNullable === false) {
132+
$processedFields[$i]['null'] = '';
133+
} elseif ($processedFields[$i]['null'] === '' && $currentNullable === false) {
131134
// Nullable by default
132-
$field[$i]['null'] = ' NULL';
135+
$processedFields[$i]['null'] = ' NULL';
133136
} elseif ($wantToAddNull === false && $currentNullable === false) {
134-
$field[$i]['null'] = '';
137+
$processedFields[$i]['null'] = '';
135138
}
136139
}
137140

138-
if ($field[$i]['_literal'] !== false) {
139-
$field[$i] = "\n\t" . $field[$i]['_literal'];
141+
if ($processedFields[$i]['_literal'] !== false) {
142+
$processedFields[$i] = "\n\t" . $processedFields[$i]['_literal'];
140143
} else {
141-
$field[$i]['_literal'] = "\n\t" . $this->_processColumn($field[$i]);
144+
$processedFields[$i]['_literal'] = "\n\t" . $this->_processColumn($processedFields[$i]);
142145

143-
if (! empty($field[$i]['comment'])) {
146+
if (! empty($processedFields[$i]['comment'])) {
144147
$sqls[] = 'COMMENT ON COLUMN '
145-
. $this->db->escapeIdentifiers($table) . '.' . $this->db->escapeIdentifiers($field[$i]['name'])
146-
. ' IS ' . $field[$i]['comment'];
148+
. $this->db->escapeIdentifiers($table) . '.' . $this->db->escapeIdentifiers($processedFields[$i]['name'])
149+
. ' IS ' . $processedFields[$i]['comment'];
147150
}
148151

149-
if ($alterType === 'MODIFY' && ! empty($field[$i]['new_name'])) {
150-
$sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name'])
151-
. ' TO ' . $this->db->escapeIdentifiers($field[$i]['new_name']);
152+
if ($alterType === 'MODIFY' && ! empty($processedFields[$i]['new_name'])) {
153+
$sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($processedFields[$i]['name'])
154+
. ' TO ' . $this->db->escapeIdentifiers($processedFields[$i]['new_name']);
152155
}
153156

154-
$field[$i] = "\n\t" . $field[$i]['_literal'];
157+
$processedFields[$i] = "\n\t" . $processedFields[$i]['_literal'];
155158
}
156159
}
157160

158161
$sql .= ' ' . $alterType . ' ';
159-
$sql .= count($field) === 1
160-
? $field[0]
161-
: '(' . implode(',', $field) . ')';
162+
$sql .= count($processedFields) === 1
163+
? $processedFields[0]
164+
: '(' . implode(',', $processedFields) . ')';
162165

163166
// RENAME COLUMN must be executed after MODIFY
164167
array_unshift($sqls, $sql);

system/Database/Postgre/Forge.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ protected function _createTableAttributes(array $attributes): string
8181
}
8282

8383
/**
84-
* @param array|string $field
84+
* @param array|string $processedFields Processed column definitions
85+
* or column names to DROP
8586
*
86-
* @return array|bool|string
87+
* @return false|list<string>|string SQL string or false
88+
* @phpstan-return ($alterType is 'DROP' ? string : list<string>|false)
8789
*/
88-
protected function _alterTable(string $alterType, string $table, $field)
90+
protected function _alterTable(string $alterType, string $table, $processedFields)
8991
{
9092
if (in_array($alterType, ['DROP', 'ADD'], true)) {
91-
return parent::_alterTable($alterType, $table, $field);
93+
return parent::_alterTable($alterType, $table, $processedFields);
9294
}
9395

9496
$sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
9597
$sqls = [];
9698

97-
foreach ($field as $data) {
99+
foreach ($processedFields as $data) {
98100
if ($data['_literal'] !== false) {
99101
return false;
100102
}

system/Database/SQLSRV/Forge.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,25 @@ protected function _createTableAttributes(array $attributes): string
126126
}
127127

128128
/**
129-
* @param array|string $field
129+
* @param array|string $processedFields Processed column definitions
130+
* or column names to DROP
130131
*
131-
* @return false|string|string[]
132+
* @return list<string>|string SQL string
133+
* @phpstan-return ($alterType is 'DROP' ? string : list<string>)
132134
*/
133-
protected function _alterTable(string $alterType, string $table, $field)
135+
protected function _alterTable(string $alterType, string $table, $processedFields)
134136
{
135137
// Handle DROP here
136138
if ($alterType === 'DROP') {
137139
// check if fields are part of any indexes
138140
$indexData = $this->db->getIndexData($table);
139141

140142
foreach ($indexData as $index) {
141-
if (is_string($field)) {
142-
$field = explode(',', $field);
143+
if (is_string($processedFields)) {
144+
$processedFields = explode(',', $processedFields);
143145
}
144146

145-
$fld = array_intersect($field, $index->fields);
147+
$fld = array_intersect($processedFields, $index->fields);
146148

147149
// Drop index if field is part of an index
148150
if ($fld !== []) {
@@ -153,7 +155,7 @@ protected function _alterTable(string $alterType, string $table, $field)
153155
$fullTable = $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table);
154156

155157
// Drop default constraints
156-
$fields = implode(',', $this->db->escape((array) $field));
158+
$fields = implode(',', $this->db->escape((array) $processedFields));
157159

158160
$sql = <<<SQL
159161
SELECT name
@@ -170,7 +172,7 @@ protected function _alterTable(string $alterType, string $table, $field)
170172

171173
$sql = 'ALTER TABLE ' . $fullTable . ' DROP ';
172174

173-
$fields = array_map(static fn ($item) => 'COLUMN [' . trim($item) . ']', (array) $field);
175+
$fields = array_map(static fn ($item) => 'COLUMN [' . trim($item) . ']', (array) $processedFields);
174176

175177
return $sql . implode(',', $fields);
176178
}
@@ -181,14 +183,14 @@ protected function _alterTable(string $alterType, string $table, $field)
181183
$sqls = [];
182184

183185
if ($alterType === 'ADD') {
184-
foreach ($field as $data) {
186+
foreach ($processedFields as $data) {
185187
$sqls[] = $sql . ($data['_literal'] !== false ? $data['_literal'] : $this->_processColumn($data));
186188
}
187189

188190
return $sqls;
189191
}
190192

191-
foreach ($field as $data) {
193+
foreach ($processedFields as $data) {
192194
if ($data['_literal'] !== false) {
193195
return false;
194196
}

system/Database/SQLite3/Forge.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,34 @@ public function dropDatabase(string $dbName): bool
109109
}
110110

111111
/**
112-
* @param array|string $field
112+
* @param array|string $processedFields Processed column definitions
113+
* or column names to DROP
113114
*
114115
* @return array|string|null
116+
* @return list<string>|string|null SQL string or null
115117
*/
116-
protected function _alterTable(string $alterType, string $table, $field)
118+
protected function _alterTable(string $alterType, string $table, $processedFields)
117119
{
118120
switch ($alterType) {
119121
case 'DROP':
120122
$sqlTable = new Table($this->db, $this);
121123

122124
$sqlTable->fromTable($table)
123-
->dropColumn($field)
125+
->dropColumn($processedFields)
124126
->run();
125127

126-
return '';
128+
return ''; // Why empty string?
127129
128130
case 'CHANGE':
129131
(new Table($this->db, $this))
130132
->fromTable($table)
131-
->modifyColumn($field)
133+
->modifyColumn($processedFields)
132134
->run();
133135

134-
return null;
136+
return null; // Why null?
135137
136138
default:
137-
return parent::_alterTable($alterType, $table, $field);
139+
return parent::_alterTable($alterType, $table, $processedFields);
138140
}
139141
}
140142

0 commit comments

Comments
 (0)