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.
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>.
20 namespace Doctrine\DBAL\Schema;
22 use Doctrine\DBAL\Platforms\AbstractPlatform;
25 * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
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
31 * @link www.doctrine-project.org
33 * @author Benjamin Eberlei <kontakt@beberlei.de>
35 abstract class AbstractAsset
43 * Namespace of the asset. If none isset the default namespace is assumed.
47 protected $_namespace;
52 protected $_quoted = false;
55 * Set name of this asset
59 protected function _setName($name)
61 if ($this->isIdentifierQuoted($name)) {
62 $this->_quoted = true;
63 $name = $this->trimQuotes($name);
65 if (strpos($name, ".") !== false) {
66 $parts = explode(".", $name);
67 $this->_namespace = $parts[0];
74 * Is this asset in the default namespace?
76 * @param string $defaultNamespaceName
79 public function isInDefaultNamespace($defaultNamespaceName)
81 return $this->_namespace == $defaultNamespaceName || $this->_namespace === null;
85 * Get namespace name of this asset.
87 * If NULL is returned this means the default namespace is used.
91 public function getNamespaceName()
93 return $this->_namespace;
97 * The shortest name is stripped of the default namespace. All other
98 * namespaced elements are returned as full-qualified names.
103 public function getShortestName($defaultNamespaceName)
105 $shortestName = $this->getName();
106 if ($this->_namespace == $defaultNamespaceName) {
107 $shortestName = $this->_name;
109 return strtolower($shortestName);
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.
118 * Every non-namespaced element is prefixed with the default namespace
119 * name which is passed as argument to this method.
123 public function getFullQualifiedName($defaultNamespaceName)
125 $name = $this->getName();
126 if ( ! $this->_namespace) {
127 $name = $defaultNamespaceName . "." . $name;
129 return strtolower($name);
133 * Check if this asset's name is quoted
137 public function isQuoted()
139 return $this->_quoted;
143 * Check if this identifier is quoted.
145 * @param string $identifier
148 protected function isIdentifierQuoted($identifier)
150 return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"'));
154 * Trim quotes from the identifier.
156 * @param string $identifier
159 protected function trimQuotes($identifier)
161 return str_replace(array('`', '"'), '', $identifier);
165 * Return name of this schema asset.
169 public function getName()
171 if ($this->_namespace) {
172 return $this->_namespace . "." . $this->_name;
178 * Get the quoted representation of this asset but only if it was defined with one. Otherwise
179 * return the plain unquoted value as inserted.
181 * @param AbstractPlatform $platform
184 public function getQuotedName(AbstractPlatform $platform)
186 $keywords = $platform->getReservedKeywordsList();
187 $parts = explode(".", $this->getName());
188 foreach ($parts as $k => $v) {
189 $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
192 return implode(".", $parts);
196 * Generate an identifier from a list of column names obeying a certain string length.
198 * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
199 * however building idents automatically for foreign keys, composite keys or such can easily create
202 * @param array $columnNames
203 * @param string $prefix
204 * @param int $maxSize
207 protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30)
209 $hash = implode("", array_map(function($column) {
210 return dechex(crc32($column));
212 return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize);