|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Classes\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\AbstractAdapter; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Colunas dos bancos |
|
9
|
|
|
* |
|
10
|
|
|
* @author Pedro Alarcao <[email protected]> |
|
11
|
|
|
* @link https://github.com/pedro151/orm-generator |
|
12
|
|
|
*/ |
|
13
|
|
|
class Column |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
const TypeNone = 0; |
|
17
|
|
|
const TypePHP = 1; |
|
18
|
|
|
const TypeDefault = 2; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Colunas dos bancos |
|
22
|
|
|
* |
|
23
|
|
|
* @author Pedro Alarcao <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
final private function __construct () |
|
26
|
|
|
{ |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* create instance |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Classes\Db\Column |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function getInstance () |
|
35
|
|
|
{ |
|
36
|
|
|
return new Column(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $name; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \Classes\Db\Constrant[] |
|
46
|
|
|
*/ |
|
47
|
|
|
private $primarykey; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $type; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $comment; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
private $nullable; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var int |
|
66
|
|
|
*/ |
|
67
|
|
|
private $max_length; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
private $column_default; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var \Classes\Db\Constrant[] |
|
76
|
|
|
*/ |
|
77
|
|
|
private $dependences; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var \Classes\Db\Constrant |
|
81
|
|
|
*/ |
|
82
|
|
|
private $refForeingkey; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @type string |
|
86
|
|
|
*/ |
|
87
|
|
|
private $sequence; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getName () |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->name; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* popula o |
|
99
|
|
|
* |
|
100
|
|
|
* @param $array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function populate ( $array ) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->name = $array[ 'name' ]; |
|
105
|
|
|
$this->type = $array[ 'type' ]; |
|
106
|
|
|
$this->nullable = $array[ 'nullable' ]; |
|
107
|
|
|
$this->max_length = $array[ 'max_length' ]; |
|
108
|
|
|
$this->column_default = $array[ 'column_default' ]; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return boolean |
|
115
|
|
|
*/ |
|
116
|
|
|
public function isPrimaryKey () |
|
117
|
|
|
{ |
|
118
|
|
|
return !empty( $this->primarykey ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isForeingkey () |
|
125
|
|
|
{ |
|
126
|
|
|
return !empty( $this->refForeingkey ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return boolean |
|
131
|
|
|
*/ |
|
132
|
|
|
public function hasDependence () |
|
133
|
|
|
{ |
|
134
|
|
|
return !empty( $this->dependences ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
public function hasColumnDefault () |
|
141
|
|
|
{ |
|
142
|
|
|
return !empty( $this->column_default ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getColumnDefault () |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->column_default ; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getType ( $type = self::TypeDefault ) |
|
157
|
|
|
{ |
|
158
|
|
|
switch ( $type ) |
|
159
|
|
|
{ |
|
160
|
|
|
case self::TypePHP: |
|
161
|
|
|
return AbstractAdapter::convertTypeToPHP ( $this->type ); |
|
162
|
|
|
case self::TypeDefault: |
|
163
|
|
|
return AbstractAdapter::convertTypeToDefault ( $this->type ); |
|
164
|
|
|
case self::TypeNone: |
|
165
|
|
|
return $this->type; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param $type |
|
171
|
|
|
* @param bool $inPHP |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
public function equalType ( $type, $inPHP = true ) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->getType ( $inPHP ) === $type; |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param AbstractAdapter $type |
|
182
|
|
|
* |
|
183
|
|
|
* @return mixed |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getTypeByConfig ( AbstractAdapter $type ) |
|
186
|
|
|
{ |
|
187
|
|
|
return $type->convertTypeToTypeFramework ( $this->getType ( false ) ); |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getComment () |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->comment; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $comment |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setComment ( $comment ) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->comment = $comment; |
|
204
|
|
|
|
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param \Classes\Db\Constrant $primarykey |
|
210
|
|
|
*/ |
|
211
|
|
|
public function setPrimaryKey ( Constrant $primarykey ) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->primarykey = $primarykey; |
|
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param \Classes\Db\Constrant $dependece |
|
220
|
|
|
*/ |
|
221
|
|
|
public function addDependece ( Constrant $dependece ) |
|
222
|
|
|
{ |
|
223
|
|
|
$this->dependences[] = $dependece; |
|
224
|
|
|
|
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param $constraint_name |
|
230
|
|
|
* @param $table_name |
|
231
|
|
|
* @param $column_name |
|
232
|
|
|
* |
|
233
|
|
|
* @return $this |
|
234
|
|
|
*/ |
|
235
|
|
|
public function createDependece ( $constraint_name, $table_name, $column_name, $database, $schema = null ) |
|
236
|
|
|
{ |
|
237
|
|
|
$objConstrantDependence = Constrant::getInstance () |
|
238
|
|
|
->populate ( |
|
239
|
|
|
array ( |
|
240
|
|
|
'constrant' => $constraint_name, |
|
241
|
|
|
'schema' => $schema, |
|
242
|
|
|
'table' => $table_name, |
|
243
|
|
|
'column' => $column_name, |
|
244
|
|
|
'database' => $database |
|
245
|
|
|
) |
|
246
|
|
|
); |
|
247
|
|
|
|
|
248
|
|
|
$this->addDependece ( $objConstrantDependence ); |
|
249
|
|
|
|
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param \Classes\Db\Constrant $reference |
|
255
|
|
|
*/ |
|
256
|
|
|
public function addRefFk ( Constrant $reference ) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->refForeingkey = $reference; |
|
259
|
|
|
|
|
260
|
|
|
return $this; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* retorna as foreingkeys |
|
265
|
|
|
* |
|
266
|
|
|
* @return \Classes\Db\Constrant |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getFks () |
|
269
|
|
|
{ |
|
270
|
|
|
return $this->refForeingkey; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Retorna as dependencias da tabela |
|
275
|
|
|
* |
|
276
|
|
|
* @return \Classes\Db\Constrant[] |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getDependences () |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->dependences; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @return bool |
|
285
|
|
|
*/ |
|
286
|
|
|
public function hasDependences () |
|
287
|
|
|
{ |
|
288
|
|
|
return (bool) count ( $this->dependences ); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Retorna a constrant da primarykey da tabela |
|
293
|
|
|
* |
|
294
|
|
|
* @return \Classes\Db\Constrant[] |
|
295
|
|
|
*/ |
|
296
|
|
|
public function getPrimaryKey () |
|
297
|
|
|
{ |
|
298
|
|
|
return $this->primarykey; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* |
|
303
|
|
|
*/ |
|
304
|
|
|
public function getMaxLength () |
|
305
|
|
|
{ |
|
306
|
|
|
return $this->max_length; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* @return bool |
|
311
|
|
|
*/ |
|
312
|
|
|
public function hasSequence () |
|
313
|
|
|
{ |
|
314
|
|
|
return (bool) $this->sequence; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* @return string |
|
319
|
|
|
*/ |
|
320
|
|
|
public function getSequence () |
|
321
|
|
|
{ |
|
322
|
|
|
return $this->sequence; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param string $sequence |
|
327
|
|
|
*/ |
|
328
|
|
|
public function setSequence ( $sequence ) |
|
329
|
|
|
{ |
|
330
|
|
|
$this->sequence = $sequence; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @return boolean |
|
335
|
|
|
*/ |
|
336
|
|
|
public function isNullable () |
|
337
|
|
|
{ |
|
338
|
|
|
return $this->nullable; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
} |
|
342
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: