Schema.php 6.65 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
/**
w  
Qiang Xue committed
14
 * Schema is the base class for retrieving metadata information.
w  
Qiang Xue committed
15 16
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
17
 * @since 2.0
w  
Qiang Xue committed
18
 */
w  
Qiang Xue committed
19
abstract class Schema extends \yii\base\Component
w  
Qiang Xue committed
20
{
w  
Qiang Xue committed
21 22
	public $connection;

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

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

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

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

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

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

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

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

		return $table;
w  
Qiang Xue committed
88 89 90 91 92 93 94 95
	}

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

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

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

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

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

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

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

}