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.
365 lines
11 KiB
365 lines
11 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\db\mssql;
|
||
|
|
||
|
use yii\db\TableSchema;
|
||
|
use yii\db\ColumnSchema;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Schema is the class for retrieving metadata from a MS SQL Server databases (version 2008 and above).
|
||
12 years ago
|
*
|
||
|
* @author Timur Ruziev <resurtm@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Schema extends \yii\db\Schema
|
||
|
{
|
||
|
/**
|
||
|
* Default schema name to be used.
|
||
|
*/
|
||
|
const DEFAULT_SCHEMA = 'dbo';
|
||
|
|
||
|
/**
|
||
|
* @var array mapping from physical column types (keys) to abstract column types (values)
|
||
|
*/
|
||
|
public $typeMap = array(
|
||
12 years ago
|
// exact numerics
|
||
|
'bigint' => self::TYPE_BIGINT,
|
||
|
'numeric' => self::TYPE_DECIMAL,
|
||
12 years ago
|
'bit' => self::TYPE_SMALLINT,
|
||
|
'smallint' => self::TYPE_SMALLINT,
|
||
12 years ago
|
'decimal' => self::TYPE_DECIMAL,
|
||
|
'smallmoney' => self::TYPE_MONEY,
|
||
12 years ago
|
'int' => self::TYPE_INTEGER,
|
||
12 years ago
|
'tinyint' => self::TYPE_SMALLINT,
|
||
|
'money' => self::TYPE_MONEY,
|
||
|
|
||
|
// approximate numerics
|
||
12 years ago
|
'float' => self::TYPE_FLOAT,
|
||
|
'real' => self::TYPE_FLOAT,
|
||
12 years ago
|
|
||
|
// date and time
|
||
12 years ago
|
'date' => self::TYPE_DATE,
|
||
12 years ago
|
'datetimeoffset' => self::TYPE_DATETIME,
|
||
|
'datetime2' => self::TYPE_DATETIME,
|
||
|
'smalldatetime' => self::TYPE_DATETIME,
|
||
|
'datetime' => self::TYPE_DATETIME,
|
||
12 years ago
|
'time' => self::TYPE_TIME,
|
||
12 years ago
|
|
||
|
// character strings
|
||
|
'char' => self::TYPE_STRING,
|
||
|
'varchar' => self::TYPE_STRING,
|
||
|
'text' => self::TYPE_TEXT,
|
||
|
|
||
|
// unicode character strings
|
||
|
'nchar' => self::TYPE_STRING,
|
||
|
'nvarchar' => self::TYPE_STRING,
|
||
|
'ntext' => self::TYPE_TEXT,
|
||
|
|
||
|
// binary strings
|
||
|
'binary' => self::TYPE_BINARY,
|
||
|
'varbinary' => self::TYPE_BINARY,
|
||
|
'image' => self::TYPE_BINARY,
|
||
|
|
||
|
// other data types
|
||
|
// 'cursor' type cannot be used with tables
|
||
12 years ago
|
'timestamp' => self::TYPE_TIMESTAMP,
|
||
12 years ago
|
'hierarchyid' => self::TYPE_STRING,
|
||
|
'uniqueidentifier' => self::TYPE_STRING,
|
||
|
'sql_variant' => self::TYPE_STRING,
|
||
|
'xml' => self::TYPE_STRING,
|
||
|
'table' => self::TYPE_STRING,
|
||
12 years ago
|
);
|
||
|
|
||
|
/**
|
||
|
* Quotes a table name for use in a query.
|
||
|
* A simple table name has no schema prefix.
|
||
|
* @param string $name table name.
|
||
|
* @return string the properly quoted table name.
|
||
|
*/
|
||
|
public function quoteSimpleTableName($name)
|
||
|
{
|
||
12 years ago
|
return strpos($name, '[') === false ? "[{$name}]" : $name;
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Quotes a column name for use in a query.
|
||
|
* A simple column name has no prefix.
|
||
|
* @param string $name column name.
|
||
|
* @return string the properly quoted column name.
|
||
|
*/
|
||
|
public function quoteSimpleColumnName($name)
|
||
|
{
|
||
12 years ago
|
return strpos($name, '[') === false && $name !== '*' ? "[{$name}]" : $name;
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Creates a query builder for the MSSQL database.
|
||
|
* @return QueryBuilder query builder interface.
|
||
|
*/
|
||
|
public function createQueryBuilder()
|
||
|
{
|
||
|
return new QueryBuilder($this->db);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Loads the metadata for the specified table.
|
||
|
* @param string $name table name
|
||
12 years ago
|
* @return TableSchema|null driver dependent table metadata. Null if the table does not exist.
|
||
12 years ago
|
*/
|
||
|
public function loadTableSchema($name)
|
||
|
{
|
||
|
$table = new TableSchema();
|
||
|
$this->resolveTableNames($table, $name);
|
||
|
$this->findPrimaryKeys($table);
|
||
|
if ($this->findColumns($table)) {
|
||
12 years ago
|
$this->findForeignKeys($table);
|
||
12 years ago
|
return $table;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Resolves the table name and schema name (if any).
|
||
|
* @param TableSchema $table the table metadata object
|
||
|
* @param string $name the table name
|
||
|
*/
|
||
|
protected function resolveTableNames($table, $name)
|
||
|
{
|
||
|
$parts = explode('.', str_replace(array('[', ']'), '', $name));
|
||
|
$partCount = count($parts);
|
||
|
if ($partCount == 3) {
|
||
12 years ago
|
// catalog name, schema name and table name passed
|
||
12 years ago
|
$table->catalogName = $parts[0];
|
||
|
$table->schemaName = $parts[1];
|
||
|
$table->name = $parts[2];
|
||
|
} elseif ($partCount == 2) {
|
||
12 years ago
|
// only schema name and table name passed
|
||
12 years ago
|
$table->schemaName = $parts[0];
|
||
|
$table->name = $parts[1];
|
||
|
} else {
|
||
12 years ago
|
// only schema name passed
|
||
12 years ago
|
$table->schemaName = static::DEFAULT_SCHEMA;
|
||
|
$table->name = $parts[0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Loads the column information into a [[ColumnSchema]] object.
|
||
|
* @param array $info column information
|
||
|
* @return ColumnSchema the column schema object
|
||
|
*/
|
||
|
protected function loadColumnSchema($info)
|
||
|
{
|
||
|
$column = new ColumnSchema();
|
||
|
|
||
|
$column->name = $info['COLUMN_NAME'];
|
||
|
$column->allowNull = $info['IS_NULLABLE'] == 'YES';
|
||
12 years ago
|
$column->dbType = $info['DATA_TYPE'];
|
||
|
$column->enumValues = array(); // mssql has only vague equivalents to enum
|
||
|
$column->isPrimaryKey = null; // primary key will be determined in findColumns() method
|
||
12 years ago
|
$column->autoIncrement = $info['IsIdentity'] == 1;
|
||
12 years ago
|
$column->unsigned = stripos($column->dbType, 'unsigned') !== false;
|
||
|
$column->comment = $info['Comment'] === null ? '' : $column['Comment'];
|
||
12 years ago
|
|
||
|
$column->type = self::TYPE_STRING;
|
||
|
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
|
||
|
$type = $matches[1];
|
||
|
if (isset($this->typeMap[$type])) {
|
||
|
$column->type = $this->typeMap[$type];
|
||
|
}
|
||
|
if (!empty($matches[2])) {
|
||
|
$values = explode(',', $matches[2]);
|
||
|
$column->size = $column->precision = (int)$values[0];
|
||
|
if (isset($values[1])) {
|
||
|
$column->scale = (int)$values[1];
|
||
|
}
|
||
|
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
|
||
|
$column->type = 'boolean';
|
||
|
} elseif ($type === 'bit') {
|
||
|
if ($column->size > 32) {
|
||
|
$column->type = 'bigint';
|
||
|
} elseif ($column->size === 32) {
|
||
|
$column->type = 'integer';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$column->phpType = $this->getColumnPhpType($column);
|
||
|
|
||
|
if ($info['COLUMN_DEFAULT'] == '(NULL)') {
|
||
12 years ago
|
$info['COLUMN_DEFAULT'] = null;
|
||
|
}
|
||
|
if ($column->type !== 'timestamp' || $info['COLUMN_DEFAULT'] !== 'CURRENT_TIMESTAMP') {
|
||
|
$column->defaultValue = $column->typecast($info['COLUMN_DEFAULT']);
|
||
12 years ago
|
}
|
||
|
|
||
|
return $column;
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Collects the metadata of table columns.
|
||
|
* @param TableSchema $table the table metadata
|
||
|
* @return boolean whether the table exists in the database
|
||
|
*/
|
||
|
protected function findColumns($table)
|
||
|
{
|
||
12 years ago
|
$columnsTableName = 'information_schema.columns';
|
||
|
$whereSql = "[t1].[table_name] = '{$table->name}'";
|
||
12 years ago
|
if ($table->catalogName !== null) {
|
||
12 years ago
|
$columnsTableName = "{$table->catalogName}.{$columnsTableName}";
|
||
|
$whereSql .= " AND [t1].[table_catalog] = '{$table->catalogName}'";
|
||
12 years ago
|
}
|
||
|
if ($table->schemaName !== null) {
|
||
12 years ago
|
$whereSql .= " AND [t1].[table_schema] = '{$table->schemaName}'";
|
||
12 years ago
|
}
|
||
|
$columnsTableName = $this->quoteTableName($columnsTableName);
|
||
|
|
||
|
$sql = <<<SQL
|
||
|
SELECT
|
||
12 years ago
|
[t1].*,
|
||
|
COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS IsIdentity,
|
||
|
CONVERT(VARCHAR, [t2].[value]) AS Comment
|
||
|
FROM {$columnsTableName} AS [t1]
|
||
|
LEFT OUTER JOIN [sys].[extended_properties] AS [t2] ON
|
||
|
[t1].[ordinal_position] = [t2].[minor_id] AND
|
||
|
OBJECT_NAME([t2].[major_id]) = [t1].[table_name] AND
|
||
|
[t2].[class] = 1 AND
|
||
|
[t2].[class_desc] = 'OBJECT_OR_COLUMN' AND
|
||
|
[t2].[name] = 'MS_Description'
|
||
12 years ago
|
WHERE {$whereSql}
|
||
|
SQL;
|
||
|
|
||
|
try {
|
||
|
$columns = $this->db->createCommand($sql)->queryAll();
|
||
|
} catch (\Exception $e) {
|
||
|
return false;
|
||
|
}
|
||
|
foreach ($columns as $column) {
|
||
|
$column = $this->loadColumnSchema($column);
|
||
|
if (is_array($table->primaryKey)) {
|
||
12 years ago
|
foreach ($table->primaryKey as $primaryKeyColumn) {
|
||
|
if (strcasecmp($column->name, $primaryKeyColumn) === 0) {
|
||
|
$column->isPrimaryKey = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
} else {
|
||
|
$column->isPrimaryKey = strcasecmp($column->name, $table->primaryKey) === 0;
|
||
|
}
|
||
|
if ($column->isPrimaryKey && $column->autoIncrement) {
|
||
|
$table->sequenceName = '';
|
||
|
}
|
||
12 years ago
|
$table->columns[$column->name] = $column;
|
||
12 years ago
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Collects the primary key column details for the given table.
|
||
|
* @param TableSchema $table the table metadata
|
||
|
*/
|
||
|
protected function findPrimaryKeys($table)
|
||
|
{
|
||
12 years ago
|
$keyColumnUsageTableName = 'information_schema.key_column_usage';
|
||
|
$tableConstraintsTableName = 'information_schema.table_constraints';
|
||
12 years ago
|
if ($table->catalogName !== null) {
|
||
|
$keyColumnUsageTableName = $table->catalogName . '.' . $keyColumnUsageTableName;
|
||
|
$tableConstraintsTableName = $table->catalogName . '.' . $tableConstraintsTableName;
|
||
|
}
|
||
|
$keyColumnUsageTableName = $this->quoteTableName($keyColumnUsageTableName);
|
||
|
$tableConstraintsTableName = $this->quoteTableName($tableConstraintsTableName);
|
||
|
|
||
|
$sql = <<<SQL
|
||
|
SELECT
|
||
12 years ago
|
[kcu].[column_name] AS [field_name]
|
||
|
FROM {$keyColumnUsageTableName} AS [kcu]
|
||
|
LEFT JOIN {$tableConstraintsTableName} AS [tc] ON
|
||
|
[kcu].[table_name] = [tc].[table_name] AND
|
||
|
[kcu].[constraint_name] = [tc].[constraint_name]
|
||
12 years ago
|
WHERE
|
||
12 years ago
|
[tc].[constraint_type] = 'PRIMARY KEY' AND
|
||
|
[kcu].[table_name] = :tableName AND
|
||
|
[kcu].[table_schema] = :schemaName
|
||
12 years ago
|
SQL;
|
||
|
|
||
|
$table->primaryKey = $this->db
|
||
|
->createCommand($sql, array(':tableName' => $table->name, ':schemaName' => $table->schemaName))
|
||
|
->queryColumn();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Collects the foreign key column details for the given table.
|
||
|
* @param TableSchema $table the table metadata
|
||
|
*/
|
||
12 years ago
|
protected function findForeignKeys($table)
|
||
12 years ago
|
{
|
||
12 years ago
|
$referentialConstraintsTableName = 'information_schema.referential_constraints';
|
||
|
$keyColumnUsageTableName = 'information_schema.key_column_usage';
|
||
12 years ago
|
if ($table->catalogName !== null) {
|
||
|
$referentialConstraintsTableName = $table->catalogName . '.' . $referentialConstraintsTableName;
|
||
|
$keyColumnUsageTableName = $table->catalogName . '.' . $keyColumnUsageTableName;
|
||
|
}
|
||
|
$referentialConstraintsTableName = $this->quoteTableName($referentialConstraintsTableName);
|
||
|
$keyColumnUsageTableName = $this->quoteTableName($keyColumnUsageTableName);
|
||
|
|
||
|
// please refer to the following page for more details:
|
||
|
// http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx
|
||
|
$sql = <<<SQL
|
||
|
SELECT
|
||
12 years ago
|
[kcu1].[column_name] AS [fk_column_name],
|
||
|
[kcu2].[table_name] AS [uq_table_name],
|
||
|
[kcu2].[column_name] AS [uq_column_name]
|
||
|
FROM {$referentialConstraintsTableName} AS [rc]
|
||
|
JOIN {$keyColumnUsageTableName} AS [kcu1] ON
|
||
|
[kcu1].[constraint_catalog] = [rc].[constraint_catalog] AND
|
||
|
[kcu1].[constraint_schema] = [rc].[constraint_schema] AND
|
||
|
[kcu1].[constraint_name] = [rc].[constraint_name]
|
||
|
JOIN {$keyColumnUsageTableName} AS [kcu2] ON
|
||
|
[kcu2].[constraint_catalog] = [rc].[constraint_catalog] AND
|
||
|
[kcu2].[constraint_schema] = [rc].[constraint_schema] AND
|
||
|
[kcu2].[constraint_name] = [rc].[constraint_name] AND
|
||
|
[kcu2].[ordinal_position] = [kcu1].[ordinal_position]
|
||
|
WHERE [kcu1].[table_name] = :tableName
|
||
12 years ago
|
SQL;
|
||
|
|
||
|
$rows = $this->db->createCommand($sql, array(':tableName' => $table->name))->queryAll();
|
||
|
$table->foreignKeys = array();
|
||
|
foreach ($rows as $row) {
|
||
|
$table->foreignKeys[] = array($row['uq_table_name'], $row['fk_column_name'] => $row['uq_column_name']);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns all table names in the database.
|
||
|
* This method should be overridden by child classes in order to support this feature
|
||
|
* because the default implementation simply throws an exception.
|
||
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
|
||
|
* @return array all table names in the database. The names have NO the schema name prefix.
|
||
|
*/
|
||
|
protected function findTableNames($schema = '')
|
||
|
{
|
||
12 years ago
|
if ($schema === '') {
|
||
|
$schema = static::DEFAULT_SCHEMA;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
$sql = <<<SQL
|
||
12 years ago
|
SELECT [t].[table]
|
||
|
FROM [information_schema].[tables] AS [t]
|
||
|
WHERE [t].[table_schema] = :schema AND [t].[table_type] = 'BASE TABLE'
|
||
12 years ago
|
SQL;
|
||
|
|
||
12 years ago
|
$names = $this->db->createCommand($sql, array(':schema' => $schema))->queryColumn();
|
||
12 years ago
|
if ($schema !== static::DEFAULT_SCHEMA) {
|
||
|
foreach ($names as $index => $name) {
|
||
|
$names[$index] = $schema . '.' . $name;
|
||
12 years ago
|
}
|
||
|
}
|
||
|
return $names;
|
||
|
}
|
||
|
}
|