1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php

143 lines
5.0 KiB
PHP
Raw Normal View History

2012-05-11 04:18:09 +04:00
<?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
2012-05-11 04:18:09 +04:00
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Mapping;
2012-06-12 03:14:17 +04:00
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Platforms\AbstractPlatform;
2012-05-11 04:18:09 +04:00
/**
* A set of rules for determining the physical column, alias and table quotes
*
2012-06-04 23:56:46 +04:00
* @since 2.3
2012-05-11 04:18:09 +04:00
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/
2012-06-12 03:14:17 +04:00
class DefaultQuoteStrategy implements QuoteStrategy
2012-05-11 04:18:09 +04:00
{
/**
2012-06-01 01:37:51 +04:00
* {@inheritdoc}
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
2012-05-11 04:18:09 +04:00
{
return isset($class->fieldMappings[$fieldName]['quoted'])
2012-06-12 03:14:17 +04:00
? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
2012-05-11 04:18:09 +04:00
: $class->fieldMappings[$fieldName]['columnName'];
}
/**
2012-06-01 01:37:51 +04:00
* {@inheritdoc}
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
2012-05-11 04:18:09 +04:00
{
return isset($class->table['quoted'])
2012-06-12 16:47:40 +04:00
? $platform->quoteIdentifier($class->table['name'])
: $class->table['name'];
2012-05-11 04:18:09 +04:00
}
2012-06-06 00:10:44 +04:00
/**
* {@inheritdoc}
*/
2012-06-12 03:14:17 +04:00
public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform)
2012-06-06 00:10:44 +04:00
{
return isset($definition['quoted'])
2012-06-12 16:47:40 +04:00
? $platform->quoteIdentifier($definition['sequenceName'])
: $definition['sequenceName'];
2012-06-06 00:10:44 +04:00
}
/**
* {@inheritdoc}
*/
2012-06-12 03:14:17 +04:00
public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
2012-06-06 00:10:44 +04:00
{
return isset($joinColumn['quoted'])
2012-06-12 03:14:17 +04:00
? $platform->quoteIdentifier($joinColumn['name'])
: $joinColumn['name'];
2012-06-06 20:14:20 +04:00
}
/**
* {@inheritdoc}
*/
2012-06-12 03:14:17 +04:00
public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
2012-06-06 20:14:20 +04:00
{
return isset($joinColumn['quoted'])
2012-06-12 03:14:17 +04:00
? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
: $joinColumn['referencedColumnName'];
2012-06-06 00:10:44 +04:00
}
2012-05-11 04:18:09 +04:00
/**
2012-06-01 01:37:51 +04:00
* {@inheritdoc}
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
2012-05-11 04:18:09 +04:00
{
2012-06-01 02:10:34 +04:00
return isset($association['joinTable']['quoted'])
2012-06-12 03:14:17 +04:00
? $platform->quoteIdentifier($association['joinTable']['name'])
2012-06-01 02:10:34 +04:00
: $association['joinTable']['name'];
2012-05-11 04:18:09 +04:00
}
/**
2012-06-01 01:37:51 +04:00
* {@inheritdoc}
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
2012-05-11 04:18:09 +04:00
{
$quotedColumnNames = array();
foreach ($class->identifier as $fieldName) {
2012-05-30 22:28:03 +04:00
if (isset($class->fieldMappings[$fieldName])) {
2012-06-12 03:14:17 +04:00
$quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
2012-05-11 04:18:09 +04:00
continue;
}
// Association defined as Id field
$joinColumns = $class->associationMappings[$fieldName]['joinColumns'];
$assocQuotedColumnNames = array_map(
2012-06-18 22:24:52 +04:00
function ($joinColumn) use ($platform)
{
2012-05-11 04:18:09 +04:00
return isset($joinColumn['quoted'])
? $platform->quoteIdentifier($joinColumn['name'])
: $joinColumn['name'];
},
$joinColumns
);
$quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
}
return $quotedColumnNames;
}
/**
2012-06-01 01:37:51 +04:00
* {@inheritdoc}
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
2012-05-11 04:18:09 +04:00
{
// 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
2012-05-11 04:18:09 +04:00
$columnName = $columnName . $counter;
2012-06-12 03:14:17 +04:00
$columnName = substr($columnName, -$platform->getMaxIdentifierLength());
2012-05-11 04:18:09 +04:00
$columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
$columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
2012-05-11 04:18:09 +04:00
2012-06-12 03:14:17 +04:00
return $platform->getSQLResultCasing($columnName);
2012-05-11 04:18:09 +04:00
}
2012-12-03 13:36:08 +04:00
}