first commit
This commit is contained in:
39
vendor/yiisoft/yii2/db/mssql/ColumnSchema.php
vendored
Normal file
39
vendor/yiisoft/yii2/db/mssql/ColumnSchema.php
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
/**
|
||||
* Class ColumnSchema for MSSQL database
|
||||
*
|
||||
* @since 2.0.23
|
||||
*/
|
||||
class ColumnSchema extends \yii\db\ColumnSchema
|
||||
{
|
||||
/**
|
||||
* @var bool whether this column is a computed column
|
||||
* @since 2.0.39
|
||||
*/
|
||||
public $isComputed;
|
||||
|
||||
|
||||
/**
|
||||
* Prepares default value and converts it according to [[phpType]]
|
||||
* @param mixed $value default value
|
||||
* @return mixed converted value
|
||||
* @since 2.0.24
|
||||
*/
|
||||
public function defaultPhpTypecast($value)
|
||||
{
|
||||
if ($value !== null) {
|
||||
// convert from MSSQL column_default format, e.g. ('1') -> 1, ('string') -> string
|
||||
$value = substr(substr($value, 2), 0, -2);
|
||||
}
|
||||
|
||||
return parent::phpTypecast($value);
|
||||
}
|
||||
}
|
||||
79
vendor/yiisoft/yii2/db/mssql/ColumnSchemaBuilder.php
vendored
Normal file
79
vendor/yiisoft/yii2/db/mssql/ColumnSchemaBuilder.php
vendored
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
use yii\db\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
|
||||
use yii\db\Expression;
|
||||
|
||||
/**
|
||||
* ColumnSchemaBuilder is the schema builder for MSSQL databases.
|
||||
*
|
||||
* @property-read string|null $checkValue The `CHECK` constraint for the column.
|
||||
* @property-read string|Expression|null $defaultValue Default value of the column.
|
||||
*
|
||||
* @author Valerii Gorbachev <darkdef@gmail.com>
|
||||
* @since 2.0.42
|
||||
*/
|
||||
class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
|
||||
{
|
||||
protected $format = '{type}{length}{notnull}{unique}{default}{check}{append}';
|
||||
|
||||
|
||||
/**
|
||||
* Builds the full string for the column's schema.
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
if ($this->getTypeCategory() === self::CATEGORY_PK) {
|
||||
$format = '{type}{check}{comment}{append}';
|
||||
} else {
|
||||
$format = $this->format;
|
||||
}
|
||||
|
||||
return $this->buildCompleteString($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes default format string to MSSQL ALTER COMMAND.
|
||||
*/
|
||||
public function setAlterColumnFormat()
|
||||
{
|
||||
$this->format = '{type}{length}{notnull}{append}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting the `Default` value for constraint
|
||||
* @return string|Expression|null default value of the column.
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
if ($this->default instanceof Expression) {
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
return $this->buildDefaultValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the `Check` value for constraint
|
||||
* @return string|null the `CHECK` constraint for the column.
|
||||
*/
|
||||
public function getCheckValue()
|
||||
{
|
||||
return $this->check !== null ? (string) $this->check : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added.
|
||||
*/
|
||||
public function isUnique()
|
||||
{
|
||||
return $this->isUnique;
|
||||
}
|
||||
}
|
||||
53
vendor/yiisoft/yii2/db/mssql/DBLibPDO.php
vendored
Normal file
53
vendor/yiisoft/yii2/db/mssql/DBLibPDO.php
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
/**
|
||||
* This is an extension of the default PDO class of DBLIB drivers.
|
||||
* It provides workarounds for improperly implemented functionalities of the DBLIB drivers.
|
||||
*
|
||||
* @author Bert Brunekreeft <bbrunekreeft@gmail.com>
|
||||
* @since 2.0.41
|
||||
*/
|
||||
class DBLibPDO extends \PDO
|
||||
{
|
||||
/**
|
||||
* Returns value of the last inserted ID.
|
||||
* @param string|null $name the sequence name. Defaults to null.
|
||||
* @return string|false last inserted ID value.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function lastInsertId($name = null)
|
||||
{
|
||||
return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a database connection attribute.
|
||||
*
|
||||
* It is necessary to override PDO's method as some MSSQL PDO driver (e.g. dblib) does not
|
||||
* support getting attributes.
|
||||
* @param int $attribute One of the PDO::ATTR_* constants.
|
||||
* @return mixed A successful call returns the value of the requested PDO attribute.
|
||||
* An unsuccessful call returns null.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getAttribute($attribute)
|
||||
{
|
||||
try {
|
||||
return parent::getAttribute($attribute);
|
||||
} catch (\PDOException $e) {
|
||||
switch ($attribute) {
|
||||
case self::ATTR_SERVER_VERSION:
|
||||
return $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)")->fetchColumn();
|
||||
default:
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
92
vendor/yiisoft/yii2/db/mssql/PDO.php
vendored
Normal file
92
vendor/yiisoft/yii2/db/mssql/PDO.php
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
/**
|
||||
* This is an extension of the default PDO class of MSSQL and DBLIB drivers.
|
||||
* It provides workarounds for improperly implemented functionalities of the MSSQL and DBLIB drivers.
|
||||
*
|
||||
* @author Timur Ruziev <resurtm@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class PDO extends \PDO
|
||||
{
|
||||
/**
|
||||
* Returns value of the last inserted ID.
|
||||
* @param string|null $sequence the sequence name. Defaults to null.
|
||||
* @return string|false last inserted ID value.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function lastInsertId($sequence = null)
|
||||
{
|
||||
return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not
|
||||
* natively support transactions.
|
||||
* @return bool the result of a transaction start.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function beginTransaction()
|
||||
{
|
||||
$this->exec('BEGIN TRANSACTION');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not
|
||||
* natively support transactions.
|
||||
* @return bool the result of a transaction commit.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function commit()
|
||||
{
|
||||
$this->exec('COMMIT TRANSACTION');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollbacks a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not
|
||||
* natively support transactions.
|
||||
* @return bool the result of a transaction roll back.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function rollBack()
|
||||
{
|
||||
$this->exec('ROLLBACK TRANSACTION');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a database connection attribute.
|
||||
*
|
||||
* It is necessary to override PDO's method as some MSSQL PDO driver (e.g. dblib) does not
|
||||
* support getting attributes.
|
||||
* @param int $attribute One of the PDO::ATTR_* constants.
|
||||
* @return mixed A successful call returns the value of the requested PDO attribute.
|
||||
* An unsuccessful call returns null.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getAttribute($attribute)
|
||||
{
|
||||
try {
|
||||
return parent::getAttribute($attribute);
|
||||
} catch (\PDOException $e) {
|
||||
switch ($attribute) {
|
||||
case self::ATTR_SERVER_VERSION:
|
||||
return $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)")->fetchColumn();
|
||||
default:
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
693
vendor/yiisoft/yii2/db/mssql/QueryBuilder.php
vendored
Normal file
693
vendor/yiisoft/yii2/db/mssql/QueryBuilder.php
vendored
Normal file
@ -0,0 +1,693 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
use yii\base\InvalidArgumentException;
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\db\Expression;
|
||||
use yii\db\Query;
|
||||
use yii\db\TableSchema;
|
||||
|
||||
/**
|
||||
* QueryBuilder is the query builder for MS SQL Server databases (version 2008 and above).
|
||||
*
|
||||
* @author Timur Ruziev <resurtm@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class QueryBuilder extends \yii\db\QueryBuilder
|
||||
{
|
||||
/**
|
||||
* @var array mapping from abstract column types (keys) to physical column types (values).
|
||||
*/
|
||||
public $typeMap = [
|
||||
Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY',
|
||||
Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY',
|
||||
Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY',
|
||||
Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY',
|
||||
Schema::TYPE_CHAR => 'nchar(1)',
|
||||
Schema::TYPE_STRING => 'nvarchar(255)',
|
||||
Schema::TYPE_TEXT => 'nvarchar(max)',
|
||||
Schema::TYPE_TINYINT => 'tinyint',
|
||||
Schema::TYPE_SMALLINT => 'smallint',
|
||||
Schema::TYPE_INTEGER => 'int',
|
||||
Schema::TYPE_BIGINT => 'bigint',
|
||||
Schema::TYPE_FLOAT => 'float',
|
||||
Schema::TYPE_DOUBLE => 'float',
|
||||
Schema::TYPE_DECIMAL => 'decimal(18,0)',
|
||||
Schema::TYPE_DATETIME => 'datetime',
|
||||
Schema::TYPE_TIMESTAMP => 'datetime',
|
||||
Schema::TYPE_TIME => 'time',
|
||||
Schema::TYPE_DATE => 'date',
|
||||
Schema::TYPE_BINARY => 'varbinary(max)',
|
||||
Schema::TYPE_BOOLEAN => 'bit',
|
||||
Schema::TYPE_MONEY => 'decimal(19,4)',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function defaultExpressionBuilders()
|
||||
{
|
||||
return array_merge(parent::defaultExpressionBuilders(), [
|
||||
'yii\db\conditions\InCondition' => 'yii\db\mssql\conditions\InConditionBuilder',
|
||||
'yii\db\conditions\LikeCondition' => 'yii\db\mssql\conditions\LikeConditionBuilder',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset)
|
||||
{
|
||||
if (!$this->hasOffset($offset) && !$this->hasLimit($limit)) {
|
||||
$orderBy = $this->buildOrderBy($orderBy);
|
||||
return $orderBy === '' ? $sql : $sql . $this->separator . $orderBy;
|
||||
}
|
||||
|
||||
if (version_compare($this->db->getSchema()->getServerVersion(), '11', '<')) {
|
||||
return $this->oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset);
|
||||
}
|
||||
|
||||
return $this->newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer.
|
||||
* @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET)
|
||||
* @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter.
|
||||
* @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details.
|
||||
* @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details.
|
||||
* @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any)
|
||||
*/
|
||||
protected function newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset)
|
||||
{
|
||||
$orderBy = $this->buildOrderBy($orderBy);
|
||||
if ($orderBy === '') {
|
||||
// ORDER BY clause is required when FETCH and OFFSET are in the SQL
|
||||
$orderBy = 'ORDER BY (SELECT NULL)';
|
||||
}
|
||||
$sql .= $this->separator . $orderBy;
|
||||
|
||||
// https://technet.microsoft.com/en-us/library/gg699618.aspx
|
||||
$offset = $this->hasOffset($offset) ? $offset : '0';
|
||||
$sql .= $this->separator . "OFFSET $offset ROWS";
|
||||
if ($this->hasLimit($limit)) {
|
||||
$sql .= $this->separator . "FETCH NEXT $limit ROWS ONLY";
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2005 to 2008.
|
||||
* @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET)
|
||||
* @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter.
|
||||
* @param int|Expression $limit the limit number. See [[\yii\db\Query::limit]] for more details.
|
||||
* @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details.
|
||||
* @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any)
|
||||
*/
|
||||
protected function oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset)
|
||||
{
|
||||
$orderBy = $this->buildOrderBy($orderBy);
|
||||
if ($orderBy === '') {
|
||||
// ROW_NUMBER() requires an ORDER BY clause
|
||||
$orderBy = 'ORDER BY (SELECT NULL)';
|
||||
}
|
||||
|
||||
$sql = preg_replace('/^([\s(])*SELECT(\s+DISTINCT)?(?!\s*TOP\s*\()/i', "\\1SELECT\\2 rowNum = ROW_NUMBER() over ($orderBy),", $sql);
|
||||
|
||||
if ($this->hasLimit($limit)) {
|
||||
if ($limit instanceof Expression) {
|
||||
$limit = '(' . (string)$limit . ')';
|
||||
}
|
||||
$sql = "SELECT TOP $limit * FROM ($sql) sub";
|
||||
} else {
|
||||
$sql = "SELECT * FROM ($sql) sub";
|
||||
}
|
||||
if ($this->hasOffset($offset)) {
|
||||
$sql .= $this->separator . "WHERE rowNum > $offset";
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL statement for renaming a DB table.
|
||||
* @param string $oldName the table to be renamed. The name will be properly quoted by the method.
|
||||
* @param string $newName the new table name. The name will be properly quoted by the method.
|
||||
* @return string the SQL statement for renaming a DB table.
|
||||
*/
|
||||
public function renameTable($oldName, $newName)
|
||||
{
|
||||
return 'sp_rename ' . $this->db->quoteTableName($oldName) . ', ' . $this->db->quoteTableName($newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL statement for renaming a column.
|
||||
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
|
||||
* @param string $oldName the old name of the column. The name will be properly quoted by the method.
|
||||
* @param string $newName the new name of the column. The name will be properly quoted by the method.
|
||||
* @return string the SQL statement for renaming a DB column.
|
||||
*/
|
||||
public function renameColumn($table, $oldName, $newName)
|
||||
{
|
||||
$table = $this->db->quoteTableName($table);
|
||||
$oldName = $this->db->quoteColumnName($oldName);
|
||||
$newName = $this->db->quoteColumnName($newName);
|
||||
return "sp_rename '{$table}.{$oldName}', {$newName}, 'COLUMN'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL statement for changing the definition of a column.
|
||||
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
|
||||
* @param string $column the name of the column to be changed. The name will be properly quoted by the method.
|
||||
* @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any)
|
||||
* into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
|
||||
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
|
||||
* @return string the SQL statement for changing the definition of a column.
|
||||
* @throws NotSupportedException if this is not supported by the underlying DBMS.
|
||||
*/
|
||||
public function alterColumn($table, $column, $type)
|
||||
{
|
||||
$sqlAfter = [$this->dropConstraintsForColumn($table, $column, 'D')];
|
||||
|
||||
$columnName = $this->db->quoteColumnName($column);
|
||||
$tableName = $this->db->quoteTableName($table);
|
||||
$constraintBase = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column);
|
||||
|
||||
if ($type instanceof \yii\db\mssql\ColumnSchemaBuilder) {
|
||||
$type->setAlterColumnFormat();
|
||||
|
||||
|
||||
$defaultValue = $type->getDefaultValue();
|
||||
if ($defaultValue !== null) {
|
||||
$sqlAfter[] = $this->addDefaultValue(
|
||||
"DF_{$constraintBase}",
|
||||
$table,
|
||||
$column,
|
||||
$defaultValue instanceof Expression ? $defaultValue : new Expression($defaultValue)
|
||||
);
|
||||
}
|
||||
|
||||
$checkValue = $type->getCheckValue();
|
||||
if ($checkValue !== null) {
|
||||
$sqlAfter[] = "ALTER TABLE {$tableName} ADD CONSTRAINT " .
|
||||
$this->db->quoteColumnName("CK_{$constraintBase}") .
|
||||
' CHECK (' . ($defaultValue instanceof Expression ? $checkValue : new Expression($checkValue)) . ')';
|
||||
}
|
||||
|
||||
if ($type->isUnique()) {
|
||||
$sqlAfter[] = "ALTER TABLE {$tableName} ADD CONSTRAINT " . $this->db->quoteColumnName("UQ_{$constraintBase}") . " UNIQUE ({$columnName})";
|
||||
}
|
||||
}
|
||||
|
||||
return 'ALTER TABLE ' . $tableName . ' ALTER COLUMN '
|
||||
. $columnName . ' '
|
||||
. $this->getColumnType($type) . "\n"
|
||||
. implode("\n", $sqlAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addDefaultValue($name, $table, $column, $value)
|
||||
{
|
||||
return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD CONSTRAINT '
|
||||
. $this->db->quoteColumnName($name) . ' DEFAULT ' . $this->db->quoteValue($value) . ' FOR '
|
||||
. $this->db->quoteColumnName($column);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dropDefaultValue($name, $table)
|
||||
{
|
||||
return 'ALTER TABLE ' . $this->db->quoteTableName($table)
|
||||
. ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SQL statement for resetting the sequence value of a table's primary key.
|
||||
* The sequence will be reset such that the primary key of the next new row inserted
|
||||
* will have the specified value or 1.
|
||||
* @param string $tableName the name of the table whose primary key sequence will be reset
|
||||
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
|
||||
* the next new row's primary key will have a value 1.
|
||||
* @return string the SQL statement for resetting sequence
|
||||
* @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table.
|
||||
*/
|
||||
public function resetSequence($tableName, $value = null)
|
||||
{
|
||||
$table = $this->db->getTableSchema($tableName);
|
||||
if ($table !== null && $table->sequenceName !== null) {
|
||||
$tableName = $this->db->quoteTableName($tableName);
|
||||
|
||||
if ($value === null || $value === 1) {
|
||||
$key = $this->db->quoteColumnName(reset($table->primaryKey));
|
||||
$subSql = (new Query())
|
||||
->select('last_value')
|
||||
->from('sys.identity_columns')
|
||||
->where(['object_id' => new Expression("OBJECT_ID('{$tableName}')")])
|
||||
->andWhere(['IS NOT', 'last_value', null])
|
||||
->createCommand($this->db)
|
||||
->getRawSql();
|
||||
$sql = "SELECT COALESCE(MAX({$key}), CASE WHEN EXISTS({$subSql}) THEN 0 ELSE 1 END) FROM {$tableName}";
|
||||
$value = $this->db->createCommand($sql)->queryScalar();
|
||||
} else {
|
||||
$value = (int) $value;
|
||||
}
|
||||
|
||||
return "DBCC CHECKIDENT ('{$tableName}', RESEED, {$value})";
|
||||
} elseif ($table === null) {
|
||||
throw new InvalidArgumentException("Table not found: $tableName");
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL statement for enabling or disabling integrity check.
|
||||
* @param bool $check whether to turn on or off the integrity check.
|
||||
* @param string $schema the schema of the tables.
|
||||
* @param string $table the table name.
|
||||
* @return string the SQL statement for checking integrity
|
||||
*/
|
||||
public function checkIntegrity($check = true, $schema = '', $table = '')
|
||||
{
|
||||
/**
|
||||
* @var Schema
|
||||
* @phpstan-var Schema<ColumnSchema>
|
||||
*/
|
||||
$dbSchema = $this->db->getSchema();
|
||||
$enable = $check ? 'CHECK' : 'NOCHECK';
|
||||
$schema = $schema ?: $dbSchema->defaultSchema;
|
||||
$tableNames = $this->db->getTableSchema($table) ? [$table] : $dbSchema->getTableNames($schema);
|
||||
$viewNames = $dbSchema->getViewNames($schema);
|
||||
$tableNames = array_diff($tableNames, $viewNames);
|
||||
$command = '';
|
||||
|
||||
foreach ($tableNames as $tableName) {
|
||||
$tableName = $this->db->quoteTableName("{$schema}.{$tableName}");
|
||||
$command .= "ALTER TABLE $tableName $enable CONSTRAINT ALL; ";
|
||||
}
|
||||
|
||||
return $command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL command for adding or updating a comment to a table or a column. The command built will check if a comment
|
||||
* already exists. If so, it will be updated, otherwise, it will be added.
|
||||
*
|
||||
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
|
||||
* @param string $table the table to be commented or whose column is to be commented. The table name will be
|
||||
* properly quoted by the method.
|
||||
* @param string|null $column optional. The name of the column to be commented. If empty, the command will add the
|
||||
* comment to the table instead. The column name will be properly quoted by the method.
|
||||
* @return string the SQL statement for adding a comment.
|
||||
* @throws InvalidArgumentException if the table does not exist.
|
||||
* @since 2.0.24
|
||||
*/
|
||||
protected function buildAddCommentSql($comment, $table, $column = null)
|
||||
{
|
||||
$tableSchema = $this->db->schema->getTableSchema($table);
|
||||
|
||||
if ($tableSchema === null) {
|
||||
throw new InvalidArgumentException("Table not found: $table");
|
||||
}
|
||||
|
||||
$schemaName = $tableSchema->schemaName ? "N'" . $tableSchema->schemaName . "'" : 'SCHEMA_NAME()';
|
||||
$tableName = 'N' . $this->db->quoteValue($tableSchema->name);
|
||||
$columnName = $column ? 'N' . $this->db->quoteValue($column) : null;
|
||||
$comment = 'N' . $this->db->quoteValue($comment);
|
||||
|
||||
$functionParams = "
|
||||
@name = N'MS_description',
|
||||
@value = $comment,
|
||||
@level0type = N'SCHEMA', @level0name = $schemaName,
|
||||
@level1type = N'TABLE', @level1name = $tableName"
|
||||
. ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';';
|
||||
|
||||
return "
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM fn_listextendedproperty (
|
||||
N'MS_description',
|
||||
'SCHEMA', $schemaName,
|
||||
'TABLE', $tableName,
|
||||
" . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . "
|
||||
)
|
||||
)
|
||||
EXEC sys.sp_addextendedproperty $functionParams
|
||||
ELSE
|
||||
EXEC sys.sp_updateextendedproperty $functionParams
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public function addCommentOnColumn($table, $column, $comment)
|
||||
{
|
||||
return $this->buildAddCommentSql($comment, $table, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public function addCommentOnTable($table, $comment)
|
||||
{
|
||||
return $this->buildAddCommentSql($comment, $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL command for removing a comment from a table or a column. The command built will check if a comment
|
||||
* already exists before trying to perform the removal.
|
||||
*
|
||||
* @param string $table the table that will have the comment removed or whose column will have the comment removed.
|
||||
* The table name will be properly quoted by the method.
|
||||
* @param string|null $column optional. The name of the column whose comment will be removed. If empty, the command
|
||||
* will remove the comment from the table instead. The column name will be properly quoted by the method.
|
||||
* @return string the SQL statement for removing the comment.
|
||||
* @throws InvalidArgumentException if the table does not exist.
|
||||
* @since 2.0.24
|
||||
*/
|
||||
protected function buildRemoveCommentSql($table, $column = null)
|
||||
{
|
||||
$tableSchema = $this->db->schema->getTableSchema($table);
|
||||
|
||||
if ($tableSchema === null) {
|
||||
throw new InvalidArgumentException("Table not found: $table");
|
||||
}
|
||||
|
||||
$schemaName = $tableSchema->schemaName ? "N'" . $tableSchema->schemaName . "'" : 'SCHEMA_NAME()';
|
||||
$tableName = 'N' . $this->db->quoteValue($tableSchema->name);
|
||||
$columnName = $column ? 'N' . $this->db->quoteValue($column) : null;
|
||||
|
||||
return "
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM fn_listextendedproperty (
|
||||
N'MS_description',
|
||||
'SCHEMA', $schemaName,
|
||||
'TABLE', $tableName,
|
||||
" . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . "
|
||||
)
|
||||
)
|
||||
EXEC sys.sp_dropextendedproperty
|
||||
@name = N'MS_description',
|
||||
@level0type = N'SCHEMA', @level0name = $schemaName,
|
||||
@level1type = N'TABLE', @level1name = $tableName"
|
||||
. ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public function dropCommentFromColumn($table, $column)
|
||||
{
|
||||
return $this->buildRemoveCommentSql($table, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public function dropCommentFromTable($table)
|
||||
{
|
||||
return $this->buildRemoveCommentSql($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of column names given model name.
|
||||
*
|
||||
* @param string|null $modelClass name of the model class
|
||||
* @return array|null array of column names
|
||||
*/
|
||||
protected function getAllColumnNames($modelClass = null)
|
||||
{
|
||||
if (!$modelClass) {
|
||||
return null;
|
||||
}
|
||||
/** @var \yii\db\ActiveRecord $modelClass */
|
||||
$schema = $modelClass::getTableSchema();
|
||||
return array_keys($schema->columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether the version of the MSSQL being used is older than 2012.
|
||||
* @throws \yii\base\InvalidConfigException
|
||||
* @throws \yii\db\Exception
|
||||
* @deprecated 2.0.14 Use [[Schema::getServerVersion]] with [[\version_compare()]].
|
||||
*/
|
||||
protected function isOldMssql()
|
||||
{
|
||||
return version_compare($this->db->getSchema()->getServerVersion(), '11', '<');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public function selectExists($rawSql)
|
||||
{
|
||||
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END';
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary.
|
||||
* @param string $table the table that data will be saved into.
|
||||
* @param array $columns the column data (name => value) to be saved into the table.
|
||||
* @return array normalized columns
|
||||
*/
|
||||
private function normalizeTableRowData($table, $columns, &$params)
|
||||
{
|
||||
if (($tableSchema = $this->db->getSchema()->getTableSchema($table)) !== null) {
|
||||
$columnSchemas = $tableSchema->columns;
|
||||
foreach ($columns as $name => $value) {
|
||||
// @see https://github.com/yiisoft/yii2/issues/12599
|
||||
if (isset($columnSchemas[$name]) && $columnSchemas[$name]->type === Schema::TYPE_BINARY && $columnSchemas[$name]->dbType === 'varbinary' && (is_string($value))) {
|
||||
// @see https://github.com/yiisoft/yii2/issues/12599
|
||||
$columns[$name] = new Expression('CONVERT(VARBINARY(MAX), ' . ('0x' . bin2hex($value)) . ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Added OUTPUT construction for getting inserted data (for SQL Server 2005 or later)
|
||||
* OUTPUT clause - The OUTPUT clause is new to SQL Server 2005 and has the ability to access
|
||||
* the INSERTED and DELETED tables as is the case with a trigger.
|
||||
*/
|
||||
public function insert($table, $columns, &$params)
|
||||
{
|
||||
$columns = $this->normalizeTableRowData($table, $columns, $params);
|
||||
|
||||
$version2005orLater = version_compare($this->db->getSchema()->getServerVersion(), '9', '>=');
|
||||
|
||||
list($names, $placeholders, $values, $params) = $this->prepareInsertValues($table, $columns, $params);
|
||||
$cols = [];
|
||||
$outputColumns = [];
|
||||
if ($version2005orLater) {
|
||||
/** @var TableSchema $schema */
|
||||
$schema = $this->db->getTableSchema($table);
|
||||
foreach ($schema->columns as $column) {
|
||||
if ($column->isComputed) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dbType = $column->dbType;
|
||||
if (in_array($dbType, ['varchar', 'nvarchar', 'binary', 'varbinary'])) {
|
||||
$dbType .= '(MAX)';
|
||||
} elseif (in_array($dbType, ['char', 'nchar'])) {
|
||||
$dbType .= "($column->size)";
|
||||
}
|
||||
|
||||
if ($column->dbType === Schema::TYPE_TIMESTAMP) {
|
||||
$dbType = $column->allowNull ? 'varbinary(8)' : 'binary(8)';
|
||||
}
|
||||
|
||||
$quoteColumnName = $this->db->quoteColumnName($column->name);
|
||||
$cols[] = $quoteColumnName . ' ' . $dbType . ' ' . ($column->allowNull ? 'NULL' : '');
|
||||
$outputColumns[] = 'INSERTED.' . $quoteColumnName;
|
||||
}
|
||||
}
|
||||
|
||||
$countColumns = count($outputColumns);
|
||||
|
||||
$sql = 'INSERT INTO ' . $this->db->quoteTableName($table)
|
||||
. (!empty($names) ? ' (' . implode(', ', $names) . ')' : '')
|
||||
. (($version2005orLater && $countColumns) ? ' OUTPUT ' . implode(',', $outputColumns) . ' INTO @temporary_inserted' : '')
|
||||
. (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values);
|
||||
|
||||
if ($version2005orLater && $countColumns) {
|
||||
$sql = 'SET NOCOUNT ON;DECLARE @temporary_inserted TABLE (' . implode(', ', $cols) . ');' . $sql .
|
||||
';SELECT * FROM @temporary_inserted';
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @see https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql
|
||||
* @see https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/
|
||||
*/
|
||||
public function upsert($table, $insertColumns, $updateColumns, &$params)
|
||||
{
|
||||
$insertColumns = $this->normalizeTableRowData($table, $insertColumns, $params);
|
||||
|
||||
list($uniqueNames, $insertNames, $updateNames) = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns, $constraints);
|
||||
if (empty($uniqueNames)) {
|
||||
return $this->insert($table, $insertColumns, $params);
|
||||
}
|
||||
if ($updateNames === []) {
|
||||
// there are no columns to update
|
||||
$updateColumns = false;
|
||||
}
|
||||
|
||||
$onCondition = ['or'];
|
||||
$quotedTableName = $this->db->quoteTableName($table);
|
||||
foreach ($constraints as $constraint) {
|
||||
$constraintCondition = ['and'];
|
||||
foreach ($constraint->columnNames as $name) {
|
||||
$quotedName = $this->db->quoteColumnName($name);
|
||||
$constraintCondition[] = "$quotedTableName.$quotedName=[EXCLUDED].$quotedName";
|
||||
}
|
||||
$onCondition[] = $constraintCondition;
|
||||
}
|
||||
$on = $this->buildCondition($onCondition, $params);
|
||||
list(, $placeholders, $values, $params) = $this->prepareInsertValues($table, $insertColumns, $params);
|
||||
|
||||
/**
|
||||
* Fix number of select query params for old MSSQL version that does not support offset correctly.
|
||||
* @see QueryBuilder::oldBuildOrderByAndLimit
|
||||
*/
|
||||
$insertNamesUsing = $insertNames;
|
||||
if (strstr($values, 'rowNum = ROW_NUMBER()') !== false) {
|
||||
$insertNamesUsing = array_merge(['[rowNum]'], $insertNames);
|
||||
}
|
||||
|
||||
$mergeSql = 'MERGE ' . $this->db->quoteTableName($table) . ' WITH (HOLDLOCK) '
|
||||
. 'USING (' . (!empty($placeholders) ? 'VALUES (' . implode(', ', $placeholders) . ')' : ltrim($values, ' ')) . ') AS [EXCLUDED] (' . implode(', ', $insertNamesUsing) . ') '
|
||||
. "ON ($on)";
|
||||
$insertValues = [];
|
||||
foreach ($insertNames as $name) {
|
||||
$quotedName = $this->db->quoteColumnName($name);
|
||||
if (strrpos($quotedName, '.') === false) {
|
||||
$quotedName = '[EXCLUDED].' . $quotedName;
|
||||
}
|
||||
$insertValues[] = $quotedName;
|
||||
}
|
||||
$insertSql = 'INSERT (' . implode(', ', $insertNames) . ')'
|
||||
. ' VALUES (' . implode(', ', $insertValues) . ')';
|
||||
if ($updateColumns === false) {
|
||||
return "$mergeSql WHEN NOT MATCHED THEN $insertSql;";
|
||||
}
|
||||
|
||||
if ($updateColumns === true) {
|
||||
$updateColumns = [];
|
||||
foreach ($updateNames as $name) {
|
||||
$quotedName = $this->db->quoteColumnName($name);
|
||||
if (strrpos($quotedName, '.') === false) {
|
||||
$quotedName = '[EXCLUDED].' . $quotedName;
|
||||
}
|
||||
$updateColumns[$name] = new Expression($quotedName);
|
||||
}
|
||||
}
|
||||
$updateColumns = $this->normalizeTableRowData($table, $updateColumns, $params);
|
||||
|
||||
list($updates, $params) = $this->prepareUpdateSets($table, $updateColumns, $params);
|
||||
$updateSql = 'UPDATE SET ' . implode(', ', $updates);
|
||||
return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql;";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update($table, $columns, $condition, &$params)
|
||||
{
|
||||
return parent::update($table, $this->normalizeTableRowData($table, $columns, $params), $condition, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getColumnType($type)
|
||||
{
|
||||
$columnType = parent::getColumnType($type);
|
||||
// remove unsupported keywords
|
||||
$columnType = preg_replace("/\s*comment '.*'/i", '', $columnType);
|
||||
$columnType = preg_replace('/ first$/i', '', $columnType);
|
||||
|
||||
return $columnType;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function extractAlias($table)
|
||||
{
|
||||
if (preg_match('/^\[.*\]$/', $table)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::extractAlias($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a SQL statement for dropping constraints for column of table.
|
||||
*
|
||||
* @param string $table the table whose constraint is to be dropped. The name will be properly quoted by the method.
|
||||
* @param string $column the column whose constraint is to be dropped. The name will be properly quoted by the method.
|
||||
* @param string $type type of constraint, leave empty for all type of constraints(for example: D - default, 'UQ' - unique, 'C' - check)
|
||||
* @see https://docs.microsoft.com/sql/relational-databases/system-catalog-views/sys-objects-transact-sql
|
||||
* @return string the DROP CONSTRAINTS SQL
|
||||
*/
|
||||
private function dropConstraintsForColumn($table, $column, $type = '')
|
||||
{
|
||||
return "DECLARE @tableName VARCHAR(MAX) = '" . $this->db->quoteTableName($table) . "'
|
||||
DECLARE @columnName VARCHAR(MAX) = '{$column}'
|
||||
|
||||
WHILE 1=1 BEGIN
|
||||
DECLARE @constraintName NVARCHAR(128)
|
||||
SET @constraintName = (SELECT TOP 1 OBJECT_NAME(cons.[object_id])
|
||||
FROM (
|
||||
SELECT sc.[constid] object_id
|
||||
FROM [sys].[sysconstraints] sc
|
||||
JOIN [sys].[columns] c ON c.[object_id]=sc.[id] AND c.[column_id]=sc.[colid] AND c.[name]=@columnName
|
||||
WHERE sc.[id] = OBJECT_ID(@tableName)
|
||||
UNION
|
||||
SELECT object_id(i.[name]) FROM [sys].[indexes] i
|
||||
JOIN [sys].[columns] c ON c.[object_id]=i.[object_id] AND c.[name]=@columnName
|
||||
JOIN [sys].[index_columns] ic ON ic.[object_id]=i.[object_id] AND i.[index_id]=ic.[index_id] AND c.[column_id]=ic.[column_id]
|
||||
WHERE i.[is_unique_constraint]=1 and i.[object_id]=OBJECT_ID(@tableName)
|
||||
) cons
|
||||
JOIN [sys].[objects] so ON so.[object_id]=cons.[object_id]
|
||||
" . (!empty($type) ? " WHERE so.[type]='{$type}'" : '') . ")
|
||||
IF @constraintName IS NULL BREAK
|
||||
EXEC (N'ALTER TABLE ' + @tableName + ' DROP CONSTRAINT [' + @constraintName + ']')
|
||||
END";
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop all constraints before column delete
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dropColumn($table, $column)
|
||||
{
|
||||
return $this->dropConstraintsForColumn($table, $column) . "\nALTER TABLE " . $this->db->quoteTableName($table)
|
||||
. ' DROP COLUMN ' . $this->db->quoteColumnName($column);
|
||||
}
|
||||
}
|
||||
833
vendor/yiisoft/yii2/db/mssql/Schema.php
vendored
Normal file
833
vendor/yiisoft/yii2/db/mssql/Schema.php
vendored
Normal file
@ -0,0 +1,833 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
use Yii;
|
||||
use yii\db\CheckConstraint;
|
||||
use yii\db\Constraint;
|
||||
use yii\db\ConstraintFinderInterface;
|
||||
use yii\db\ConstraintFinderTrait;
|
||||
use yii\db\DefaultValueConstraint;
|
||||
use yii\db\ForeignKeyConstraint;
|
||||
use yii\db\IndexConstraint;
|
||||
use yii\db\ViewFinderTrait;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\db\Schema as BaseSchema;
|
||||
|
||||
/**
|
||||
* Schema is the class for retrieving metadata from MS SQL Server databases (version 2008 and above).
|
||||
*
|
||||
* @author Timur Ruziev <resurtm@gmail.com>
|
||||
* @since 2.0
|
||||
*
|
||||
* @template T of ColumnSchema
|
||||
* @extends BaseSchema<T>
|
||||
*/
|
||||
class Schema extends BaseSchema implements ConstraintFinderInterface
|
||||
{
|
||||
use ViewFinderTrait;
|
||||
use ConstraintFinderTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $columnSchemaClass = 'yii\db\mssql\ColumnSchema';
|
||||
/**
|
||||
* @var string the default schema used for the current session.
|
||||
*/
|
||||
public $defaultSchema = 'dbo';
|
||||
/**
|
||||
* @var array mapping from physical column types (keys) to abstract column types (values)
|
||||
*/
|
||||
public $typeMap = [
|
||||
// exact numbers
|
||||
'bigint' => self::TYPE_BIGINT,
|
||||
'numeric' => self::TYPE_DECIMAL,
|
||||
'bit' => self::TYPE_SMALLINT,
|
||||
'smallint' => self::TYPE_SMALLINT,
|
||||
'decimal' => self::TYPE_DECIMAL,
|
||||
'smallmoney' => self::TYPE_MONEY,
|
||||
'int' => self::TYPE_INTEGER,
|
||||
'tinyint' => self::TYPE_TINYINT,
|
||||
'money' => self::TYPE_MONEY,
|
||||
// approximate numbers
|
||||
'float' => self::TYPE_FLOAT,
|
||||
'double' => self::TYPE_DOUBLE,
|
||||
'real' => self::TYPE_FLOAT,
|
||||
// date and time
|
||||
'date' => self::TYPE_DATE,
|
||||
'datetimeoffset' => self::TYPE_DATETIME,
|
||||
'datetime2' => self::TYPE_DATETIME,
|
||||
'smalldatetime' => self::TYPE_DATETIME,
|
||||
'datetime' => self::TYPE_DATETIME,
|
||||
'time' => self::TYPE_TIME,
|
||||
// character strings
|
||||
'char' => self::TYPE_CHAR,
|
||||
'varchar' => self::TYPE_STRING,
|
||||
'text' => self::TYPE_TEXT,
|
||||
// unicode character strings
|
||||
'nchar' => self::TYPE_CHAR,
|
||||
'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
|
||||
'timestamp' => self::TYPE_TIMESTAMP,
|
||||
'hierarchyid' => self::TYPE_STRING,
|
||||
'uniqueidentifier' => self::TYPE_STRING,
|
||||
'sql_variant' => self::TYPE_STRING,
|
||||
'xml' => self::TYPE_STRING,
|
||||
'table' => self::TYPE_STRING,
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $tableQuoteCharacter = ['[', ']'];
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $columnQuoteCharacter = ['[', ']'];
|
||||
|
||||
|
||||
/**
|
||||
* Resolves the table name and schema name (if any).
|
||||
* @param string $name the table name
|
||||
* @return TableSchema resolved table, schema, etc. names.
|
||||
*/
|
||||
protected function resolveTableName($name)
|
||||
{
|
||||
$resolvedName = new TableSchema();
|
||||
$parts = $this->getTableNameParts($name);
|
||||
$partCount = count($parts);
|
||||
if ($partCount === 4) {
|
||||
// server name, catalog name, schema name and table name passed
|
||||
$resolvedName->catalogName = $parts[1];
|
||||
$resolvedName->schemaName = $parts[2];
|
||||
$resolvedName->name = $parts[3];
|
||||
$resolvedName->fullName = $resolvedName->catalogName . '.' . $resolvedName->schemaName . '.' . $resolvedName->name;
|
||||
} elseif ($partCount === 3) {
|
||||
// catalog name, schema name and table name passed
|
||||
$resolvedName->catalogName = $parts[0];
|
||||
$resolvedName->schemaName = $parts[1];
|
||||
$resolvedName->name = $parts[2];
|
||||
$resolvedName->fullName = $resolvedName->catalogName . '.' . $resolvedName->schemaName . '.' . $resolvedName->name;
|
||||
} elseif ($partCount === 2) {
|
||||
// only schema name and table name passed
|
||||
$resolvedName->schemaName = $parts[0];
|
||||
$resolvedName->name = $parts[1];
|
||||
$resolvedName->fullName = ($resolvedName->schemaName !== $this->defaultSchema ? $resolvedName->schemaName . '.' : '') . $resolvedName->name;
|
||||
} else {
|
||||
// only table name passed
|
||||
$resolvedName->schemaName = $this->defaultSchema;
|
||||
$resolvedName->fullName = $resolvedName->name = $parts[0];
|
||||
}
|
||||
|
||||
return $resolvedName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @param string $name
|
||||
* @return array
|
||||
* @since 2.0.22
|
||||
*/
|
||||
protected function getTableNameParts($name)
|
||||
{
|
||||
$parts = [$name];
|
||||
preg_match_all('/([^.\[\]]+)|\[([^\[\]]+)\]/', $name, $matches);
|
||||
if (isset($matches[0]) && is_array($matches[0]) && !empty($matches[0])) {
|
||||
$parts = $matches[0];
|
||||
}
|
||||
|
||||
$parts = str_replace(['[', ']'], '', $parts);
|
||||
|
||||
return $parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @see https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-principals-transact-sql
|
||||
*/
|
||||
protected function findSchemaNames()
|
||||
{
|
||||
static $sql = <<<'SQL'
|
||||
SELECT [s].[name]
|
||||
FROM [sys].[schemas] AS [s]
|
||||
INNER JOIN [sys].[database_principals] AS [p] ON [p].[principal_id] = [s].[principal_id]
|
||||
WHERE [p].[is_fixed_role] = 0 AND [p].[sid] IS NOT NULL
|
||||
ORDER BY [s].[name] ASC
|
||||
SQL;
|
||||
|
||||
return $this->db->createCommand($sql)->queryColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function findTableNames($schema = '')
|
||||
{
|
||||
if ($schema === '') {
|
||||
$schema = $this->defaultSchema;
|
||||
}
|
||||
|
||||
$sql = <<<'SQL'
|
||||
SELECT [t].[table_name]
|
||||
FROM [INFORMATION_SCHEMA].[TABLES] AS [t]
|
||||
WHERE [t].[table_schema] = :schema AND [t].[table_type] IN ('BASE TABLE', 'VIEW')
|
||||
ORDER BY [t].[table_name]
|
||||
SQL;
|
||||
return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTableSchema($name)
|
||||
{
|
||||
$table = new TableSchema();
|
||||
$this->resolveTableNames($table, $name);
|
||||
$this->findPrimaryKeys($table);
|
||||
if ($this->findColumns($table)) {
|
||||
$this->findForeignKeys($table);
|
||||
return $table;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSchemaMetadata($schema, $type, $refresh)
|
||||
{
|
||||
$metadata = [];
|
||||
$methodName = 'getTable' . ucfirst($type);
|
||||
$tableNames = array_map(function ($table) {
|
||||
return $this->quoteSimpleTableName($table);
|
||||
}, $this->getTableNames($schema, $refresh));
|
||||
foreach ($tableNames as $name) {
|
||||
if ($schema !== '') {
|
||||
$name = $schema . '.' . $name;
|
||||
}
|
||||
$tableMetadata = $this->$methodName($name, $refresh);
|
||||
if ($tableMetadata !== null) {
|
||||
$metadata[] = $tableMetadata;
|
||||
}
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTablePrimaryKey($tableName)
|
||||
{
|
||||
return $this->loadTableConstraints($tableName, 'primaryKey');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTableForeignKeys($tableName)
|
||||
{
|
||||
return $this->loadTableConstraints($tableName, 'foreignKeys');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTableIndexes($tableName)
|
||||
{
|
||||
static $sql = <<<'SQL'
|
||||
SELECT
|
||||
[i].[name] AS [name],
|
||||
[iccol].[name] AS [column_name],
|
||||
[i].[is_unique] AS [index_is_unique],
|
||||
[i].[is_primary_key] AS [index_is_primary]
|
||||
FROM [sys].[indexes] AS [i]
|
||||
INNER JOIN [sys].[index_columns] AS [ic]
|
||||
ON [ic].[object_id] = [i].[object_id] AND [ic].[index_id] = [i].[index_id]
|
||||
INNER JOIN [sys].[columns] AS [iccol]
|
||||
ON [iccol].[object_id] = [ic].[object_id] AND [iccol].[column_id] = [ic].[column_id]
|
||||
WHERE [i].[object_id] = OBJECT_ID(:fullName)
|
||||
ORDER BY [ic].[key_ordinal] ASC
|
||||
SQL;
|
||||
|
||||
$resolvedName = $this->resolveTableName($tableName);
|
||||
$indexes = $this->db->createCommand($sql, [
|
||||
':fullName' => $resolvedName->fullName,
|
||||
])->queryAll();
|
||||
$indexes = $this->normalizePdoRowKeyCase($indexes, true);
|
||||
$indexes = ArrayHelper::index($indexes, null, 'name');
|
||||
$result = [];
|
||||
foreach ($indexes as $name => $index) {
|
||||
$result[] = new IndexConstraint([
|
||||
'isPrimary' => (bool)$index[0]['index_is_primary'],
|
||||
'isUnique' => (bool)$index[0]['index_is_unique'],
|
||||
'name' => $name,
|
||||
'columnNames' => ArrayHelper::getColumn($index, 'column_name'),
|
||||
]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTableUniques($tableName)
|
||||
{
|
||||
return $this->loadTableConstraints($tableName, 'uniques');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTableChecks($tableName)
|
||||
{
|
||||
return $this->loadTableConstraints($tableName, 'checks');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadTableDefaultValues($tableName)
|
||||
{
|
||||
return $this->loadTableConstraints($tableName, 'defaults');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createSavepoint($name)
|
||||
{
|
||||
$this->db->createCommand("SAVE TRANSACTION $name")->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function releaseSavepoint($name)
|
||||
{
|
||||
// does nothing as MSSQL does not support this
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rollBackSavepoint($name)
|
||||
{
|
||||
$this->db->createCommand("ROLLBACK TRANSACTION $name")->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a query builder for the MSSQL database.
|
||||
* @return QueryBuilder query builder interface.
|
||||
*/
|
||||
public function createQueryBuilder()
|
||||
{
|
||||
return Yii::createObject(QueryBuilder::className(), [$this->db]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = $this->getTableNameParts($name);
|
||||
$partCount = count($parts);
|
||||
if ($partCount === 4) {
|
||||
// server name, catalog name, schema name and table name passed
|
||||
$table->catalogName = $parts[1];
|
||||
$table->schemaName = $parts[2];
|
||||
$table->name = $parts[3];
|
||||
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;
|
||||
} elseif ($partCount === 3) {
|
||||
// catalog name, schema name and table name passed
|
||||
$table->catalogName = $parts[0];
|
||||
$table->schemaName = $parts[1];
|
||||
$table->name = $parts[2];
|
||||
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;
|
||||
} elseif ($partCount === 2) {
|
||||
// only schema name and table name passed
|
||||
$table->schemaName = $parts[0];
|
||||
$table->name = $parts[1];
|
||||
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
|
||||
} else {
|
||||
// only table name passed
|
||||
$table->schemaName = $this->defaultSchema;
|
||||
$table->fullName = $table->name = $parts[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the column information into a [[ColumnSchema]] object.
|
||||
* @param array $info column information
|
||||
* @return ColumnSchema the column schema object
|
||||
*
|
||||
* @phpstan-return T
|
||||
* @psalm-return T
|
||||
*/
|
||||
protected function loadColumnSchema($info)
|
||||
{
|
||||
$isVersion2017orLater = version_compare($this->db->getSchema()->getServerVersion(), '14', '>=');
|
||||
$column = $this->createColumnSchema();
|
||||
|
||||
$column->name = $info['column_name'];
|
||||
$column->allowNull = $info['is_nullable'] === 'YES';
|
||||
$column->dbType = $info['data_type'];
|
||||
$column->enumValues = []; // mssql has only vague equivalents to enum
|
||||
$column->isPrimaryKey = null; // primary key will be determined in findColumns() method
|
||||
$column->autoIncrement = $info['is_identity'] == 1;
|
||||
$column->isComputed = (bool)$info['is_computed'];
|
||||
$column->unsigned = stripos($column->dbType, 'unsigned') !== false;
|
||||
$column->comment = $info['comment'] === null ? '' : $info['comment'];
|
||||
|
||||
$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 ($isVersion2017orLater && $type === 'bit') {
|
||||
$column->type = 'boolean';
|
||||
}
|
||||
|
||||
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 ($isVersion2017orLater === false) {
|
||||
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)') {
|
||||
$info['column_default'] = null;
|
||||
}
|
||||
if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP')) {
|
||||
$column->defaultValue = $column->defaultPhpTypecast($info['column_default']);
|
||||
}
|
||||
|
||||
return $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the metadata of table columns.
|
||||
* @param TableSchema $table the table metadata
|
||||
* @return bool whether the table exists in the database
|
||||
*/
|
||||
protected function findColumns($table)
|
||||
{
|
||||
$columnsTableName = 'INFORMATION_SCHEMA.COLUMNS';
|
||||
$whereSql = '[t1].[table_name] = ' . $this->db->quoteValue($table->name);
|
||||
if ($table->catalogName !== null) {
|
||||
$columnsTableName = "{$table->catalogName}.{$columnsTableName}";
|
||||
$whereSql .= " AND [t1].[table_catalog] = '{$table->catalogName}'";
|
||||
}
|
||||
if ($table->schemaName !== null) {
|
||||
$whereSql .= " AND [t1].[table_schema] = '{$table->schemaName}'";
|
||||
}
|
||||
$columnsTableName = $this->quoteTableName($columnsTableName);
|
||||
|
||||
$sql = <<<SQL
|
||||
SELECT
|
||||
[t1].[column_name],
|
||||
[t1].[is_nullable],
|
||||
CASE WHEN [t1].[data_type] IN ('char','varchar','nchar','nvarchar','binary','varbinary') THEN
|
||||
CASE WHEN [t1].[character_maximum_length] = NULL OR [t1].[character_maximum_length] = -1 THEN
|
||||
[t1].[data_type]
|
||||
ELSE
|
||||
[t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[character_maximum_length]))) + ')'
|
||||
END
|
||||
ELSE
|
||||
[t1].[data_type]
|
||||
END AS 'data_type',
|
||||
[t1].[column_default],
|
||||
COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity,
|
||||
COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsComputed') AS is_computed,
|
||||
(
|
||||
SELECT CONVERT(VARCHAR, [t2].[value])
|
||||
FROM [sys].[extended_properties] AS [t2]
|
||||
WHERE
|
||||
[t2].[class] = 1 AND
|
||||
[t2].[class_desc] = 'OBJECT_OR_COLUMN' AND
|
||||
[t2].[name] = 'MS_Description' AND
|
||||
[t2].[major_id] = OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[table_name]) AND
|
||||
[t2].[minor_id] = COLUMNPROPERTY(OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[TABLE_NAME]), [t1].[COLUMN_NAME], 'ColumnID')
|
||||
) as comment
|
||||
FROM {$columnsTableName} AS [t1]
|
||||
WHERE {$whereSql}
|
||||
SQL;
|
||||
|
||||
try {
|
||||
$columns = $this->db->createCommand($sql)->queryAll();
|
||||
if (empty($columns)) {
|
||||
return false;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
foreach ($columns as $column) {
|
||||
$column = $this->loadColumnSchema($column);
|
||||
foreach ($table->primaryKey as $primaryKey) {
|
||||
if (strcasecmp($column->name, $primaryKey) === 0) {
|
||||
$column->isPrimaryKey = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($column->isPrimaryKey && $column->autoIncrement) {
|
||||
$table->sequenceName = '';
|
||||
}
|
||||
$table->columns[$column->name] = $column;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the constraint details for the given table and constraint type.
|
||||
* @param TableSchema $table
|
||||
* @param string $type either PRIMARY KEY or UNIQUE
|
||||
* @return array each entry contains index_name and field_name
|
||||
* @since 2.0.4
|
||||
*/
|
||||
protected function findTableConstraints($table, $type)
|
||||
{
|
||||
$keyColumnUsageTableName = 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
|
||||
$tableConstraintsTableName = 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS';
|
||||
if ($table->catalogName !== null) {
|
||||
$keyColumnUsageTableName = $table->catalogName . '.' . $keyColumnUsageTableName;
|
||||
$tableConstraintsTableName = $table->catalogName . '.' . $tableConstraintsTableName;
|
||||
}
|
||||
$keyColumnUsageTableName = $this->quoteTableName($keyColumnUsageTableName);
|
||||
$tableConstraintsTableName = $this->quoteTableName($tableConstraintsTableName);
|
||||
|
||||
$sql = <<<SQL
|
||||
SELECT
|
||||
[kcu].[constraint_name] AS [index_name],
|
||||
[kcu].[column_name] AS [field_name]
|
||||
FROM {$keyColumnUsageTableName} AS [kcu]
|
||||
LEFT JOIN {$tableConstraintsTableName} AS [tc] ON
|
||||
[kcu].[table_schema] = [tc].[table_schema] AND
|
||||
[kcu].[table_name] = [tc].[table_name] AND
|
||||
[kcu].[constraint_name] = [tc].[constraint_name]
|
||||
WHERE
|
||||
[tc].[constraint_type] = :type AND
|
||||
[kcu].[table_name] = :tableName AND
|
||||
[kcu].[table_schema] = :schemaName
|
||||
SQL;
|
||||
|
||||
return $this->db
|
||||
->createCommand($sql, [
|
||||
':tableName' => $table->name,
|
||||
':schemaName' => $table->schemaName,
|
||||
':type' => $type,
|
||||
])
|
||||
->queryAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the primary key column details for the given table.
|
||||
* @param TableSchema $table the table metadata
|
||||
*/
|
||||
protected function findPrimaryKeys($table)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->findTableConstraints($table, 'PRIMARY KEY') as $row) {
|
||||
$result[] = $row['field_name'];
|
||||
}
|
||||
$table->primaryKey = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the foreign key column details for the given table.
|
||||
* @param TableSchema $table the table metadata
|
||||
*/
|
||||
protected function findForeignKeys($table)
|
||||
{
|
||||
$object = $table->name;
|
||||
if ($table->schemaName !== null) {
|
||||
$object = $table->schemaName . '.' . $object;
|
||||
}
|
||||
if ($table->catalogName !== null) {
|
||||
$object = $table->catalogName . '.' . $object;
|
||||
}
|
||||
|
||||
// please refer to the following page for more details:
|
||||
// http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx
|
||||
$sql = <<<'SQL'
|
||||
SELECT
|
||||
[fk].[name] AS [fk_name],
|
||||
[cp].[name] AS [fk_column_name],
|
||||
OBJECT_NAME([fk].[referenced_object_id]) AS [uq_table_name],
|
||||
[cr].[name] AS [uq_column_name]
|
||||
FROM
|
||||
[sys].[foreign_keys] AS [fk]
|
||||
INNER JOIN [sys].[foreign_key_columns] AS [fkc] ON
|
||||
[fk].[object_id] = [fkc].[constraint_object_id]
|
||||
INNER JOIN [sys].[columns] AS [cp] ON
|
||||
[fk].[parent_object_id] = [cp].[object_id] AND
|
||||
[fkc].[parent_column_id] = [cp].[column_id]
|
||||
INNER JOIN [sys].[columns] AS [cr] ON
|
||||
[fk].[referenced_object_id] = [cr].[object_id] AND
|
||||
[fkc].[referenced_column_id] = [cr].[column_id]
|
||||
WHERE
|
||||
[fk].[parent_object_id] = OBJECT_ID(:object)
|
||||
SQL;
|
||||
|
||||
$rows = $this->db->createCommand($sql, [
|
||||
':object' => $object,
|
||||
])->queryAll();
|
||||
|
||||
$table->foreignKeys = [];
|
||||
foreach ($rows as $row) {
|
||||
if (!isset($table->foreignKeys[$row['fk_name']])) {
|
||||
$table->foreignKeys[$row['fk_name']][] = $row['uq_table_name'];
|
||||
}
|
||||
$table->foreignKeys[$row['fk_name']][$row['fk_column_name']] = $row['uq_column_name'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function findViewNames($schema = '')
|
||||
{
|
||||
if ($schema === '') {
|
||||
$schema = $this->defaultSchema;
|
||||
}
|
||||
|
||||
$sql = <<<'SQL'
|
||||
SELECT [t].[table_name]
|
||||
FROM [INFORMATION_SCHEMA].[TABLES] AS [t]
|
||||
WHERE [t].[table_schema] = :schema AND [t].[table_type] = 'VIEW'
|
||||
ORDER BY [t].[table_name]
|
||||
SQL;
|
||||
|
||||
return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all unique indexes for the given table.
|
||||
*
|
||||
* Each array element is of the following structure:
|
||||
*
|
||||
* ```
|
||||
* [
|
||||
* 'IndexName1' => ['col1' [, ...]],
|
||||
* 'IndexName2' => ['col2' [, ...]],
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* @param TableSchema $table the table metadata
|
||||
* @return array all unique indexes for the given table.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public function findUniqueIndexes($table)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->findTableConstraints($table, 'UNIQUE') as $row) {
|
||||
$result[$row['index_name']][] = $row['field_name'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads multiple types of constraints and returns the specified ones.
|
||||
* @param string $tableName table name.
|
||||
* @param string $returnType return type:
|
||||
* - primaryKey
|
||||
* - foreignKeys
|
||||
* - uniques
|
||||
* - checks
|
||||
* - defaults
|
||||
* @return mixed constraints.
|
||||
*/
|
||||
private function loadTableConstraints($tableName, $returnType)
|
||||
{
|
||||
static $sql = <<<'SQL'
|
||||
SELECT
|
||||
[o].[name] AS [name],
|
||||
COALESCE([ccol].[name], [dcol].[name], [fccol].[name], [kiccol].[name]) AS [column_name],
|
||||
RTRIM([o].[type]) AS [type],
|
||||
OBJECT_SCHEMA_NAME([f].[referenced_object_id]) AS [foreign_table_schema],
|
||||
OBJECT_NAME([f].[referenced_object_id]) AS [foreign_table_name],
|
||||
[ffccol].[name] AS [foreign_column_name],
|
||||
[f].[update_referential_action_desc] AS [on_update],
|
||||
[f].[delete_referential_action_desc] AS [on_delete],
|
||||
[c].[definition] AS [check_expr],
|
||||
[d].[definition] AS [default_expr]
|
||||
FROM (SELECT OBJECT_ID(:fullName) AS [object_id]) AS [t]
|
||||
INNER JOIN [sys].[objects] AS [o]
|
||||
ON [o].[parent_object_id] = [t].[object_id] AND [o].[type] IN ('PK', 'UQ', 'C', 'D', 'F')
|
||||
LEFT JOIN [sys].[check_constraints] AS [c]
|
||||
ON [c].[object_id] = [o].[object_id]
|
||||
LEFT JOIN [sys].[columns] AS [ccol]
|
||||
ON [ccol].[object_id] = [c].[parent_object_id] AND [ccol].[column_id] = [c].[parent_column_id]
|
||||
LEFT JOIN [sys].[default_constraints] AS [d]
|
||||
ON [d].[object_id] = [o].[object_id]
|
||||
LEFT JOIN [sys].[columns] AS [dcol]
|
||||
ON [dcol].[object_id] = [d].[parent_object_id] AND [dcol].[column_id] = [d].[parent_column_id]
|
||||
LEFT JOIN [sys].[key_constraints] AS [k]
|
||||
ON [k].[object_id] = [o].[object_id]
|
||||
LEFT JOIN [sys].[index_columns] AS [kic]
|
||||
ON [kic].[object_id] = [k].[parent_object_id] AND [kic].[index_id] = [k].[unique_index_id]
|
||||
LEFT JOIN [sys].[columns] AS [kiccol]
|
||||
ON [kiccol].[object_id] = [kic].[object_id] AND [kiccol].[column_id] = [kic].[column_id]
|
||||
LEFT JOIN [sys].[foreign_keys] AS [f]
|
||||
ON [f].[object_id] = [o].[object_id]
|
||||
LEFT JOIN [sys].[foreign_key_columns] AS [fc]
|
||||
ON [fc].[constraint_object_id] = [o].[object_id]
|
||||
LEFT JOIN [sys].[columns] AS [fccol]
|
||||
ON [fccol].[object_id] = [fc].[parent_object_id] AND [fccol].[column_id] = [fc].[parent_column_id]
|
||||
LEFT JOIN [sys].[columns] AS [ffccol]
|
||||
ON [ffccol].[object_id] = [fc].[referenced_object_id] AND [ffccol].[column_id] = [fc].[referenced_column_id]
|
||||
ORDER BY [kic].[key_ordinal] ASC, [fc].[constraint_column_id] ASC
|
||||
SQL;
|
||||
|
||||
$resolvedName = $this->resolveTableName($tableName);
|
||||
$constraints = $this->db->createCommand($sql, [
|
||||
':fullName' => $resolvedName->fullName,
|
||||
])->queryAll();
|
||||
$constraints = $this->normalizePdoRowKeyCase($constraints, true);
|
||||
$constraints = ArrayHelper::index($constraints, null, ['type', 'name']);
|
||||
$result = [
|
||||
'primaryKey' => null,
|
||||
'foreignKeys' => [],
|
||||
'uniques' => [],
|
||||
'checks' => [],
|
||||
'defaults' => [],
|
||||
];
|
||||
foreach ($constraints as $type => $names) {
|
||||
foreach ($names as $name => $constraint) {
|
||||
switch ($type) {
|
||||
case 'PK':
|
||||
$result['primaryKey'] = new Constraint([
|
||||
'name' => $name,
|
||||
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'),
|
||||
]);
|
||||
break;
|
||||
case 'F':
|
||||
$result['foreignKeys'][] = new ForeignKeyConstraint([
|
||||
'name' => $name,
|
||||
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'),
|
||||
'foreignSchemaName' => $constraint[0]['foreign_table_schema'],
|
||||
'foreignTableName' => $constraint[0]['foreign_table_name'],
|
||||
'foreignColumnNames' => ArrayHelper::getColumn($constraint, 'foreign_column_name'),
|
||||
'onDelete' => str_replace('_', '', $constraint[0]['on_delete']),
|
||||
'onUpdate' => str_replace('_', '', $constraint[0]['on_update']),
|
||||
]);
|
||||
break;
|
||||
case 'UQ':
|
||||
$result['uniques'][] = new Constraint([
|
||||
'name' => $name,
|
||||
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'),
|
||||
]);
|
||||
break;
|
||||
case 'C':
|
||||
$result['checks'][] = new CheckConstraint([
|
||||
'name' => $name,
|
||||
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'),
|
||||
'expression' => $constraint[0]['check_expr'],
|
||||
]);
|
||||
break;
|
||||
case 'D':
|
||||
$result['defaults'][] = new DefaultValueConstraint([
|
||||
'name' => $name,
|
||||
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'),
|
||||
'value' => $constraint[0]['default_expr'],
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($result as $type => $data) {
|
||||
$this->setTableMetadata($tableName, $type, $data);
|
||||
}
|
||||
|
||||
return $result[$returnType];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function quoteColumnName($name)
|
||||
{
|
||||
if (preg_match('/^\[.*\]$/', $name)) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
return parent::quoteColumnName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieving inserted data from a primary key request of type uniqueidentifier (for SQL Server 2005 or later)
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function insert($table, $columns)
|
||||
{
|
||||
$command = $this->db->createCommand()->insert($table, $columns);
|
||||
if (!$command->execute()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$isVersion2005orLater = version_compare($this->db->getSchema()->getServerVersion(), '9', '>=');
|
||||
$inserted = $isVersion2005orLater ? $command->pdoStatement->fetch() : [];
|
||||
|
||||
$tableSchema = $this->getTableSchema($table);
|
||||
$result = [];
|
||||
foreach ($tableSchema->primaryKey as $name) {
|
||||
// @see https://github.com/yiisoft/yii2/issues/13828 & https://github.com/yiisoft/yii2/issues/17474
|
||||
if (isset($inserted[$name])) {
|
||||
$result[$name] = $inserted[$name];
|
||||
} elseif ($tableSchema->columns[$name]->autoIncrement) {
|
||||
// for a version earlier than 2005
|
||||
$result[$name] = $this->getLastInsertID($tableSchema->sequenceName);
|
||||
} elseif (isset($columns[$name])) {
|
||||
$result[$name] = $columns[$name];
|
||||
} else {
|
||||
$result[$name] = $tableSchema->columns[$name]->defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createColumnSchemaBuilder($type, $length = null)
|
||||
{
|
||||
return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]);
|
||||
}
|
||||
}
|
||||
34
vendor/yiisoft/yii2/db/mssql/SqlsrvPDO.php
vendored
Normal file
34
vendor/yiisoft/yii2/db/mssql/SqlsrvPDO.php
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
/**
|
||||
* This is an extension of the default PDO class of SQLSRV driver.
|
||||
* It provides workarounds for improperly implemented functionalities of the SQLSRV driver.
|
||||
*
|
||||
* @author Timur Ruziev <resurtm@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class SqlsrvPDO extends \PDO
|
||||
{
|
||||
/**
|
||||
* Returns value of the last inserted ID.
|
||||
*
|
||||
* SQLSRV driver implements [[PDO::lastInsertId()]] method but with a single peculiarity:
|
||||
* when `$sequence` value is a null or an empty string it returns an empty string.
|
||||
* But when parameter is not specified it works as expected and returns actual
|
||||
* last inserted ID (like the other PDO drivers).
|
||||
* @param string|null $sequence the sequence name. Defaults to null.
|
||||
* @return string|false last inserted ID value.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function lastInsertId($sequence = null)
|
||||
{
|
||||
return !$sequence ? parent::lastInsertId() : parent::lastInsertId($sequence);
|
||||
}
|
||||
}
|
||||
23
vendor/yiisoft/yii2/db/mssql/TableSchema.php
vendored
Normal file
23
vendor/yiisoft/yii2/db/mssql/TableSchema.php
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql;
|
||||
|
||||
/**
|
||||
* TableSchema represents the metadata of a database table.
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class TableSchema extends \yii\db\TableSchema
|
||||
{
|
||||
/**
|
||||
* @var string|null name of the catalog (database) that this table belongs to.
|
||||
* Defaults to null, meaning no catalog (or the current database).
|
||||
*/
|
||||
public $catalogName;
|
||||
}
|
||||
65
vendor/yiisoft/yii2/db/mssql/conditions/InConditionBuilder.php
vendored
Normal file
65
vendor/yiisoft/yii2/db/mssql/conditions/InConditionBuilder.php
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql\conditions;
|
||||
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\db\Expression;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
|
||||
* @since 2.0.14
|
||||
*/
|
||||
class InConditionBuilder extends \yii\db\conditions\InConditionBuilder
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws NotSupportedException if `$columns` is an array
|
||||
*/
|
||||
protected function buildSubqueryInCondition($operator, $columns, $values, &$params)
|
||||
{
|
||||
if (is_array($columns)) {
|
||||
throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
|
||||
}
|
||||
|
||||
return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildCompositeInCondition($operator, $columns, $values, &$params)
|
||||
{
|
||||
$quotedColumns = [];
|
||||
foreach ($columns as $i => $column) {
|
||||
if ($column instanceof Expression) {
|
||||
$column = $column->expression;
|
||||
}
|
||||
$quotedColumns[$i] = strpos($column, '(') === false ? $this->queryBuilder->db->quoteColumnName($column) : $column;
|
||||
}
|
||||
$vss = [];
|
||||
foreach ($values as $value) {
|
||||
$vs = [];
|
||||
foreach ($columns as $i => $column) {
|
||||
if ($column instanceof Expression) {
|
||||
$column = $column->expression;
|
||||
}
|
||||
if (isset($value[$column])) {
|
||||
$phName = $this->queryBuilder->bindParam($value[$column], $params);
|
||||
$vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
|
||||
} else {
|
||||
$vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
|
||||
}
|
||||
}
|
||||
$vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
|
||||
}
|
||||
|
||||
return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
|
||||
}
|
||||
}
|
||||
25
vendor/yiisoft/yii2/db/mssql/conditions/LikeConditionBuilder.php
vendored
Normal file
25
vendor/yiisoft/yii2/db/mssql/conditions/LikeConditionBuilder.php
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\db\mssql\conditions;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
class LikeConditionBuilder extends \yii\db\conditions\LikeConditionBuilder
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $escapingReplacements = [
|
||||
'%' => '[%]',
|
||||
'_' => '[_]',
|
||||
'[' => '[[]',
|
||||
']' => '[]]',
|
||||
'\\' => '[\\]',
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user