DefaultQuoteStrategy.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping;
  20. use Doctrine\DBAL\Platforms\AbstractPlatform;
  21. use function array_map;
  22. use function array_merge;
  23. use function is_numeric;
  24. use function preg_replace;
  25. use function substr;
  26. /**
  27. * A set of rules for determining the physical column, alias and table quotes
  28. */
  29. class DefaultQuoteStrategy implements QuoteStrategy
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
  35. {
  36. return isset($class->fieldMappings[$fieldName]['quoted'])
  37. ? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
  38. : $class->fieldMappings[$fieldName]['columnName'];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @todo Table names should be computed in DBAL depending on the platform
  44. */
  45. public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
  46. {
  47. $tableName = $class->table['name'];
  48. if (! empty($class->table['schema'])) {
  49. $tableName = $class->table['schema'] . '.' . $class->table['name'];
  50. if (! $platform->supportsSchemas() && $platform->canEmulateSchemas()) {
  51. $tableName = $class->table['schema'] . '__' . $class->table['name'];
  52. }
  53. }
  54. return isset($class->table['quoted'])
  55. ? $platform->quoteIdentifier($tableName)
  56. : $tableName;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform)
  62. {
  63. return isset($definition['quoted'])
  64. ? $platform->quoteIdentifier($definition['sequenceName'])
  65. : $definition['sequenceName'];
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
  71. {
  72. return isset($joinColumn['quoted'])
  73. ? $platform->quoteIdentifier($joinColumn['name'])
  74. : $joinColumn['name'];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
  80. {
  81. return isset($joinColumn['quoted'])
  82. ? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
  83. : $joinColumn['referencedColumnName'];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
  89. {
  90. $schema = '';
  91. if (isset($association['joinTable']['schema'])) {
  92. $schema = $association['joinTable']['schema'];
  93. $schema .= ! $platform->supportsSchemas() && $platform->canEmulateSchemas() ? '__' : '.';
  94. }
  95. $tableName = $association['joinTable']['name'];
  96. if (isset($association['joinTable']['quoted'])) {
  97. $tableName = $platform->quoteIdentifier($tableName);
  98. }
  99. return $schema . $tableName;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
  105. {
  106. $quotedColumnNames = [];
  107. foreach ($class->identifier as $fieldName) {
  108. if (isset($class->fieldMappings[$fieldName])) {
  109. $quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
  110. continue;
  111. }
  112. // Association defined as Id field
  113. $joinColumns = $class->associationMappings[$fieldName]['joinColumns'];
  114. $assocQuotedColumnNames = array_map(
  115. static function ($joinColumn) use ($platform) {
  116. return isset($joinColumn['quoted'])
  117. ? $platform->quoteIdentifier($joinColumn['name'])
  118. : $joinColumn['name'];
  119. },
  120. $joinColumns
  121. );
  122. $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
  123. }
  124. return $quotedColumnNames;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ?ClassMetadata $class = null)
  130. {
  131. // 1 ) Concatenate column name and counter
  132. // 2 ) Trim the column alias to the maximum identifier length of the platform.
  133. // If the alias is to long, characters are cut off from the beginning.
  134. // 3 ) Strip non alphanumeric characters
  135. // 4 ) Prefix with "_" if the result its numeric
  136. $columnName .= '_' . $counter;
  137. $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
  138. $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
  139. $columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
  140. return $platform->getSQLResultCasing($columnName);
  141. }
  142. }