Repository navigation
Expand file tree
/
Copy pathclass-wp-sqlite-information-schema-exception.php
More file actions
169 lines (156 loc) · 4.95 KB
/
Copy pathclass-wp-sqlite-information-schema-exception.php
File metadata and controls
169 lines (156 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* SQLite information schema exception.
*
* This class is used to represent errors that may occur when building
* the MySQL information schema for emulation in SQLite.
*
* @access private
*/
class WP_SQLite_Information_Schema_Exception extends Exception {
// Information schema exception types.
const TYPE_DUPLICATE_TABLE_NAME = 'duplicate-table-name';
const TYPE_DUPLICATE_COLUMN_NAME = 'duplicate-column-name';
const TYPE_DUPLICATE_KEY_NAME = 'duplicate-key-name';
const TYPE_KEY_COLUMN_NOT_FOUND = 'key-column-not-found';
const TYPE_CONSTRAINT_DOES_NOT_EXIST = 'constraint-does-not-exist';
const TYPE_MULTIPLE_CONSTRAINTS_WITH_NAME = 'multiple-constraints-with-name';
const TYPE_INVALID_DEFAULT_VALUE = 'invalid-default-value';
/**
* The exception type.
*
* @var string
*/
private $type;
/**
* The data to be passed with the exception.
*
* @var array
*/
private $data;
/**
* Constructor.
*
* @param string $type The exception type.
* @param string $message The exception message.
* @param array $data The data to be passed with the exception.
* @param Throwable|null $previous The previous throwable used for the exception chaining.
*/
public function __construct(
string $type,
string $message,
array $data = array(),
?Throwable $previous = null
) {
parent::__construct( $message, 0, $previous );
$this->type = $type;
$this->data = $data;
}
/**
* Get the type of the exception.
*
* @return string The type of the exception.
*/
public function get_type(): string {
return $this->type;
}
/**
* Get the data associated with the exception.
*
* @return array The data associated with the exception.
*/
public function get_data(): array {
return $this->data;
}
/**
* Create a duplicate table name exception.
*
* @param string $table_name The name of the affected table.
* @return self The exception instance.
*/
public static function duplicate_table_name( string $table_name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_DUPLICATE_TABLE_NAME,
sprintf( "Table '%s' already exists.", $table_name ),
array( 'table_name' => $table_name )
);
}
/**
* Create a duplicate column name exception.
*
* @param string $column_name The name of the affected column.
* @return self The exception instance.
*/
public static function duplicate_column_name( string $column_name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_DUPLICATE_COLUMN_NAME,
sprintf( "Column '%s' already exists.", $column_name ),
array( 'column_name' => $column_name )
);
}
/**
* Create a duplicate key name exception.
*
* @param string $key_name The name of the affected key.
* @return self The exception instance.
*/
public static function duplicate_key_name( string $key_name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_DUPLICATE_KEY_NAME,
sprintf( "Key '%s' already exists.", $key_name ),
array( 'key_name' => $key_name )
);
}
/**
* Create a key column not found exception.
*
* @param string $column_name The name of the affected column.
* @return self The exception instance.
*/
public static function key_column_not_found( string $column_name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_KEY_COLUMN_NOT_FOUND,
sprintf( "Key column '%s' doesn't exist in table.", $column_name ),
array( 'column_name' => $column_name )
);
}
/**
* Create a constraint does not exist exception.
*
* @param string $name The name of the affected constraint.
* @return self The exception instance.
*/
public static function constraint_does_not_exist( string $name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_CONSTRAINT_DOES_NOT_EXIST,
sprintf( "Constraint '%s' does not exist.", $name ),
array( 'name' => $name )
);
}
/**
* Create a multiple constraints with name exception.
*
* @param string $name The name of the affected constraint.
* @return self The exception instance.
*/
public static function multiple_constraints_with_name( string $name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_MULTIPLE_CONSTRAINTS_WITH_NAME,
sprintf( "Table has multiple constraints with the name '%s'. Please use constraint specific 'DROP' clause.", $name ),
array( 'name' => $name )
);
}
/**
* Create an invalid default value exception.
*
* @param string $column_name The name of the affected column.
* @return self The exception instance.
*/
public static function invalid_default_value( string $column_name ): WP_SQLite_Information_Schema_Exception {
return new self(
self::TYPE_INVALID_DEFAULT_VALUE,
sprintf( "Invalid default value for '%s'.", $column_name ),
array( 'column_name' => $column_name )
);
}
}