22use PDO, Closure, Exception,
88 private $_isConnected =
false;
95 private $_transactionNestingLevel = 0;
102 private $_transactionIsolationLevel;
109 private $_nestTransactionsWithSavepoints;
116 private $_params = array();
145 private $_isRollbackOnly =
false;
158 $this->_driver = $driver;
159 $this->_params = $params;
161 if (isset($params[
'pdo'])) {
162 $this->_conn = $params[
'pdo'];
163 $this->_isConnected =
true;
171 if ( ! $eventManager) {
175 $this->_config = $config;
176 $this->_eventManager = $eventManager;
177 if ( ! isset($params[
'platform'])) {
179 }
else if ($params[
'platform'] instanceof Platforms\AbstractPlatform) {
180 $this->_platform = $params[
'platform'];
184 $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
194 return $this->_params;
204 return $this->_driver->getDatabase($this);
214 return isset($this->_params[
'host']) ? $this->_params[
'host'] :
null;
224 return isset($this->_params[
'port']) ? $this->_params[
'port'] :
null;
234 return isset($this->_params[
'user']) ? $this->_params[
'user'] :
null;
244 return isset($this->_params[
'password']) ? $this->_params[
'password'] :
null;
295 if ($this->_isConnected)
return false;
297 $driverOptions = isset($this->_params[
'driverOptions']) ?
298 $this->_params[
'driverOptions'] : array();
299 $user = isset($this->_params[
'user']) ? $this->_params[
'user'] :
null;
300 $password = isset($this->_params[
'password']) ?
301 $this->_params[
'password'] :
null;
303 $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);
304 $this->_isConnected =
true;
322 public function fetchAssoc($statement, array $params = array())
324 return $this->
executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC);
335 public function fetchArray($statement, array $params = array())
337 return $this->
executeQuery($statement, $params)->fetch(PDO::FETCH_NUM);
349 public function fetchColumn($statement, array $params = array(), $colnum = 0)
351 return $this->
executeQuery($statement, $params)->fetchColumn($colnum);
361 return $this->_isConnected;
371 return $this->_transactionNestingLevel > 0;
381 public function delete($tableName, array $identifier)
387 foreach (array_keys($identifier) as $columnName) {
388 $criteria[] = $columnName .
' = ?';
391 $query =
'DELETE FROM ' . $tableName .
' WHERE ' . implode(
' AND ', $criteria);
393 return $this->
executeUpdate($query, array_values($identifier));
405 $this->_isConnected =
false;
415 $this->_transactionIsolationLevel = $level;
417 return $this->
executeUpdate($this->_platform->getSetTransactionIsolationSQL($level));
427 return $this->_transactionIsolationLevel;
437 public function update($tableName, array $data, array $identifier)
441 foreach ($data as $columnName => $value) {
442 $set[] = $columnName .
' = ?';
445 $params = array_merge(array_values($data), array_values($identifier));
447 $sql =
'UPDATE ' . $tableName .
' SET ' . implode(
', ', $set)
448 .
' WHERE ' . implode(
' = ? AND ', array_keys($identifier))
461 public function insert($tableName, array $data)
467 $placeholders = array();
469 foreach ($data as $columnName => $value) {
470 $cols[] = $columnName;
471 $placeholders[] =
'?';
474 $query =
'INSERT INTO ' . $tableName
475 .
' (' . implode(
', ', $cols) .
')'
476 .
' VALUES (' . implode(
', ', $placeholders) .
')';
506 return $this->_platform->quoteIdentifier($str);
516 public function quote($input, $type =
null)
520 return $this->_conn->quote($input, $type);
530 public function fetchAll($sql, array $params = array())
532 return $this->
executeQuery($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
559 public function executeQuery($query, array $params = array(), $types = array())
563 $hasLogger = $this->_config->getSQLLogger() !==
null;
565 $this->_config->getSQLLogger()->startQuery($query, $params, $types);
569 $stmt = $this->_conn->prepare($query);
571 $this->_bindTypedValues($stmt, $params, $types);
574 $stmt->execute($params);
577 $stmt = $this->_conn->query($query);
581 $this->_config->getSQLLogger()->stopQuery();
598 public function project($query, array $params, Closure $function)
601 $stmt = $this->
executeQuery($query, $params ?: array());
603 while ($row = $stmt->fetch()) {
604 $result[] = $function($row);
607 $stmt->closeCursor();
623 return call_user_func_array(array($this->_conn,
'query'), func_get_args());
638 public function executeUpdate($query, array $params = array(), array $types = array())
642 $hasLogger = $this->_config->getSQLLogger() !==
null;
644 $this->_config->getSQLLogger()->startQuery($query, $params, $types);
648 $stmt = $this->_conn->prepare($query);
650 $this->_bindTypedValues($stmt, $params, $types);
653 $stmt->execute($params);
655 $result = $stmt->rowCount();
657 $result = $this->_conn->exec($query);
661 $this->_config->getSQLLogger()->stopQuery();
673 public function exec($statement)
676 return $this->_conn->exec($statement);
686 return $this->_transactionNestingLevel;
697 return $this->_conn->errorCode();
708 return $this->_conn->errorInfo();
725 return $this->_conn->lastInsertId($seqName);
744 }
catch (Exception $e) {
758 if ($this->_transactionNestingLevel > 0) {
762 if (!$this->_platform->supportsSavepoints()) {
766 $this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
776 return $this->_nestTransactionsWithSavepoints;
787 return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
799 ++$this->_transactionNestingLevel;
801 if ($this->_transactionNestingLevel == 1) {
802 $this->_conn->beginTransaction();
803 }
else if ($this->_nestTransactionsWithSavepoints) {
817 if ($this->_transactionNestingLevel == 0) {
820 if ($this->_isRollbackOnly) {
826 if ($this->_transactionNestingLevel == 1) {
827 $this->_conn->commit();
828 }
else if ($this->_nestTransactionsWithSavepoints) {
832 --$this->_transactionNestingLevel;
845 if ($this->_transactionNestingLevel == 0) {
851 if ($this->_transactionNestingLevel == 1) {
852 $this->_transactionNestingLevel = 0;
853 $this->_conn->rollback();
854 $this->_isRollbackOnly =
false;
855 }
else if ($this->_nestTransactionsWithSavepoints) {
857 --$this->_transactionNestingLevel;
859 $this->_isRollbackOnly =
true;
860 --$this->_transactionNestingLevel;
873 if (!$this->_platform->supportsSavepoints()) {
877 $this->_conn->exec($this->_platform->createSavePoint($savepoint));
889 if (!$this->_platform->supportsSavepoints()) {
893 if ($this->_platform->supportsReleaseSavepoints()) {
894 $this->_conn->exec($this->_platform->releaseSavePoint($savepoint));
907 if (!$this->_platform->supportsSavepoints()) {
911 $this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
934 if ( ! $this->_schemaManager) {
935 $this->_schemaManager = $this->_driver->getSchemaManager($this);
949 if ($this->_transactionNestingLevel == 0) {
952 $this->_isRollbackOnly =
true;
963 if ($this->_transactionNestingLevel == 0) {
966 return $this->_isRollbackOnly;
979 return Type::getType($type)->convertToDatabaseValue($value, $this->_platform);
992 return Type::getType($type)->convertToPHPValue($value, $this->_platform);
1005 private function _bindTypedValues($stmt, array $params, array $types)
1008 if (is_int(key($params))) {
1010 $typeOffset = array_key_exists(0, $types) ? -1 : 0;
1012 foreach ($params as $position => $value) {
1013 $typeIndex = $bindIndex + $typeOffset;
1014 if (isset($types[$typeIndex])) {
1015 $type = $types[$typeIndex];
1016 if (is_string($type)) {
1017 $type = Type::getType($type);
1019 if ($type instanceof Type) {
1020 $value = $type->convertToDatabaseValue($value, $this->_platform);
1021 $bindingType = $type->getBindingType();
1023 $bindingType = $type;
1025 $stmt->bindValue($bindIndex, $value, $bindingType);
1027 $stmt->bindValue($bindIndex, $value);
1033 foreach ($params as $name => $value) {
1034 if (isset($types[$name])) {
1035 $type = $types[$name];
1036 if (is_string($type)) {
1037 $type = Type::getType($type);
1039 if ($type instanceof Type) {
1040 $value = $type->convertToDatabaseValue($value, $this->_platform);
1041 $bindingType = $type->getBindingType();
1043 $bindingType = $type;
1045 $stmt->bindValue($name, $value, $bindingType);
1047 $stmt->bindValue($name, $value);
static mayNotAlterNestedTransactionWithSavepointsInTransaction()
static noActiveTransaction()
static commitFailedRollbackOnly()
static savepointsNotSupported()
insert($tableName, array $data)
fetchAll($sql, array $params=array())
setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
lastInsertId($seqName=null)
createSavepoint($savepoint)
__construct(array $params, Driver $driver, Configuration $config=null, EventManager $eventManager=null)
fetchAssoc($statement, array $params=array())
releaseSavepoint($savepoint)
const TRANSACTION_REPEATABLE_READ
project($query, array $params, Closure $function)
convertToDatabaseValue($value, $type)
transactional(Closure $func)
getTransactionNestingLevel()
setTransactionIsolation($level)
const TRANSACTION_READ_COMMITTED
const TRANSACTION_READ_UNCOMMITTED
const TRANSACTION_SERIALIZABLE
fetchColumn($statement, array $params=array(), $colnum=0)
quote($input, $type=null)
getNestTransactionsWithSavepoints()
convertToPHPValue($value, $type)
_getNestedTransactionSavePointName()
getTransactionIsolation()
fetchArray($statement, array $params=array())
executeUpdate($query, array $params=array(), array $types=array())
update($tableName, array $data, array $identifier)
rollbackSavepoint($savepoint)
executeQuery($query, array $params=array(), $types=array())
static invalidPlatformSpecified()