ColumnSchema.php 3.8 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 11
 * @license http://www.yiiframework.com/license/
 */

/**
w  
Qiang Xue committed
12
 * ColumnSchema class describes the column meta data of a database table.
w  
Qiang Xue committed
13 14
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
15
 * @since 2.0
w  
Qiang Xue committed
16
 */
w  
Qiang Xue committed
17
class ColumnSchema extends CComponent
w  
Qiang Xue committed
18 19 20 21 22 23 24 25
{
	/**
	 * @var string name of this column (without quotes).
	 */
	public $name;
	/**
	 * @var string raw name of this column. This is the quoted name that can be used in SQL queries.
	 */
w  
Qiang Xue committed
26
	public $quotedName;
w  
Qiang Xue committed
27 28 29 30 31
	/**
	 * @var boolean whether this column can be null.
	 */
	public $allowNull;
	/**
w  
Qiang Xue committed
32 33
	 * @var string logical type of this column. Possible logic types include:
	 * string, text, boolean, integer, float, decimal, datetime, timestamp, time, date, binary
w  
Qiang Xue committed
34
	 */
w  
Qiang Xue committed
35
	public $type;
w  
Qiang Xue committed
36
	/**
w  
Qiang Xue committed
37 38
	 * @var string the PHP type of this column. Possible PHP types include:
	 * string, boolean, integer, double.
w  
Qiang Xue committed
39
	 */
w  
Qiang Xue committed
40 41 42 43 44
	public $phpType;
	/**
	 * @var string the DB type of this column.
	 */
	public $dbType;
w  
Qiang Xue committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	/**
	 * @var mixed default value of this column
	 */
	public $defaultValue;
	/**
	 * @var integer size of the column.
	 */
	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 a foreign key
	 */
	public $isForeignKey;
	/**
	 * @var boolean whether this column is auto-incremental
	 */
	public $autoIncrement = false;


	/**
	 * Initializes the column with its DB type and default value.
	 * This sets up the column's PHP type, size, precision, scale as well as default value.
	 * @param string $dbType the column's DB type
	 * @param mixed $defaultValue the default value
	 */
	public function init($dbType, $defaultValue)
	{
		$this->dbType = $dbType;
		$this->extractType($dbType);
		$this->extractLimit($dbType);
		if ($defaultValue !== null)
			$this->extractDefault($defaultValue);
	}

	/**
	 * Extracts the PHP type from DB type.
	 * @param string $dbType DB type
	 */
	protected function extractType($dbType)
	{
		if (stripos($dbType, 'int') !== false && stripos($dbType, 'unsigned int') === false)
			$this->type = 'integer';
		elseif (stripos($dbType, 'bool') !== false)
			$this->type = 'boolean';
		elseif (preg_match('/(real|floa|doub)/i', $dbType))
			$this->type = 'double';
		else
			$this->type = 'string';
	}

	/**
	 * Extracts size, precision and scale information from column's DB type.
	 * @param string $dbType the column's DB type
	 */
	protected function extractLimit($dbType)
	{
		if (strpos($dbType, '(') && preg_match('/\((.*)\)/', $dbType, $matches))
		{
			$values = explode(',', $matches[1]);
			$this->size = $this->precision = (int)$values[0];
			if (isset($values[1]))
				$this->scale = (int)$values[1];
		}
	}

	/**
	 * Extracts the default value for the column.
	 * The value is typecasted to correct PHP type.
	 * @param mixed $defaultValue the default value obtained from metadata
	 */
	protected function extractDefault($defaultValue)
	{
		$this->defaultValue = $this->typecast($defaultValue);
	}

	/**
	 * Converts the input value to the type that this column is of.
	 * @param mixed $value input value
	 * @return mixed converted value
	 */
	public function typecast($value)
	{
		if (gettype($value) === $this->type || $value === null || $value instanceof CDbExpression)
			return $value;
		if ($value === '')
			return $this->type === 'string' ? '' : null;
		switch ($this->type)
		{
			case 'string': return (string)$value;
			case 'integer': return (integer)$value;
			case 'boolean': return (boolean)$value;
			case 'double':
			default: return $value;
		}
	}
}