1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309 |
- <?php
-
-
- namespace Doctrine\DBAL;
-
- use PDO, Closure, Exception,
- Doctrine\DBAL\Types\Type,
- Doctrine\DBAL\Driver\Connection as DriverConnection,
- Doctrine\Common\EventManager,
- Doctrine\DBAL\DBALException,
- Doctrine\DBAL\Cache\ResultCacheStatement,
- Doctrine\DBAL\Cache\QueryCacheProfile,
- Doctrine\DBAL\Cache\ArrayStatement,
- Doctrine\DBAL\Cache\CacheException;
-
-
- class Connection implements DriverConnection
- {
-
-
- const TRANSACTION_READ_UNCOMMITTED = 1;
-
-
-
- const TRANSACTION_READ_COMMITTED = 2;
-
-
-
- const TRANSACTION_REPEATABLE_READ = 3;
-
-
-
- const TRANSACTION_SERIALIZABLE = 4;
-
-
-
- const PARAM_INT_ARRAY = 101;
-
-
-
- const PARAM_STR_ARRAY = 102;
-
-
-
- const ARRAY_PARAM_OFFSET = 100;
-
-
-
- protected $_conn;
-
-
-
- protected $_config;
-
-
-
- protected $_eventManager;
-
-
-
- protected $_expr;
-
-
-
- private $_isConnected = false;
-
-
-
- private $_transactionNestingLevel = 0;
-
-
-
- private $_transactionIsolationLevel;
-
-
-
- private $_nestTransactionsWithSavepoints;
-
-
-
- private $_params = array();
-
-
-
- protected $_platform;
-
-
-
- protected $_schemaManager;
-
-
-
- protected $_driver;
-
-
-
- private $_isRollbackOnly = false;
-
- private $_defaultFetchMode = PDO::FETCH_ASSOC;
-
-
-
- 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;
- }
-
-
- 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();
- }
-
-
-
- public function getParams()
- {
- return $this->_params;
- }
-
-
-
- public function getDatabase()
- {
- return $this->_driver->getDatabase($this);
- }
-
-
-
- public function getHost()
- {
- return isset($this->_params['host']) ? $this->_params['host'] : null;
- }
-
-
-
- public function getPort()
- {
- return isset($this->_params['port']) ? $this->_params['port'] : null;
- }
-
-
-
- public function getUsername()
- {
- return isset($this->_params['user']) ? $this->_params['user'] : null;
- }
-
-
-
- public function getPassword()
- {
- return isset($this->_params['password']) ? $this->_params['password'] : null;
- }
-
-
-
- public function getDriver()
- {
- return $this->_driver;
- }
-
-
-
- public function getConfiguration()
- {
- return $this->_config;
- }
-
-
-
- public function getEventManager()
- {
- return $this->_eventManager;
- }
-
-
-
- public function getDatabasePlatform()
- {
- return $this->_platform;
- }
-
-
-
- public function getExpressionBuilder()
- {
- return $this->_expr;
- }
-
-
-
- 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;
- }
-
-
-
- public function setFetchMode($fetchMode)
- {
- $this->_defaultFetchMode = $fetchMode;
- }
-
-
-
- public function fetchAssoc($statement, array $params = array())
- {
- return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC);
- }
-
-
-
- public function fetchArray($statement, array $params = array())
- {
- return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_NUM);
- }
-
-
-
- public function fetchColumn($statement, array $params = array(), $colnum = 0)
- {
- return $this->executeQuery($statement, $params)->fetchColumn($colnum);
- }
-
-
-
- public function isConnected()
- {
- return $this->_isConnected;
- }
-
-
-
- public function isTransactionActive()
- {
- return $this->_transactionNestingLevel > 0;
- }
-
-
-
- public function delete($tableName, array $identifier)
- {
- $this->connect();
-
- $criteria = array();
-
- foreach (array_keys($identifier) as $columnName) {
- $criteria[] = $columnName . ' = ?';
- }
-
- $query = 'DELETE FROM ' . $tableName . ' WHERE ' . implode(' AND ', $criteria);
-
- return $this->executeUpdate($query, array_values($identifier));
- }
-
-
-
- public function close()
- {
- unset($this->_conn);
-
- $this->_isConnected = false;
- }
-
-
-
- public function setTransactionIsolation($level)
- {
- $this->_transactionIsolationLevel = $level;
-
- return $this->executeUpdate($this->_platform->getSetTransactionIsolationSQL($level));
- }
-
-
-
- public function getTransactionIsolation()
- {
- return $this->_transactionIsolationLevel;
- }
-
-
-
- public function update($tableName, array $data, array $identifier, array $types = array())
- {
- $this->connect();
- $set = array();
- foreach ($data as $columnName => $value) {
- $set[] = $columnName . ' = ?';
- }
-
- $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);
- }
-
-
-
- public function insert($tableName, array $data, array $types = array())
- {
- $this->connect();
-
-
- $cols = array();
- $placeholders = array();
-
- foreach ($data as $columnName => $value) {
- $cols[] = $columnName;
- $placeholders[] = '?';
- }
-
- $query = 'INSERT INTO ' . $tableName
- . ' (' . implode(', ', $cols) . ')'
- . ' VALUES (' . implode(', ', $placeholders) . ')';
-
- return $this->executeUpdate($query, array_values($data), $types);
- }
-
-
-
- public function quoteIdentifier($str)
- {
- return $this->_platform->quoteIdentifier($str);
- }
-
-
-
- public function quote($input, $type = null)
- {
- $this->connect();
-
- list($value, $bindingType) = $this->getBindingInfo($input, $type);
- return $this->_conn->quote($value, $bindingType);
- }
-
-
-
- public function fetchAll($sql, array $params = array())
- {
- return $this->executeQuery($sql, $params)->fetchAll();
- }
-
-
-
- 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;
- }
-
-
-
- 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;
- }
-
-
-
- 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);
-
-
- if ($data = $resultCache->fetch($cacheKey)) {
-
- 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;
- }
-
-
-
- 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;
- }
-
-
-
- public function query()
- {
- $this->connect();
-
- $args = func_get_args();
-
- $logger = $this->_config->getSQLLogger();
- if ($logger) {
- $logger->startQuery($args[0]);
- }
-
- try {
- $statement = call_user_func_array(array($this->_conn, 'query'), $args);
- } catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, func_get_arg(0));
- }
-
- $statement->setFetchMode($this->_defaultFetchMode);
-
- if ($logger) {
- $logger->stopQuery();
- }
-
- return $statement;
- }
-
-
-
- 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;
- }
-
-
-
- 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;
- }
-
-
-
- public function getTransactionNestingLevel()
- {
- return $this->_transactionNestingLevel;
- }
-
-
-
- public function errorCode()
- {
- $this->connect();
- return $this->_conn->errorCode();
- }
-
-
-
- public function errorInfo()
- {
- $this->connect();
- return $this->_conn->errorInfo();
- }
-
-
-
- public function lastInsertId($seqName = null)
- {
- $this->connect();
- return $this->_conn->lastInsertId($seqName);
- }
-
-
-
- public function transactional(Closure $func)
- {
- $this->beginTransaction();
- try {
- $func($this);
- $this->commit();
- } catch (Exception $e) {
- $this->rollback();
- throw $e;
- }
- }
-
-
-
- public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
- {
- if ($this->_transactionNestingLevel > 0) {
- throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
- }
-
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
-
- $this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
- }
-
-
-
- public function getNestTransactionsWithSavepoints()
- {
- return $this->_nestTransactionsWithSavepoints;
- }
-
-
-
- protected function _getNestedTransactionSavePointName()
- {
- return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
- }
-
-
-
- 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();
- }
- }
- }
-
-
-
- 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;
- }
-
-
-
- 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;
- }
- }
-
-
-
- public function createSavepoint($savepoint)
- {
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
-
- $this->_conn->exec($this->_platform->createSavePoint($savepoint));
- }
-
-
-
- public function releaseSavepoint($savepoint)
- {
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
-
- if ($this->_platform->supportsReleaseSavepoints()) {
- $this->_conn->exec($this->_platform->releaseSavePoint($savepoint));
- }
- }
-
-
-
- public function rollbackSavepoint($savepoint)
- {
- if ( ! $this->_platform->supportsSavepoints()) {
- throw ConnectionException::savepointsNotSupported();
- }
-
- $this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
- }
-
-
-
- public function getWrappedConnection()
- {
- $this->connect();
-
- return $this->_conn;
- }
-
-
-
- public function getSchemaManager()
- {
- if ( ! $this->_schemaManager) {
- $this->_schemaManager = $this->_driver->getSchemaManager($this);
- }
-
- return $this->_schemaManager;
- }
-
-
-
- public function setRollbackOnly()
- {
- if ($this->_transactionNestingLevel == 0) {
- throw ConnectionException::noActiveTransaction();
- }
- $this->_isRollbackOnly = true;
- }
-
-
-
- public function isRollbackOnly()
- {
- if ($this->_transactionNestingLevel == 0) {
- throw ConnectionException::noActiveTransaction();
- }
- return $this->_isRollbackOnly;
- }
-
-
-
- public function convertToDatabaseValue($value, $type)
- {
- return Type::getType($type)->convertToDatabaseValue($value, $this->_platform);
- }
-
-
-
- public function convertToPHPValue($value, $type)
- {
- return Type::getType($type)->convertToPHPValue($value, $this->_platform);
- }
-
-
-
- private function _bindTypedValues($stmt, array $params, array $types)
- {
-
- if (is_int(key($params))) {
-
- $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 {
-
- 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);
- }
- }
- }
- }
-
-
-
- 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;
- }
- return array($value, $bindingType);
- }
-
-
-
- public function resolveParams(array $params, array $types)
- {
- $resolvedParams = array();
-
-
- if (is_int(key($params))) {
-
- $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 {
-
- 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;
- }
-
-
-
- public function createQueryBuilder()
- {
- return new Query\QueryBuilder($this);
- }
- }
|