Skip to content

Commit

Permalink
Merge pull request #1 from xhuberty/2.5
Browse files Browse the repository at this point in the history
Add a new QuoteStrategy that automatically escape database reserved k…
  • Loading branch information
xhuberty committed Jan 8, 2016
2 parents f09689c + 72676c3 commit 1073571
Showing 1 changed file with 176 additions and 0 deletions.
176 changes: 176 additions & 0 deletions lib/Doctrine/ORM/Mapping/EscapingQuoteStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Mapping;

use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* A set of rules for determining the physical column, alias and table quotes and automatically escape database reserved
* keyword
*
*@author Xavier HUBERTY <[email protected]>
*/
class EscapingQuoteStrategy implements QuoteStrategy
{
/**
* {@inheritdoc}
*/
public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
{
if(isset($class->fieldMappings[$fieldName]['quoted'])){
return $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($fieldName)){
return $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName']);
}
return $class->fieldMappings[$fieldName]['columnName'];
}

/**
* {@inheritdoc}
*/
public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
{
if(isset($class->table['quoted'])){
return $platform->quoteIdentifier($class->table['name']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($class->table['name'])){
return $platform->quoteIdentifier($class->table['name']);
}
return $class->table['name'];
}

/**
* {@inheritdoc}
*/
public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform)
{
if(isset($definition['quoted'])){
return $platform->quoteIdentifier($class->table['name']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($definition['sequenceName'])){
return $platform->quoteIdentifier($definition['sequenceName']);
}
return $definition['sequenceName'];
}

/**
* {@inheritdoc}
*/
public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
{
if(isset($joinColumn['quoted'])){
return $platform->quoteIdentifier($joinColumn['name']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($joinColumn['name'])){
return $platform->quoteIdentifier($joinColumn['name']);
}
return $joinColumn['name'];
}

/**
* {@inheritdoc}
*/
public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
{
if(isset($joinColumn['quoted'])){
return $platform->quoteIdentifier($joinColumn['referencedColumnName']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($joinColumn['referencedColumnName'])){
return $platform->quoteIdentifier($joinColumn['referencedColumnName']);
}
return $joinColumn['referencedColumnName'];
}

/**
* {@inheritdoc}
*/
public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
{
if(isset($association['joinTable']['quoted'])){
return $platform->quoteIdentifier($association['joinTable']['name']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($association['joinTable']['name'])){
return $platform->quoteIdentifier($association['joinTable']['name']);
}
return $association['joinTable']['name'];
}

/**
* {@inheritdoc}
*/
public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
{
$quotedColumnNames = array();

foreach ($class->identifier as $fieldName) {
if (isset($class->fieldMappings[$fieldName])) {
$quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);

continue;
}

// Association defined as Id field
$joinColumns = $class->associationMappings[$fieldName]['joinColumns'];
$assocQuotedColumnNames = array_map(
function ($joinColumn) use ($platform)
{
if(isset($joinColumn['quoted'])){
return $platform->quoteIdentifier($joinColumn['name']);
}
$reservedKeyList = $platform->getReservedKeywordsList();
if($reservedKeyList->isKeyword($joinColumn['name'])){
return $platform->quoteIdentifier($joinColumn['name']);
}
return $joinColumn['name'];
},
$joinColumns
);

$quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
}

return $quotedColumnNames;
}

/**
* {@inheritdoc}
*/
public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
{
// 1 ) Concatenate column name and counter
// 2 ) Trim the column alias to the maximum identifier length of the platform.
// If the alias is to long, characters are cut off from the beginning.
// 3 ) Strip non alphanumeric characters
// 4 ) Prefix with "_" if the result its numeric
$columnName = $columnName . '_' . $counter;
$columnName = substr($columnName, -$platform->getMaxIdentifierLength());
$columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
$columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;

return $platform->getSQLResultCasing($columnName);
}
}

0 comments on commit 1073571

Please sign in to comment.