SchemaException.php 3.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

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;
16
    const FOREIGNKEY_DOESNT_EXIST = 100;
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    /**
     * @param string $tableName
     * @return SchemaException
     */
    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
     * @return SchemaException
     */
    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
     * @return SchemaException
     */
40
    static public function indexDoesNotExist($indexName, $table)
41
    {
42
        return new self("Index '$indexName' does not exist on table '$table'.", self::INDEX_DOESNT_EXIST);
43 44 45 46 47 48
    }

    /**
     * @param string $indexName
     * @return SchemaException
     */
49
    static public function indexAlreadyExists($indexName, $table)
50
    {
51
        return new self("An index with name '$indexName' was already defined on table '$table'.", self::INDEX_ALREADY_EXISTS);
52 53 54 55 56 57
    }

    /**
     * @param string $columnName
     * @return SchemaException
     */
58
    static public function columnDoesNotExist($columnName, $table)
59
    {
60
        return new self("There is no column with name '$columnName' on table '$table'.", self::COLUMN_DOESNT_EXIST);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    /**
     *
     * @param  string $tableName
     * @return SchemaException
     */
    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
     * @return SchemaException
     */
    static public function columnAlreadyExists($tableName, $columnName)
    {
        return new self(
            "The column '".$columnName."' on table '".$tableName."' already exists.", self::COLUMN_ALREADY_EXISTS
        );
    }

    /**
     * @param string $sequenceName
     * @return SchemaException
     */
    static public function sequenceAlreadyExists($sequenceName)
    {
        return new self("The sequence '".$sequenceName."' already exists.", self::SEQUENCE_ALREADY_EXISTS);
    }

    /**
     * @param string $sequenceName
     * @return SchemaException
     */
    static public function sequenceDoesNotExist($sequenceName)
    {
        return new self("There exists no sequence with the name '".$sequenceName."'.", self::SEQUENCE_DOENST_EXIST);
    }
103 104 105 106 107

    /**
     * @param  string $fkName
     * @return SchemaException
     */
108
    static public function foreignKeyDoesNotExist($fkName, $table)
109
    {
110
        return new self("There exists no foreign key with the name '$fkName' on table '$table'.", self::FOREIGNKEY_DOESNT_EXIST);
111
    }
112

113
    static public function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
114 115 116 117 118 119 120 121
    {
        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."
        );
    }
122 123 124 125

    static public function alterTableChangeNotSupported($changeName) {
        return new self ("Alter table change not supported, given '$changeName'");
    }
126
}