Schema.php 6.67 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * Schema class file.
w  
Qiang Xue committed
4 5 6 7 8 9 10
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
11 12
namespace yii\db\dao;

w  
Qiang Xue committed
13 14
use yii\db\Exception;

w  
Qiang Xue committed
15
/**
w  
Qiang Xue committed
16
 * Schema is the base class for retrieving metadata information.
w  
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
19
 * @since 2.0
w  
Qiang Xue committed
20
 */
Qiang Xue committed
21
abstract class Schema extends \yii\base\Object
w  
Qiang Xue committed
22
{
w  
Qiang Xue committed
23 24
	public $connection;

w  
Qiang Xue committed
25 26 27 28 29 30 31
	private $_tableNames = array();
	private $_tables = array();
	private $_builder;

	/**
	 * Loads the metadata for the specified table.
	 * @param string $name table name
w  
Qiang Xue committed
32
	 * @return TableSchema driver dependent table metadata, null if the table does not exist.
w  
Qiang Xue committed
33
	 */
w  
Qiang Xue committed
34
	abstract protected function loadTableSchema($name);
w  
Qiang Xue committed
35 36 37

	/**
	 * Constructor.
Alexander Makarov committed
38
	 * @param CDbConnection $connection database connection.
w  
Qiang Xue committed
39
	 */
w  
Qiang Xue committed
40
	public function __construct($connection)
w  
Qiang Xue committed
41
	{
w  
Qiang Xue committed
42
		$this->connection = $connection;
w  
Qiang Xue committed
43 44 45 46
	}

	/**
	 * Obtains the metadata for the named table.
w  
Qiang Xue committed
47
	 * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
w  
Qiang Xue committed
48 49
	 * @return CDbTableSchema table metadata. Null if the named table does not exist.
	 */
w  
Qiang Xue committed
50
	public function getTableSchema($name)
w  
Qiang Xue committed
51
	{
w  
Qiang Xue committed
52
		if (isset($this->_tables[$name])) {
w  
Qiang Xue committed
53
			return $this->_tables[$name];
w  
Qiang Xue committed
54
		}
w  
Qiang Xue committed
55

w  
Qiang Xue committed
56
		if (strpos($name, '{{') !== false) {
w  
Qiang Xue committed
57
			$realName = preg_replace('/\{\{(.*?)\}\}/', $this->connection->tablePrefix . '$1', $name);
w  
Qiang Xue committed
58 59 60 61
		}
		else {
			$realName = $name;
		}
w  
Qiang Xue committed
62 63

		$db = $this->connection;
w  
Qiang Xue committed
64

w  
Qiang Xue committed
65 66 67 68 69 70 71
		// 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) {
w  
Qiang Xue committed
72
			$key = __CLASS__ . "/{$db->dsn}/{$db->username}/{$name}";
w  
Qiang Xue committed
73 74 75 76
			if (($table = $cache->get($key)) === false) {
				$table = $this->loadTableSchema($realName);
				if ($table !== null) {
					$cache->set($key, $table, $db->schemaCachingDuration);
w  
Qiang Xue committed
77 78
				}
			}
w  
Qiang Xue committed
79 80 81 82 83
			$this->_tables[$name] = $table;
		}
		else {
			$this->_tables[$name] = $table = $this->loadTableSchema($realName);
		}
w  
Qiang Xue committed
84

w  
Qiang Xue committed
85 86
		if (isset($qcDuration)) { // re-enable query caching
			$db->queryCachingDuration = $qcDuration;
w  
Qiang Xue committed
87
		}
w  
Qiang Xue committed
88 89

		return $table;
w  
Qiang Xue committed
90 91 92 93 94 95 96 97
	}

	/**
	 * 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).
	 */
w  
Qiang Xue committed
98
	public function getTableSchemas($schema = '')
w  
Qiang Xue committed
99 100
	{
		$tables = array();
w  
Qiang Xue committed
101 102 103 104
		foreach ($this->getTableNames($schema) as $name) {
			if (($table = $this->getTableSchema($name)) !== null) {
				$tables[] = $table;
			}
w  
Qiang Xue committed
105 106 107 108 109 110 111 112 113 114 115 116
		}
		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 = '')
	{
w  
Qiang Xue committed
117
		if (!isset($this->_tableNames[$schema])) {
w  
Qiang Xue committed
118
			$this->_tableNames[$schema] = $this->findTableNames($schema);
w  
Qiang Xue committed
119
		}
w  
Qiang Xue committed
120 121 122 123
		return $this->_tableNames[$schema];
	}

	/**
w  
Qiang Xue committed
124
	 * @return QueryBuilder the query builder for this connection.
w  
Qiang Xue committed
125
	 */
w  
Qiang Xue committed
126
	public function getQueryBuilder()
w  
Qiang Xue committed
127
	{
w  
Qiang Xue committed
128 129 130 131
		if ($this->_builder === null) {
			$this->_builder = $this->createQueryBuilder();
		}
		return $this->_builder;
w  
Qiang Xue committed
132 133 134 135 136 137 138 139 140
	}

	/**
	 * 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()
	{
w  
Qiang Xue committed
141
		$db = $this->connection;
w  
Qiang Xue committed
142 143 144 145
		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);
w  
Qiang Xue committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
			}
		}
		$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)
	{
w  
Qiang Xue committed
161
		if (strpos($name, '.') === false) {
w  
Qiang Xue committed
162
			return $this->quoteSimpleTableName($name);
w  
Qiang Xue committed
163
		}
w  
Qiang Xue committed
164
		$parts = explode('.', $name);
w  
Qiang Xue committed
165
		foreach ($parts as $i => $part) {
w  
Qiang Xue committed
166
			$parts[$i] = $this->quoteSimpleTableName($part);
w  
Qiang Xue committed
167
		}
w  
Qiang Xue committed
168 169 170 171 172 173 174 175 176 177 178 179
		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)
	{
w  
Qiang Xue committed
180
		return strpos($name, "'") !== false ? $name : "'" . $name . "'";
w  
Qiang Xue committed
181 182 183 184 185 186 187 188 189 190 191
	}

	/**
	 * 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)
	{
w  
Qiang Xue committed
192
		if (($pos = strrpos($name, '.')) !== false) {
w  
Qiang Xue committed
193 194 195 196 197
			$prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.';
			$name = substr($name, $pos + 1);
		}
		else
			$prefix = '';
w  
Qiang Xue committed
198
		return $prefix . $this->quoteSimpleColumnName($name);
w  
Qiang Xue committed
199 200 201 202 203 204 205 206 207 208
	}

	/**
	 * 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)
	{
w  
Qiang Xue committed
209
		return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"';
w  
Qiang Xue committed
210 211 212
	}

	/**
w  
Qiang Xue committed
213 214 215
	 * 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
w  
Qiang Xue committed
216
	 */
w  
Qiang Xue committed
217
	public function createQueryBuilder()
w  
Qiang Xue committed
218
	{
w  
Qiang Xue committed
219
		return new QueryBuilder($this);
w  
Qiang Xue committed
220 221 222 223 224 225 226 227 228 229 230 231
	}

	/**
	 * 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 = '')
	{
w  
Qiang Xue committed
232
		throw new Exception(get_class($this) . 'does not support fetching all table names.');
w  
Qiang Xue committed
233 234 235
	}

}