Skip to content

Commit

Permalink
Merge pull request propelorm#1694 from propelorm/feature/performance
Browse files Browse the repository at this point in the history
Updated TableMap generator to add column name map for normalization, …
  • Loading branch information
dereuromark authored Jan 13, 2021
2 parents 8aaf565 + 832c9f1 commit a422bdd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
59 changes: 59 additions & 0 deletions src/Propel/Generator/Builder/Om/TableMapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ protected function addClassBody(&$script)
$this->addAttributes($script);

$script .= $this->addFieldsAttributes();
$this->addNormalizedColumnNameMap($script);

if ($table->hasValueSetColumns()) {
$this->addValueSetColumnAttributes($script);
Expand Down Expand Up @@ -444,6 +445,64 @@ protected function addFieldsAttributes()
]);
}

/**
* @param string $script
*
* @return void
*/
protected function addNormalizedColumnNameMap(&$script): void
{
$script .= '
/**
* Holds a list of column names and their normalized version.
*
* @var string[]
*/
protected $normalizedColumnNameMap = [' . PHP_EOL;

$table = $this->getTable();
$tableColumns = $table->getColumns();

foreach ($tableColumns as $num => $column) {
$normalizedName = strtoupper($column->getName());

// ColumnName => COLUMN_NAME
$script .= '
\'' . $column->getPhpName() . '\' => \'' . $normalizedName . '\',';

// TableName.ColumnName => COLUMN_NAME
$script .= '
\'' . $table->getPhpName() . '.' . $column->getPhpName() . '\' => \'' . $normalizedName . '\',';

// columnName => COLUMN_NAME
$script .= '
\'' . $column->getCamelCaseName() . '\' => \'' . $normalizedName . '\',';

// tableName.columnName => COLUMN_NAME
$script .= '
\'' . $table->getCamelCaseName() . '.' . $column->getCamelCaseName() . '\' => \'' . $normalizedName . '\',';

// TableNameTableMap::COL_COLUMN_NAME => COLUMN_NAME
$script .= '
\'' . $this->getColumnConstant($column, $this->getTableMapClass()) . '\' => \'' . $normalizedName . '\',';

// COL_COLUMN_NAME => COLUMN_NAME
$script .= '
\'' . $column->getConstantName() . '\' => \'' . $normalizedName . '\',';

// column_name => COLUMN_NAME
$script .= '
\'' . $column->getName() . '\' => \'' . $normalizedName . '\',';

// table_name.column_name => COLUMN_NAME
$script .= '
\'' . $table->getName() . '.' . $column->getName() . '\' => \'' . $normalizedName . '\',';
}

$script .= '
];' . PHP_EOL;
}

/**
* Closes class.
*
Expand Down
23 changes: 20 additions & 3 deletions src/Propel/Runtime/Map/TableMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class TableMap
*/
protected $columnsByPhpName = [];

/**
* Map of normalized column names
*
* @var string[]
*/
protected $normalizedColumnNameMap = [];

/**
* The database this table belongs to
*
Expand Down Expand Up @@ -411,6 +418,16 @@ private static function getPrimaryKey(Criteria $criteria)
return $pk;
}

/**
* @param string $columnName
*
* @return string
*/
protected function getNormalizedColumnName(string $columnName): string
{
return $this->normalizedColumnNameMap[$columnName] ?? ColumnMap::normalizeName($columnName);
}

/**
* Add a column to the table.
*
Expand Down Expand Up @@ -445,7 +462,7 @@ public function addColumn($name, $phpName, $type, $isNotNull = false, $size = nu
$this->foreignKeys[$name] = $col;
}

$this->columns[ColumnMap::normalizeName($name)] = $col;
$this->columns[$this->getNormalizedColumnName($name)] = $col;
$this->columnsByPhpName[$phpName] = $col;

return $col;
Expand Down Expand Up @@ -479,7 +496,7 @@ public function hasColumn($name, $normalize = true)
if ($name instanceof ColumnMap) {
$name = $name->getName();
} elseif ($normalize) {
$name = ColumnMap::normalizeName($name);
$name = $this->getNormalizedColumnName($name);
}

return isset($this->columns[$name]);
Expand All @@ -498,7 +515,7 @@ public function hasColumn($name, $normalize = true)
public function getColumn($name, $normalize = true)
{
if ($normalize) {
$name = ColumnMap::normalizeName($name);
$name = $this->getNormalizedColumnName($name);
}
if (!$this->hasColumn($name, false)) {
throw new ColumnNotFoundException(sprintf('Cannot fetch ColumnMap for undefined column: %s in table %s.', $name, $this->getName()));
Expand Down

0 comments on commit a422bdd

Please sign in to comment.