1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db; |
9
|
|
|
|
10
|
|
|
use yii\base\BaseObject; |
11
|
|
|
use yii\helpers\StringHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ColumnSchema class describes the metadata of a column in a database table. |
15
|
|
|
* |
16
|
|
|
* @author Qiang Xue <[email protected]> |
17
|
|
|
* @since 2.0 |
18
|
|
|
*/ |
19
|
|
|
class ColumnSchema extends BaseObject |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string name of this column (without quotes). |
23
|
|
|
*/ |
24
|
|
|
public $name; |
25
|
|
|
/** |
26
|
|
|
* @var bool whether this column can be null. |
27
|
|
|
*/ |
28
|
|
|
public $allowNull; |
29
|
|
|
/** |
30
|
|
|
* @var string abstract type of this column. Possible abstract types include: |
31
|
|
|
* char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime, |
32
|
|
|
* timestamp, time, date, binary, and money. |
33
|
|
|
*/ |
34
|
|
|
public $type; |
35
|
|
|
/** |
36
|
|
|
* @var string the PHP type of this column. Possible PHP types include: |
37
|
|
|
* `string`, `boolean`, `integer`, `double`, `array`. |
38
|
|
|
*/ |
39
|
|
|
public $phpType; |
40
|
|
|
/** |
41
|
|
|
* @var string the DB type of this column. Possible DB types vary according to the type of DBMS. |
42
|
|
|
*/ |
43
|
|
|
public $dbType; |
44
|
|
|
/** |
45
|
|
|
* @var mixed default value of this column |
46
|
|
|
*/ |
47
|
|
|
public $defaultValue; |
48
|
|
|
/** |
49
|
|
|
* @var array enumerable values. This is set only if the column is declared to be an enumerable type. |
50
|
|
|
*/ |
51
|
|
|
public $enumValues; |
52
|
|
|
/** |
53
|
|
|
* @var int display size of the column. |
54
|
|
|
*/ |
55
|
|
|
public $size; |
56
|
|
|
/** |
57
|
|
|
* @var int precision of the column data, if it is numeric. |
58
|
|
|
*/ |
59
|
|
|
public $precision; |
60
|
|
|
/** |
61
|
|
|
* @var int scale of the column data, if it is numeric. |
62
|
|
|
*/ |
63
|
|
|
public $scale; |
64
|
|
|
/** |
65
|
|
|
* @var bool whether this column is a primary key |
66
|
|
|
*/ |
67
|
|
|
public $isPrimaryKey; |
68
|
|
|
/** |
69
|
|
|
* @var bool whether this column is auto-incremental |
70
|
|
|
*/ |
71
|
|
|
public $autoIncrement = false; |
72
|
|
|
/** |
73
|
|
|
* @var bool whether this column is unsigned. This is only meaningful |
74
|
|
|
* when [[type]] is `smallint`, `integer` or `bigint`. |
75
|
|
|
*/ |
76
|
|
|
public $unsigned; |
77
|
|
|
/** |
78
|
|
|
* @var string comment of this column. Not all DBMS support this. |
79
|
|
|
*/ |
80
|
|
|
public $comment; |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Converts the input value according to [[phpType]] after retrieval from the database. |
85
|
|
|
* If the value is null or an [[Expression]], it will not be converted. |
86
|
|
|
* @param mixed $value input value |
87
|
|
|
* @return mixed converted value |
88
|
|
|
*/ |
89
|
559 |
|
public function phpTypecast($value) |
90
|
|
|
{ |
91
|
559 |
|
return $this->typecast($value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Converts the input value according to [[type]] and [[dbType]] for use in a db query. |
96
|
|
|
* If the value is null or an [[Expression]], it will not be converted. |
97
|
|
|
* @param mixed $value input value |
98
|
|
|
* @return mixed converted value. This may also be an array containing the value as the first element |
99
|
|
|
* and the PDO type as the second element. |
100
|
|
|
*/ |
101
|
168 |
|
public function dbTypecast($value) |
102
|
|
|
{ |
103
|
|
|
// the default implementation does the same as casting for PHP, but it should be possible |
104
|
|
|
// to override this with annotation of explicit PDO type. |
105
|
168 |
|
return $this->typecast($value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Converts the input value according to [[phpType]] after retrieval from the database. |
110
|
|
|
* If the value is null or an [[Expression]], it will not be converted. |
111
|
|
|
* @param mixed $value input value |
112
|
|
|
* @return mixed converted value |
113
|
|
|
* @since 2.0.3 |
114
|
|
|
*/ |
115
|
953 |
|
protected function typecast($value) |
116
|
|
|
{ |
117
|
953 |
|
if ($value === '' |
118
|
|
|
&& !in_array( |
119
|
46 |
|
$this->type, |
120
|
|
|
[ |
121
|
46 |
|
Schema::TYPE_TEXT, |
122
|
|
|
Schema::TYPE_STRING, |
123
|
|
|
Schema::TYPE_BINARY, |
124
|
|
|
Schema::TYPE_CHAR |
125
|
|
|
], |
126
|
953 |
|
true) |
127
|
|
|
) { |
128
|
6 |
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
953 |
|
if ($value === null |
132
|
953 |
|
|| gettype($value) === $this->phpType |
133
|
629 |
|
|| $value instanceof ExpressionInterface |
134
|
953 |
|
|| $value instanceof Query |
135
|
|
|
) { |
136
|
891 |
|
return $value; |
137
|
|
|
} |
138
|
|
|
|
139
|
623 |
|
if (is_array($value) |
140
|
623 |
|
&& count($value) === 2 |
141
|
623 |
|
&& isset($value[1]) |
142
|
623 |
|
&& in_array($value[1], $this->getPdoParamTypes(), true) |
143
|
|
|
) { |
144
|
|
|
return new PdoValue($value[0], $value[1]); |
145
|
|
|
} |
146
|
|
|
|
147
|
623 |
|
switch ($this->phpType) { |
148
|
623 |
|
case 'resource': |
149
|
566 |
|
case 'string': |
150
|
181 |
|
if (is_resource($value)) { |
151
|
|
|
return $value; |
152
|
|
|
} |
153
|
181 |
|
if (is_float($value)) { |
154
|
|
|
// ensure type cast always has . as decimal separator in all locales |
155
|
6 |
|
return StringHelper::floatToString($value); |
156
|
|
|
} |
157
|
175 |
|
return (string) $value; |
158
|
461 |
|
case 'integer': |
159
|
457 |
|
return (int) $value; |
160
|
94 |
|
case 'boolean': |
161
|
|
|
// treating a 0 bit value as false too |
162
|
|
|
// https://github.com/yiisoft/yii2/issues/9006 |
163
|
24 |
|
return (bool) $value && $value !== "\0"; |
164
|
91 |
|
case 'double': |
165
|
88 |
|
return (float) $value; |
166
|
|
|
} |
167
|
|
|
|
168
|
3 |
|
return $value; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return int[] array of numbers that represent possible PDO parameter types |
173
|
|
|
*/ |
174
|
|
|
private function getPdoParamTypes() |
175
|
|
|
{ |
176
|
|
|
return [\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_NULL, \PDO::PARAM_STMT]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|