Skip to content

Commit

Permalink
Oracle: Fix ORA-00972: identifier is too long (30 char limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Jun 30, 2020
1 parent 07360b6 commit 53568b6
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 117 deletions.
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -19,7 +19,7 @@
],
"require" : {
"php" : ">=7.1",
"mouf/magic-query" : "^1.3.0",
"mouf/magic-query" : "^1.4",
"mouf/schema-analyzer": "^1.1.4",
"doctrine/dbal": "^2.9.2",
"psr/log": "~1.0",
Expand All @@ -32,7 +32,7 @@
"mouf/utils.log.psr.multi-logger": "^1.0",
"symfony/filesystem": "^2.7 || ^3 || ^4 || ^5",
"ramsey/uuid": "^3.7",
"doctrine/annotations": "^1.6",
"doctrine/annotations": "^1.10",
"zendframework/zend-code": "^3.4",
"psr/container": "^1",
"ext-PDO": "*",
Expand All @@ -45,7 +45,7 @@
"phpunit/phpunit": "^7.4.4 || ^8.0.0",
"php-coveralls/php-coveralls": "^2.1",
"wa72/simplelogger" : "^1.0",
"friendsofphp/php-cs-fixer": "^2.15.1",
"friendsofphp/php-cs-fixer": "^2.16",
"symfony/process": "^3 || ^4 || ^5",
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0",
"phpstan/phpstan": "^0.11.5",
Expand Down
22 changes: 18 additions & 4 deletions src/QueryFactory/AbstractQueryFactory.php
Expand Up @@ -165,21 +165,35 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc
foreach ($allFetchedTables as $table) {
foreach ($this->schema->getTable($table)->getColumns() as $column) {
$columnName = $column->getName();
$columnDescList[$table.'____'.$columnName] = [
'as' => $table.'____'.$columnName,
$alias = self::getColumnAlias($table, $columnName);
$columnDescList[$alias] = [
'as' => $alias,
'table' => $table,
'column' => $columnName,
'type' => $column->getType(),
'tableGroup' => $tableGroups[$table],
];
$columnsList[] = $mysqlPlatform->quoteIdentifier($table).'.'.$mysqlPlatform->quoteIdentifier($columnName).' as '.
$connection->quoteIdentifier($table.'____'.$columnName);
$columnsList[] = sprintf(
'%s.%s as %s',
$mysqlPlatform->quoteIdentifier($table),
$mysqlPlatform->quoteIdentifier($columnName),
$connection->quoteIdentifier($alias)
);
}
}

return [$columnDescList, $columnsList, $reconstructedOrderBy];
}

public static function getColumnAlias(string $tableName, string $columnName): string
{
$alias = $tableName.'____'.$columnName;
if (strlen($alias) <= 30) { // Older oracle version had a limit of 30 characters for identifiers
return $alias;
}
return substr($columnName, 0, 20) . crc32($tableName.'____'.$columnName);
}

abstract protected function compute(): void;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php
Expand Up @@ -211,7 +211,7 @@ private function formatSelect(array $baseSelect): array
$pkColumns = $table->getPrimaryKeyColumns();
foreach ($table->getColumns() as $column) {
$columnName = $column->getName();
$alias = "{$tableName}____{$columnName}";
$alias = AbstractQueryFactory::getColumnAlias($tableName, $columnName);
$astColumn = [
'expr_type' => 'colref',
'base_expr' => $connection->quoteIdentifier($tableName) . '.' . $connection->quoteIdentifier($columnName),
Expand Down
2 changes: 1 addition & 1 deletion src/QueryFactory/QueryFactory.php
Expand Up @@ -31,7 +31,7 @@ public function getMagicSql() : string;
public function getMagicSqlCount() : string;

/**
* @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
* @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
*/
public function getColumnDescriptors() : array;
}

0 comments on commit 53568b6

Please sign in to comment.