AbstractAsset.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. use Doctrine\DBAL\Platforms\AbstractPlatform;
  23. /**
  24. * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
  25. *
  26. * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
  27. * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure
  28. *
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link www.doctrine-project.org
  31. * @since 2.0
  32. * @version $Revision$
  33. * @author Benjamin Eberlei <kontakt@beberlei.de>
  34. */
  35. abstract class AbstractAsset
  36. {
  37. /**
  38. * @var string
  39. */
  40. protected $_name;
  41. protected $_quoted = false;
  42. /**
  43. * Set name of this asset
  44. *
  45. * @param string $name
  46. */
  47. protected function _setName($name)
  48. {
  49. if ($this->isQuoted($name)) {
  50. $this->_quoted = true;
  51. $name = $this->trimQuotes($name);
  52. }
  53. $this->_name = $name;
  54. }
  55. /**
  56. * Check if this identifier is quoted.
  57. *
  58. * @param string $identifier
  59. * @return bool
  60. */
  61. protected function isQuoted($identifier)
  62. {
  63. return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"'));
  64. }
  65. /**
  66. * Trim quotes from the identifier.
  67. *
  68. * @param string $identifier
  69. * @return string
  70. */
  71. protected function trimQuotes($identifier)
  72. {
  73. return trim($identifier, '`"');
  74. }
  75. /**
  76. * Return name of this schema asset.
  77. *
  78. * @return string
  79. */
  80. public function getName()
  81. {
  82. return $this->_name;
  83. }
  84. /**
  85. * Get the quoted representation of this asset but only if it was defined with one. Otherwise
  86. * return the plain unquoted value as inserted.
  87. *
  88. * @param AbstractPlatform $platform
  89. * @return string
  90. */
  91. public function getQuotedName(AbstractPlatform $platform)
  92. {
  93. return ($this->_quoted) ? $platform->quoteIdentifier($this->_name) : $this->_name;
  94. }
  95. /**
  96. * Generate an identifier from a list of column names obeying a certain string length.
  97. *
  98. * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
  99. * however building idents automatically for foreign keys, composite keys or such can easily create
  100. * very long names.
  101. *
  102. * @param array $columnNames
  103. * @param string $prefix
  104. * @param int $maxSize
  105. * @return string
  106. */
  107. protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30)
  108. {
  109. /*$columnCount = count($columnNames);
  110. $postfixLen = strlen($postfix);
  111. $parts = array_map(function($columnName) use($columnCount, $postfixLen, $maxSize) {
  112. return substr($columnName, -floor(($maxSize-$postfixLen)/$columnCount - 1));
  113. }, $columnNames);
  114. $parts[] = $postfix;
  115. $identifier = trim(implode("_", $parts), '_');
  116. // using implicit schema support of DB2 and Postgres there might be dots in the auto-generated
  117. // identifier names which can easily be replaced by underscores.
  118. $identifier = str_replace(".", "_", $identifier);
  119. if (is_numeric(substr($identifier, 0, 1))) {
  120. $identifier = "i" . substr($identifier, 0, strlen($identifier)-1);
  121. }
  122. return $identifier;*/
  123. $hash = implode("", array_map(function($column) {
  124. return dechex(crc32($column));
  125. }, $columnNames));
  126. return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize);
  127. }
  128. }