Skip to content

Commit 9f9dac2

Browse files
authored
Merge pull request #206 from thecodingmachine/fix/oracle-identifier-too-long
Oracle: Fix `ORA-00972: identifier is too long` (30 char limit)
2 parents 07360b6 + 53568b6 commit 9f9dac2

File tree

6 files changed

+505
-117
lines changed

6 files changed

+505
-117
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require" : {
2121
"php" : ">=7.1",
22-
"mouf/magic-query" : "^1.3.0",
22+
"mouf/magic-query" : "^1.4",
2323
"mouf/schema-analyzer": "^1.1.4",
2424
"doctrine/dbal": "^2.9.2",
2525
"psr/log": "~1.0",
@@ -32,7 +32,7 @@
3232
"mouf/utils.log.psr.multi-logger": "^1.0",
3333
"symfony/filesystem": "^2.7 || ^3 || ^4 || ^5",
3434
"ramsey/uuid": "^3.7",
35-
"doctrine/annotations": "^1.6",
35+
"doctrine/annotations": "^1.10",
3636
"zendframework/zend-code": "^3.4",
3737
"psr/container": "^1",
3838
"ext-PDO": "*",
@@ -45,7 +45,7 @@
4545
"phpunit/phpunit": "^7.4.4 || ^8.0.0",
4646
"php-coveralls/php-coveralls": "^2.1",
4747
"wa72/simplelogger" : "^1.0",
48-
"friendsofphp/php-cs-fixer": "^2.15.1",
48+
"friendsofphp/php-cs-fixer": "^2.16",
4949
"symfony/process": "^3 || ^4 || ^5",
5050
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0",
5151
"phpstan/phpstan": "^0.11.5",

src/QueryFactory/AbstractQueryFactory.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,35 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc
165165
foreach ($allFetchedTables as $table) {
166166
foreach ($this->schema->getTable($table)->getColumns() as $column) {
167167
$columnName = $column->getName();
168-
$columnDescList[$table.'____'.$columnName] = [
169-
'as' => $table.'____'.$columnName,
168+
$alias = self::getColumnAlias($table, $columnName);
169+
$columnDescList[$alias] = [
170+
'as' => $alias,
170171
'table' => $table,
171172
'column' => $columnName,
172173
'type' => $column->getType(),
173174
'tableGroup' => $tableGroups[$table],
174175
];
175-
$columnsList[] = $mysqlPlatform->quoteIdentifier($table).'.'.$mysqlPlatform->quoteIdentifier($columnName).' as '.
176-
$connection->quoteIdentifier($table.'____'.$columnName);
176+
$columnsList[] = sprintf(
177+
'%s.%s as %s',
178+
$mysqlPlatform->quoteIdentifier($table),
179+
$mysqlPlatform->quoteIdentifier($columnName),
180+
$connection->quoteIdentifier($alias)
181+
);
177182
}
178183
}
179184

180185
return [$columnDescList, $columnsList, $reconstructedOrderBy];
181186
}
182187

188+
public static function getColumnAlias(string $tableName, string $columnName): string
189+
{
190+
$alias = $tableName.'____'.$columnName;
191+
if (strlen($alias) <= 30) { // Older oracle version had a limit of 30 characters for identifiers
192+
return $alias;
193+
}
194+
return substr($columnName, 0, 20) . crc32($tableName.'____'.$columnName);
195+
}
196+
183197
abstract protected function compute(): void;
184198

185199
/**

src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private function formatSelect(array $baseSelect): array
211211
$pkColumns = $table->getPrimaryKeyColumns();
212212
foreach ($table->getColumns() as $column) {
213213
$columnName = $column->getName();
214-
$alias = "{$tableName}____{$columnName}";
214+
$alias = AbstractQueryFactory::getColumnAlias($tableName, $columnName);
215215
$astColumn = [
216216
'expr_type' => 'colref',
217217
'base_expr' => $connection->quoteIdentifier($tableName) . '.' . $connection->quoteIdentifier($columnName),

src/QueryFactory/QueryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getMagicSql() : string;
3131
public function getMagicSqlCount() : string;
3232

3333
/**
34-
* @return mixed[][] An array of column descriptors. The key is in the form "$tableName____$columnName". Value is an array with those keys: as, table, colum, type, tableGroup
34+
* @return mixed[][] An array of column descriptors. The key might be in the form "$tableName____$columnName". Value is an array with those keys: as, table, column, type, tableGroup
3535
*/
3636
public function getColumnDescriptors() : array;
3737
}

0 commit comments

Comments
 (0)