AbstractAsset.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\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. *
  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->isIdentifierQuoted($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 asset's name is quoted
  123. *
  124. * @return bool
  125. */
  126. public function isQuoted()
  127. {
  128. return $this->_quoted;
  129. }
  130. /**
  131. * Check if this identifier is quoted.
  132. *
  133. * @param string $identifier
  134. * @return bool
  135. */
  136. protected function isIdentifierQuoted($identifier)
  137. {
  138. return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"'));
  139. }
  140. /**
  141. * Trim quotes from the identifier.
  142. *
  143. * @param string $identifier
  144. * @return string
  145. */
  146. protected function trimQuotes($identifier)
  147. {
  148. return str_replace(array('`', '"'), '', $identifier);
  149. }
  150. /**
  151. * Return name of this schema asset.
  152. *
  153. * @return string
  154. */
  155. public function getName()
  156. {
  157. if ($this->_namespace) {
  158. return $this->_namespace . "." . $this->_name;
  159. }
  160. return $this->_name;
  161. }
  162. /**
  163. * Get the quoted representation of this asset but only if it was defined with one. Otherwise
  164. * return the plain unquoted value as inserted.
  165. *
  166. * @param AbstractPlatform $platform
  167. * @return string
  168. */
  169. public function getQuotedName(AbstractPlatform $platform)
  170. {
  171. $keywords = $platform->getReservedKeywordsList();
  172. $parts = explode(".", $this->getName());
  173. foreach ($parts as $k => $v) {
  174. $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
  175. }
  176. return implode(".", $parts);
  177. }
  178. /**
  179. * Generate an identifier from a list of column names obeying a certain string length.
  180. *
  181. * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
  182. * however building idents automatically for foreign keys, composite keys or such can easily create
  183. * very long names.
  184. *
  185. * @param array $columnNames
  186. * @param string $prefix
  187. * @param int $maxSize
  188. * @return string
  189. */
  190. protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30)
  191. {
  192. $hash = implode("", array_map(function($column) {
  193. return dechex(crc32($column));
  194. }, $columnNames));
  195. return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize);
  196. }
  197. }