Skip to content

Commit

Permalink
Merge pull request #206 from thecodingmachine/fix/oracle-identifier-t…
Browse files Browse the repository at this point in the history
…oo-long

Oracle: Fix `ORA-00972: identifier is too long` (30 char limit)
  • Loading branch information
moufmouf authored Jun 30, 2020
2 parents 07360b6 + 53568b6 commit 9f9dac2
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 117 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
Loading

0 comments on commit 9f9dac2

Please sign in to comment.