1: <?php
2: /**
3: * CDbColumnSchema class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CDbColumnSchema class describes the column meta data of a database table.
13: *
14: * @author Qiang Xue <qiang.xue@gmail.com>
15: * @package system.db.schema
16: * @since 1.0
17: */
18: class CDbColumnSchema extends CComponent
19: {
20: /**
21: * @var string name of this column (without quotes).
22: */
23: public $name;
24: /**
25: * @var string raw name of this column. This is the quoted name that can be used in SQL queries.
26: */
27: public $rawName;
28: /**
29: * @var boolean whether this column can be null.
30: */
31: public $allowNull;
32: /**
33: * @var string the DB type of this column.
34: */
35: public $dbType;
36: /**
37: * @var string the PHP type of this column.
38: */
39: public $type;
40: /**
41: * @var mixed default value of this column
42: */
43: public $defaultValue;
44: /**
45: * @var integer size of the column.
46: */
47: public $size;
48: /**
49: * @var integer precision of the column data, if it is numeric.
50: */
51: public $precision;
52: /**
53: * @var integer scale of the column data, if it is numeric.
54: */
55: public $scale;
56: /**
57: * @var boolean whether this column is a primary key
58: */
59: public $isPrimaryKey;
60: /**
61: * @var boolean whether this column is a foreign key
62: */
63: public $isForeignKey;
64: /**
65: * @var boolean whether this column is auto-incremental
66: * @since 1.1.7
67: */
68: public $autoIncrement=false;
69: /**
70: * @var string comment of this column. Default value is empty string which means that no comment
71: * has been set for the column. Null value means that RDBMS does not support column comments
72: * at all (SQLite) or comment retrieval for the active RDBMS is not yet supported by the framework.
73: * @since 1.1.13
74: */
75: public $comment='';
76:
77: /**
78: * Initializes the column with its DB type and default value.
79: * This sets up the column's PHP type, size, precision, scale as well as default value.
80: * @param string $dbType the column's DB type
81: * @param mixed $defaultValue the default value
82: */
83: public function init($dbType, $defaultValue)
84: {
85: $this->dbType=$dbType;
86: $this->extractType($dbType);
87: $this->extractLimit($dbType);
88: if($defaultValue!==null)
89: $this->extractDefault($defaultValue);
90: }
91:
92: /**
93: * Extracts the PHP type from DB type.
94: * @param string $dbType DB type
95: */
96: protected function extractType($dbType)
97: {
98: if(stripos($dbType,'int')!==false && stripos($dbType,'unsigned int')===false)
99: $this->type='integer';
100: elseif(stripos($dbType,'bool')!==false)
101: $this->type='boolean';
102: elseif(preg_match('/(real|floa|doub)/i',$dbType))
103: $this->type='double';
104: else
105: $this->type='string';
106: }
107:
108: /**
109: * Extracts size, precision and scale information from column's DB type.
110: * @param string $dbType the column's DB type
111: */
112: protected function extractLimit($dbType)
113: {
114: if(strpos($dbType,'(') && preg_match('/\((.*)\)/',$dbType,$matches))
115: {
116: $values=explode(',',$matches[1]);
117: $this->size=$this->precision=(int)$values[0];
118: if(isset($values[1]))
119: $this->scale=(int)$values[1];
120: }
121: }
122:
123: /**
124: * Extracts the default value for the column.
125: * The value is typecasted to correct PHP type.
126: * @param mixed $defaultValue the default value obtained from metadata
127: */
128: protected function extractDefault($defaultValue)
129: {
130: $this->defaultValue=$this->typecast($defaultValue);
131: }
132:
133: /**
134: * Converts the input value to the type that this column is of.
135: * @param mixed $value input value
136: * @return mixed converted value
137: */
138: public function typecast($value)
139: {
140: if(gettype($value)===$this->type || $value===null || $value instanceof CDbExpression)
141: return $value;
142: if($value==='' && $this->allowNull)
143: return $this->type==='string' ? '' : null;
144: switch($this->type)
145: {
146: case 'string': return (string)$value;
147: case 'integer': return (integer)$value;
148: case 'boolean': return (boolean)$value;
149: case 'double':
150: default: return $value;
151: }
152: }
153: }
154: