. */ namespace Doctrine\ORM\Mapping; /** * A set of rules for determining the physical column, alias and table quotes * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.4 * @author Fabio B. Silva */ class DefaultQuoteStrategy extends QuoteStrategy { public function isQuotedIdentifier($identifier) { return strlen($identifier) > 0 && $identifier[0] === '`'; } public function getUnquotedIdentifier($identifier) { return trim($identifier, '`'); } /** * @param string $fieldName * @param ClassMetadata $class * @return string */ public function getColumnName($fieldName, ClassMetadata $class) { return isset($class->fieldMappings[$fieldName]['quoted']) ? $this->platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName']) : $class->fieldMappings[$fieldName]['columnName']; } /** * Gets the (possibly quoted) primary table name of this class for safe use * in an SQL statement. * * @param ClassMetadata $class * @return string */ public function getTableName(ClassMetadata $class) { return isset($class->table['quoted']) ? $this->platform->quoteIdentifier($class->table['name']) : $class->table['name']; } /** * Gets the (possibly quoted) name of the join table. * * @param ClassMetadata $class * @return string */ public function getJoinTableName($relation, ClassMetadata $class) { $assoc = $class->associationMappings[$relation]; return isset($assoc['joinTable']['quoted']) ? $this->platform->quoteIdentifier($assoc['joinTable']['name']) : $assoc['joinTable']['name']; } /** * Gets the (possibly quoted) identifier column names for safe use in an SQL statement. * * @param ClassMetadata $class * @return array */ public function getIdentifierColumnNames(ClassMetadata $class) { $quotedColumnNames = array(); foreach ($class->identifier as $fieldName) { if (isset($this->fieldMappings[$fieldName])) { $quotedColumnNames[] = $this->getColumnName($fieldName, $class); continue; } // Association defined as Id field $platform = $this->platform; $joinColumns = $class->associationMappings[$fieldName]['joinColumns']; $assocQuotedColumnNames = array_map( function ($joinColumn) use ($platform) { return isset($joinColumn['quoted']) ? $platform->quoteIdentifier($joinColumn['name']) : $joinColumn['name']; }, $joinColumns ); $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames); } return $quotedColumnNames; } /** * @param string $columnName * @param string $counter * @param ClassMetadata $class * @return string */ public function getColumnAlias($columnName, $counter, ClassMetadata $class = null) { // 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. // And strip non alphanumeric characters $columnName = $columnName . $counter; $columnName = substr($columnName, -$this->platform->getMaxIdentifierLength()); $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName); return $this->platform->getSQLResultCasing($columnName); } }