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.

107 lines
2.9 KiB

14 years ago
<?php
/**
* ColumnSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\db\dao\mysql;
14 years ago
/**
* ColumnSchema class describes the column meta data of a MySQL table.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ColumnSchema extends \yii\db\dao\ColumnSchema
{
/**
* Extracts the PHP type from DB type.
* @param string $dbType DB type
*/
14 years ago
public function initTypes($dbType)
14 years ago
{
14 years ago
static $typeMap = array( // dbType => type
13 years ago
'tinyint' => self::TYPE_SMALLINT,
'bit' => self::TYPE_SMALLINT,
'smallint' => self::TYPE_SMALLINT,
'mediumint' => self::TYPE_INTEGER,
'int' => self::TYPE_INTEGER,
'integer' => self::TYPE_INTEGER,
'bigint' => self::TYPE_BIGINT,
'float' => self::TYPE_FLOAT,
'double' => self::TYPE_FLOAT,
'real' => self::TYPE_FLOAT,
'decimal' => self::TYPE_DECIMAL,
'numeric' => self::TYPE_DECIMAL,
'tinytext' => self::TYPE_TEXT,
'mediumtext' => self::TYPE_TEXT,
'longtext' => self::TYPE_TEXT,
'text' => self::TYPE_TEXT,
'varchar' => self::TYPE_STRING,
'string' => self::TYPE_STRING,
'char' => self::TYPE_STRING,
'datetime' => self::TYPE_DATETIME,
'year' => self::TYPE_DATE,
'date' => self::TYPE_DATE,
'time' => self::TYPE_TIME,
'timestamp' => self::TYPE_TIMESTAMP,
'enum' => self::TYPE_STRING,
14 years ago
);
$this->dbType = $dbType;
13 years ago
$this->type = self::TYPE_STRING;
14 years ago
$this->unsigned = strpos($this->dbType, 'unsigned') !== false;
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $this->dbType, $matches)) {
$type = $matches[1];
if (isset($typeMap[$type])) {
$this->type = $typeMap[$type];
}
if (!empty($matches[2])) {
13 years ago
if ($type === 'enum') {
14 years ago
$values = explode(',', $matches[2]);
foreach ($values as $i => $value) {
$values[$i] = trim($value, "'");
}
$this->enumValues = $values;
13 years ago
} else {
14 years ago
$values = explode(',', $matches[2]);
$this->size = $this->precision = (int)$values[0];
if (isset($values[1])) {
$this->scale = (int)$values[1];
}
if ($this->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
$this->type = 'boolean';
13 years ago
} elseif ($type === 'bit') {
14 years ago
if ($this->size > 32) {
$this->type = 'bigint';
13 years ago
} elseif ($this->size === 32) {
14 years ago
$this->type = 'integer';
}
}
}
}
}
13 years ago
$this->phpType = $this->extractPhpType();
14 years ago
}
/**
* Extracts the default value for the column.
13 years ago
* The value is typecast to correct PHP type.
14 years ago
* @param mixed $defaultValue the default value obtained from metadata
*/
14 years ago
public function initDefaultValue($defaultValue)
14 years ago
{
14 years ago
if ($this->type === 'timestamp' && $defaultValue === 'CURRENT_TIMESTAMP') {
14 years ago
$this->defaultValue = null;
13 years ago
} else {
14 years ago
$this->defaultValue = $this->typecast($defaultValue);
}
14 years ago
}
}