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.
		
		
		
		
			
				
					542 lines
				
				18 KiB
			
		
		
			
		
	
	
					542 lines
				
				18 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\db;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | use PDO;
 | ||
|  | use Yii;
 | ||
| 
											13 years ago
										 | use yii\base\Component;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
|  | use yii\base\NotSupportedException;
 | ||
| 
											13 years ago
										 | use yii\caching\Cache;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											14 years ago
										 |  * Connection represents a connection to a database via [PDO](http://www.php.net/manual/en/ref.pdo.php).
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * Connection works together with [[Command]], [[DataReader]] and [[Transaction]]
 | ||
|  |  * to provide data access to various DBMS in a common set of APIs. They are a thin wrapper
 | ||
|  |  * of the [[PDO PHP extension]](http://www.php.net/manual/en/ref.pdo.php).
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * To establish a DB connection, set [[dsn]], [[username]] and [[password]], and then
 | ||
| 
											13 years ago
										 |  * call [[open()]] to be true.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * The following example shows how to create a Connection instance and establish
 | ||
| 
											14 years ago
										 |  * the DB connection:
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
| 
											13 years ago
										 |  * $connection = new \yii\db\Connection(array(
 | ||
| 
											14 years ago
										 |  *     'dsn' => $dsn,
 | ||
|  |  *     'username' => $username,
 | ||
|  |  *     'password' => $password,
 | ||
| 
											14 years ago
										 |  * ));
 | ||
| 
											13 years ago
										 |  * $connection->open();
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
|  |  *
 | ||
| 
											14 years ago
										 |  * After the DB connection is established, one can execute SQL statements like the following:
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * ~~~
 | ||
|  |  * $command = $connection->createCommand('SELECT * FROM tbl_post');
 | ||
| 
											14 years ago
										 |  * $posts = $command->queryAll();
 | ||
|  |  * $command = $connection->createCommand('UPDATE tbl_post SET status=1');
 | ||
|  |  * $command->execute();
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
|  |  *
 | ||
| 
											14 years ago
										 |  * One can also do prepared SQL execution and bind parameters to the prepared SQL.
 | ||
|  |  * When the parameters are coming from user input, you should use this approach
 | ||
|  |  * to prevent SQL injection attacks. The following is an example:
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
|  |  * $command = $connection->createCommand('SELECT * FROM tbl_post WHERE id=:id');
 | ||
|  |  * $command->bindValue(':id', $_GET['id']);
 | ||
|  |  * $post = $command->query();
 | ||
|  |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * For more information about how to perform various DB queries, please refer to [[Command]].
 | ||
|  |  *
 | ||
|  |  * If the underlying DBMS supports transactions, you can perform transactional SQL queries
 | ||
|  |  * like the following:
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
|  |  * $transaction = $connection->beginTransaction();
 | ||
|  |  * try {
 | ||
| 
											13 years ago
										 |  *     $connection->createCommand($sql1)->execute();
 | ||
|  |  *     $connection->createCommand($sql2)->execute();
 | ||
|  |  *     // ... executing other SQL statements ...
 | ||
|  |  *     $transaction->commit();
 | ||
| 
											14 years ago
										 |  * } catch(Exception $e) {
 | ||
| 
											13 years ago
										 |  *     $transaction->rollback();
 | ||
| 
											14 years ago
										 |  * }
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * Connection is often used as an application component and configured in the application
 | ||
| 
											14 years ago
										 |  * configuration like the following:
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * ~~~
 | ||
| 
											14 years ago
										 |  * array(
 | ||
| 
											14 years ago
										 |  *	 'components' => array(
 | ||
|  |  *		 'db' => array(
 | ||
| 
											13 years ago
										 |  *			 'class' => '\yii\db\Connection',
 | ||
| 
											14 years ago
										 |  *			 'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
 | ||
|  |  *			 'username' => 'root',
 | ||
|  |  *			 'password' => '',
 | ||
|  |  *			 'charset' => 'utf8',
 | ||
|  |  *		 ),
 | ||
|  |  *	 ),
 | ||
| 
											14 years ago
										 |  * )
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * @property boolean $isActive Whether the DB connection is established. This property is read-only.
 | ||
| 
											13 years ago
										 |  * @property Transaction $transaction The currently active transaction. Null if no active transaction.
 | ||
|  |  * @property Schema $schema The database schema information for the current connection.
 | ||
| 
											14 years ago
										 |  * @property QueryBuilder $queryBuilder The query builder.
 | ||
|  |  * @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the sequence object.
 | ||
|  |  * @property string $driverName Name of the DB driver currently being used.
 | ||
| 
											13 years ago
										 |  * @property array $querySummary The statistical results of SQL queries.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class Connection extends Component
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @event Event an event that is triggered after a DB connection is established
 | ||
|  | 	 */
 | ||
|  | 	const EVENT_AFTER_OPEN = 'afterOpen';
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * @var string the Data Source Name, or DSN, contains the information required to connect to the database.
 | ||
|  | 	 * Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) on
 | ||
|  | 	 * the format of the DSN string.
 | ||
| 
											14 years ago
										 | 	 * @see charset
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public $dsn;
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the username for establishing DB connection. Defaults to empty string.
 | ||
|  | 	 */
 | ||
|  | 	public $username = '';
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the password for establishing DB connection. Defaults to empty string.
 | ||
|  | 	 */
 | ||
|  | 	public $password = '';
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array PDO attributes (name => value) that should be set when calling [[open()]]
 | ||
| 
											14 years ago
										 | 	 * to establish a DB connection. Please refer to the
 | ||
|  | 	 * [PHP manual](http://www.php.net/manual/en/function.PDO-setAttribute.php) for
 | ||
|  | 	 * details about available attributes.
 | ||
|  | 	 */
 | ||
|  | 	public $attributes;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var PDO the PHP PDO instance associated with this DB connection.
 | ||
| 
											13 years ago
										 | 	 * This property is mainly managed by [[open()]] and [[close()]] methods.
 | ||
| 
											14 years ago
										 | 	 * When a DB connection is active, this property will represent a PDO instance;
 | ||
|  | 	 * otherwise, it will be null.
 | ||
|  | 	 */
 | ||
|  | 	public $pdo;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether to enable schema caching.
 | ||
|  | 	 * Note that in order to enable truly schema caching, a valid cache component as specified
 | ||
| 
											13 years ago
										 | 	 * by [[schemaCache]] must be enabled and [[enableSchemaCache]] must be set true.
 | ||
| 
											13 years ago
										 | 	 * @see schemaCacheDuration
 | ||
|  | 	 * @see schemaCacheExclude
 | ||
| 
											13 years ago
										 | 	 * @see schemaCache
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $enableSchemaCache = false;
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * @var integer number of seconds that table metadata can remain valid in cache.
 | ||
| 
											14 years ago
										 | 	 * Use 0 to indicate that the cached data will never expire.
 | ||
| 
											13 years ago
										 | 	 * @see enableSchemaCache
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $schemaCacheDuration = 3600;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
|  | 	 * @var array list of tables whose metadata should NOT be cached. Defaults to empty array.
 | ||
| 
											14 years ago
										 | 	 * The table names may contain schema prefix, if any. Do not quote the table names.
 | ||
| 
											13 years ago
										 | 	 * @see enableSchemaCache
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $schemaCacheExclude = array();
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var Cache|string the cache object or the ID of the cache application component that
 | ||
|  | 	 * is used to cache the table metadata.
 | ||
| 
											13 years ago
										 | 	 * @see enableSchemaCache
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $schemaCache = 'cache';
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether to enable query caching.
 | ||
| 
											14 years ago
										 | 	 * Note that in order to enable query caching, a valid cache component as specified
 | ||
| 
											13 years ago
										 | 	 * by [[queryCache]] must be enabled and [[enableQueryCache]] must be set true.
 | ||
| 
											14 years ago
										 | 	 *
 | ||
| 
											13 years ago
										 | 	 * Methods [[beginCache()]] and [[endCache()]] can be used as shortcuts to turn on
 | ||
|  | 	 * and off query caching on the fly.
 | ||
|  | 	 * @see queryCacheDuration
 | ||
| 
											13 years ago
										 | 	 * @see queryCache
 | ||
| 
											13 years ago
										 | 	 * @see queryCacheDependency
 | ||
|  | 	 * @see beginCache()
 | ||
|  | 	 * @see endCache()
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $enableQueryCache = false;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var integer number of seconds that query results can remain valid in cache.
 | ||
| 
											13 years ago
										 | 	 * Defaults to 3600, meaning 3600 seconds, or one hour.
 | ||
| 
											13 years ago
										 | 	 * Use 0 to indicate that the cached data will never expire.
 | ||
|  | 	 * @see enableQueryCache
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $queryCacheDuration = 3600;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var \yii\caching\Dependency the dependency that will be used when saving query results into cache.
 | ||
|  | 	 * Defaults to null, meaning no dependency.
 | ||
|  | 	 * @see enableQueryCache
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $queryCacheDependency;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var Cache|string the cache object or the ID of the cache application component
 | ||
|  | 	 * that is used for query caching.
 | ||
| 
											13 years ago
										 | 	 * @see enableQueryCache
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $queryCache = 'cache';
 | ||
| 
											14 years ago
										 | 	/**
 | ||
|  | 	 * @var string the charset used for database connection. The property is only used
 | ||
|  | 	 * for MySQL and PostgreSQL databases. Defaults to null, meaning using default charset
 | ||
|  | 	 * as specified by the database.
 | ||
|  | 	 *
 | ||
|  | 	 * Note that if you're using GBK or BIG5 then it's highly recommended to
 | ||
|  | 	 * update to PHP 5.3.6+ and to specify charset via DSN like
 | ||
|  | 	 * 'mysql:dbname=mydatabase;host=127.0.0.1;charset=GBK;'.
 | ||
|  | 	 */
 | ||
|  | 	public $charset;
 | ||
|  | 	/**
 | ||
|  | 	 * @var boolean whether to turn on prepare emulation. Defaults to false, meaning PDO
 | ||
|  | 	 * will use the native prepare support if available. For some databases (such as MySQL),
 | ||
|  | 	 * this may need to be set true so that PDO can emulate the prepare support to bypass
 | ||
| 
											14 years ago
										 | 	 * the buggy native prepare support.
 | ||
|  | 	 * The default value is null, which means the PDO ATTR_EMULATE_PREPARES value will not be changed.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public $emulatePrepare;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the common prefix or suffix for table names. If a table name is given
 | ||
|  | 	 * as `{{%TableName}}`, then the percentage character `%` will be replaced with this
 | ||
|  | 	 * property value. For example, `{{%post}}` becomes `{{tbl_post}}` if this property is
 | ||
| 
											13 years ago
										 | 	 * set as `"tbl_"`.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public $tablePrefix;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array mapping between PDO driver names and [[Schema]] classes.
 | ||
| 
											14 years ago
										 | 	 * The keys of the array are PDO driver names while the values the corresponding
 | ||
| 
											13 years ago
										 | 	 * schema class name or configuration. Please refer to [[Yii::createObject()]] for
 | ||
| 
											14 years ago
										 | 	 * details on how to specify a configuration.
 | ||
|  | 	 *
 | ||
| 
											13 years ago
										 | 	 * This property is mainly used by [[getSchema()]] when fetching the database schema information.
 | ||
| 
											14 years ago
										 | 	 * You normally do not need to set this property unless you want to use your own
 | ||
| 
											13 years ago
										 | 	 * [[Schema]] class to support DBMS that is not supported by Yii.
 | ||
|  | 	 */
 | ||
|  | 	public $schemaMap = array(
 | ||
| 
											13 years ago
										 | 		'pgsql' => 'yii\db\pgsql\Schema',    // PostgreSQL
 | ||
|  | 		'mysqli' => 'yii\db\mysql\Schema',   // MySQL
 | ||
|  | 		'mysql' => 'yii\db\mysql\Schema',    // MySQL
 | ||
|  | 		'sqlite' => 'yii\db\sqlite\Schema',  // sqlite 3
 | ||
| 
											13 years ago
										 | 		'sqlite2' => 'yii\db\sqlite\Schema', // sqlite 2
 | ||
| 
											13 years ago
										 | 		'sqlsrv' => 'yii\db\mssql\Schema',   // newer MSSQL driver on MS Windows hosts
 | ||
| 
											13 years ago
										 | 		'oci' => 'yii\db\oci\Schema',        // Oracle driver
 | ||
| 
											13 years ago
										 | 		'mssql' => 'yii\db\mssql\Schema',    // older MSSQL driver on MS Windows hosts
 | ||
|  | 		'dblib' => 'yii\db\mssql\Schema',    // dblib drivers on GNU/Linux (and maybe other OSes) hosts
 | ||
| 
											14 years ago
										 | 	);
 | ||
| 
											14 years ago
										 | 	/**
 | ||
|  | 	 * @var Transaction the currently active transaction
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	private $_transaction;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var Schema the database schema
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	private $_schema;
 | ||
| 
											14 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Closes the connection when this component is being serialized.
 | ||
| 
											14 years ago
										 | 	 * @return array
 | ||
|  | 	 */
 | ||
|  | 	public function __sleep()
 | ||
|  | 	{
 | ||
|  | 		$this->close();
 | ||
|  | 		return array_keys(get_object_vars($this));
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Returns a value indicating whether the DB connection is established.
 | ||
| 
											14 years ago
										 | 	 * @return boolean whether the DB connection is established
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function getIsActive()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		return $this->pdo !== null;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Turns on query caching.
 | ||
|  | 	 * This method is provided as a shortcut to setting two properties that are related
 | ||
|  | 	 * with query caching: [[queryCacheDuration]] and [[queryCacheDependency]].
 | ||
| 
											14 years ago
										 | 	 * @param integer $duration the number of seconds that query results may remain valid in cache.
 | ||
| 
											13 years ago
										 | 	 * If not set, it will use the value of [[queryCacheDuration]]. See [[queryCacheDuration]] for more details.
 | ||
| 
											14 years ago
										 | 	 * @param \yii\caching\Dependency $dependency the dependency for the cached query result.
 | ||
| 
											13 years ago
										 | 	 * See [[queryCacheDependency]] for more details.
 | ||
|  | 	 */
 | ||
|  | 	public function beginCache($duration = null, $dependency = null)
 | ||
|  | 	{
 | ||
|  | 		$this->enableQueryCache = true;
 | ||
|  | 		if ($duration !== null) {
 | ||
|  | 			$this->queryCacheDuration = $duration;
 | ||
|  | 		}
 | ||
|  | 		$this->queryCacheDependency = $dependency;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Turns off query caching.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function endCache()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$this->enableQueryCache = false;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Establishes a DB connection.
 | ||
|  | 	 * It does nothing if a DB connection has already been established.
 | ||
|  | 	 * @throws Exception if connection fails
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public function open()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		if ($this->pdo === null) {
 | ||
|  | 			if (empty($this->dsn)) {
 | ||
| 
											13 years ago
										 | 				throw new InvalidConfigException('Connection::dsn cannot be empty.');
 | ||
| 
											14 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 			$token = 'Opening DB connection: ' . $this->dsn;
 | ||
| 
											14 years ago
										 | 			try {
 | ||
| 
											13 years ago
										 | 				Yii::trace($token, __METHOD__);
 | ||
|  | 				Yii::beginProfile($token, __METHOD__);
 | ||
| 
											14 years ago
										 | 				$this->pdo = $this->createPdoInstance();
 | ||
| 
											13 years ago
										 | 				$this->initConnection();
 | ||
| 
											13 years ago
										 | 				Yii::endProfile($token, __METHOD__);
 | ||
| 
											14 years ago
										 | 			}
 | ||
| 
											14 years ago
										 | 			catch (\PDOException $e) {
 | ||
| 
											13 years ago
										 | 				Yii::endProfile($token, __METHOD__);
 | ||
| 
											13 years ago
										 | 				Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __METHOD__);
 | ||
| 
											14 years ago
										 | 				$message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.';
 | ||
| 
											13 years ago
										 | 				throw new Exception($message, $e->errorInfo, (int)$e->getCode());
 | ||
| 
											14 years ago
										 | 			}
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Closes the currently active DB connection.
 | ||
|  | 	 * It does nothing if the connection is already closed.
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	public function close()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		if ($this->pdo !== null) {
 | ||
| 
											13 years ago
										 | 			Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__);
 | ||
| 
											14 years ago
										 | 			$this->pdo = null;
 | ||
| 
											13 years ago
										 | 			$this->_schema = null;
 | ||
| 
											14 years ago
										 | 			$this->_transaction = null;
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Creates the PDO instance.
 | ||
| 
											14 years ago
										 | 	 * This method is called by [[open]] to establish a DB connection.
 | ||
| 
											14 years ago
										 | 	 * The default implementation will create a PHP PDO instance.
 | ||
|  | 	 * You may override this method if the default PDO needs to be adapted for certain DBMS.
 | ||
| 
											13 years ago
										 | 	 * @return PDO the pdo instance
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	protected function createPdoInstance()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$pdoClass = 'PDO';
 | ||
| 
											14 years ago
										 | 		if (($pos = strpos($this->dsn, ':')) !== false) {
 | ||
|  | 			$driver = strtolower(substr($this->dsn, 0, $pos));
 | ||
|  | 			if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') {
 | ||
| 
											13 years ago
										 | 				$pdoClass = 'yii\db\mssql\PDO';
 | ||
| 
											14 years ago
										 | 			}
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 		return new $pdoClass($this->dsn, $this->username, $this->password, $this->attributes);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Initializes the DB connection.
 | ||
|  | 	 * This method is invoked right after the DB connection is established.
 | ||
| 
											13 years ago
										 | 	 * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`
 | ||
|  | 	 * if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty.
 | ||
| 
											13 years ago
										 | 	 * It then triggers an [[EVENT_AFTER_OPEN]] event.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	protected function initConnection()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 | ||
|  | 		if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
 | ||
|  | 			$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 		if ($this->charset !== null && in_array($this->getDriverName(), array('pgsql', 'mysql', 'mysqli'))) {
 | ||
|  | 			$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		$this->trigger(self::EVENT_AFTER_OPEN);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Creates a command for execution.
 | ||
| 
											14 years ago
										 | 	 * @param string $sql the SQL statement to be executed
 | ||
|  | 	 * @param array $params the parameters to be bound to the SQL statement
 | ||
| 
											14 years ago
										 | 	 * @return Command the DB command
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public function createCommand($sql = null, $params = array())
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		$this->open();
 | ||
| 
											13 years ago
										 | 		$command = new Command(array(
 | ||
| 
											13 years ago
										 | 			'db' => $this,
 | ||
| 
											13 years ago
										 | 			'sql' => $sql,
 | ||
|  | 		));
 | ||
| 
											13 years ago
										 | 		return $command->bindValues($params);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the currently active transaction.
 | ||
| 
											14 years ago
										 | 	 * @return Transaction the currently active transaction. Null if no active transaction.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function getTransaction()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		return $this->_transaction && $this->_transaction->isActive ? $this->_transaction : null;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Starts a transaction.
 | ||
| 
											14 years ago
										 | 	 * @return Transaction the transaction initiated
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function beginTransaction()
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		$this->open();
 | ||
| 
											13 years ago
										 | 		$this->_transaction = new Transaction(array(
 | ||
| 
											13 years ago
										 | 			'db' => $this,
 | ||
| 
											13 years ago
										 | 		));
 | ||
|  | 		$this->_transaction->begin();
 | ||
|  | 		return $this->_transaction;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Returns the schema information for the database opened by this connection.
 | ||
|  | 	 * @return Schema the schema information for the database opened by this connection.
 | ||
| 
											13 years ago
										 | 	 * @throws NotSupportedException if there is no support for the current driver type
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function getSchema()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->_schema !== null) {
 | ||
|  | 			return $this->_schema;
 | ||
| 
											14 years ago
										 | 		} else {
 | ||
| 
											14 years ago
										 | 			$driver = $this->getDriverName();
 | ||
| 
											13 years ago
										 | 			if (isset($this->schemaMap[$driver])) {
 | ||
| 
											13 years ago
										 | 				$this->_schema = Yii::createObject($this->schemaMap[$driver]);
 | ||
| 
											13 years ago
										 | 				$this->_schema->db = $this;
 | ||
| 
											13 years ago
										 | 				return $this->_schema;
 | ||
| 
											14 years ago
										 | 			} else {
 | ||
| 
											13 years ago
										 | 				throw new NotSupportedException("Connection does not support reading schema information for '$driver' DBMS.");
 | ||
| 
											14 years ago
										 | 			}
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 	/**
 | ||
|  | 	 * Returns the query builder for the current DB connection.
 | ||
|  | 	 * @return QueryBuilder the query builder for the current DB connection.
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	public function getQueryBuilder()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->getSchema()->getQueryBuilder();
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * Obtains the schema information for the named table.
 | ||
|  | 	 * @param string $name 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 schema information. Null if the named table does not exist.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function getTableSchema($name, $refresh = false)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->getSchema()->getTableSchema($name, $refresh);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 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 = '')
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->getSchema()->getLastInsertID($sequenceName);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Quotes a string value for use in a query.
 | ||
| 
											14 years ago
										 | 	 * Note that if the parameter is not a string, it will be returned without change.
 | ||
| 
											14 years ago
										 | 	 * @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)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->getSchema()->quoteValue($str);
 | ||
| 
											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.
 | ||
| 
											13 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
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function quoteTableName($name)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		return $this->getSchema()->quoteTableName($name);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Quotes a column name for use in a query.
 | ||
| 
											13 years ago
										 | 	 * If the column name contains prefix, the prefix will also be properly quoted.
 | ||
|  | 	 * 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
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function quoteColumnName($name)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		return $this->getSchema()->quoteColumnName($name);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Processes a SQL statement by quoting table and column names that are enclosed within double brackets.
 | ||
|  | 	 * Tokens enclosed within double curly brackets are treated as table names, while
 | ||
|  | 	 * tokens enclosed within double square brackets are column names. They will be quoted accordingly.
 | ||
|  | 	 * Also, the percentage character "%" in a table name will be replaced with [[tablePrefix]].
 | ||
|  | 	 * @param string $sql the SQL to be quoted
 | ||
|  | 	 * @return string the quoted SQL
 | ||
|  | 	 */
 | ||
|  | 	public function quoteSql($sql)
 | ||
|  | 	{
 | ||
|  | 		$db = $this;
 | ||
| 
											13 years ago
										 | 		return preg_replace_callback('/(\\{\\{([%\w\-\. ]+)\\}\\}|\\[\\[([\w\-\. ]+)\\]\\])/',
 | ||
| 
											13 years ago
										 | 			function($matches) use($db) {
 | ||
|  | 				if (isset($matches[3])) {
 | ||
|  | 					return $db->quoteColumnName($matches[3]);
 | ||
|  | 				} else {
 | ||
| 
											13 years ago
										 | 					return str_replace('%', $db->tablePrefix, $db->quoteTableName($matches[2]));
 | ||
| 
											13 years ago
										 | 				}
 | ||
|  | 			}, $sql);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Returns the name of the DB driver for the current [[dsn]].
 | ||
| 
											14 years ago
										 | 	 * @return string name of the DB driver
 | ||
|  | 	 */
 | ||
|  | 	public function getDriverName()
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		if (($pos = strpos($this->dsn, ':')) !== false) {
 | ||
|  | 			return strtolower(substr($this->dsn, 0, $pos));
 | ||
| 
											14 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			return strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |