Browse Source

Fixes #13571: Fix `yii\db\mssql\QueryBuilder::checkIntegrity` for all tables

tags/2.0.12
Bob Olde Hampsink 8 years ago committed by Alexander Makarov
parent
commit
0ea45c78a8
  1. 2
      framework/CHANGELOG.md
  2. 47
      framework/db/ViewFinderTrait.php
  3. 24
      framework/db/mssql/QueryBuilder.php
  4. 22
      framework/db/mssql/Schema.php
  5. 2
      framework/db/pgsql/QueryBuilder.php
  6. 31
      framework/db/pgsql/Schema.php

2
framework/CHANGELOG.md

@ -4,6 +4,8 @@ Yii Framework 2 Change Log
2.0.12 under development
--------------------------
- Bug #13571: Fix `yii\db\mssql\QueryBuilder::checkIntegrity` for all tables (boboldehampsink)
- Bug #11230: Include `defaultRoles` in `yii\rbac\DbManager->getRolesByUser()` results (developeruz)
- Bug #11404: `yii\base\Model::loadMultiple()` returns true even if `yii\base\Model::load()` returns false (zvook)
- Bug #13306: Wildcard in `reloadableScripts` in `yii.js` allows 0 characters (arogachev)

47
framework/db/ViewFinderTrait.php

@ -0,0 +1,47 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
/**
* ViewFinderTrait implements the method getViewNames for finding views in a database.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Bob Olde Hampsink <b.oldehampsink@nerds.company>
* @since 2.0.12
*/
trait ViewFinderTrait
{
/**
* @var array list of ALL view names in the database
*/
private $_viewNames = [];
/**
* Returns all views names in the database.
* @param string $schema the schema of the views. Defaults to empty string, meaning the current or default schema.
* @return array all views names in the database. The names have NO schema name prefix.
*/
abstract protected function findViewNames($schema = '');
/**
* Returns all view names in the database.
* @param string $schema the schema of the views. Defaults to empty string, meaning the current or default schema name.
* If not empty, the returned view names will be prefixed with the schema name.
* @param bool $refresh whether to fetch the latest available view names. If this is false,
* view names fetched previously (if available) will be returned.
* @return string[] all view names in the database.
*/
public function getViewNames($schema = '', $refresh = false)
{
if (!isset($this->_viewNames[$schema]) || $refresh) {
$this->_viewNames[$schema] = $this->findViewNames($schema);
}
return $this->_viewNames[$schema];
}
}

24
framework/db/mssql/QueryBuilder.php

@ -198,23 +198,25 @@ class QueryBuilder extends \yii\db\QueryBuilder
/**
* 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. Defaults to empty string, meaning the current or default schema.
* @param string $table the table name. Defaults to empty string, meaning that no table will be changed.
* @param string $schema the schema of the tables.
* @param string $table the table name.
* @return string the SQL statement for checking integrity
* @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
*/
public function checkIntegrity($check = true, $schema = '', $table = '')
{
if ($schema !== '') {
$table = "{$schema}.{$table}";
}
$table = $this->db->quoteTableName($table);
if ($this->db->getTableSchema($table) === null) {
throw new InvalidParamException("Table not found: $table");
}
$enable = $check ? 'CHECK' : 'NOCHECK';
$schema = $schema ? $schema : $this->db->getSchema()->defaultSchema;
$tableNames = $this->db->getTableSchema($table) ? [$table] : $this->db->getSchema()->getTableNames($schema);
$viewNames = $this->db->getSchema()->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 "ALTER TABLE {$table} {$enable} CONSTRAINT ALL";
return $command;
}
/**

22
framework/db/mssql/Schema.php

@ -8,6 +8,7 @@
namespace yii\db\mssql;
use yii\db\ColumnSchema;
use yii\db\ViewFinderTrait;
/**
* Schema is the class for retrieving metadata from a MS SQL Server databases (version 2008 and above).
@ -17,6 +18,8 @@ use yii\db\ColumnSchema;
*/
class Schema extends \yii\db\Schema
{
use ViewFinderTrait;
/**
* @var string the default schema used for the current session.
*/
@ -412,6 +415,25 @@ SQL;
}
/**
* @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:
*

2
framework/db/pgsql/QueryBuilder.php

@ -195,7 +195,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
$command = '';
foreach ($tableNames as $tableName) {
$tableName = '"' . $schema . '"."' . $tableName . '"';
$tableName = $this->db->quoteTableName("{$schema}.{$tableName}");
$command .= "ALTER TABLE $tableName $enable TRIGGER ALL; ";
}

31
framework/db/pgsql/Schema.php

@ -10,6 +10,7 @@ namespace yii\db\pgsql;
use yii\db\Expression;
use yii\db\TableSchema;
use yii\db\ColumnSchema;
use yii\db\ViewFinderTrait;
/**
* Schema is the class for retrieving metadata from a PostgreSQL database
@ -22,6 +23,8 @@ use yii\db\ColumnSchema;
*/
class Schema extends \yii\db\Schema
{
use ViewFinderTrait;
/**
* @var string the default schema used for the current session.
*/
@ -110,11 +113,6 @@ class Schema extends \yii\db\Schema
'xml' => self::TYPE_STRING,
];
/**
* @var array list of ALL view names in the database
*/
private $_viewNames = [];
/**
* Creates a query builder for the PostgreSQL database.
@ -212,10 +210,7 @@ SQL;
}
/**
* Returns all views names in the database.
* @param string $schema the schema of the views. Defaults to empty string, meaning the current or default schema.
* @return array all views names in the database. The names have NO schema name prefix.
* @since 2.0.9
* @inheritdoc
*/
protected function findViewNames($schema = '')
{
@ -233,24 +228,6 @@ SQL;
}
/**
* Returns all view names in the database.
* @param string $schema the schema of the views. Defaults to empty string, meaning the current or default schema name.
* If not empty, the returned view names will be prefixed with the schema name.
* @param bool $refresh whether to fetch the latest available view names. If this is false,
* view names fetched previously (if available) will be returned.
* @return string[] all view names in the database.
* @since 2.0.9
*/
public function getViewNames($schema = '', $refresh = false)
{
if (!isset($this->_viewNames[$schema]) || $refresh) {
$this->_viewNames[$schema] = $this->findViewNames($schema);
}
return $this->_viewNames[$schema];
}
/**
* Collects the foreign key column details for the given table.
* @param TableSchema $table the table metadata
*/

Loading…
Cancel
Save