You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.9 KiB

14 years ago
<?php
/**
14 years ago
* ColumnSchema class file.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
14 years ago
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\db\dao;
14 years ago
/**
14 years ago
* ColumnSchema class describes the column meta data of a database table.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class ColumnSchema extends \yii\base\Component
14 years ago
{
/**
* @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.
*/
14 years ago
public $quotedName;
14 years ago
/**
* @var boolean whether this column can be null.
*/
public $allowNull;
/**
14 years ago
* @var string logical type of this column. Possible logic types include:
14 years ago
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary
14 years ago
*/
14 years ago
public $type;
14 years ago
/**
14 years ago
* @var string the PHP type of this column. Possible PHP types include:
* string, boolean, integer, double.
14 years ago
*/
14 years ago
public $phpType;
/**
* @var string the DB type of this column.
*/
public $dbType;
14 years ago
/**
* @var mixed default value of this column
*/
public $defaultValue;
/**
14 years ago
* @var array enumerable values
*/
public $enumValues;
/**
14 years ago
* @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 auto-incremental
*/
public $autoIncrement = false;
/**
14 years ago
* @var boolean whether this column is unsigned. This is only meaningful
* when [[type]] is `integer` or `bigint`.
14 years ago
*/
14 years ago
public $unsigned;
14 years ago
/**
* Extracts the PHP type from DB type.
* @return string PHP type name.
14 years ago
*/
14 years ago
protected function getPhpType()
14 years ago
{
14 years ago
static $typeMap = array( // logical type => php type
'smallint' => 'integer',
'integer' => 'integer',
'bigint' => 'integer',
'boolean' => 'boolean',
'float' => 'double',
);
if (isset($typeMap[$this->type])) {
if ($this->type === 'bigint') {
return PHP_INT_SIZE == 8 && !$this->unsigned ? 'integer' : 'string';
}
elseif ($this->type === 'integer') {
return PHP_INT_SIZE == 4 && $this->unsigned ? 'string' : 'integer';
}
return $typeMap[$this->type];
14 years ago
}
14 years ago
return 'string';
14 years ago
}
/**
* 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)
{
14 years ago
if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
14 years ago
return $value;
14 years ago
}
switch ($this->phpType) {
14 years ago
case 'string': return (string)$value;
case 'integer': return (integer)$value;
case 'boolean': return (boolean)$value;
}
14 years ago
return $value;
14 years ago
}
}