Failed Conditions
Pull Request — develop (#3348)
by Sergei
22:47
created

SchemaException::namedIndexRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\DBALException;
8
use function implode;
9
use function sprintf;
10
11
class SchemaException extends DBALException
12
{
13
    public const TABLE_DOESNT_EXIST       = 10;
14
    public const TABLE_ALREADY_EXISTS     = 20;
15
    public const COLUMN_DOESNT_EXIST      = 30;
16
    public const COLUMN_ALREADY_EXISTS    = 40;
17
    public const INDEX_DOESNT_EXIST       = 50;
18
    public const INDEX_ALREADY_EXISTS     = 60;
19
    public const SEQUENCE_DOENST_EXIST    = 70;
20
    public const SEQUENCE_ALREADY_EXISTS  = 80;
21
    public const INDEX_INVALID_NAME       = 90;
22
    public const FOREIGNKEY_DOESNT_EXIST  = 100;
23
    public const CONSTRAINT_DOESNT_EXIST  = 110;
24
    public const NAMESPACE_ALREADY_EXISTS = 120;
25
26 385
    public static function tableDoesNotExist(string $tableName) : self
27
    {
28 385
        return new self("There is no table with name '" . $tableName . "' in the schema.", self::TABLE_DOESNT_EXIST);
29
    }
30
31 145
    public static function indexNameInvalid(string $indexName) : self
32
    {
33 145
        return new self(
34 145
            sprintf('Invalid index-name %s given, has to be [a-zA-Z0-9_]', $indexName),
35 145
            self::INDEX_INVALID_NAME
36
        );
37
    }
38
39 218
    public static function indexDoesNotExist(string $indexName, string $table) : self
40
    {
41 218
        return new self(
42 218
            sprintf("Index '%s' does not exist on table '%s'.", $indexName, $table),
43 218
            self::INDEX_DOESNT_EXIST
44
        );
45
    }
46
47 195
    public static function indexAlreadyExists(string $indexName, string $table) : self
48
    {
49 195
        return new self(
50 195
            sprintf("An index with name '%s' was already defined on table '%s'.", $indexName, $table),
51 195
            self::INDEX_ALREADY_EXISTS
52
        );
53
    }
54
55 268
    public static function columnDoesNotExist(string $columnName, string $table) : self
56
    {
57 268
        return new self(
58 268
            sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
59 268
            self::COLUMN_DOESNT_EXIST
60
        );
61
    }
62
63 289
    public static function namespaceAlreadyExists(string $namespaceName) : self
64
    {
65 289
        return new self(
66 289
            sprintf("The namespace with name '%s' already exists.", $namespaceName),
67 289
            self::NAMESPACE_ALREADY_EXISTS
68
        );
69
    }
70
71 361
    public static function tableAlreadyExists(string $tableName) : self
72
    {
73 361
        return new self("The table with name '" . $tableName . "' already exists.", self::TABLE_ALREADY_EXISTS);
74
    }
75
76 241
    public static function columnAlreadyExists(string $tableName, string $columnName) : self
77
    {
78 241
        return new self(
79 241
            "The column '" . $columnName . "' on table '" . $tableName . "' already exists.",
80 241
            self::COLUMN_ALREADY_EXISTS
81
        );
82
    }
83
84 313
    public static function sequenceAlreadyExists(string $sequenceName) : self
85
    {
86 313
        return new self("The sequence '" . $sequenceName . "' already exists.", self::SEQUENCE_ALREADY_EXISTS);
87
    }
88
89 337
    public static function sequenceDoesNotExist(string $sequenceName) : self
90
    {
91 337
        return new self("There exists no sequence with the name '" . $sequenceName . "'.", self::SEQUENCE_DOENST_EXIST);
92
    }
93
94
    public static function uniqueConstraintDoesNotExist(string $constraintName, string $table) : self
95
    {
96
        return new self(sprintf(
97
            'There exists no unique constraint with the name "%s" on table "%s".',
98
            $constraintName,
99
            $table
100
        ), self::CONSTRAINT_DOESNT_EXIST);
101
    }
102
103
    public static function foreignKeyDoesNotExist(string $fkName, string $table) : self
104
    {
105
        return new self(
106
            sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
107
            self::FOREIGNKEY_DOESNT_EXIST
108
        );
109
    }
110
111
    public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey) : self
112
    {
113
        return new self(
114
            'The performed schema operation on ' . $localTable->getName() . ' requires a named foreign key, ' .
115
            'but the given foreign key from (' . implode(', ', $foreignKey->getColumns()) . ') onto foreign table ' .
116
            "'" . $foreignKey->getForeignTableName() . "' (" . implode(', ', $foreignKey->getForeignColumns()) . ') is currently ' .
117
            'unnamed.'
118
        );
119
    }
120
121
    public static function namedIndexRequired(Table $table, Index $index) : self
122
    {
123
        return new self(
124
            sprintf(
125
                'The performed schema operation on %s requires a named index, but the given index on (%s) is currently unnamed.',
126
                $table->getName(),
127
                implode(', ', $index->getColumns())
128
            )
129
        );
130
    }
131
132
    public static function alterTableChangeNotSupported(string $changeName) : self
133
    {
134
        return new self(
135
            sprintf("Alter table change not supported, given '%s'", $changeName)
136
        );
137
    }
138
}
139