Yii2 framework backup
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.

144 lines
4.3 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
13 years ago
use yii\base\Object;
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
*/
class ColumnSchema extends Object
13 years ago
{
/**
* @var string name of this column (without quotes).
*/
public $name;
/**
* @var boolean whether this column can be null.
*/
public $allowNull;
/**
* @var string abstract type of this column. Possible abstract types include:
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
* timestamp, time, date, binary, and money.
*/
public $type;
/**
* @var string the PHP type of this column. Possible PHP types include:
* `string`, `boolean`, `integer`, `double`.
*/
public $phpType;
/**
* @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
*/
public $dbType;
/**
* @var mixed default value of this column
*/
public $defaultValue;
/**
* @var array enumerable values. This is set only if the column is declared to be an enumerable type.
*/
public $enumValues;
/**
* @var integer display 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;
/**
* @var boolean whether this column is unsigned. This is only meaningful
* when [[type]] is `smallint`, `integer` or `bigint`.
*/
public $unsigned;
/**
* @var string comment of this column. Not all DBMS support this.
*/
public $comment;
12 years ago
/**
* Converts the input value according to [[phpType]] after retrieval from the database.
* If the value is null or an [[Expression]], it will not be converted.
* @param mixed $value input value
* @return mixed converted value
*/
public function phpTypecast($value)
{
return $this->typecast($value);
}
/**
* Converts the input value according to [[type]] and [[dbType]] for use in a db query.
* If the value is null or an [[Expression]], it will not be converted.
* @param mixed $value input value
* @return mixed converted value. This may also be an array containing the value as the first element
* and the PDO type as the second element.
*/
public function dbTypecast($value)
{
// the default implementation does the same as casting for PHP, but it should be possible
// to override this with annotation of explicit PDO type.
return $this->typecast($value);
}
/**
* Converts the input value according to [[phpType]] after retrieval from the database.
* If the value is null or an [[Expression]], it will not be converted.
* @param mixed $value input value
* @return mixed converted value
* @since 2.0.3
*/
protected function typecast($value)
{
if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY) {
return null;
}
if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
return $value;
}
switch ($this->phpType) {
10 years ago
case 'resource':
case 'string':
if (is_resource($value)) {
return $value;
}
if (is_float($value)) {
// ensure type cast always has . as decimal separator in all locales
return str_replace(',', '.', (string)$value);
}
return (string)$value;
case 'integer':
10 years ago
return (int) $value;
case 'boolean':
10 years ago
return (bool) $value;
case 'double':
return (double) $value;
}
13 years ago
return $value;
}
13 years ago
}