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.

128 lines
3.1 KiB

13 years ago
<?php
/**
13 years ago
* ColumnSchema class file.
13 years ago
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
13 years ago
13 years ago
/**
13 years ago
* ColumnSchema class describes the metadata of a column in a database table.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class ColumnSchema extends \yii\base\Component
13 years ago
{
/**
* @var string name of this column (without quotes).
*/
public $name;
/**
13 years ago
* @var string the quoted name of this column.
13 years ago
*/
13 years ago
public $quotedName;
13 years ago
/**
* @var boolean whether this column can be null.
*/
public $allowNull;
/**
13 years ago
* @var string logical type of this column. Possible logic types include:
13 years ago
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
* timestamp, time, date, binary, and money.
13 years ago
*/
13 years ago
public $type;
13 years ago
/**
13 years ago
* @var string the PHP type of this column. Possible PHP types include:
* string, boolean, integer, double.
13 years ago
*/
13 years ago
public $phpType;
/**
13 years ago
* @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
13 years ago
*/
public $dbType;
13 years ago
/**
* @var mixed default value of this column
*/
public $defaultValue;
/**
13 years ago
* @var array enumerable values. This is set only if the column is declared to be an enumerable type.
13 years ago
*/
public $enumValues;
/**
13 years ago
* @var integer display size of the column.
13 years ago
*/
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;
/**
13 years ago
* @var boolean whether this column is unsigned. This is only meaningful
13 years ago
* when [[type]] is `smallint`, `integer` or `bigint`.
13 years ago
*/
13 years ago
public $unsigned;
13 years ago
/**
* Extracts the PHP type from DB type.
*/
13 years ago
public function resolvePhpType()
13 years ago
{
13 years ago
static $typeMap = array( // logical type => php type
'smallint' => 'integer',
13 years ago
'integer' => 'integer',
'bigint' => 'integer',
'boolean' => 'boolean',
'float' => 'double',
13 years ago
);
if (isset($typeMap[$this->type])) {
if ($this->type === 'bigint') {
13 years ago
$this->phpType = PHP_INT_SIZE == 8 && !$this->unsigned ? 'integer' : 'string';
13 years ago
} elseif ($this->type === 'integer') {
13 years ago
$this->phpType = PHP_INT_SIZE == 4 && $this->unsigned ? 'string' : 'integer';
} else {
$this->phpType = $typeMap[$this->type];
13 years ago
}
13 years ago
} else {
$this->phpType = 'string';
13 years ago
}
}
/**
13 years ago
* Converts the input value according to [[phpType]].
* If the value is null or an [[Expression]], it will not be converted.
13 years ago
* @param mixed $value input value
* @return mixed converted value
*/
public function typecast($value)
{
13 years ago
if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
13 years ago
return $value;
13 years ago
}
switch ($this->phpType) {
13 years ago
case 'string':
return (string)$value;
case 'integer':
return (integer)$value;
case 'boolean':
return (boolean)$value;
13 years ago
}
13 years ago
return $value;
13 years ago
}
}