Yii2 Bootstrap 3
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.

254 lines
7.2 KiB

13 years ago
<?php
/**
13 years ago
* Schema class file.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
13 years ago
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\db\dao;
13 years ago
use yii\db\Exception;
13 years ago
/**
13 years ago
* Schema is the base class for retrieving metadata information.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
abstract class Schema extends \yii\base\Object
13 years ago
{
13 years ago
const TYPE_PK = 'pk';
const TYPE_STRING = 'string';
const TYPE_TEXT = 'text';
const TYPE_SMALLINT = 'smallint';
const TYPE_INTEGER = 'integer';
const TYPE_BIGINT = 'bigint';
const TYPE_FLOAT = 'float';
const TYPE_DECIMAL = 'decimal';
const TYPE_DATETIME = 'datetime';
const TYPE_TIMESTAMP = 'timestamp';
const TYPE_TIME = 'time';
const TYPE_DATE = 'date';
const TYPE_BINARY = 'binary';
const TYPE_BOOLEAN = 'boolean';
const TYPE_MONEY = 'money';
13 years ago
/**
* @var \yii\db\dao\Connection the database connection
*/
13 years ago
public $connection;
13 years ago
private $_tableNames = array();
private $_tables = array();
private $_builder;
/**
* Loads the metadata for the specified table.
* @param string $name table name
13 years ago
* @return TableSchema driver dependent table metadata, null if the table does not exist.
13 years ago
*/
13 years ago
abstract protected function loadTableSchema($name);
13 years ago
/**
* Constructor.
13 years ago
* @param Connection $connection database connection.
13 years ago
*/
13 years ago
public function __construct($connection)
13 years ago
{
13 years ago
$this->connection = $connection;
13 years ago
}
/**
* Obtains the metadata for the named table.
13 years ago
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
13 years ago
* @return TableSchema table metadata. Null if the named table does not exist.
13 years ago
*/
13 years ago
public function getTableSchema($name)
13 years ago
{
13 years ago
if (isset($this->_tables[$name])) {
13 years ago
return $this->_tables[$name];
13 years ago
}
13 years ago
13 years ago
if (strpos($name, '{{') !== false) {
13 years ago
$realName = preg_replace('/\{\{(.*?)\}\}/', $this->connection->tablePrefix . '$1', $name);
13 years ago
} else {
13 years ago
$realName = $name;
}
13 years ago
$db = $this->connection;
13 years ago
13 years ago
// temporarily disable query caching
if ($db->queryCachingDuration >= 0) {
$qcDuration = $db->queryCachingDuration;
$db->queryCachingDuration = -1;
}
if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) {
13 years ago
$key = __CLASS__ . "/{$db->dsn}/{$db->username}/{$name}";
13 years ago
if (($table = $cache->get($key)) === false) {
$table = $this->loadTableSchema($realName);
if ($table !== null) {
$cache->set($key, $table, $db->schemaCachingDuration);
13 years ago
}
}
13 years ago
$this->_tables[$name] = $table;
13 years ago
} else {
13 years ago
$this->_tables[$name] = $table = $this->loadTableSchema($realName);
}
13 years ago
13 years ago
if (isset($qcDuration)) { // re-enable query caching
$db->queryCachingDuration = $qcDuration;
13 years ago
}
13 years ago
return $table;
13 years ago
}
/**
* Returns the metadata for all tables in the database.
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
* @return array the metadata for all tables in the database.
* Each array element is an instance of {@link CDbTableSchema} (or its child class).
*/
13 years ago
public function getTableSchemas($schema = '')
13 years ago
{
$tables = array();
13 years ago
foreach ($this->getTableNames($schema) as $name) {
if (($table = $this->getTableSchema($name)) !== null) {
$tables[] = $table;
}
13 years ago
}
return $tables;
}
/**
* Returns all table names in the database.
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
* If not empty, the returned table names will be prefixed with the schema name.
* @return array all table names in the database.
*/
public function getTableNames($schema = '')
{
13 years ago
if (!isset($this->_tableNames[$schema])) {
13 years ago
$this->_tableNames[$schema] = $this->findTableNames($schema);
13 years ago
}
13 years ago
return $this->_tableNames[$schema];
}
/**
13 years ago
* @return QueryBuilder the query builder for this connection.
13 years ago
*/
13 years ago
public function getQueryBuilder()
13 years ago
{
13 years ago
if ($this->_builder === null) {
$this->_builder = $this->createQueryBuilder();
}
return $this->_builder;
13 years ago
}
/**
* Refreshes the schema.
* This method resets the loaded table metadata and command builder
* so that they can be recreated to reflect the change of schema.
*/
public function refresh()
{
13 years ago
$db = $this->connection;
13 years ago
if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) {
foreach ($this->_tables as $name => $table) {
$key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}";
$cache->delete($key);
13 years ago
}
}
$this->_tables = array();
$this->_tableNames = array();
}
/**
* Quotes a table name for use in a query.
* If the table name contains schema prefix, the prefix will also be properly quoted.
* @param string $name table name
* @return string the properly quoted table name
* @see quoteSimpleTableName
*/
public function quoteTableName($name)
{
13 years ago
if (strpos($name, '.') === false) {
13 years ago
return $this->quoteSimpleTableName($name);
13 years ago
}
13 years ago
$parts = explode('.', $name);
13 years ago
foreach ($parts as $i => $part) {
13 years ago
$parts[$i] = $this->quoteSimpleTableName($part);
13 years ago
}
13 years ago
return implode('.', $parts);
}
/**
* Quotes a simple table name for use in a query.
* A simple table name does not schema prefix.
* @param string $name table name
* @return string the properly quoted table name
*/
public function quoteSimpleTableName($name)
{
13 years ago
return strpos($name, "'") !== false ? $name : "'" . $name . "'";
13 years ago
}
/**
* Quotes a column name for use in a query.
* If the column name contains prefix, the prefix will also be properly quoted.
* @param string $name column name
* @return string the properly quoted column name
* @see quoteSimpleColumnName
*/
public function quoteColumnName($name)
{
13 years ago
if (($pos = strrpos($name, '.')) !== false) {
13 years ago
$prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.';
$name = substr($name, $pos + 1);
13 years ago
} else
13 years ago
{
13 years ago
$prefix = '';
13 years ago
}
13 years ago
return $prefix . $this->quoteSimpleColumnName($name);
13 years ago
}
/**
* Quotes a simple column name for use in a query.
* A simple column name does not contain prefix.
* @param string $name column name
* @return string the properly quoted column name
*/
public function quoteSimpleColumnName($name)
{
13 years ago
return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"';
13 years ago
}
/**
13 years ago
* Creates a query builder for the database.
* This method may be overridden by child classes to create a DBMS-specific query builder.
* @return QueryBuilder query builder instance
13 years ago
*/
13 years ago
public function createQueryBuilder()
13 years ago
{
13 years ago
return new QueryBuilder($this);
13 years ago
}
/**
* 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.
* If not empty, the returned table names will be prefixed with the schema name.
* @return array all table names in the database.
*/
protected function findTableNames($schema = '')
{
13 years ago
throw new Exception(get_class($this) . 'does not support fetching all table names.');
13 years ago
}
}