|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Schema definition |
|
7
|
|
|
* |
|
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 0.3.0 |
|
21
|
|
|
* @author Glynn Quelch <[email protected]> |
|
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
23
|
|
|
* @package PinkCrab\Table_Builder |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace PinkCrab\Table_Builder; |
|
27
|
|
|
|
|
28
|
|
|
use Exception; |
|
29
|
|
|
use PinkCrab\Table_Builder\Index; |
|
30
|
|
|
use PinkCrab\Table_Builder\Column; |
|
31
|
|
|
use PinkCrab\Table_Builder\Foreign_Key; |
|
32
|
|
|
use PinkCrab\Table_Builder\Exception\Schema_Exception; |
|
33
|
|
|
|
|
34
|
|
|
class Schema { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The table name |
|
38
|
|
|
* |
|
39
|
|
|
* @since 0.3.0 |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $table_name; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The table name prefix |
|
46
|
|
|
* |
|
47
|
|
|
* @since 0.3.0 |
|
48
|
|
|
* @var string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $prefix = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Table columns |
|
54
|
|
|
* |
|
55
|
|
|
* @since 0.3.0 |
|
56
|
|
|
* @var array<Column> |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $columns = array(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* All table indexes |
|
62
|
|
|
* |
|
63
|
|
|
* @since 0.3.0 |
|
64
|
|
|
* @var array<Index> |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $indexes = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* All foreign key relations |
|
70
|
|
|
* |
|
71
|
|
|
* @since 0.3.0 |
|
72
|
|
|
* @var array<Foreign_Key> |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $foreign_keys = array(); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates an instance of Schema |
|
78
|
|
|
* |
|
79
|
|
|
* @since 0.3.0 |
|
80
|
|
|
* @param string $table_name |
|
81
|
|
|
* @param callable(Schema):void|null $configure |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct( string $table_name, ?callable $configure = null ) { |
|
84
|
|
|
$this->table_name = $table_name; |
|
85
|
|
|
if ( is_callable( $configure ) ) { |
|
86
|
|
|
$configure( $this ); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the table name |
|
92
|
|
|
* |
|
93
|
|
|
* @since 0.3.0 |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function get_table_name(): string { |
|
97
|
|
|
return \sprintf( |
|
98
|
|
|
'%s%s', |
|
99
|
|
|
$this->get_prefix(), |
|
100
|
|
|
$this->table_name |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Sets the table names prefix |
|
106
|
|
|
* |
|
107
|
|
|
* If null, will be treated as no prefix. |
|
108
|
|
|
* |
|
109
|
|
|
* @since 0.3.0 |
|
110
|
|
|
* @param string|null $prefix |
|
111
|
|
|
* @return self |
|
112
|
|
|
*/ |
|
113
|
|
|
public function prefix( ?string $prefix = null ): self { |
|
114
|
|
|
$this->prefix = $prefix; |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Checks if the table name should be prefixed. |
|
120
|
|
|
* |
|
121
|
|
|
* @since 0.3.0 |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
public function has_prefix(): bool { |
|
125
|
|
|
return $this->prefix !== null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the table name prefix |
|
130
|
|
|
* |
|
131
|
|
|
* @since 0.3.0 |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function get_prefix(): string { |
|
135
|
|
|
return $this->prefix ?? ''; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Adds a new column to the schema |
|
141
|
|
|
* |
|
142
|
|
|
* @since 0.3.0 |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* @return Column |
|
145
|
|
|
*/ |
|
146
|
|
|
public function column( string $name ): Column { |
|
147
|
|
|
$column = new Column( $name ); |
|
148
|
|
|
|
|
149
|
|
|
$this->columns[ $name ] = $column; |
|
150
|
|
|
return $column; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get table columns |
|
156
|
|
|
* |
|
157
|
|
|
* @since 0.3.0 |
|
158
|
|
|
* @return Column[] |
|
159
|
|
|
*/ |
|
160
|
|
|
public function get_columns(): array { |
|
161
|
|
|
return $this->columns; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Checks if a column has been set based on name. |
|
166
|
|
|
* |
|
167
|
|
|
* @since 0.3.0 |
|
168
|
|
|
* @param string $name |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
|
|
public function has_column( string $name ): bool { |
|
172
|
|
|
return count( |
|
173
|
|
|
array_filter( |
|
174
|
|
|
$this->get_columns(), |
|
175
|
|
|
function( Column $column ) use ( $name ): bool { |
|
176
|
|
|
return $column->get_name() === $name; |
|
177
|
|
|
} |
|
178
|
|
|
) |
|
179
|
|
|
) >= 1; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Removes a column from the stack based on its name. |
|
184
|
|
|
* |
|
185
|
|
|
* @since 0.3.0 |
|
186
|
|
|
* @param string $name |
|
187
|
|
|
* @return self |
|
188
|
|
|
* @throws Schema_Exception (301) If column doesn't exist. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function remove_column( string $name ): self { |
|
191
|
|
|
if ( ! $this->has_column( $name ) ) { |
|
192
|
|
|
throw Schema_Exception::column_not_exist( $this, $name ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
unset( $this->columns[ $name ] ); |
|
196
|
|
|
|
|
197
|
|
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Sets an foreign key to the table |
|
202
|
|
|
* |
|
203
|
|
|
* @since 0.3.0 |
|
204
|
|
|
* @param string $column The column this FK index is set to. |
|
205
|
|
|
* @param string|null $key_name if not set, will use the column name a tempalte. |
|
206
|
|
|
* @return \PinkCrab\Table_Builder\Foreign_Key |
|
207
|
|
|
*/ |
|
208
|
|
|
public function foreign_key( string $column, ?string $key_name = null ): Foreign_Key { |
|
209
|
|
|
$foreign_key = new Foreign_Key( $column, $key_name ); |
|
210
|
|
|
|
|
211
|
|
|
$this->foreign_keys[] = $foreign_key; |
|
212
|
|
|
return $foreign_key; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Checks if foreign keys have been set. |
|
217
|
|
|
* |
|
218
|
|
|
* @return bool |
|
219
|
|
|
*/ |
|
220
|
|
|
public function has_foreign_keys(): bool { |
|
221
|
|
|
return count( $this->foreign_keys ) !== 0; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* |
|
226
|
|
|
* Returns all the defined indexes. |
|
227
|
|
|
* |
|
228
|
|
|
* @since 0.3.0 |
|
229
|
|
|
* @return array<int, \PinkCrab\Table_Builder\Foreign_Key> |
|
230
|
|
|
*/ |
|
231
|
|
|
public function get_foreign_keys(): array { |
|
232
|
|
|
return $this->foreign_keys; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Checks if foreign keys have been set. |
|
237
|
|
|
* |
|
238
|
|
|
* @return bool |
|
239
|
|
|
*/ |
|
240
|
|
|
public function has_indexes(): bool { |
|
241
|
|
|
return count( $this->indexes ) !== 0; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Sets an index to the table. |
|
246
|
|
|
* |
|
247
|
|
|
* @since 0.3.0 |
|
248
|
|
|
* @param string $key_name |
|
249
|
|
|
* @return \PinkCrab\Table_Builder\Index |
|
250
|
|
|
*/ |
|
251
|
|
|
public function index( string $column, ?string $key_name = null ): Index { |
|
252
|
|
|
$index = new Index( $column, $key_name ); |
|
253
|
|
|
|
|
254
|
|
|
$this->indexes[] = $index; |
|
255
|
|
|
return $index; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Returns all the defined indexes. |
|
260
|
|
|
* |
|
261
|
|
|
* @since 0.2.0 |
|
262
|
|
|
* @return array<int, \PinkCrab\Table_Builder\Index> |
|
263
|
|
|
*/ |
|
264
|
|
|
public function get_indexes(): array { |
|
265
|
|
|
return $this->indexes; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|