AbstractAsset.php 6.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema;

22 23
use Doctrine\DBAL\Platforms\AbstractPlatform;

24 25 26 27 28 29
/**
 * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
 *
 * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
 * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure
 *
Benjamin Morel's avatar
Benjamin Morel committed
30 31 32
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 34 35 36 37 38 39 40
 */
abstract class AbstractAsset
{
    /**
     * @var string
     */
    protected $_name;

41 42 43
    /**
     * Namespace of the asset. If none isset the default namespace is assumed.
     *
Benjamin Morel's avatar
Benjamin Morel committed
44
     * @var string|null
45
     */
Benjamin Morel's avatar
Benjamin Morel committed
46
    protected $_namespace = null;
47 48

    /**
49
     * @var bool
50
     */
51 52
    protected $_quoted = false;

53
    /**
Benjamin Morel's avatar
Benjamin Morel committed
54
     * Sets the name of this asset.
55 56
     *
     * @param string $name
Benjamin Morel's avatar
Benjamin Morel committed
57 58
     *
     * @return void
59 60 61
     */
    protected function _setName($name)
    {
62
        if ($this->isIdentifierQuoted($name)) {
63 64
            $this->_quoted = true;
            $name = $this->trimQuotes($name);
65
        }
66 67 68 69 70
        if (strpos($name, ".") !== false) {
            $parts = explode(".", $name);
            $this->_namespace = $parts[0];
            $name = $parts[1];
        }
71 72 73
        $this->_name = $name;
    }

74 75 76 77
    /**
     * Is this asset in the default namespace?
     *
     * @param string $defaultNamespaceName
Benjamin Morel's avatar
Benjamin Morel committed
78
     *
79
     * @return bool
80 81 82 83 84 85 86
     */
    public function isInDefaultNamespace($defaultNamespaceName)
    {
        return $this->_namespace == $defaultNamespaceName || $this->_namespace === null;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
87
     * Gets the namespace name of this asset.
88 89 90
     *
     * If NULL is returned this means the default namespace is used.
     *
Benjamin Morel's avatar
Benjamin Morel committed
91
     * @return string|null
92 93 94 95 96 97 98 99 100 101
     */
    public function getNamespaceName()
    {
        return $this->_namespace;
    }

    /**
     * The shortest name is stripped of the default namespace. All other
     * namespaced elements are returned as full-qualified names.
     *
102
     * @param string $defaultNamespaceName
Benjamin Morel's avatar
Benjamin Morel committed
103
     *
104 105 106 107 108 109 110 111
     * @return string
     */
    public function getShortestName($defaultNamespaceName)
    {
        $shortestName = $this->getName();
        if ($this->_namespace == $defaultNamespaceName) {
            $shortestName = $this->_name;
        }
Benjamin Morel's avatar
Benjamin Morel committed
112

113 114 115 116 117 118 119 120 121 122 123 124
        return strtolower($shortestName);
    }

    /**
     * The normalized name is full-qualified and lowerspaced. Lowerspacing is
     * actually wrong, but we have to do it to keep our sanity. If you are
     * using database objects that only differentiate in the casing (FOO vs
     * Foo) then you will NOT be able to use Doctrine Schema abstraction.
     *
     * Every non-namespaced element is prefixed with the default namespace
     * name which is passed as argument to this method.
     *
Benjamin Morel's avatar
Benjamin Morel committed
125 126
     * @param string $defaultNamespaceName
     *
127 128 129 130 131
     * @return string
     */
    public function getFullQualifiedName($defaultNamespaceName)
    {
        $name = $this->getName();
132
        if ( ! $this->_namespace) {
133 134
            $name = $defaultNamespaceName . "." . $name;
        }
Benjamin Morel's avatar
Benjamin Morel committed
135

136 137 138
        return strtolower($name);
    }

139
    /**
Benjamin Morel's avatar
Benjamin Morel committed
140
     * Checks if this asset's name is quoted.
141
     *
142
     * @return bool
143 144 145 146 147 148
     */
    public function isQuoted()
    {
        return $this->_quoted;
    }

149
    /**
Benjamin Morel's avatar
Benjamin Morel committed
150
     * Checks if this identifier is quoted.
151
     *
Benjamin Morel's avatar
Benjamin Morel committed
152 153
     * @param string $identifier
     *
154
     * @return bool
155
     */
156
    protected function isIdentifierQuoted($identifier)
157
    {
158
        return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"' || $identifier[0] == '['));
159 160 161 162
    }

    /**
     * Trim quotes from the identifier.
163
     *
Benjamin Morel's avatar
Benjamin Morel committed
164 165
     * @param string $identifier
     *
166 167 168 169
     * @return string
     */
    protected function trimQuotes($identifier)
    {
170
        return str_replace(['`', '"', '[', ']'], '', $identifier);
171 172
    }

173
    /**
Benjamin Morel's avatar
Benjamin Morel committed
174
     * Returns the name of this schema asset.
175
     *
176 177 178 179
     * @return string
     */
    public function getName()
    {
180 181 182
        if ($this->_namespace) {
            return $this->_namespace . "." . $this->_name;
        }
183

184
        return $this->_name;
185
    }
186

187
    /**
Benjamin Morel's avatar
Benjamin Morel committed
188
     * Gets the quoted representation of this asset but only if it was defined with one. Otherwise
189 190
     * return the plain unquoted value as inserted.
     *
Benjamin Morel's avatar
Benjamin Morel committed
191 192
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
     *
193 194 195 196
     * @return string
     */
    public function getQuotedName(AbstractPlatform $platform)
    {
197
        $keywords = $platform->getReservedKeywordsList();
198
        $parts = explode(".", $this->getName());
199
        foreach ($parts as $k => $v) {
200 201 202 203
            $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
        }

        return implode(".", $parts);
204 205
    }

206
    /**
Benjamin Morel's avatar
Benjamin Morel committed
207
     * Generates an identifier from a list of column names obeying a certain string length.
208 209 210 211 212
     *
     * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
     * however building idents automatically for foreign keys, composite keys or such can easily create
     * very long names.
     *
213 214 215
     * @param array  $columnNames
     * @param string $prefix
     * @param int    $maxSize
Benjamin Morel's avatar
Benjamin Morel committed
216
     *
217 218
     * @return string
     */
219
    protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30)
220
    {
Steve Müller's avatar
Steve Müller committed
221
        $hash = implode("", array_map(function ($column) {
222 223
            return dechex(crc32($column));
        }, $columnNames));
Benjamin Morel's avatar
Benjamin Morel committed
224

225
        return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize);
226
    }
227
}