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.
323 lines
10 KiB
323 lines
10 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
13 years ago
|
* Driver class file.
|
||
14 years ago
|
*
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright © 2008 Yii Software LLC
|
||
14 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\db;
|
||
14 years ago
|
|
||
14 years ago
|
use yii\db\Exception;
|
||
|
|
||
14 years ago
|
/**
|
||
12 years ago
|
* Schema is the base class for concrete DBMS-specific schema classes.
|
||
13 years ago
|
*
|
||
12 years ago
|
* Schema represents the database schema information that is DBMS specific.
|
||
13 years ago
|
*
|
||
12 years ago
|
* @property QueryBuilder $queryBuilder the query builder for the DBMS represented by this schema
|
||
13 years ago
|
* @property array $tableNames the names of all tables in this database.
|
||
12 years ago
|
* @property array $tableSchemas the schema information for all tables in this database.
|
||
14 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
14 years ago
|
* @since 2.0
|
||
14 years ago
|
*/
|
||
12 years ago
|
abstract class Schema extends \yii\base\Object
|
||
14 years ago
|
{
|
||
13 years ago
|
/**
|
||
13 years ago
|
* The followings are the supported abstract column data types.
|
||
|
*/
|
||
13 years ago
|
const TYPE_PK = 'pk';
|
||
13 years ago
|
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 Connection the database connection
|
||
13 years ago
|
*/
|
||
14 years ago
|
public $connection;
|
||
13 years ago
|
/**
|
||
|
* @var array list of ALL table names in the database
|
||
|
*/
|
||
14 years ago
|
private $_tableNames = array();
|
||
13 years ago
|
/**
|
||
13 years ago
|
* @var array list of loaded table metadata (table name => TableSchema)
|
||
13 years ago
|
*/
|
||
14 years ago
|
private $_tables = array();
|
||
13 years ago
|
/**
|
||
|
* @var QueryBuilder the query builder for this database
|
||
|
*/
|
||
14 years ago
|
private $_builder;
|
||
|
|
||
|
/**
|
||
|
* Loads the metadata for the specified table.
|
||
|
* @param string $name table name
|
||
13 years ago
|
* @return TableSchema DBMS-dependent table metadata, null if the table does not exist.
|
||
14 years ago
|
*/
|
||
14 years ago
|
abstract protected function loadTableSchema($name);
|
||
14 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Obtains the metadata for the named table.
|
||
14 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
|
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
|
||
13 years ago
|
* @return TableSchema table metadata. Null if the named table does not exist.
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function getTableSchema($name, $refresh = false)
|
||
14 years ago
|
{
|
||
13 years ago
|
if (isset($this->_tables[$name]) && !$refresh) {
|
||
14 years ago
|
return $this->_tables[$name];
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
$db = $this->connection;
|
||
12 years ago
|
$realName = $this->getRealTableName($name);
|
||
13 years ago
|
|
||
12 years ago
|
/** @var $cache \yii\caching\Cache */
|
||
|
if ($db->enableSchemaCache && ($cache = \Yii::$application->getComponent($db->schemaCacheID)) !== null && !in_array($name, $db->schemaCacheExclude, true)) {
|
||
13 years ago
|
$key = $this->getCacheKey($name);
|
||
|
if ($refresh || ($table = $cache->get($key)) === false) {
|
||
14 years ago
|
$table = $this->loadTableSchema($realName);
|
||
|
if ($table !== null) {
|
||
12 years ago
|
$cache->set($key, $table, $db->schemaCacheDuration);
|
||
14 years ago
|
}
|
||
|
}
|
||
14 years ago
|
$this->_tables[$name] = $table;
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$this->_tables[$name] = $table = $this->loadTableSchema($realName);
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
return $table;
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Returns the cache key for the specified table name.
|
||
|
* @param string $name the table name
|
||
|
* @return string the cache key
|
||
|
*/
|
||
|
public function getCacheKey($name)
|
||
|
{
|
||
|
return __CLASS__ . "/{$this->connection->dsn}/{$this->connection->username}/{$name}";
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 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.
|
||
13 years ago
|
* Each array element is an instance of [[TableSchema]] (or its child class).
|
||
14 years ago
|
*/
|
||
14 years ago
|
public function getTableSchemas($schema = '')
|
||
14 years ago
|
{
|
||
|
$tables = array();
|
||
14 years ago
|
foreach ($this->getTableNames($schema) as $name) {
|
||
|
if (($table = $this->getTableSchema($name)) !== null) {
|
||
|
$tables[] = $table;
|
||
|
}
|
||
14 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.
|
||
13 years ago
|
* @param boolean $refresh whether to fetch the latest available table names. If this is false,
|
||
|
* table names fetched previously (if available) will be returned.
|
||
14 years ago
|
* @return array all table names in the database.
|
||
|
*/
|
||
13 years ago
|
public function getTableNames($schema = '', $refresh = false)
|
||
14 years ago
|
{
|
||
13 years ago
|
if (!isset($this->_tableNames[$schema]) || $refresh) {
|
||
14 years ago
|
$this->_tableNames[$schema] = $this->findTableNames($schema);
|
||
14 years ago
|
}
|
||
14 years ago
|
return $this->_tableNames[$schema];
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* @return QueryBuilder the query builder for this connection.
|
||
14 years ago
|
*/
|
||
14 years ago
|
public function getQueryBuilder()
|
||
14 years ago
|
{
|
||
14 years ago
|
if ($this->_builder === null) {
|
||
|
$this->_builder = $this->createQueryBuilder();
|
||
|
}
|
||
|
return $this->_builder;
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Refreshes the schema.
|
||
12 years ago
|
* This method cleans up all cached table schemas so that they can be re-created later
|
||
|
* to reflect the database schema change.
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function refresh()
|
||
14 years ago
|
{
|
||
12 years ago
|
/** @var $cache \yii\caching\Cache */
|
||
12 years ago
|
if ($this->connection->enableSchemaCache && ($cache = \Yii::$application->getComponent($this->connection->schemaCacheID)) !== null) {
|
||
|
foreach ($this->_tables as $name => $table) {
|
||
|
$cache->delete($this->getCacheKey($name));
|
||
14 years ago
|
}
|
||
|
}
|
||
12 years ago
|
$this->_tables = array();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
public function createQueryBuilder()
|
||
|
{
|
||
|
return new QueryBuilder($this->connection);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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 = '')
|
||
|
{
|
||
|
throw new Exception(get_class($this) . ' does not support fetching all table names.');
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns the ID of the last inserted row or sequence value.
|
||
|
* @param string $sequenceName name of the sequence object (required by some DBMS)
|
||
|
* @return string the row ID of the last row inserted, or the last value retrieved from the sequence object
|
||
|
* @see http://www.php.net/manual/en/function.PDO-lastInsertId.php
|
||
|
*/
|
||
|
public function getLastInsertID($sequenceName = '')
|
||
|
{
|
||
|
if ($this->connection->isActive) {
|
||
|
return $this->connection->pdo->lastInsertId($sequenceName);
|
||
|
} else {
|
||
|
throw new Exception('DB Connection is not active.');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Quotes a string value for use in a query.
|
||
|
* Note that if the parameter is not a string, it will be returned without change.
|
||
|
* @param string $str string to be quoted
|
||
|
* @return string the properly quoted string
|
||
|
* @see http://www.php.net/manual/en/function.PDO-quote.php
|
||
|
*/
|
||
|
public function quoteValue($str)
|
||
|
{
|
||
|
if (!is_string($str)) {
|
||
|
return $str;
|
||
|
}
|
||
|
|
||
|
$this->connection->open();
|
||
|
if (($value = $this->connection->pdo->quote($str)) !== false) {
|
||
|
return $value;
|
||
|
} else { // the driver doesn't support quote (e.g. oci)
|
||
|
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Quotes a table name for use in a query.
|
||
|
* If the table name contains schema prefix, the prefix will also be properly quoted.
|
||
12 years ago
|
* If the table name is already quoted or contains special characters including '(', '[[' and '{{',
|
||
|
* then this method will do nothing.
|
||
14 years ago
|
* @param string $name table name
|
||
|
* @return string the properly quoted table name
|
||
|
* @see quoteSimpleTableName
|
||
|
*/
|
||
|
public function quoteTableName($name)
|
||
|
{
|
||
12 years ago
|
if (strpos($name, '(') !== false || strpos($name, '[[') !== false || strpos($name, '{{') !== false) {
|
||
|
return $name;
|
||
|
}
|
||
14 years ago
|
if (strpos($name, '.') === false) {
|
||
14 years ago
|
return $this->quoteSimpleTableName($name);
|
||
14 years ago
|
}
|
||
14 years ago
|
$parts = explode('.', $name);
|
||
14 years ago
|
foreach ($parts as $i => $part) {
|
||
14 years ago
|
$parts[$i] = $this->quoteSimpleTableName($part);
|
||
14 years ago
|
}
|
||
14 years ago
|
return implode('.', $parts);
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Quotes a column name for use in a query.
|
||
|
* If the column name contains prefix, the prefix will also be properly quoted.
|
||
12 years ago
|
* If the column name is already quoted or contains special characters including '(', '[[' and '{{',
|
||
|
* then this method will do nothing.
|
||
14 years ago
|
* @param string $name column name
|
||
|
* @return string the properly quoted column name
|
||
|
* @see quoteSimpleColumnName
|
||
|
*/
|
||
|
public function quoteColumnName($name)
|
||
|
{
|
||
12 years ago
|
if (strpos($name, '(') !== false || strpos($name, '[[') !== false || strpos($name, '{{') !== false) {
|
||
|
return $name;
|
||
|
}
|
||
14 years ago
|
if (($pos = strrpos($name, '.')) !== false) {
|
||
14 years ago
|
$prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.';
|
||
|
$name = substr($name, $pos + 1);
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$prefix = '';
|
||
13 years ago
|
}
|
||
14 years ago
|
return $prefix . $this->quoteSimpleColumnName($name);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Quotes a simple table name for use in a query.
|
||
|
* A simple table name should contain the table name only without any schema prefix.
|
||
|
* If the table name is already quoted, this method will do nothing.
|
||
|
* @param string $name table name
|
||
|
* @return string the properly quoted table name
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function quoteSimpleTableName($name)
|
||
14 years ago
|
{
|
||
12 years ago
|
return strpos($name, "'") !== false ? $name : "'" . $name . "'";
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Quotes a simple column name for use in a query.
|
||
|
* A simple column name should contain the column name only without any prefix.
|
||
|
* If the column name is already quoted or is the asterisk character '*', this method will do nothing.
|
||
|
* @param string $name column name
|
||
|
* @return string the properly quoted column name
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function quoteSimpleColumnName($name)
|
||
14 years ago
|
{
|
||
12 years ago
|
return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"';
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns the real name of a table name.
|
||
|
* This method will strip off curly brackets from the given table name
|
||
|
* and replace the percentage character in the name with [[Connection::tablePrefix]].
|
||
|
* @param string $name the table name to be converted
|
||
|
* @return string the real name of the given table name
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function getRealTableName($name)
|
||
14 years ago
|
{
|
||
12 years ago
|
if ($this->connection->enableAutoQuoting && strpos($name, '{{') !== false) {
|
||
|
$name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name);
|
||
|
return str_replace('%', $this->connection->tablePrefix, $name);
|
||
|
} else {
|
||
|
return $name;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
}
|