SchemaException.php 5.91 KB
Newer Older
1
<?php
Benjamin Morel's avatar
Benjamin Morel committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */
19 20 21 22 23 24 25 26 27 28 29 30 31 32

namespace Doctrine\DBAL\Schema;

class SchemaException extends \Doctrine\DBAL\DBALException
{
    const TABLE_DOESNT_EXIST = 10;
    const TABLE_ALREADY_EXISTS = 20;
    const COLUMN_DOESNT_EXIST = 30;
    const COLUMN_ALREADY_EXISTS = 40;
    const INDEX_DOESNT_EXIST = 50;
    const INDEX_ALREADY_EXISTS = 60;
    const SEQUENCE_DOENST_EXIST = 70;
    const SEQUENCE_ALREADY_EXISTS = 80;
    const INDEX_INVALID_NAME = 90;
33
    const FOREIGNKEY_DOESNT_EXIST = 100;
34
    const NAMESPACE_ALREADY_EXISTS = 110;
35 36 37

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
38 39
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
40 41 42 43 44 45 46 47
     */
    static public function tableDoesNotExist($tableName)
    {
        return new self("There is no table with name '".$tableName."' in the schema.", self::TABLE_DOESNT_EXIST);
    }

    /**
     * @param string $indexName
Benjamin Morel's avatar
Benjamin Morel committed
48 49
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
50 51 52 53 54 55 56 57
     */
    static public function indexNameInvalid($indexName)
    {
        return new self("Invalid index-name $indexName given, has to be [a-zA-Z0-9_]", self::INDEX_INVALID_NAME);
    }

    /**
     * @param string $indexName
Benjamin Morel's avatar
Benjamin Morel committed
58 59 60
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
61
     */
62
    static public function indexDoesNotExist($indexName, $table)
63
    {
64
        return new self("Index '$indexName' does not exist on table '$table'.", self::INDEX_DOESNT_EXIST);
65 66 67 68
    }

    /**
     * @param string $indexName
Benjamin Morel's avatar
Benjamin Morel committed
69 70 71
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
72
     */
73
    static public function indexAlreadyExists($indexName, $table)
74
    {
75
        return new self("An index with name '$indexName' was already defined on table '$table'.", self::INDEX_ALREADY_EXISTS);
76 77 78 79
    }

    /**
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
80 81 82
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
83
     */
84
    static public function columnDoesNotExist($columnName, $table)
85
    {
86
        return new self("There is no column with name '$columnName' on table '$table'.", self::COLUMN_DOESNT_EXIST);
87 88
    }

89 90 91 92 93 94 95 96 97 98 99 100 101
    /**
     * @param string $namespaceName
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
    static public function namespaceAlreadyExists($namespaceName)
    {
        return new self(
            sprintf("The namespace with name '%s' already exists.", $namespaceName),
            self::NAMESPACE_ALREADY_EXISTS
        );
    }

102
    /**
Benjamin Morel's avatar
Benjamin Morel committed
103
     * @param string $tableName
104
     *
Benjamin Morel's avatar
Benjamin Morel committed
105
     * @return \Doctrine\DBAL\Schema\SchemaException
106 107 108 109 110 111 112 113 114
     */
    static public function tableAlreadyExists($tableName)
    {
        return new self("The table with name '".$tableName."' already exists.", self::TABLE_ALREADY_EXISTS);
    }

    /**
     * @param string $tableName
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
115 116
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
117 118 119 120 121 122 123 124 125 126
     */
    static public function columnAlreadyExists($tableName, $columnName)
    {
        return new self(
            "The column '".$columnName."' on table '".$tableName."' already exists.", self::COLUMN_ALREADY_EXISTS
        );
    }

    /**
     * @param string $sequenceName
Benjamin Morel's avatar
Benjamin Morel committed
127 128
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
129 130 131 132 133 134 135 136
     */
    static public function sequenceAlreadyExists($sequenceName)
    {
        return new self("The sequence '".$sequenceName."' already exists.", self::SEQUENCE_ALREADY_EXISTS);
    }

    /**
     * @param string $sequenceName
Benjamin Morel's avatar
Benjamin Morel committed
137 138
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
139 140 141 142 143
     */
    static public function sequenceDoesNotExist($sequenceName)
    {
        return new self("There exists no sequence with the name '".$sequenceName."'.", self::SEQUENCE_DOENST_EXIST);
    }
144 145

    /**
Benjamin Morel's avatar
Benjamin Morel committed
146 147 148 149
     * @param string $fkName
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
150
     */
151
    static public function foreignKeyDoesNotExist($fkName, $table)
152
    {
153
        return new self("There exists no foreign key with the name '$fkName' on table '$table'.", self::FOREIGNKEY_DOESNT_EXIST);
154
    }
155

Benjamin Morel's avatar
Benjamin Morel committed
156 157 158 159 160 161
    /**
     * @param \Doctrine\DBAL\Schema\Table                $localTable
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
162
    static public function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
163 164 165 166 167 168 169 170
    {
        return new self(
            "The performed schema operation on ".$localTable->getName()." requires a named foreign key, ".
            "but the given foreign key from (".implode(", ", $foreignKey->getColumns()).") onto foreign table ".
            "'".$foreignKey->getForeignTableName()."' (".implode(", ", $foreignKey->getForeignColumns()).") is currently ".
            "unnamed."
        );
    }
171

Benjamin Morel's avatar
Benjamin Morel committed
172 173 174 175 176 177 178
    /**
     * @param string $changeName
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
    static public function alterTableChangeNotSupported($changeName)
    {
179
        return new self("Alter table change not supported, given '$changeName'");
180
    }
Benjamin Morel's avatar
Benjamin Morel committed
181
}