1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\FluidSchema; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\Column; |
7
|
|
|
use Doctrine\DBAL\Schema\Table; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
|
10
|
|
|
class FluidColumn |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var FluidSchema |
14
|
|
|
*/ |
15
|
|
|
private $fluidSchema; |
16
|
|
|
/** |
17
|
|
|
* @var FluidTable |
18
|
|
|
*/ |
19
|
|
|
private $fluidTable; |
20
|
|
|
/** |
21
|
|
|
* @var Column |
22
|
|
|
*/ |
23
|
|
|
private $column; |
24
|
|
|
/** |
25
|
|
|
* @var Table |
26
|
|
|
*/ |
27
|
|
|
private $table; |
28
|
|
|
/** |
29
|
|
|
* @var NamingStrategyInterface |
30
|
|
|
*/ |
31
|
|
|
private $namingStrategy; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* FluidColumn constructor. |
35
|
|
|
* @param FluidSchema $fluidSchema |
36
|
|
|
* @param FluidTable $fluidTable |
37
|
|
|
* @param Table $table |
38
|
|
|
* @param Column $column |
39
|
|
|
*/ |
40
|
|
|
public function __construct(FluidSchema $fluidSchema, FluidTable $fluidTable, Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
41
|
|
|
{ |
42
|
|
|
$this->fluidSchema = $fluidSchema; |
43
|
|
|
$this->fluidTable = $fluidTable; |
44
|
|
|
$this->column = $column; |
45
|
|
|
$this->table = $table; |
46
|
|
|
$this->namingStrategy = $namingStrategy; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function integer(): FluidColumnOptions |
50
|
|
|
{ |
51
|
|
|
$this->column->setType(Type::getType(Type::INTEGER)); |
52
|
|
|
return $this->getOptions(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function smallInt(): FluidColumnOptions |
56
|
|
|
{ |
57
|
|
|
$this->column->setType(Type::getType(Type::SMALLINT)); |
58
|
|
|
return $this->getOptions(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function bigInt(): FluidColumnOptions |
62
|
|
|
{ |
63
|
|
|
$this->column->setType(Type::getType(Type::BIGINT)); |
64
|
|
|
return $this->getOptions(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function decimal(int $precision = 10, int $scale = 0): FluidColumnOptions |
68
|
|
|
{ |
69
|
|
|
$this->column->setType(Type::getType(Type::DECIMAL)); |
70
|
|
|
$this->column->setPrecision($precision); |
71
|
|
|
$this->column->setScale($scale); |
72
|
|
|
return $this->getOptions(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function float(int $precision = 10, int $scale = 0): FluidColumnOptions |
76
|
|
|
{ |
77
|
|
|
$this->column->setType(Type::getType(Type::FLOAT)); |
78
|
|
|
$this->column->setPrecision($precision); |
79
|
|
|
$this->column->setScale($scale); |
80
|
|
|
return $this->getOptions(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function string(?int $length = null, bool $fixed = false): FluidColumnOptions |
84
|
|
|
{ |
85
|
|
|
$this->column->setType(Type::getType(Type::STRING)); |
86
|
|
|
$this->column->setLength($length); |
87
|
|
|
$this->column->setFixed($fixed); |
88
|
|
|
return $this->getOptions(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function text(): FluidColumnOptions |
92
|
|
|
{ |
93
|
|
|
$this->column->setType(Type::getType(Type::TEXT)); |
94
|
|
|
return $this->getOptions(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function guid(): FluidColumnOptions |
98
|
|
|
{ |
99
|
|
|
$this->column->setType(Type::getType(Type::GUID)); |
100
|
|
|
return $this->getOptions(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* From Doctrine DBAL 2.4+. |
105
|
|
|
*/ |
106
|
|
|
public function binary(?int $length = null, bool $fixed = false): FluidColumnOptions |
107
|
|
|
{ |
108
|
|
|
$this->column->setType(Type::getType(Type::BINARY)); |
109
|
|
|
$this->column->setLength($length); |
110
|
|
|
$this->column->setFixed($fixed); |
111
|
|
|
return $this->getOptions(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function blob(): FluidColumnOptions |
115
|
|
|
{ |
116
|
|
|
$this->column->setType(Type::getType(Type::BLOB)); |
117
|
|
|
return $this->getOptions(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function boolean(): FluidColumnOptions |
121
|
|
|
{ |
122
|
|
|
$this->column->setType(Type::getType(Type::BOOLEAN)); |
123
|
|
|
return $this->getOptions(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function date(): FluidColumnOptions |
127
|
|
|
{ |
128
|
|
|
$this->column->setType(Type::getType(Type::DATE)); |
129
|
|
|
return $this->getOptions(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function dateImmutable(): FluidColumnOptions |
133
|
|
|
{ |
134
|
|
|
$this->column->setType(Type::getType(Type::DATE_IMMUTABLE)); |
135
|
|
|
return $this->getOptions(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function datetime(): FluidColumnOptions |
139
|
|
|
{ |
140
|
|
|
$this->column->setType(Type::getType(Type::DATETIME)); |
141
|
|
|
return $this->getOptions(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function datetimeImmutable(): FluidColumnOptions |
145
|
|
|
{ |
146
|
|
|
$this->column->setType(Type::getType(Type::DATETIME_IMMUTABLE)); |
147
|
|
|
return $this->getOptions(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function datetimeTz(): FluidColumnOptions |
151
|
|
|
{ |
152
|
|
|
$this->column->setType(Type::getType(Type::DATETIMETZ)); |
153
|
|
|
return $this->getOptions(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function datetimeTzImmutable(): FluidColumnOptions |
157
|
|
|
{ |
158
|
|
|
$this->column->setType(Type::getType(Type::DATETIMETZ_IMMUTABLE)); |
159
|
|
|
return $this->getOptions(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function time(): FluidColumnOptions |
163
|
|
|
{ |
164
|
|
|
$this->column->setType(Type::getType(Type::TIME)); |
165
|
|
|
return $this->getOptions(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function timeImmutable(): FluidColumnOptions |
169
|
|
|
{ |
170
|
|
|
$this->column->setType(Type::getType(Type::TIME_IMMUTABLE)); |
171
|
|
|
return $this->getOptions(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function dateInterval(): FluidColumnOptions |
175
|
|
|
{ |
176
|
|
|
$this->column->setType(Type::getType(Type::DATEINTERVAL)); |
177
|
|
|
return $this->getOptions(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function array(): FluidColumnOptions |
181
|
|
|
{ |
182
|
|
|
$this->column->setType(Type::getType(Type::TARRAY)); |
183
|
|
|
return $this->getOptions(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function simpleArray(): FluidColumnOptions |
187
|
|
|
{ |
188
|
|
|
$this->column->setType(Type::getType(Type::SIMPLE_ARRAY)); |
189
|
|
|
return $this->getOptions(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function json(): FluidColumnOptions |
193
|
|
|
{ |
194
|
|
|
$this->column->setType(Type::getType(Type::JSON)); |
195
|
|
|
return $this->getOptions(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @deprecated From DBAL 2.6, use json() instead. |
200
|
|
|
* @return FluidColumnOptions |
201
|
|
|
*/ |
202
|
|
|
public function jsonArray(): FluidColumnOptions |
203
|
|
|
{ |
204
|
|
|
$this->column->setType(Type::getType(Type::JSON_ARRAY)); |
205
|
|
|
return $this->getOptions(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function object(): FluidColumnOptions |
209
|
|
|
{ |
210
|
|
|
$this->column->setType(Type::getType(Type::OBJECT)); |
211
|
|
|
return $this->getOptions(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function references(string $tableName, ?string $constraintName = null, string $onUpdate = 'RESTRICT', string $onDelete = 'RESTRICT'): FluidColumnOptions |
215
|
|
|
{ |
216
|
|
|
$tableName = $this->namingStrategy->quoteIdentifier($tableName); |
217
|
|
|
|
218
|
|
|
$table = $this->fluidSchema->getDbalSchema()->getTable($tableName); |
219
|
|
|
|
220
|
|
|
$referencedColumns = $table->getPrimaryKeyColumns(); |
221
|
|
|
|
222
|
|
|
if (count($referencedColumns) > 1) { |
223
|
|
|
throw new FluidSchemaException('You cannot reference a table with a primary key on several columns using FluidSchema. Use DBAL Schema methods instead.'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$referencedColumnName = $this->namingStrategy->quoteIdentifier($referencedColumns[0]); |
227
|
|
|
$referencedColumn = $table->getColumn($referencedColumnName); |
228
|
|
|
|
229
|
|
|
$this->column->setType($referencedColumn->getType()); |
230
|
|
|
$this->column->setLength($referencedColumn->getLength()); |
231
|
|
|
$this->column->setFixed($referencedColumn->getFixed()); |
232
|
|
|
$this->column->setScale($referencedColumn->getScale()); |
233
|
|
|
$this->column->setPrecision($referencedColumn->getPrecision()); |
234
|
|
|
$this->column->setUnsigned($referencedColumn->getUnsigned()); |
235
|
|
|
|
236
|
|
|
$this->table->addForeignKeyConstraint($table, [$this->namingStrategy->quoteIdentifier($this->column->getName())], $referencedColumns, [ |
237
|
|
|
'onUpdate' => $onUpdate, |
238
|
|
|
'onDelete' => $onDelete |
239
|
|
|
], $constraintName); |
240
|
|
|
return $this->getOptions(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
private function getOptions(): FluidColumnOptions |
244
|
|
|
{ |
245
|
|
|
return new FluidColumnOptions($this->fluidTable, $this->column, $this->namingStrategy); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns the underlying DBAL column. |
250
|
|
|
* @return Column |
251
|
|
|
*/ |
252
|
|
|
public function getDbalColumn(): Column |
253
|
|
|
{ |
254
|
|
|
return $this->column; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|