2008-09-12 12:51:56 +04:00
|
|
|
<?php
|
2008-09-12 21:25:38 +04:00
|
|
|
/*
|
2009-08-11 02:43:27 +04:00
|
|
|
* $Id$
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* 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-08-11 14:51:38 +04:00
|
|
|
use Doctrine\Common\EventManager,
|
2009-12-08 22:41:47 +03:00
|
|
|
Doctrine\DBAL\DBALException;
|
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
|
|
|
*
|
2009-08-11 02:43:27 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 3938 $
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (MDB2 library)
|
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
|
|
|
{
|
2009-05-28 15:30:27 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level READ UNCOMMITTED.
|
|
|
|
*/
|
|
|
|
const TRANSACTION_READ_UNCOMMITTED = 1;
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level READ COMMITTED.
|
|
|
|
*/
|
|
|
|
const TRANSACTION_READ_COMMITTED = 2;
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level REPEATABLE READ.
|
|
|
|
*/
|
|
|
|
const TRANSACTION_REPEATABLE_READ = 3;
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* Constant for transaction isolation level SERIALIZABLE.
|
|
|
|
*/
|
|
|
|
const TRANSACTION_SERIALIZABLE = 4;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-29 19:38:46 +04:00
|
|
|
/**
|
|
|
|
* Derived PDO constants
|
|
|
|
*/
|
|
|
|
const FETCH_ASSOC = 2;
|
|
|
|
const FETCH_BOTH = 4;
|
|
|
|
const FETCH_COLUMN = 7;
|
|
|
|
const FETCH_NUM = 3;
|
|
|
|
const ATTR_AUTOCOMMIT = 0;
|
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The wrapped driver connection.
|
|
|
|
*
|
|
|
|
* @var Doctrine\DBAL\Driver\Connection
|
|
|
|
*/
|
|
|
|
protected $_conn;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The Configuration.
|
|
|
|
*
|
|
|
|
* @var Doctrine\DBAL\Configuration
|
|
|
|
*/
|
|
|
|
protected $_config;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The EventManager.
|
|
|
|
*
|
|
|
|
* @var Doctrine\Common\EventManager
|
|
|
|
*/
|
|
|
|
protected $_eventManager;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* Whether or not a connection has been established.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-10-23 19:03:00 +04:00
|
|
|
private $_isConnected = false;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The transaction nesting level.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2009-10-23 19:03:00 +04:00
|
|
|
private $_transactionNestingLevel = 0;
|
2009-05-30 15:33:06 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The currently active transaction isolation level.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2009-10-23 19:03:00 +04:00
|
|
|
private $_transactionIsolationLevel;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The parameters used during creation of the Connection instance.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-10-23 19:03:00 +04:00
|
|
|
private $_params = array();
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The DatabasePlatform object that provides information about the
|
|
|
|
* database platform used by the connection.
|
|
|
|
*
|
|
|
|
* @var Doctrine\DBAL\Platforms\AbstractPlatform
|
|
|
|
*/
|
|
|
|
protected $_platform;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* The schema manager.
|
|
|
|
*
|
|
|
|
* @var Doctrine\DBAL\Schema\SchemaManager
|
|
|
|
*/
|
|
|
|
protected $_schemaManager;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
/**
|
2009-05-28 15:19:27 +04:00
|
|
|
* The used DBAL driver.
|
|
|
|
*
|
|
|
|
* @var Doctrine\DBAL\Driver
|
|
|
|
*/
|
|
|
|
protected $_driver;
|
2009-10-23 19:03:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag that indicates whether the current transaction is marked for rollback only.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $_isRollbackOnly = false;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
/**
|
|
|
|
* Initializes a new instance of the Connection class.
|
|
|
|
*
|
|
|
|
* @param array $params The connection parameters.
|
|
|
|
* @param Driver $driver
|
|
|
|
* @param Configuration $config
|
|
|
|
* @param EventManager $eventManager
|
|
|
|
*/
|
|
|
|
public function __construct(array $params, Driver $driver, Configuration $config = null,
|
|
|
|
EventManager $eventManager = null)
|
|
|
|
{
|
|
|
|
$this->_driver = $driver;
|
|
|
|
$this->_params = $params;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
if (isset($params['pdo'])) {
|
|
|
|
$this->_conn = $params['pdo'];
|
|
|
|
$this->_isConnected = true;
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:19:27 +04:00
|
|
|
// Create default config and event manager if none given
|
|
|
|
if ( ! $config) {
|
|
|
|
$config = new Configuration();
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
if ( ! $eventManager) {
|
2009-05-28 15:19:27 +04:00
|
|
|
$eventManager = new EventManager();
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
$this->_config = $config;
|
2009-05-28 15:19:27 +04:00
|
|
|
$this->_eventManager = $eventManager;
|
2009-12-16 00:06:32 +03:00
|
|
|
if ( ! isset($params['platform'])) {
|
2009-12-08 22:41:47 +03:00
|
|
|
$this->_platform = $driver->getDatabasePlatform();
|
2009-12-16 00:06:32 +03:00
|
|
|
} else if ($params['platform'] instanceof Platforms\AbstractPlatform) {
|
2009-12-08 22:41:47 +03:00
|
|
|
$this->_platform = $params['platform'];
|
|
|
|
} else {
|
|
|
|
throw DBALException::invalidPlatformSpecified();
|
|
|
|
}
|
2009-05-28 15:19:27 +04:00
|
|
|
$this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
2009-05-28 15:19:27 +04:00
|
|
|
|
2009-05-28 06:04:51 +04:00
|
|
|
/**
|
2009-05-30 15:33:06 +04:00
|
|
|
* Gets the parameters used during instantiation.
|
2009-05-28 06:04:51 +04:00
|
|
|
*
|
|
|
|
* @return array $params
|
|
|
|
*/
|
|
|
|
public function getParams()
|
|
|
|
{
|
|
|
|
return $this->_params;
|
|
|
|
}
|
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
/**
|
2009-05-30 15:33:06 +04:00
|
|
|
* Gets the name of the database this Connection is connected to.
|
2009-05-28 15:30:27 +04:00
|
|
|
*
|
|
|
|
* @return string $database
|
|
|
|
*/
|
|
|
|
public function getDatabase()
|
|
|
|
{
|
|
|
|
return $this->_driver->getDatabase($this);
|
|
|
|
}
|
2009-05-30 17:57:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the hostname of the currently connected database.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHost()
|
|
|
|
{
|
2009-07-15 02:36:09 +04:00
|
|
|
return isset($this->_params['host']) ? $this->_params['host'] : null;
|
2009-05-30 17:57:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the port of the currently connected database.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getPort()
|
|
|
|
{
|
2009-07-15 02:36:09 +04:00
|
|
|
return isset($this->_params['port']) ? $this->_params['port'] : null;
|
2009-05-30 17:57:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the username used by this connection.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUsername()
|
|
|
|
{
|
2009-07-15 02:36:09 +04:00
|
|
|
return isset($this->_params['user']) ? $this->_params['user'] : null;
|
2009-05-30 17:57:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the password used by this connection.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPassword()
|
|
|
|
{
|
2009-07-15 02:36:09 +04:00
|
|
|
return isset($this->_params['password']) ? $this->_params['password'] : null;
|
2009-05-30 17:57:57 +04:00
|
|
|
}
|
2009-05-28 15:30:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the DBAL driver instance.
|
|
|
|
*
|
|
|
|
* @return Doctrine\DBAL\Driver
|
|
|
|
*/
|
|
|
|
public function getDriver()
|
|
|
|
{
|
|
|
|
return $this->_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Configuration used by the Connection.
|
|
|
|
*
|
|
|
|
* @return Doctrine\DBAL\Configuration
|
|
|
|
*/
|
|
|
|
public function getConfiguration()
|
|
|
|
{
|
|
|
|
return $this->_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EventManager used by the Connection.
|
|
|
|
*
|
|
|
|
* @return Doctrine\Common\EventManager
|
|
|
|
*/
|
|
|
|
public function getEventManager()
|
|
|
|
{
|
|
|
|
return $this->_eventManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the DatabasePlatform for the connection.
|
|
|
|
*
|
|
|
|
* @return Doctrine\DBAL\Platforms\AbstractPlatform
|
|
|
|
*/
|
|
|
|
public function getDatabasePlatform()
|
|
|
|
{
|
|
|
|
return $this->_platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establishes the connection with the database.
|
|
|
|
*
|
2009-12-09 15:37:57 +03:00
|
|
|
* @return boolean TRUE if the connection was successfully established, FALSE if
|
|
|
|
* the connection is already open.
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
|
|
|
public function connect()
|
|
|
|
{
|
|
|
|
if ($this->_isConnected) return false;
|
|
|
|
|
|
|
|
$driverOptions = isset($this->_params['driverOptions']) ?
|
2009-05-30 15:33:06 +04:00
|
|
|
$this->_params['driverOptions'] : array();
|
|
|
|
$user = isset($this->_params['user']) ? $this->_params['user'] : null;
|
2009-05-28 15:30:27 +04:00
|
|
|
$password = isset($this->_params['password']) ?
|
2009-05-30 15:33:06 +04:00
|
|
|
$this->_params['password'] : null;
|
|
|
|
|
|
|
|
$this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);
|
2009-05-28 15:30:27 +04:00
|
|
|
$this->_isConnected = true;
|
|
|
|
|
2010-02-01 00:51:15 +03:00
|
|
|
if ($this->_eventManager->hasListeners(Events::postConnect)) {
|
|
|
|
$eventArgs = new \Doctrine\DBAL\Event\ConnectionEventArgs($this);
|
|
|
|
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
|
|
|
|
}
|
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-09 15:37:57 +03:00
|
|
|
* Prepares and executes an SQL query and returns the first row of the result
|
|
|
|
* as an associative array.
|
|
|
|
*
|
2009-05-28 15:30:27 +04:00
|
|
|
* @param string $statement The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
|
|
|
* @return array
|
2009-12-09 15:37:57 +03:00
|
|
|
* @todo Rename: fetchAssoc
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
|
|
|
public function fetchRow($statement, array $params = array())
|
|
|
|
{
|
2009-05-29 19:38:46 +04:00
|
|
|
return $this->execute($statement, $params)->fetch(Connection::FETCH_ASSOC);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-09 15:37:57 +03:00
|
|
|
* Prepares and executes an SQL query and returns the first row of the result
|
|
|
|
* as a numerically indexed array.
|
2009-05-28 15:30:27 +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())
|
|
|
|
{
|
2009-05-29 19:38:46 +04:00
|
|
|
return $this->execute($statement, $params)->fetch(Connection::FETCH_NUM);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-09 15:37:57 +03:00
|
|
|
* Prepares and executes an SQL query and returns the value of a single column
|
|
|
|
* of the first row of the result.
|
|
|
|
*
|
2009-05-28 15:30:27 +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
|
2009-12-09 15:37:57 +03:00
|
|
|
* @return mixed
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
|
|
|
public function fetchColumn($statement, array $params = array(), $colnum = 0)
|
|
|
|
{
|
2009-11-08 14:07:49 +03:00
|
|
|
return $this->execute($statement, $params)->fetchColumn($colnum);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether an actual connection to the database is established.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isConnected()
|
|
|
|
{
|
|
|
|
return $this->_isConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes table row(s) matching the specified identifier.
|
|
|
|
*
|
2009-12-09 15:37:57 +03:00
|
|
|
* @param string $table The table to delete data from.
|
2009-05-28 15:30:27 +04:00
|
|
|
* @param array $identifier An associateve array containing identifier fieldname-value pairs.
|
|
|
|
* @return integer The number of affected rows
|
|
|
|
*/
|
|
|
|
public function delete($tableName, array $identifier)
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
$criteria = array();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
foreach (array_keys($identifier) as $id) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$criteria[] = $id . ' = ?';
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$query = 'DELETE FROM ' . $tableName . ' WHERE ' . implode(' AND ', $criteria);
|
2009-05-28 15:30:27 +04:00
|
|
|
|
2009-08-11 02:43:27 +04:00
|
|
|
return $this->executeUpdate($query, array_values($identifier));
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
unset($this->_conn);
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
$this->_isConnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the transaction isolation level.
|
|
|
|
*
|
|
|
|
* @param integer $level The level to set.
|
|
|
|
*/
|
|
|
|
public function setTransactionIsolation($level)
|
|
|
|
{
|
|
|
|
$this->_transactionIsolationLevel = $level;
|
2009-08-11 02:43:27 +04:00
|
|
|
|
|
|
|
return $this->executeUpdate($this->_platform->getSetTransactionIsolationSql($level));
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currently active transaction isolation level.
|
|
|
|
*
|
|
|
|
* @return integer The current transaction isolation level.
|
|
|
|
*/
|
|
|
|
public function getTransactionIsolation()
|
|
|
|
{
|
|
|
|
return $this->_transactionIsolationLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates table row(s) with specified data
|
|
|
|
*
|
|
|
|
* @throws Doctrine\DBAL\ConnectionException if something went wrong at the database level
|
|
|
|
* @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)
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
if (empty($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = array();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
foreach ($data as $columnName => $value) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$set[] = $columnName . ' = ?';
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$params = array_merge(array_values($data), array_values($identifier));
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $set)
|
|
|
|
. ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
|
|
|
|
. ' = ?';
|
2009-05-28 15:30:27 +04:00
|
|
|
|
2009-08-11 02:43:27 +04:00
|
|
|
return $this->executeUpdate($sql, $params);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
if (empty($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// column names are specified as array keys
|
|
|
|
$cols = array();
|
|
|
|
$a = array();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
foreach ($data as $columnName => $value) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$cols[] = $columnName;
|
2009-05-28 15:30:27 +04:00
|
|
|
$a[] = '?';
|
|
|
|
}
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$query = 'INSERT INTO ' . $tableName
|
2009-08-11 02:43:27 +04:00
|
|
|
. ' (' . implode(', ', $cols) . ')'
|
|
|
|
. ' VALUES (' . implode(', ', $a) . ')';
|
2009-05-28 15:30:27 +04:00
|
|
|
|
2009-08-11 02:43:27 +04:00
|
|
|
return $this->executeUpdate($query, array_values($data));
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the charset on the current connection
|
|
|
|
*
|
|
|
|
* @param string charset
|
|
|
|
*/
|
|
|
|
public function setCharset($charset)
|
|
|
|
{
|
2009-08-11 02:43:27 +04:00
|
|
|
$this->executeUpdate($this->_platform->getSetCharsetSql($charset));
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quote a string so it can be safely used as a table or column name, even if
|
|
|
|
* it is a reserved name.
|
|
|
|
*
|
|
|
|
* Delimiting style depends on the underlying database platform that is being used.
|
|
|
|
*
|
|
|
|
* NOTE: Just because you CAN use delimited identifiers doesn't mean
|
|
|
|
* you SHOULD use them. In general, they end up causing way more
|
|
|
|
* problems than they solve.
|
|
|
|
*
|
|
|
|
* @param string $str identifier name to be quoted
|
|
|
|
* @return string quoted identifier string
|
|
|
|
*/
|
|
|
|
public function quoteIdentifier($str)
|
|
|
|
{
|
2009-05-29 14:23:13 +04:00
|
|
|
return $this->_platform->quoteIdentifier($str);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quotes a given input parameter.
|
|
|
|
*
|
|
|
|
* @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-09-05 22:27:37 +04:00
|
|
|
$this->connect();
|
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
return $this->_conn->quote($input, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-09 15:37:57 +03:00
|
|
|
* Prepares and executes an SQL query and returns the result as an associative array.
|
2009-05-28 15:30:27 +04:00
|
|
|
*
|
|
|
|
* @param string $sql The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetchAll($sql, array $params = array())
|
|
|
|
{
|
2009-05-29 19:38:46 +04:00
|
|
|
return $this->execute($sql, $params)->fetchAll(Connection::FETCH_ASSOC);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares an SQL statement.
|
|
|
|
*
|
2009-12-09 15:37:57 +03:00
|
|
|
* @param string $statement The SQL statement to prepare.
|
|
|
|
* @return Statement The prepared statement.
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
|
|
|
public function prepare($statement)
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
return $this->_conn->prepare($statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-09 15:37:57 +03:00
|
|
|
* Prepares and executes an SQL query.
|
2009-05-28 15:30:27 +04:00
|
|
|
*
|
2009-12-09 15:37:57 +03:00
|
|
|
* @param string $query The SQL query to prepare and execute.
|
|
|
|
* @param array $params The parameters, if any.
|
|
|
|
* @return Statement The prepared and executed statement.
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
|
|
|
public function execute($query, array $params = array())
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
if ($this->_config->getSqlLogger()) {
|
|
|
|
$this->_config->getSqlLogger()->logSql($query, $params);
|
|
|
|
}
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
if ( ! empty($params)) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$stmt = $this->_conn->prepare($query);
|
2009-05-28 15:30:27 +04:00
|
|
|
$stmt->execute($params);
|
|
|
|
} else {
|
|
|
|
$stmt = $this->_conn->query($query);
|
|
|
|
}
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
return $stmt;
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
2009-12-09 15:37:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares and executes an SQL query and returns the result, optionally applying a
|
|
|
|
* transformation on the rows of the result.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query to execute.
|
|
|
|
* @param array $params The parameters, if any.
|
|
|
|
* @param Closure $mapper The transformation function that is applied on each row.
|
|
|
|
* The function receives a single paramater, an array, that
|
|
|
|
* represents a row of the result set.
|
|
|
|
* @return mixed The (possibly transformed) result of the query.
|
|
|
|
*/
|
|
|
|
public function query($query, array $params = array(), \Closure $mapper = null)
|
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
$stmt = $this->execute($query, $params);
|
|
|
|
|
|
|
|
while ($row = $stmt->fetch()) {
|
|
|
|
if ($mapper === null) {
|
|
|
|
$result[] = $row;
|
|
|
|
} else {
|
|
|
|
$result[] = $mapper($row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2009-05-28 15:30:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters.
|
|
|
|
*
|
|
|
|
* @param string $query sql query
|
|
|
|
* @param array $params query parameters
|
2009-05-30 17:57:57 +04:00
|
|
|
* @return integer
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
2009-08-11 02:43:27 +04:00
|
|
|
public function executeUpdate($query, array $params = array())
|
2009-05-28 15:30:27 +04:00
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
if ($this->_config->getSqlLogger()) {
|
|
|
|
$this->_config->getSqlLogger()->logSql($query, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($params)) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$stmt = $this->_conn->prepare($query);
|
2009-05-28 15:30:27 +04:00
|
|
|
$stmt->execute($params);
|
2009-06-14 21:34:28 +04:00
|
|
|
$result = $stmt->rowCount();
|
2009-05-28 15:30:27 +04:00
|
|
|
} else {
|
2009-06-14 21:34:28 +04:00
|
|
|
$result = $this->_conn->exec($query);
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
return $result;
|
2009-05-28 15:30:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current transaction nesting level.
|
|
|
|
*
|
2009-12-16 00:06:32 +03:00
|
|
|
* @return integer The nesting level. A value of 0 means theres no active transaction.
|
2009-05-28 15:30:27 +04:00
|
|
|
*/
|
|
|
|
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();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
return $this->_conn->errorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch extended error information associated with the last operation on the database handle
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function errorInfo()
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:30:27 +04:00
|
|
|
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.
|
|
|
|
*/
|
2009-05-28 15:26:08 +04:00
|
|
|
public function lastInsertId($seqName = null)
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
return $this->_conn->lastInsertId($seqName);
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
/**
|
2009-10-24 01:47:25 +04:00
|
|
|
* Start a transaction by suspending auto-commit mode.
|
2009-05-28 15:26:08 +04:00
|
|
|
*
|
2009-10-23 19:03:00 +04:00
|
|
|
* @return void
|
2009-05-28 15:26:08 +04:00
|
|
|
*/
|
|
|
|
public function beginTransaction()
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
|
|
|
$this->_conn->beginTransaction();
|
|
|
|
}
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
++$this->_transactionNestingLevel;
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
/**
|
2009-10-24 01:47:25 +04:00
|
|
|
* Commits the current transaction.
|
2009-05-28 15:26:08 +04:00
|
|
|
*
|
2009-10-23 19:03:00 +04:00
|
|
|
* @return void
|
2009-10-24 01:47:25 +04:00
|
|
|
* @throws ConnectionException If the commit failed due to no active transaction or
|
|
|
|
* because the transaction was marked for rollback only.
|
2009-05-28 15:26:08 +04:00
|
|
|
*/
|
|
|
|
public function commit()
|
|
|
|
{
|
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
|
|
|
throw ConnectionException::commitFailedNoActiveTransaction();
|
|
|
|
}
|
2009-10-23 19:03:00 +04:00
|
|
|
if ($this->_isRollbackOnly) {
|
|
|
|
throw ConnectionException::commitFailedRollbackOnly();
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
$this->connect();
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:26:08 +04:00
|
|
|
if ($this->_transactionNestingLevel == 1) {
|
|
|
|
$this->_conn->commit();
|
|
|
|
}
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
--$this->_transactionNestingLevel;
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2009-12-09 15:37:57 +03:00
|
|
|
* @throws ConnectionException If the rollback operation failed.
|
2009-05-28 15:24:22 +04:00
|
|
|
*/
|
|
|
|
public function rollback()
|
2009-05-28 15:26:08 +04:00
|
|
|
{
|
2009-05-28 15:24:22 +04:00
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
|
|
|
throw ConnectionException::rollbackFailedNoActiveTransaction();
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
$this->connect();
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
if ($this->_transactionNestingLevel == 1) {
|
|
|
|
$this->_transactionNestingLevel = 0;
|
|
|
|
$this->_conn->rollback();
|
2009-10-23 19:03:00 +04:00
|
|
|
$this->_isRollbackOnly = false;
|
2009-07-17 22:13:03 +04:00
|
|
|
} else {
|
2009-10-24 01:47:25 +04:00
|
|
|
$this->_isRollbackOnly = true;
|
2009-07-17 22:13:03 +04:00
|
|
|
--$this->_transactionNestingLevel;
|
2009-05-28 15:24:22 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
/**
|
|
|
|
* Gets the wrapped driver connection.
|
|
|
|
*
|
|
|
|
* @return Doctrine\DBAL\Driver\Connection
|
|
|
|
*/
|
|
|
|
public function getWrappedConnection()
|
|
|
|
{
|
|
|
|
$this->connect();
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
return $this->_conn;
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
/**
|
|
|
|
* Gets the SchemaManager that can be used to inspect or change the
|
|
|
|
* database schema through the connection.
|
|
|
|
*
|
|
|
|
* @return Doctrine\DBAL\Schema\SchemaManager
|
|
|
|
*/
|
|
|
|
public function getSchemaManager()
|
|
|
|
{
|
|
|
|
if ( ! $this->_schemaManager) {
|
|
|
|
$this->_schemaManager = $this->_driver->getSchemaManager($this);
|
|
|
|
}
|
2009-08-11 02:43:27 +04:00
|
|
|
|
2009-05-28 15:24:22 +04:00
|
|
|
return $this->_schemaManager;
|
|
|
|
}
|
2009-10-23 19:03:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the current transaction so that the only possible
|
|
|
|
* outcome for the transaction to be rolled back.
|
|
|
|
*
|
|
|
|
* @throws BadMethodCallException If no transaction is active.
|
|
|
|
*/
|
|
|
|
public function setRollbackOnly()
|
|
|
|
{
|
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
|
|
|
throw ConnectionException::noActiveTransaction();
|
|
|
|
}
|
|
|
|
$this->_isRollbackOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the current transaction is marked for rollback only.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @throws BadMethodCallException If no transaction is active.
|
|
|
|
*/
|
|
|
|
public function getRollbackOnly()
|
|
|
|
{
|
|
|
|
if ($this->_transactionNestingLevel == 0) {
|
|
|
|
throw ConnectionException::noActiveTransaction();
|
|
|
|
}
|
|
|
|
return $this->_isRollbackOnly;
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|