AbstractAsset.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Platforms\AbstractPlatform;
  21. /**
  22. * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
  23. *
  24. * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
  25. * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure
  26. *
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 2.0
  30. * @author Benjamin Eberlei <kontakt@beberlei.de>
  31. */
  32. abstract class AbstractAsset
  33. {
  34. /**
  35. * @var string
  36. */
  37. protected $_name;
  38. /**
  39. * Namespace of the asset. If none isset the default namespace is assumed.
  40. *
  41. * @var string
  42. */
  43. protected $_namespace;
  44. /**
  45. * @var bool
  46. */
  47. protected $_quoted = false;
  48. /**
  49. * Set name of this asset
  50. *
  51. * @param string $name
  52. */
  53. protected function _setName($name)
  54. {
  55. if ($this->isQuoted($name)) {
  56. $this->_quoted = true;
  57. $name = $this->trimQuotes($name);
  58. }
  59. if (strpos($name, ".") !== false) {
  60. $parts = explode(".", $name);
  61. $this->_namespace = $parts[0];
  62. $name = $parts[1];
  63. }
  64. $this->_name = $name;
  65. }
  66. /**
  67. * Is this asset in the default namespace?
  68. *
  69. * @param string $defaultNamespaceName
  70. * @return bool
  71. */
  72. public function isInDefaultNamespace($defaultNamespaceName)
  73. {
  74. return $this->_namespace == $defaultNamespaceName || $this->_namespace === null;
  75. }
  76. /**
  77. * Get namespace name of this asset.
  78. *
  79. * If NULL is returned this means the default namespace is used.
  80. *
  81. * @return string
  82. */
  83. public function getNamespaceName()
  84. {
  85. return $this->_namespace;
  86. }
  87. /**
  88. * The shortest name is stripped of the default namespace. All other
  89. * namespaced elements are returned as full-qualified names.
  90. *
  91. * @param string
  92. * @return string
  93. */
  94. public function getShortestName($defaultNamespaceName)
  95. {
  96. $shortestName = $this->getName();
  97. if ($this->_namespace == $defaultNamespaceName) {
  98. $shortestName = $this->_name;
  99. }
  100. return strtolower($shortestName);
  101. }
  102. /**
  103. * The normalized name is full-qualified and lowerspaced. Lowerspacing is
  104. * actually wrong, but we have to do it to keep our sanity. If you are
  105. * using database objects that only differentiate in the casing (FOO vs
  106. * Foo) then you will NOT be able to use Doctrine Schema abstraction.
  107. *
  108. * Every non-namespaced element is prefixed with the default namespace
  109. * name which is passed as argument to this method.
  110. *
  111. * @return string
  112. */
  113. public function getFullQualifiedName($defaultNamespaceName)
  114. {
  115. $name = $this->getName();
  116. if (!$this->_namespace) {
  117. $name = $defaultNamespaceName . "." . $name;
  118. }
  119. return strtolower($name);
  120. }
  121. /**
  122. * Check if this identifier is quoted.
  123. *
  124. * @param string $identifier
  125. * @return bool
  126. */
  127. protected function isQuoted($identifier)
  128. {
  129. return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"'));
  130. }
  131. /**
  132. * Trim quotes from the identifier.
  133. *
  134. * @param string $identifier
  135. * @return string
  136. */
  137. protected function trimQuotes($identifier)
  138. {
  139. return str_replace(array('`', '"'), '', $identifier);
  140. }
  141. /**
  142. * Return name of this schema asset.
  143. *
  144. * @return string
  145. */
  146. public function getName()
  147. {
  148. if ($this->_namespace) {
  149. return $this->_namespace . "." . $this->_name;
  150. }
  151. return $this->_name;
  152. }
  153. /**
  154. * Get the quoted representation of this asset but only if it was defined with one. Otherwise
  155. * return the plain unquoted value as inserted.
  156. *
  157. * @param AbstractPlatform $platform
  158. * @return string
  159. */
  160. public function getQuotedName(AbstractPlatform $platform)
  161. {
  162. $keywords = $platform->getReservedKeywordsList();
  163. $parts = explode(".", $this->getName());
  164. foreach ($parts AS $k => $v) {
  165. $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
  166. }
  167. return implode(".", $parts);
  168. }
  169. /**
  170. * Generate an identifier from a list of column names obeying a certain string length.
  171. *
  172. * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
  173. * however building idents automatically for foreign keys, composite keys or such can easily create
  174. * very long names.
  175. *
  176. * @param array $columnNames
  177. * @param string $prefix
  178. * @param int $maxSize
  179. * @return string
  180. */
  181. protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30)
  182. {
  183. $hash = implode("", array_map(function($column) {
  184. return dechex(crc32($column));
  185. }, $columnNames));
  186. return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize);
  187. }
  188. }