-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathColumnInterface.php
More file actions
379 lines (340 loc) · 9.97 KB
/
ColumnInterface.php
File metadata and controls
379 lines (340 loc) · 9.97 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Schema\Column;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constraint\ForeignKey;
use Yiisoft\Db\Exception\NotSupportedException;
/**
* This interface defines a set of methods that must be implemented by a class that represents a database table column.
*/
interface ColumnInterface
{
/**
* The database assigns auto incremented column a unique value automatically whenever you insert a new row into
* the table. This is useful for getting unique IDs for data such as customer or employee numbers.
* You can set the autoIncrement for `INTEGER` or `BIGINT` data types.
*
* If not set explicitly with this method call, the column isn't auto incremented.
*
* ```php
* $columns = [
* 'id' => ColumnBuilder::primaryKey()->autoIncrement(),
* ];
* ```
*/
public function autoIncrement(bool $autoIncrement = true): static;
/**
* The check constraint for the column to specify an expression that must be true for each row in the table.
*
* ```php
* $columns = [
* 'age' => ColumnBuilder::integer()->check('age > 0'),
* ];
* ```
*/
public function check(?string $check): static;
/**
* The comment for a column in a database table.
*
* The comment can give more information about the purpose or usage of the column.
*
* ```php
* $columns = [
* 'description' => ColumnBuilder::text()->comment('Description of the product'),
* ];
* ```
*/
public function comment(?string $comment): static;
/**
* A computed column is a virtual column that computes its values from an expression.
*
* If not set explicitly with this method call, the column isn't computed.
*
* ```php
* $columns = [
* 'full_name' => ColumnBuilder::text()->computed(true),
* ];
* ```
*/
public function computed(bool $computed = true): static;
/**
* Sets a database data type for the column.
*
* The data type can be one of the built-in data types supported by the database server (such as `INTEGER`,
* `VARCHAR`, `DATETIME`, etc.), a custom data type defined by the database server, or `null` if the database
* allows untyped columns.
*
* ```php
* $columns = [
* 'description' => ColumnBuilder::text()->dbType('text'),
* ];
* ```
*/
public function dbType(?string $dbType): static;
/**
* Convert a value from its PHP representation to a database-specific representation.
*
* yiisoft/db calls it automatically by when preparing an SQL statement, so you don't usually need to call it
* directly in your code.
*
* If the value is `null` or an {@see Expression}, there will be no conversion.
*
* @throws NotSupportedException
*/
public function dbTypecast(mixed $value): mixed;
/**
* A value that's automatically assigned to a column when you insert a new row into the
* database table. The default value can be a constant value, function, value derived from other columns,
* non-computed column name, or their combinations.
*
* ```php
* $columns = [
* 'description' => ColumnBuilder::text()->defaultValue('Description of the product'),
* ];
* ```
*/
public function defaultValue(mixed $defaultValue): static;
/**
* Extra SQL to append to the generated SQL for a column.
*
* This can be useful for adding custom constraints or other SQL statements that aren't supported by the column
* schema itself.
*
* ```php
* $columns = [
* 'updated_at' => ColumnBuilder::integer()->extra('ON UPDATE CURRENT_TIMESTAMP'),
* ];
* ```
*/
public function extra(?string $extra): static;
/**
* Returns the check constraint for the column.
*
* @see check()
* @psalm-mutation-free
*/
public function getCheck(): ?string;
/**
* @return string|null The comment of the column.
*
* @see comment()
* @psalm-mutation-free
*/
public function getComment(): ?string;
/**
* @return string|null The database data type of the column.
* Null means the column has no type in the database.
*
* Note that the type includes size for columns supporting it, e.g. `varchar(128)`. The size can be obtained
* separately via {@see getSize()}.
*
* @see dbType()
* @psalm-mutation-free
*/
public function getDbType(): ?string;
/**
* @return mixed The default value of the column.
*
* @see defaultValue()
* @psalm-mutation-free
*/
public function getDefaultValue(): mixed;
/**
* @return string|null The extra SQL for the column.
*
* @see extra()
* @psalm-mutation-free
*/
public function getExtra(): ?string;
/**
* @return string|null The name of the column.
*
* @psalm-mutation-free
*/
public function getName(): ?string;
/**
* Returns the reference to the foreign key constraint.
*
* @see reference()
* @psalm-mutation-free
*/
public function getReference(): ?ForeignKey;
/**
* @return int|null The scale of the column.
*
* @see scale()
* @psalm-mutation-free
*/
public function getScale(): ?int;
/**
* @return int|null The size of the column.
*
* @see size()
* @psalm-mutation-free
*/
public function getSize(): ?int;
/**
* @return string The type of the column.
* @psalm-return ColumnType::*
*
* @see type()
* @psalm-mutation-free
*/
public function getType(): string;
/** @psalm-mutation-free */
public function hasDefaultValue(): bool;
/**
* Whether this column is auto incremental.
*
* This is only meaningful when {@see type} is `smallint`, `integer` or `bigint`.
*
* @see autoIncrement()
* @psalm-mutation-free
*/
public function isAutoIncrement(): bool;
/**
* Whether this column is computed.
*
* @see computed()
* @psalm-mutation-free
*/
public function isComputed(): bool;
/**
* Whether this column is not nullable.
*
* @see notNull()
* @psalm-mutation-free
*/
public function isNotNull(): ?bool;
/**
* Whether this column is a part of primary key.
*
* @see primaryKey()
* @psalm-mutation-free
*/
public function isPrimaryKey(): bool;
/**
* Whether this column has a unique index.
*
* @see unique()
* @psalm-mutation-free
*/
public function isUnique(): bool;
/**
* Whether this column is unsigned. This is only meaningful when {@see type} is `tinyint`, `smallint`, `integer`
* or `bigint`.
*
* @see unsigned()
* @psalm-mutation-free
*/
public function isUnsigned(): bool;
/**
* Whether the column is not nullable.
*
* ```php
* $columns = [
* 'description' => ColumnBuilder::text()->notNull(),
* ];
* ```
*/
public function notNull(bool $notNull = true): static;
/**
* Whether the column is nullable. Alias of {@see notNull(false)}.
*/
public function null(): static;
/**
* Converts the input value after retrieval from the database.
*
* @throws NotSupportedException
*/
public function phpTypecast(mixed $value): mixed;
/**
* The primary key is a column or set of columns that uniquely identifies each row in a table.
*
* ```php
* $columns = [
* 'id' => ColumnBuilder::primaryKey(),
* ];
* ```
*/
public function primaryKey(bool $primaryKey = true): static;
/**
* The reference to the foreign key constraint.
*
* ```php
* $reference = new ForeignKeyConstraint();
* $reference->foreignTableName('user');
* $reference->foreignColumnNames(['id']);
*
* $columns = [
* 'user_id' => ColumnBuilder::integer()->reference($reference),
* ];
* ```
*/
public function reference(?ForeignKey $reference): static;
/**
* The scale is the number of digits to the right of the decimal point and is only meaningful when {@see type} is
* `decimal`.
*
* ```php
* $columns = [
* 'price' => ColumnBuilder::decimal(10, 2)->scale(2),
* ];
* ```
*/
public function scale(?int $scale): static;
/**
* The size refers to the number of characters or digits allowed in a column of a database table. The size is
* typically used for character or numeric data types, such as `VARCHAR`, `INT` or DECIMAL, to specify the maximum
* length or precision of the data in the column.
*
* ```php
* $columns = [
* 'name' => ColumnBuilder::string()->size(255),
* ];
* ```
*/
public function size(?int $size): static;
/**
* The database type of the column.
*
* ```php
* $columns = [
* 'description' => ColumnBuilder::text()->type('text'),
* ];
*
* @psalm-param ColumnType::* $type
*/
public function type(string $type): static;
/**
* Whether the column has a unique index.
*
* ```php
* $columns = [
* 'username' => ColumnBuilder::string()->unique(),
* ];
* ```
*/
public function unique(bool $unique = true): static;
/**
* Whether the column type is an unsigned integer.
* It's a data type that can only represent positive whole numbers only.
*
* ```php
* $columns = [
* 'age' => ColumnBuilder::integer()->unsigned(),
* ];
* ```
*/
public function unsigned(bool $unsigned = true): static;
/**
* Returns a new instance with the specified name of the column.
*
* ```php
* $columns = [
* 'id' => ColumnBuilder::primaryKey()->withName('id'),
* ];
* ```
*/
public function withName(?string $name): static;
}