2008-09-12 12:51:56 +04:00
|
|
|
<?php
|
2008-09-12 21:25:38 +04:00
|
|
|
/*
|
|
|
|
* $Id: Connection.php 4933 2008-09-12 10:58:33Z romanb $
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2009-02-04 19:35:36 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL;
|
2008-09-12 21:25:38 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\Common\EventManager;
|
|
|
|
use Doctrine\Common\DoctrineException;
|
2008-09-12 12:51:56 +04:00
|
|
|
|
|
|
|
/**
|
2008-12-18 17:08:11 +03:00
|
|
|
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
|
2008-09-13 14:28:29 +04:00
|
|
|
* events, transaction isolation levels, configuration, emulated transaction nesting,
|
|
|
|
* lazy connecting and more.
|
2008-09-12 12:51:56 +04:00
|
|
|
*
|
2008-09-12 21:25:38 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision: 4933 $
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (MDB2 library)
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @todo
|
|
|
|
* 1) REPLICATION SUPPORT
|
|
|
|
* Replication support should be tackled at this layer (DBAL).
|
|
|
|
* There can be options that look like:
|
|
|
|
* 'slaves' => array(
|
|
|
|
* 'slave1' => array(
|
|
|
|
* user, pass etc.
|
|
|
|
* ),
|
|
|
|
* 'slave2' => array(
|
|
|
|
* user, pass etc.
|
|
|
|
* )),
|
|
|
|
* 'slaveConnectionResolver' => new MySlaveConnectionResolver(),
|
|
|
|
* 'masters' => array(...),
|
|
|
|
* 'masterConnectionResolver' => new MyMasterConnectionResolver()
|
2008-09-12 12:51:56 +04:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* Doctrine\DBAL could ship with a simple standard broker that uses a primitive
|
2008-09-12 21:25:38 +04:00
|
|
|
* round-robin approach to distribution. User can provide its own brokers.
|
2008-09-12 12:51:56 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class Connection
|
2008-09-12 12:51:56 +04:00
|
|
|
{
|
2008-09-13 14:28:29 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level READ UNCOMMITTED.
|
|
|
|
*/
|
2008-09-12 12:51:56 +04:00
|
|
|
const TRANSACTION_READ_UNCOMMITTED = 1;
|
2008-09-13 14:28:29 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level READ COMMITTED.
|
|
|
|
*/
|
2008-09-12 12:51:56 +04:00
|
|
|
const TRANSACTION_READ_COMMITTED = 2;
|
2008-09-13 14:28:29 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level REPEATABLE READ.
|
|
|
|
*/
|
2008-09-12 12:51:56 +04:00
|
|
|
const TRANSACTION_REPEATABLE_READ = 3;
|
2008-09-13 14:28:29 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level SERIALIZABLE.
|
|
|
|
*/
|
2008-09-12 12:51:56 +04:00
|
|
|
const TRANSACTION_SERIALIZABLE = 4;
|
|
|
|
|
2008-09-12 21:25:38 +04:00
|
|
|
/**
|
|
|
|
* The wrapped driver connection.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\DBAL\Driver\Connection
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
protected $_conn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Configuration.
|
|
|
|
*
|
2008-12-18 17:21:21 +03:00
|
|
|
* @var Doctrine\DBAL\Configuration
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
protected $_config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The EventManager.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\Common\EventManager
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
protected $_eventManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not a connection has been established.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_isConnected = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The transaction nesting level.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $_transactionNestingLevel = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently active transaction isolation level.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $_transactionIsolationLevel;
|
|
|
|
|
|
|
|
/**
|
2008-12-18 17:08:11 +03:00
|
|
|
* The parameters used during creation of the Connection instance.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_params = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The query count. Represents the number of executed database queries by the connection.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $_queryCount = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DatabasePlatform object that provides information about the
|
|
|
|
* database platform used by the connection.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\DBAL\Platforms\AbstractPlatform
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
protected $_platform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The schema manager.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\DBAL\Schema\SchemaManager
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
protected $_schemaManager;
|
|
|
|
|
2008-09-13 14:28:29 +04:00
|
|
|
/**
|
|
|
|
* The used DBAL driver.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\DBAL\Driver
|
2008-09-13 14:28:29 +04:00
|
|
|
*/
|
|
|
|
protected $_driver;
|
2009-05-05 21:20:55 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to quote identifiers. Read from the configuration upon construction.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_quoteIdentifiers = false;
|
2008-09-13 14:28:29 +04:00
|
|
|
|
2008-09-12 21:25:38 +04:00
|
|
|
/**
|
2008-12-18 17:08:11 +03:00
|
|
|
* Initializes a new instance of the Connection class.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param array $params The connection parameters.
|
2009-02-07 20:02:13 +03:00
|
|
|
* @param Driver $driver
|
|
|
|
* @param Configuration $config
|
|
|
|
* @param EventManager $eventManager
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
public function __construct(array $params, Driver $driver, Configuration $config = null,
|
2009-01-22 22:38:10 +03:00
|
|
|
EventManager $eventManager = null)
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
|
|
|
$this->_driver = $driver;
|
|
|
|
$this->_params = $params;
|
|
|
|
|
|
|
|
if (isset($params['pdo'])) {
|
|
|
|
$this->_conn = $params['pdo'];
|
|
|
|
$this->_isConnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create default config and event manager if none given
|
|
|
|
if ( ! $config) {
|
2009-05-05 21:20:55 +04:00
|
|
|
$config = new Configuration();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
if ( ! $eventManager) {
|
2009-05-05 21:20:55 +04:00
|
|
|
$eventManager = new EventManager();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
$this->_config = $config;
|
|
|
|
$this->_eventManager = $eventManager;
|
2008-09-12 21:25:38 +04:00
|
|
|
$this->_platform = $driver->getDatabasePlatform();
|
|
|
|
$this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
|
2009-05-05 21:20:55 +04:00
|
|
|
$this->_quoteIdentifiers = $config->getQuoteIdentifiers();
|
|
|
|
$this->_platform->setQuoteIdentifiers($this->_quoteIdentifiers);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
2009-02-17 15:25:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the DBAL driver instance.
|
|
|
|
*
|
|
|
|
* @return Doctrine\DBAL\Driver
|
|
|
|
*/
|
|
|
|
public function getDriver()
|
|
|
|
{
|
|
|
|
return $this->_driver;
|
|
|
|
}
|
2008-09-12 21:25:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Configuration used by the Connection.
|
|
|
|
*
|
2009-02-07 20:02:13 +03:00
|
|
|
* @return Doctrine\DBAL\Configuration
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function getConfiguration()
|
|
|
|
{
|
|
|
|
return $this->_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EventManager used by the Connection.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @return Doctrine\Common\EventManager
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function getEventManager()
|
|
|
|
{
|
|
|
|
return $this->_eventManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the DatabasePlatform for the connection.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @return Doctrine\DBAL\Platforms\AbstractPlatform
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function getDatabasePlatform()
|
|
|
|
{
|
|
|
|
return $this->_platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establishes the connection with the database.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function connect()
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
if ($this->_isConnected) return false;
|
2008-09-12 21:25:38 +04:00
|
|
|
|
|
|
|
$driverOptions = isset($this->_params['driverOptions']) ?
|
|
|
|
$this->_params['driverOptions'] : array();
|
|
|
|
$user = isset($this->_params['user']) ?
|
|
|
|
$this->_params['user'] : null;
|
|
|
|
$password = isset($this->_params['password']) ?
|
|
|
|
$this->_params['password'] : null;
|
2008-09-13 14:28:29 +04:00
|
|
|
|
|
|
|
$this->_conn = $this->_driver->connect(
|
|
|
|
$this->_params,
|
2008-09-12 21:25:38 +04:00
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
$driverOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->_isConnected = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-13 14:28:29 +04:00
|
|
|
* Whether an actual connection to the database is established.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-09-13 14:28:29 +04:00
|
|
|
public function isConnected()
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
2008-09-13 14:28:29 +04:00
|
|
|
return $this->_isConnected;
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes table row(s) matching the specified identifier.
|
|
|
|
*
|
|
|
|
* @param string $table The table to delete data from
|
|
|
|
* @param array $identifier An associateve array containing identifier fieldname-value pairs.
|
|
|
|
* @return integer The number of affected rows
|
|
|
|
*/
|
|
|
|
public function delete($tableName, array $identifier)
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
$criteria = array();
|
|
|
|
foreach (array_keys($identifier) as $id) {
|
|
|
|
$criteria[] = $this->quoteIdentifier($id) . ' = ?';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = 'DELETE FROM '
|
|
|
|
. $this->quoteIdentifier($tableName)
|
|
|
|
. ' WHERE ' . implode(' AND ', $criteria);
|
|
|
|
|
|
|
|
return $this->exec($query, array_values($identifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates table row(s) with specified data
|
|
|
|
*
|
2009-02-20 08:46:20 +03:00
|
|
|
* @throws Doctrine\DBAL\ConnectionException if something went wrong at the database level
|
2008-09-12 21:25:38 +04:00
|
|
|
* @param string $table The table to insert data into
|
|
|
|
* @param array $values An associateve array containing column-value pairs.
|
|
|
|
* @return mixed boolean false if empty value array was given,
|
|
|
|
* otherwise returns the number of affected rows
|
|
|
|
*/
|
|
|
|
public function update($tableName, array $data, array $identifier)
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
if (empty($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = array();
|
|
|
|
foreach ($data as $columnName => $value) {
|
2009-02-02 14:55:50 +03:00
|
|
|
$set[] = $this->quoteIdentifier($columnName) . ' = ?';
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$params = array_merge(array_values($data), array_values($identifier));
|
|
|
|
|
|
|
|
$sql = 'UPDATE ' . $this->quoteIdentifier($tableName)
|
|
|
|
. ' SET ' . implode(', ', $set)
|
|
|
|
. ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
|
|
|
|
. ' = ?';
|
2008-09-12 12:51:56 +04:00
|
|
|
|
2008-09-12 21:25:38 +04:00
|
|
|
return $this->exec($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a table row with specified data.
|
|
|
|
*
|
|
|
|
* @param string $table The table to insert data into.
|
|
|
|
* @param array $fields An associateve array containing fieldname-value pairs.
|
|
|
|
* @return mixed boolean false if empty value array was given,
|
|
|
|
* otherwise returns the number of affected rows
|
|
|
|
*/
|
|
|
|
public function insert($tableName, array $data)
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
if (empty($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// column names are specified as array keys
|
|
|
|
$cols = array();
|
|
|
|
$a = array();
|
|
|
|
foreach ($data as $columnName => $value) {
|
|
|
|
$cols[] = $this->quoteIdentifier($columnName);
|
2009-02-02 14:55:50 +03:00
|
|
|
$a[] = '?';
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$query = 'INSERT INTO ' . $this->quoteIdentifier($tableName)
|
|
|
|
. ' (' . implode(', ', $cols) . ') '
|
|
|
|
. 'VALUES (';
|
|
|
|
$query .= implode(', ', $a) . ')';
|
|
|
|
|
|
|
|
return $this->exec($query, array_values($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the charset on the current connection
|
|
|
|
*
|
|
|
|
* @param string charset
|
|
|
|
*/
|
|
|
|
public function setCharset($charset)
|
|
|
|
{
|
|
|
|
$this->exec($this->_platform->getSetCharsetSql($charset));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-05 21:20:55 +04:00
|
|
|
* Quote a string so it can be safely used as a table or column name, even if
|
|
|
|
* it is a reserved name.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2009-05-05 21:20:55 +04:00
|
|
|
* Delimiting style depends on the underlying database platform that is being used.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2009-05-05 21:20:55 +04:00
|
|
|
* NOTE: Just because you CAN use delimited identifiers doesn't mean
|
2008-09-12 21:25:38 +04:00
|
|
|
* you SHOULD use them. In general, they end up causing way more
|
|
|
|
* problems than they solve.
|
|
|
|
*
|
|
|
|
* @param string $str identifier name to be quoted
|
|
|
|
* @param bool $checkOption check the 'quote_identifier' option
|
|
|
|
*
|
|
|
|
* @return string quoted identifier string
|
|
|
|
*/
|
|
|
|
public function quoteIdentifier($str)
|
|
|
|
{
|
2009-05-05 21:20:55 +04:00
|
|
|
if ($this->_quoteIdentifiers) {
|
|
|
|
return $this->_platform->quoteIdentifier($str);
|
|
|
|
}
|
|
|
|
return $str;
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-13 14:28:29 +04:00
|
|
|
* Quotes a given input parameter.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param mixed $input Parameter to be quoted.
|
|
|
|
* @param string $type Type of the parameter.
|
|
|
|
* @return string The quoted parameter.
|
|
|
|
*/
|
|
|
|
public function quote($input, $type = null)
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
return $this->_conn->quote($input, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_ASSOC).
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2009-02-07 20:02:13 +03:00
|
|
|
* @param string $sql The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
2008-09-12 21:25:38 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
public function fetchAll($sql, array $params = array())
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
return $this->execute($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Convenience method for PDO::query("...") followed by $stmt->fetchColumn().
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2009-02-07 20:02:13 +03:00
|
|
|
* @param string $statement The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
|
|
|
* @param int $colnum 0-indexed column number to retrieve
|
2008-09-12 21:25:38 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fetchOne($statement, array $params = array(), $colnum = 0)
|
|
|
|
{
|
|
|
|
return $this->execute($statement, $params)->fetchColumn($colnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Convenience method for PDO::query("...") followed by $stmt->fetch(PDO::FETCH_ASSOC).
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2009-02-07 20:02:13 +03:00
|
|
|
* @param string $statement The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
2008-09-12 21:25:38 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetchRow($statement, array $params = array())
|
|
|
|
{
|
|
|
|
return $this->execute($statement, $params)->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Convenience method for PDO::query("...") followed by $stmt->fetch(PDO::FETCH_NUM).
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param string $statement sql query to be executed
|
|
|
|
* @param array $params prepared statement params
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetchArray($statement, array $params = array())
|
|
|
|
{
|
|
|
|
return $this->execute($statement, $params)->fetch(PDO::FETCH_NUM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_COLUMN, ...).
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param string $statement sql query to be executed
|
|
|
|
* @param array $params prepared statement params
|
|
|
|
* @param int $colnum 0-indexed column number to retrieve
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetchColumn($statement, array $params = array(), $colnum = 0)
|
|
|
|
{
|
|
|
|
return $this->execute($statement, $params)->fetchAll(PDO::FETCH_COLUMN, $colnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_BOTH).
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param string $statement sql query to be executed
|
|
|
|
* @param array $params prepared statement params
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetchBoth($statement, array $params = array())
|
|
|
|
{
|
|
|
|
return $this->execute($statement, $params)->fetchAll(PDO::FETCH_BOTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-13 14:28:29 +04:00
|
|
|
* Prepares an SQL statement.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param string $statement
|
2009-02-17 13:54:18 +03:00
|
|
|
* @return Statement
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function prepare($statement)
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-02-02 14:55:50 +03:00
|
|
|
return $this->_conn->prepare($statement);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* Queries the database with limit and offset added to the query and returns
|
|
|
|
* a Statement object.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
2009-02-07 20:02:13 +03:00
|
|
|
* @return Statement
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function select($query, $limit = 0, $offset = 0)
|
|
|
|
{
|
|
|
|
if ($limit > 0 || $offset > 0) {
|
|
|
|
$query = $this->_platform->modifyLimitQuery($query, $limit, $offset);
|
|
|
|
}
|
|
|
|
return $this->execute($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an SQL SELECT query with the given parameters.
|
|
|
|
*
|
|
|
|
* @param string $query sql query
|
|
|
|
* @param array $params query parameters
|
|
|
|
*
|
2009-02-20 08:46:20 +03:00
|
|
|
* @return PDOStatement
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function execute($query, array $params = array())
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
try {
|
2009-03-30 23:43:05 +04:00
|
|
|
echo "DBAL:" . $query . PHP_EOL;
|
2008-09-12 21:25:38 +04:00
|
|
|
if ( ! empty($params)) {
|
|
|
|
$stmt = $this->prepare($query);
|
|
|
|
$stmt->execute($params);
|
|
|
|
return $stmt;
|
|
|
|
} else {
|
|
|
|
$stmt = $this->_conn->query($query);
|
|
|
|
$this->_queryCount++;
|
|
|
|
return $stmt;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->rethrowException($e, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters.
|
|
|
|
*
|
|
|
|
* @param string $query sql query
|
|
|
|
* @param array $params query parameters
|
|
|
|
*
|
2009-02-20 08:46:20 +03:00
|
|
|
* @return PDOStatement
|
2008-09-12 21:25:38 +04:00
|
|
|
* @todo Rename to executeUpdate().
|
|
|
|
*/
|
|
|
|
public function exec($query, array $params = array()) {
|
|
|
|
$this->connect();
|
|
|
|
try {
|
2009-02-06 20:16:39 +03:00
|
|
|
echo $query . PHP_EOL;
|
2008-09-12 21:25:38 +04:00
|
|
|
if ( ! empty($params)) {
|
2009-02-06 20:16:39 +03:00
|
|
|
var_dump($params);
|
2008-09-12 21:25:38 +04:00
|
|
|
$stmt = $this->prepare($query);
|
|
|
|
$stmt->execute($params);
|
|
|
|
return $stmt->rowCount();
|
|
|
|
} else {
|
|
|
|
$count = $this->_conn->exec($query);
|
|
|
|
$this->_queryCount++;
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
2009-02-02 14:55:50 +03:00
|
|
|
//TODO: Wrap
|
2009-01-07 20:46:02 +03:00
|
|
|
throw $e;
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps the given exception into a driver-specific exception and rethrows it.
|
|
|
|
*
|
2009-02-20 08:46:20 +03:00
|
|
|
* @throws Doctrine\DBAL\ConnectionException
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
2009-05-14 14:03:09 +04:00
|
|
|
public function rethrowException(\Exception $e, $invoker)
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
throw $e;
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of queries executed by the connection.
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getQueryCount()
|
|
|
|
{
|
|
|
|
return $this->_queryCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
$this->clear();
|
|
|
|
unset($this->_conn);
|
|
|
|
$this->_isConnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the transaction isolation level.
|
|
|
|
*
|
|
|
|
* @param integer $level The level to set.
|
|
|
|
*/
|
|
|
|
public function setTransactionIsolation($level)
|
|
|
|
{
|
|
|
|
$this->_transactionIsolationLevel = $level;
|
2008-09-13 14:28:29 +04:00
|
|
|
return $this->exec($this->_platform->getSetTransactionIsolationSql($level));
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currently active transaction isolation level.
|
|
|
|
*
|
|
|
|
* @return integer The current transaction isolation level.
|
|
|
|
*/
|
|
|
|
public function getTransactionIsolation()
|
|
|
|
{
|
|
|
|
return $this->_transactionIsolationLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-13 14:28:29 +04:00
|
|
|
* Returns the current transaction nesting level.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @return integer The nesting level. A value of 0 means theres no active transaction.
|
|
|
|
*/
|
|
|
|
public function getTransactionNestingLevel()
|
|
|
|
{
|
|
|
|
return $this->_transactionNestingLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the SQLSTATE associated with the last operation on the database handle
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function errorCode()
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
return $this->_conn->errorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch extended error information associated with the last operation on the database handle
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function errorInfo()
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
return $this->_conn->errorInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of the last inserted row, or the last value from a sequence object,
|
|
|
|
* depending on the underlying driver.
|
|
|
|
*
|
|
|
|
* Note: This method may not return a meaningful or consistent result across different drivers,
|
|
|
|
* because the underlying database may not even support the notion of auto-increment fields or sequences.
|
|
|
|
*
|
|
|
|
* @param string $table Name of the table into which a new row was inserted.
|
|
|
|
* @param string $field Name of the field into which a new row was inserted.
|
|
|
|
*/
|
|
|
|
public function lastInsertId($seqName = null)
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
return $this->_conn->lastInsertId($seqName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a transaction or set a savepoint.
|
|
|
|
*
|
|
|
|
* if trying to set a savepoint and there is no active transaction
|
|
|
|
* a new transaction is being started.
|
2008-09-13 14:28:29 +04:00
|
|
|
*
|
|
|
|
* @return boolean
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function beginTransaction()
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_conn->beginTransaction();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
++$this->_transactionNestingLevel;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits the database changes done during a transaction that is in
|
|
|
|
* progress or release a savepoint. This function may only be called when
|
|
|
|
* auto-committing is disabled, otherwise it will fail.
|
|
|
|
*
|
2008-09-13 14:28:29 +04:00
|
|
|
* @return boolean FALSE if commit couldn't be performed, TRUE otherwise
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function commit()
|
|
|
|
{
|
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
2009-02-19 10:00:54 +03:00
|
|
|
throw ConnectionException::commitFailedNoActiveTransaction();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
if ($this->_transactionNestingLevel == 1) {
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_conn->commit();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
--$this->_transactionNestingLevel;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel any database changes done during a transaction or since a specific
|
|
|
|
* savepoint that is in progress. This function may only be called when
|
|
|
|
* auto-committing is disabled, otherwise it will fail. Therefore, a new
|
|
|
|
* transaction is implicitly started after canceling the pending changes.
|
|
|
|
*
|
|
|
|
* this method can be listened with onPreTransactionRollback and onTransactionRollback
|
|
|
|
* eventlistener methods
|
|
|
|
*
|
|
|
|
* @param string $savepoint Name of a savepoint to rollback to.
|
2009-02-20 08:46:20 +03:00
|
|
|
* @throws Doctrine\DBAL\ConnectionException If the rollback operation fails at database level.
|
2008-09-12 21:25:38 +04:00
|
|
|
* @return boolean FALSE if rollback couldn't be performed, TRUE otherwise.
|
|
|
|
*/
|
|
|
|
public function rollback()
|
|
|
|
{
|
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
2009-02-19 10:00:54 +03:00
|
|
|
throw ConnectionException::rollbackFailedNoActiveTransaction();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
if ($this->_transactionNestingLevel == 1) {
|
|
|
|
$this->_transactionNestingLevel = 0;
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_conn->rollback();
|
2008-09-12 21:25:38 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
--$this->_transactionNestingLevel;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quotes pattern (% and _) characters in a string)
|
|
|
|
*
|
|
|
|
* EXPERIMENTAL
|
|
|
|
*
|
|
|
|
* WARNING: this function is experimental and may change signature at
|
|
|
|
* any time until labelled as non-experimental
|
|
|
|
*
|
|
|
|
* @param string the input string to quote
|
|
|
|
*
|
|
|
|
* @return string quoted string
|
|
|
|
*/
|
|
|
|
protected function _escapePattern($text)
|
|
|
|
{
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the wrapped driver connection.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @return Doctrine\DBAL\Driver\Connection
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function getWrappedConnection()
|
|
|
|
{
|
2009-03-30 23:43:05 +04:00
|
|
|
$this->connect();
|
2008-09-12 21:25:38 +04:00
|
|
|
return $this->_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SchemaManager that can be used to inspect or change the
|
|
|
|
* database schema through the connection.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @return Doctrine\DBAL\Schema\SchemaManager
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
public function getSchemaManager()
|
|
|
|
{
|
|
|
|
if ( ! $this->_schemaManager) {
|
2008-09-13 14:28:29 +04:00
|
|
|
$this->_schemaManager = $this->_driver->getSchemaManager($this);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
return $this->_schemaManager;
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|