123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410 |
- <?php
- /*
- * 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 MIT license. For more information, see
- * <http://www.doctrine-project.org>.
- */
- namespace Doctrine\DBAL;
- use PDO;
- use Closure;
- use Exception;
- use Doctrine\DBAL\Types\Type;
- use Doctrine\DBAL\Driver\Connection as DriverConnection;
- use Doctrine\Common\EventManager;
- use Doctrine\DBAL\DBALException;
- use Doctrine\DBAL\Cache\ResultCacheStatement;
- use Doctrine\DBAL\Cache\QueryCacheProfile;
- use Doctrine\DBAL\Cache\ArrayStatement;
- use Doctrine\DBAL\Cache\CacheException;
- /**
- * A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
- * events, transaction isolation levels, configuration, emulated transaction nesting,
- * lazy connecting and more.
- *
- * @link www.doctrine-project.org
- * @since 2.0
- * @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)
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- */
- class Connection implements DriverConnection
- {
- /**
- * Constant for transaction isolation level READ UNCOMMITTED.
- */
- const TRANSACTION_READ_UNCOMMITTED = 1;
- /**
- * Constant for transaction isolation level READ COMMITTED.
- */
- const TRANSACTION_READ_COMMITTED = 2;
- /**
- * Constant for transaction isolation level REPEATABLE READ.
- */
- const TRANSACTION_REPEATABLE_READ = 3;
- /**
- * Constant for transaction isolation level SERIALIZABLE.
- */
- const TRANSACTION_SERIALIZABLE = 4;
- /**
- * Represents an array of ints to be expanded by Doctrine SQL parsing.
- *
- * @var integer
- */
- const PARAM_INT_ARRAY = 101;
- /**
- * Represents an array of strings to be expanded by Doctrine SQL parsing.
- *
- * @var integer
- */
- const PARAM_STR_ARRAY = 102;
- /**
- * Offset by which PARAM_* constants are detected as arrays of the param type.
- *
- * @var integer
- */
- const ARRAY_PARAM_OFFSET = 100;
- /**
- * The wrapped driver connection.
- *
- * @var \Doctrine\DBAL\Driver\Connection
- */
- protected $_conn;
- /**
- * @var \Doctrine\DBAL\Configuration
- */
- protected $_config;
- /**
- * @var \Doctrine\Common\EventManager
- */
- protected $_eventManager;
- /**
- * @var \Doctrine\DBAL\Query\Expression\ExpressionBuilder
- */
- protected $_expr;
- /**
- * Whether or not a connection has been established.
- *
- * @var boolean
- */
- private $_isConnected = false;
- /**
- * The transaction nesting level.
- *
- * @var integer
- */
- private $_transactionNestingLevel = 0;
- /**
- * The currently active transaction isolation level.
- *
- * @var integer
- */
- private $_transactionIsolationLevel;
- /**
- * If nested transactions should use savepoints.
- *
- * @var integer
- */
- private $_nestTransactionsWithSavepoints;
- /**
- * The parameters used during creation of the Connection instance.
- *
- * @var array
- */
- private $_params = array();
- /**
- * The DatabasePlatform object that provides information about the
- * database platform used by the connection.
- *
- * @var \Doctrine\DBAL\Platforms\AbstractPlatform
- */
- protected $_platform;
- /**
- * The schema manager.
- *
- * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
- */
- protected $_schemaManager;
- /**
- * The used DBAL driver.
- *
- * @var \Doctrine\DBAL\Driver
- */
- protected $_driver;
- /**
- * Flag that indicates whether the current transaction is marked for rollback only.
- *
- * @var boolean
- */
- private $_isRollbackOnly = false;
- /**
- * @var integer
- */
- protected $defaultFetchMode = PDO::FETCH_ASSOC;
- /**
- * Initializes a new instance of the Connection class.
- *
- * @param array $params The connection parameters.
- * @param \Doctrine\DBAL\Driver $driver The driver to use.
- * @param \Doctrine\DBAL\Configuration|null $config The configuration, optional.
- * @param \Doctrine\Common\EventManager|null $eventManager The event manager, optional.
- *
- * @throws \Doctrine\DBAL\DBALException
- */
- public function __construct(array $params, Driver $driver, Configuration $config = null,
- EventManager $eventManager = null)
- {
- $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) {
- $config = new Configuration();
- }
- if ( ! $eventManager) {
- $eventManager = new EventManager();
- }
- $this->_config = $config;
- $this->_eventManager = $eventManager;
- $this->_expr = new Query\Expression\ExpressionBuilder($this);
- if ( ! isset($params['platform'])) {
- $this->_platform = $driver->getDatabasePlatform();
- } else if ($params['platform'] instanceof Platforms\AbstractPlatform) {
- $this->_platform = $params['platform'];
- } else {
- throw DBALException::invalidPlatformSpecified();
- }
- $this->_platform->setEventManager($eventManager);
- $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
- }
- /**
- * Gets the parameters used during instantiation.
- *
- * @return array
- */
- public function getParams()
- {
- return $this->_params;
- }
- /**
- * Gets the name of the database this Connection is connected to.
- *
- * @return string
- */
- public function getDatabase()
- {
- return $this->_driver->getDatabase($this);
- }
- /**
- * Gets the hostname of the currently connected database.
- *
- * @return string|null
- */
- public function getHost()
- {
- return isset($this->_params['host']) ? $this->_params['host'] : null;
- }
- /**
- * Gets the port of the currently connected database.
- *
- * @return mixed
- */
- public function getPort()
- {
- return isset($this->_params['port']) ? $this->_params['port'] : null;
- }
- /**
- * Gets the username used by this connection.
- *
- * @return string|null
- */
- public function getUsername()
- {
- return isset($this->_params['user']) ? $this->_params['user'] : null;
- }
- /**
- * Gets the password used by this connection.
- *
- * @return string|null
- */
- public function getPassword()
- {
- return isset($this->_params['password']) ? $this->_params['password'] : null;
- }
- /**
- * 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;
- }
- /**
- * Gets the ExpressionBuilder for the connection.
- *
- * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder
- */
- public function getExpressionBuilder()
- {
- return $this->_expr;
- }
- /**
- * Establishes the connection with the database.
- *
- * @return boolean TRUE if the connection was successfully established, FALSE if
- * the connection is already open.
- */
- public function connect()
- {
- if ($this->_isConnected) return false;
- $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;
- $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);
- $this->_isConnected = true;
- if ($this->_eventManager->hasListeners(Events::postConnect)) {
- $eventArgs = new Event\ConnectionEventArgs($this);
- $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
- }
- return true;
- }
- /**
- * Sets the fetch mode.
- *
- * @param integer $fetchMode
- *
- * @return void
- */
- public function setFetchMode($fetchMode)
- {
- $this->defaultFetchMode = $fetchMode;
- }
- /**
- * Prepares and executes an SQL query and returns the first row of the result
- * as an associative array.
- *
- * @param string $statement The SQL query.
- * @param array $params The query parameters.
- *
- * @return array
- */
- public function fetchAssoc($statement, array $params = array())
- {
- return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC);
- }
- /**
- * Prepares and executes an SQL query and returns the first row of the result
- * as a numerically indexed array.
- *
- * @param string $statement The SQL query to be executed.
- * @param array $params The prepared statement params.
- *
- * @return array
- */
- public function fetchArray($statement, array $params = array())
- {
- return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_NUM);
- }
- /**
- * Prepares and executes an SQL query and returns the value of a single column
- * of the first row of the result.
- *
- * @param string $statement The SQL query to be executed.
- * @param array $params The prepared statement params.
- * @param integer $column The 0-indexed column number to retrieve.
- *
- * @return mixed
- */
- public function fetchColumn($statement, array $params = array(), $column = 0)
- {
- return $this->executeQuery($statement, $params)->fetchColumn($column);
- }
- /**
- * Whether an actual connection to the database is established.
- *
- * @return boolean
- */
- public function isConnected()
- {
- return $this->_isConnected;
- }
- /**
- * Checks whether a transaction is currently active.
- *
- * @return boolean TRUE if a transaction is currently active, FALSE otherwise.
- */
- public function isTransactionActive()
- {
- return $this->_transactionNestingLevel > 0;
- }
- /**
- * Executes an SQL DELETE statement on a table.
- *
- * @param string $tableName The name of the table on which to delete.
- * @param array $identifier The deletion criteria. An associative array containing column-value pairs.
- * @param array $types The types of identifiers.
- *
- * @return integer The number of affected rows.
- */
- public function delete($tableName, array $identifier, array $types = array())
- {
- $this->connect();
- $criteria = array();
- foreach (array_keys($identifier) as $columnName) {
- $criteria[] = $columnName . ' = ?';
- }
- if ( ! is_int(key($types))) {
- $types = $this->extractTypeValues($identifier, $types);
- }
- $query = 'DELETE FROM ' . $tableName . ' WHERE ' . implode(' AND ', $criteria);
- return $this->executeUpdate($query, array_values($identifier), $types);
- }
- /**
- * Closes the connection.
- *
- * @return void
- */
- public function close()
- {
- unset($this->_conn);
- $this->_isConnected = false;
- }
- /**
- * Sets the transaction isolation level.
- *
- * @param integer $level The level to set.
- *
- * @return integer
- */
- public function setTransactionIsolation($level)
- {
- $this->_transactionIsolationLevel = $level;
- return $this->executeUpdate($this->_platform->getSetTransactionIsolationSQL($level));
- }
- /**
- * Gets the currently active transaction isolation level.
- *
- * @return integer The current transaction isolation level.
- */
- public function getTransactionIsolation()
- {
- return $this->_transactionIsolationLevel;
- }
- /**
- * Executes an SQL UPDATE statement on a table.
- *
- * @param string $tableName The name of the table to update.
- * @param array $data An associative array containing column-value pairs.
- * @param array $identifier The update criteria. An associative array containing column-value pairs.
- * @param array $types Types of the merged $data and $identifier arrays in that order.
- *
- * @return integer The number of affected rows.
- */
- public function update($tableName, array $data, array $identifier, array $types = array())
- {
- $this->connect();
- $set = array();
- foreach ($data as $columnName => $value) {
- $set[] = $columnName . ' = ?';
- }
- if ( ! is_int(key($types))) {
- $types = $this->extractTypeValues(array_merge($data, $identifier), $types);
- }
- $params = array_merge(array_values($data), array_values($identifier));
- $sql = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $set)
- . ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
- . ' = ?';
- return $this->executeUpdate($sql, $params, $types);
- }
- /**
- * Inserts a table row with specified data.
- *
- * @param string $tableName The name of the table to insert data into.
- * @param array $data An associative array containing column-value pairs.
- * @param array $types Types of the inserted data.
- *
- * @return integer The number of affected rows.
- */
- public function insert($tableName, array $data, array $types = array())
- {
- $this->connect();
- if ( ! is_int(key($types))) {
- $types = $this->extractTypeValues($data, $types);
- }
- $query = 'INSERT INTO ' . $tableName
- . ' (' . implode(', ', array_keys($data)) . ')'
- . ' VALUES (' . implode(', ', array_fill(0, count($data), '?')) . ')';
- return $this->executeUpdate($query, array_values($data), $types);
- }
- /**
- * Extract ordered type list from two associate key lists of data and types.
- *
- * @param array $data
- * @param array $types
- *
- * @return array
- */
- private function extractTypeValues(array $data, array $types)
- {
- $typeValues = array();
- foreach ($data as $k => $_) {
- $typeValues[] = isset($types[$k])
- ? $types[$k]
- : \PDO::PARAM_STR;
- }
- return $typeValues;
- }
- /**
- * Quotes 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 quoted identifiers does not mean
- * you SHOULD use them. In general, they end up causing way more
- * problems than they solve.
- *
- * @param string $str The name to be quoted.
- *
- * @return string The quoted name.
- */
- public function quoteIdentifier($str)
- {
- return $this->_platform->quoteIdentifier($str);
- }
- /**
- * Quotes a given input parameter.
- *
- * @param mixed $input The parameter to be quoted.
- * @param string|null $type The type of the parameter.
- *
- * @return string The quoted parameter.
- */
- public function quote($input, $type = null)
- {
- $this->connect();
- list($value, $bindingType) = $this->getBindingInfo($input, $type);
- return $this->_conn->quote($value, $bindingType);
- }
- /**
- * Prepares and executes an SQL query and returns the result as an associative array.
- *
- * @param string $sql The SQL query.
- * @param array $params The query parameters.
- * @param array $types The query parameter types.
- *
- * @return array
- */
- public function fetchAll($sql, array $params = array(), $types = array())
- {
- return $this->executeQuery($sql, $params, $types)->fetchAll();
- }
- /**
- * Prepares an SQL statement.
- *
- * @param string $statement The SQL statement to prepare.
- *
- * @return \Doctrine\DBAL\Driver\Statement The prepared statement.
- *
- * @throws \Doctrine\DBAL\DBALException
- */
- public function prepare($statement)
- {
- $this->connect();
- try {
- $stmt = new Statement($statement, $this);
- } catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $statement);
- }
- $stmt->setFetchMode($this->defaultFetchMode);
- return $stmt;
- }
- /**
- * Executes an, optionally parametrized, SQL query.
- *
- * If the query is parametrized, a prepared statement is used.
- * If an SQLLogger is configured, the execution is logged.
- *
- * @param string $query The SQL query to execute.
- * @param array $params The parameters to bind to the query, if any.
- * @param array $types The types the previous parameters are in.
- * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
- *
- * @return \Doctrine\DBAL\Driver\Statement The executed statement.
- *
- * @throws \Doctrine\DBAL\DBALException
- *
- * @internal PERF: Directly prepares a driver statement, not a wrapper.
- */
- public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
- {
- if ($qcp !== null) {
- return $this->executeCacheQuery($query, $params, $types, $qcp);
- }
- $this->connect();
- $logger = $this->_config->getSQLLogger();
- if ($logger) {
- $logger->startQuery($query, $params, $types);
- }
- try {
- if ($params) {
- list($query, $params, $types) = SQLParserUtils::expandListParameters($query, $params, $types);
- $stmt = $this->_conn->prepare($query);
- if ($types) {
- $this->_bindTypedValues($stmt, $params, $types);
- $stmt->execute();
- } else {
- $stmt->execute($params);
- }
- } else {
- $stmt = $this->_conn->query($query);
- }
- } catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
- }
- $stmt->setFetchMode($this->defaultFetchMode);
- if ($logger) {
- $logger->stopQuery();
- }
- return $stmt;
- }
- /**
- * Executes a caching query.
- *
- * @param string $query The SQL query to execute.
- * @param array $params The parameters to bind to the query, if any.
- * @param array $types The types the previous parameters are in.
- * @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile.
- *
- * @return \Doctrine\DBAL\Driver\ResultStatement
- *
- * @throws \Doctrine\DBAL\Cache\CacheException
- */
- public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
- {
- $resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl();
- if ( ! $resultCache) {
- throw CacheException::noResultDriverConfigured();
- }
- list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types);
- // fetch the row pointers entry
- if ($data = $resultCache->fetch($cacheKey)) {
- // is the real key part of this row pointers map or is the cache only pointing to other cache keys?
- if (isset($data[$realKey])) {
- $stmt = new ArrayStatement($data[$realKey]);
- } else if (array_key_exists($realKey, $data)) {
- $stmt = new ArrayStatement(array());
- }
- }
- if (!isset($stmt)) {
- $stmt = new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
- }
- $stmt->setFetchMode($this->defaultFetchMode);
- return $stmt;
- }
- /**
- * Executes an, optionally parametrized, SQL query and returns the result,
- * applying a given projection/transformation function on each row of the result.
- *
- * @param string $query The SQL query to execute.
- * @param array $params The parameters, if any.
- * @param \Closure $function The transformation function that is applied on each row.
- * The function receives a single parameter, an array, that
- * represents a row of the result set.
- *
- * @return mixed The projected result of the query.
- */
- public function project($query, array $params, Closure $function)
- {
- $result = array();
- $stmt = $this->executeQuery($query, $params ?: array());
- while ($row = $stmt->fetch()) {
- $result[] = $function($row);
- }
- $stmt->closeCursor();
- return $result;
- }
- /**
- * Executes an SQL statement, returning a result set as a Statement object.
- *
- * @return \Doctrine\DBAL\Driver\Statement
- *
- * @throws \Doctrine\DBAL\DBALException
- */
- public function query()
- {
- $this->connect();
- $args = func_get_args();
- $logger = $this->_config->getSQLLogger();
- if ($logger) {
- $logger->startQuery($args[0]);
- }
- try {
- switch (func_num_args()) {
- case 1:
- $statement = $this->_conn->query($args[0]);
- break;
- case 2:
- $statement = $this->_conn->query($args[0], $args[1]);
- break;
- default:
- $statement = call_user_func_array(array($this->_conn, 'query'), $args);
- break;
- }
- } catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $args[0]);
- }
- $statement->setFetchMode($this->defaultFetchMode);
- if ($logger) {
- $logger->stopQuery();
- }
- return $statement;
- }
- /**
- * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
- * and returns the number of affected rows.
- *
- * This method supports PDO binding types as well as DBAL mapping types.
- *
- * @param string $query The SQL query.
- * @param array $params The query parameters.
- * @param array $types The parameter types.
- *
- * @return integer The number of affected rows.
- *
- * @throws \Doctrine\DBAL\DBALException
- *
- * @internal PERF: Directly prepares a driver statement, not a wrapper.
- */
- public function executeUpdate($query, array $params = array(), array $types = array())
- {
- $this->connect();
- $logger = $this->_config->getSQLLogger();
- if ($logger) {
- $logger->startQuery($query, $params, $types);
- }
- try {
- if ($params) {
- list($query, $params, $types) = SQLParserUtils::expandListParameters($query, $params, $types);
- $stmt = $this->_conn->prepare($query);
- if ($types) {
- $this->_bindTypedValues($stmt, $params, $types);
- $stmt->execute();
- } else {
- $stmt->execute($params);
- }
- $result = $stmt->rowCount();
- } else {
- $result = $this->_conn->exec($query);
- }
- } catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
- }
- if ($logger) {
- $logger->stopQuery();
- }
- return $result;
- }
- /**
- * Executes an SQL statement and return the number of affected rows.
- *
- * @param string $statement
- *
- * @return integer The number of affected rows.
- *
- * @throws \Doctrine\DBAL\DBALException
- */
- public function exec($statement)
- {
- $this->connect();
- $logger = $this->_config->getSQLLogger();
- if ($logger) {
- $logger->startQuery($statement);
- }
- try {
- $result = $this->_conn->exec($statement);
- } catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $statement);
- }
- if ($logger) {
- $logger->stopQuery();
- }
- return $result;
- }
- /**
- * Returns the current transaction nesting level.
- *
- * @return integer The nesting level. A value of 0 means there's no active transaction.
- */
- public function getTransactionNestingLevel()
- {
- return $this->_transactionNestingLevel;
- }
- /**
- * Fetches the SQLSTATE associated with the last database operation.
- *
- * @return integer The last error code.
- */
- public function errorCode()
- {
- $this->connect();
- return $this->_conn->errorCode();
- }
- /**
- * Fetches extended error information associated with the last database operation.
- *
- * @return array The last error information.
- */
- 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/IDENTITY
- * columns or sequences.
- *
- * @param string|null $seqName Name of the sequence object from which the ID should be returned.
- *
- * @return string A string representation of the last inserted ID.
- */
- public function lastInsertId($seqName = null)
- {
- $this->connect();
- return $this->_conn->lastInsertId($seqName);
- }
- /**
- * Executes a function in a transaction.
- *
- * The function gets passed this Connection instance as an (optional) parameter.
- *
- * If an exception occurs during execution of the function or transaction commit,
- * the transaction is rolled back and the exception re-thrown.
- *
- * @param \Closure $func The function to execute transactionally.
- *
- * @return void
- *
- * @throws \Exception
- */
- public function transactional(Closure $func)
- {
- $this->beginTransaction();
- try {
- $func($this);
- $this->commit();
- } catch (Exception $e) {
- $this->rollback();
- throw $e;
- }
- }
- /**
- * Sets if nested transactions should use savepoints.
- *
- * @param boolean $nestTransactionsWithSavepoints
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\ConnectionException
- */
- public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
- {
- if ($this->_transactionNestingLevel > 0) {
- throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
- }
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
- $this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
- }
- /**
- * Gets if nested transactions should use savepoints.
- *
- * @return boolean
- */
- public function getNestTransactionsWithSavepoints()
- {
- return $this->_nestTransactionsWithSavepoints;
- }
- /**
- * Returns the savepoint name to use for nested transactions are false if they are not supported
- * "savepointFormat" parameter is not set
- *
- * @return mixed A string with the savepoint name or false.
- */
- protected function _getNestedTransactionSavePointName()
- {
- return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
- }
- /**
- * Starts a transaction by suspending auto-commit mode.
- *
- * @return void
- */
- public function beginTransaction()
- {
- $this->connect();
- ++$this->_transactionNestingLevel;
- $logger = $this->_config->getSQLLogger();
- if ($this->_transactionNestingLevel == 1) {
- if ($logger) {
- $logger->startQuery('"START TRANSACTION"');
- }
- $this->_conn->beginTransaction();
- if ($logger) {
- $logger->stopQuery();
- }
- } else if ($this->_nestTransactionsWithSavepoints) {
- if ($logger) {
- $logger->startQuery('"SAVEPOINT"');
- }
- $this->createSavepoint($this->_getNestedTransactionSavePointName());
- if ($logger) {
- $logger->stopQuery();
- }
- }
- }
- /**
- * Commits the current transaction.
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\ConnectionException If the commit failed due to no active transaction or
- * because the transaction was marked for rollback only.
- */
- public function commit()
- {
- if ($this->_transactionNestingLevel == 0) {
- throw ConnectionException::noActiveTransaction();
- }
- if ($this->_isRollbackOnly) {
- throw ConnectionException::commitFailedRollbackOnly();
- }
- $this->connect();
- $logger = $this->_config->getSQLLogger();
- if ($this->_transactionNestingLevel == 1) {
- if ($logger) {
- $logger->startQuery('"COMMIT"');
- }
- $this->_conn->commit();
- if ($logger) {
- $logger->stopQuery();
- }
- } else if ($this->_nestTransactionsWithSavepoints) {
- if ($logger) {
- $logger->startQuery('"RELEASE SAVEPOINT"');
- }
- $this->releaseSavepoint($this->_getNestedTransactionSavePointName());
- if ($logger) {
- $logger->stopQuery();
- }
- }
- --$this->_transactionNestingLevel;
- }
- /**
- * Cancels any database changes done during the current transaction.
- *
- * This method can be listened with onPreTransactionRollback and onTransactionRollback
- * eventlistener methods.
- *
- * @throws \Doctrine\DBAL\ConnectionException If the rollback operation failed.
- */
- public function rollBack()
- {
- if ($this->_transactionNestingLevel == 0) {
- throw ConnectionException::noActiveTransaction();
- }
- $this->connect();
- $logger = $this->_config->getSQLLogger();
- if ($this->_transactionNestingLevel == 1) {
- if ($logger) {
- $logger->startQuery('"ROLLBACK"');
- }
- $this->_transactionNestingLevel = 0;
- $this->_conn->rollback();
- $this->_isRollbackOnly = false;
- if ($logger) {
- $logger->stopQuery();
- }
- } else if ($this->_nestTransactionsWithSavepoints) {
- if ($logger) {
- $logger->startQuery('"ROLLBACK TO SAVEPOINT"');
- }
- $this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
- --$this->_transactionNestingLevel;
- if ($logger) {
- $logger->stopQuery();
- }
- } else {
- $this->_isRollbackOnly = true;
- --$this->_transactionNestingLevel;
- }
- }
- /**
- * Creates a new savepoint.
- *
- * @param string $savepoint The name of the savepoint to create.
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\ConnectionException
- */
- public function createSavepoint($savepoint)
- {
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
- $this->_conn->exec($this->_platform->createSavePoint($savepoint));
- }
- /**
- * Releases the given savepoint.
- *
- * @param string $savepoint The name of the savepoint to release.
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\ConnectionException
- */
- public function releaseSavepoint($savepoint)
- {
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
- if ($this->_platform->supportsReleaseSavepoints()) {
- $this->_conn->exec($this->_platform->releaseSavePoint($savepoint));
- }
- }
- /**
- * Rolls back to the given savepoint.
- *
- * @param string $savepoint The name of the savepoint to rollback to.
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\ConnectionException
- */
- public function rollbackSavepoint($savepoint)
- {
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
- $this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
- }
- /**
- * Gets the wrapped driver connection.
- *
- * @return \Doctrine\DBAL\Driver\Connection
- */
- public function getWrappedConnection()
- {
- $this->connect();
- return $this->_conn;
- }
- /**
- * Gets the SchemaManager that can be used to inspect or change the
- * database schema through the connection.
- *
- * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
- */
- public function getSchemaManager()
- {
- if ( ! $this->_schemaManager) {
- $this->_schemaManager = $this->_driver->getSchemaManager($this);
- }
- return $this->_schemaManager;
- }
- /**
- * Marks the current transaction so that the only possible
- * outcome for the transaction to be rolled back.
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\ConnectionException If no transaction is active.
- */
- public function setRollbackOnly()
- {
- if ($this->_transactionNestingLevel == 0) {
- throw ConnectionException::noActiveTransaction();
- }
- $this->_isRollbackOnly = true;
- }
- /**
- * Checks whether the current transaction is marked for rollback only.
- *
- * @return boolean
- *
- * @throws \Doctrine\DBAL\ConnectionException If no transaction is active.
- */
- public function isRollbackOnly()
- {
- if ($this->_transactionNestingLevel == 0) {
- throw ConnectionException::noActiveTransaction();
- }
- return $this->_isRollbackOnly;
- }
- /**
- * Converts a given value to its database representation according to the conversion
- * rules of a specific DBAL mapping type.
- *
- * @param mixed $value The value to convert.
- * @param string $type The name of the DBAL mapping type.
- *
- * @return mixed The converted value.
- */
- public function convertToDatabaseValue($value, $type)
- {
- return Type::getType($type)->convertToDatabaseValue($value, $this->_platform);
- }
- /**
- * Converts a given value to its PHP representation according to the conversion
- * rules of a specific DBAL mapping type.
- *
- * @param mixed $value The value to convert.
- * @param string $type The name of the DBAL mapping type.
- *
- * @return mixed The converted type.
- */
- public function convertToPHPValue($value, $type)
- {
- return Type::getType($type)->convertToPHPValue($value, $this->_platform);
- }
- /**
- * Binds a set of parameters, some or all of which are typed with a PDO binding type
- * or DBAL mapping type, to a given statement.
- *
- * @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
- * @param array $params The map/list of named/positional parameters.
- * @param array $types The parameter types (PDO binding types or DBAL mapping types).
- *
- * @return void
- *
- * @internal Duck-typing used on the $stmt parameter to support driver statements as well as
- * raw PDOStatement instances.
- */
- private function _bindTypedValues($stmt, array $params, array $types)
- {
- // Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
- if (is_int(key($params))) {
- // Positional parameters
- $typeOffset = array_key_exists(0, $types) ? -1 : 0;
- $bindIndex = 1;
- foreach ($params as $value) {
- $typeIndex = $bindIndex + $typeOffset;
- if (isset($types[$typeIndex])) {
- $type = $types[$typeIndex];
- list($value, $bindingType) = $this->getBindingInfo($value, $type);
- $stmt->bindValue($bindIndex, $value, $bindingType);
- } else {
- $stmt->bindValue($bindIndex, $value);
- }
- ++$bindIndex;
- }
- } else {
- // Named parameters
- foreach ($params as $name => $value) {
- if (isset($types[$name])) {
- $type = $types[$name];
- list($value, $bindingType) = $this->getBindingInfo($value, $type);
- $stmt->bindValue($name, $value, $bindingType);
- } else {
- $stmt->bindValue($name, $value);
- }
- }
- }
- }
- /**
- * Gets the binding type of a given type. The given type can be a PDO or DBAL mapping type.
- *
- * @param mixed $value The value to bind.
- * @param mixed $type The type to bind (PDO or DBAL).
- *
- * @return array [0] => the (escaped) value, [1] => the binding type.
- */
- private function getBindingInfo($value, $type)
- {
- if (is_string($type)) {
- $type = Type::getType($type);
- }
- if ($type instanceof Type) {
- $value = $type->convertToDatabaseValue($value, $this->_platform);
- $bindingType = $type->getBindingType();
- } else {
- $bindingType = $type; // PDO::PARAM_* constants
- }
- return array($value, $bindingType);
- }
- /**
- * Resolves the parameters to a format which can be displayed.
- *
- * @internal This is a purely internal method. If you rely on this method, you are advised to
- * copy/paste the code as this method may change, or be removed without prior notice.
- *
- * @param array $params
- * @param array $types
- *
- * @return array
- */
- public function resolveParams(array $params, array $types)
- {
- $resolvedParams = array();
- // Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
- if (is_int(key($params))) {
- // Positional parameters
- $typeOffset = array_key_exists(0, $types) ? -1 : 0;
- $bindIndex = 1;
- foreach ($params as $value) {
- $typeIndex = $bindIndex + $typeOffset;
- if (isset($types[$typeIndex])) {
- $type = $types[$typeIndex];
- list($value,) = $this->getBindingInfo($value, $type);
- $resolvedParams[$bindIndex] = $value;
- } else {
- $resolvedParams[$bindIndex] = $value;
- }
- ++$bindIndex;
- }
- } else {
- // Named parameters
- foreach ($params as $name => $value) {
- if (isset($types[$name])) {
- $type = $types[$name];
- list($value,) = $this->getBindingInfo($value, $type);
- $resolvedParams[$name] = $value;
- } else {
- $resolvedParams[$name] = $value;
- }
- }
- }
- return $resolvedParams;
- }
- /**
- * Creates a new instance of a SQL query builder.
- *
- * @return \Doctrine\DBAL\Query\QueryBuilder
- */
- public function createQueryBuilder()
- {
- return new Query\QueryBuilder($this);
- }
- }
|