ColumnSchema.php 3.14 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * ColumnSchema class file.
w  
Qiang Xue committed
4 5 6
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
7
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
8 9 10
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
11 12
namespace yii\db\dao;

w  
Qiang Xue committed
13
/**
Qiang Xue committed
14
 * ColumnSchema class describes the metadata of a column in a database table.
w  
Qiang Xue committed
15 16
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
17
 * @since 2.0
w  
Qiang Xue committed
18
 */
w  
Qiang Xue committed
19
class ColumnSchema extends \yii\base\Component
w  
Qiang Xue committed
20 21 22 23 24 25
{
	/**
	 * @var string name of this column (without quotes).
	 */
	public $name;
	/**
Qiang Xue committed
26
	 * @var string the quoted name of this column.
w  
Qiang Xue committed
27
	 */
w  
Qiang Xue committed
28
	public $quotedName;
w  
Qiang Xue committed
29 30 31 32 33
	/**
	 * @var boolean whether this column can be null.
	 */
	public $allowNull;
	/**
w  
Qiang Xue committed
34
	 * @var string logical type of this column. Possible logic types include:
Qiang Xue committed
35 36
	 * string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
	 * timestamp, time, date, binary, and money.
w  
Qiang Xue committed
37
	 */
w  
Qiang Xue committed
38
	public $type;
w  
Qiang Xue committed
39
	/**
w  
Qiang Xue committed
40 41
	 * @var string the PHP type of this column. Possible PHP types include:
	 * string, boolean, integer, double.
w  
Qiang Xue committed
42
	 */
w  
Qiang Xue committed
43 44
	public $phpType;
	/**
Qiang Xue committed
45
	 * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
w  
Qiang Xue committed
46 47
	 */
	public $dbType;
w  
Qiang Xue committed
48 49 50 51
	/**
	 * @var mixed default value of this column
	 */
	public $defaultValue;
w  
Qiang Xue committed
52
	/**
Qiang Xue committed
53
	 * @var array enumerable values. This is set only if the column is declared to be an enumerable type.
w  
Qiang Xue committed
54 55
	 */
	public $enumValues;
w  
Qiang Xue committed
56
	/**
Qiang Xue committed
57
	 * @var integer display size of the column.
w  
Qiang Xue committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	 */
	public $size;
	/**
	 * @var integer precision of the column data, if it is numeric.
	 */
	public $precision;
	/**
	 * @var integer scale of the column data, if it is numeric.
	 */
	public $scale;
	/**
	 * @var boolean whether this column is a primary key
	 */
	public $isPrimaryKey;
	/**
	 * @var boolean whether this column is auto-incremental
	 */
	public $autoIncrement = false;
	/**
w  
Qiang Xue committed
77
	 * @var boolean whether this column is unsigned. This is only meaningful
Qiang Xue committed
78
	 * when [[type]] is `smallint`, `integer` or `bigint`.
w  
Qiang Xue committed
79
	 */
w  
Qiang Xue committed
80
	public $unsigned;
w  
Qiang Xue committed
81 82 83 84

	/**
	 * Extracts the PHP type from DB type.
	 */
Qiang Xue committed
85
	public function resolvePhpType()
w  
Qiang Xue committed
86
	{
w  
Qiang Xue committed
87 88
		static $typeMap = array( // logical type => php type
			'smallint' => 'integer',
Qiang Xue committed
89 90 91 92
			'integer' => 'integer',
			'bigint' => 'integer',
			'boolean' => 'boolean',
			'float' => 'double',
w  
Qiang Xue committed
93 94 95
		);
		if (isset($typeMap[$this->type])) {
			if ($this->type === 'bigint') {
Qiang Xue committed
96
				$this->phpType = PHP_INT_SIZE == 8 && !$this->unsigned ? 'integer' : 'string';
Qiang Xue committed
97
			} elseif ($this->type === 'integer') {
Qiang Xue committed
98 99 100
				$this->phpType = PHP_INT_SIZE == 4 && $this->unsigned ? 'string' : 'integer';
			} else {
				$this->phpType = $typeMap[$this->type];
w  
Qiang Xue committed
101
			}
Qiang Xue committed
102 103
		} else {
			$this->phpType = 'string';
w  
Qiang Xue committed
104 105 106 107
		}
	}

	/**
Qiang Xue committed
108 109
	 * Converts the input value according to [[phpType]].
	 * If the value is null or an [[Expression]], it will not be converted.
w  
Qiang Xue committed
110 111 112 113 114
	 * @param mixed $value input value
	 * @return mixed converted value
	 */
	public function typecast($value)
	{
w  
Qiang Xue committed
115
		if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
w  
Qiang Xue committed
116
			return $value;
w  
Qiang Xue committed
117 118
		}
		switch ($this->phpType) {
Qiang Xue committed
119 120 121 122 123 124
			case 'string':
				return (string)$value;
			case 'integer':
				return (integer)$value;
			case 'boolean':
				return (boolean)$value;
w  
Qiang Xue committed
125
		}
w  
Qiang Xue committed
126
		return $value;
w  
Qiang Xue committed
127 128
	}
}