DefaultQuoteStrategy.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\ORM\Mapping\ClassMetadata;
  21. use Doctrine\DBAL\Platforms\AbstractPlatform;
  22. /**
  23. * A set of rules for determining the physical column, alias and table quotes
  24. *
  25. * @since 2.3
  26. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  27. */
  28. class DefaultQuoteStrategy implements QuoteStrategy
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
  34. {
  35. return isset($class->fieldMappings[$fieldName]['quoted'])
  36. ? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
  37. : $class->fieldMappings[$fieldName]['columnName'];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
  43. {
  44. return isset($class->table['quoted'])
  45. ? $platform->quoteIdentifier($class->table['name'])
  46. : $class->table['name'];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform)
  52. {
  53. return isset($definition['quoted'])
  54. ? $platform->quoteIdentifier($definition['sequenceName'])
  55. : $definition['sequenceName'];
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
  61. {
  62. return isset($joinColumn['quoted'])
  63. ? $platform->quoteIdentifier($joinColumn['name'])
  64. : $joinColumn['name'];
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
  70. {
  71. return isset($joinColumn['quoted'])
  72. ? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
  73. : $joinColumn['referencedColumnName'];
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
  79. {
  80. return isset($association['joinTable']['quoted'])
  81. ? $platform->quoteIdentifier($association['joinTable']['name'])
  82. : $association['joinTable']['name'];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
  88. {
  89. $quotedColumnNames = array();
  90. foreach ($class->identifier as $fieldName) {
  91. if (isset($class->fieldMappings[$fieldName])) {
  92. $quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
  93. continue;
  94. }
  95. // Association defined as Id field
  96. $joinColumns = $class->associationMappings[$fieldName]['joinColumns'];
  97. $assocQuotedColumnNames = array_map(
  98. function ($joinColumn) use ($platform)
  99. {
  100. return isset($joinColumn['quoted'])
  101. ? $platform->quoteIdentifier($joinColumn['name'])
  102. : $joinColumn['name'];
  103. },
  104. $joinColumns
  105. );
  106. $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
  107. }
  108. return $quotedColumnNames;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
  114. {
  115. // Trim the column alias to the maximum identifier length of the platform.
  116. // If the alias is to long, characters are cut off from the beginning.
  117. // And strip non alphanumeric characters
  118. $columnName = $columnName . $counter;
  119. $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
  120. $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
  121. return $platform->getSQLResultCasing($columnName);
  122. }
  123. }