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