MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Connection.php
Ir para a documentação deste ficheiro.
1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\DBAL;
21
22use PDO, Closure, Exception,
24 Doctrine\DBAL\Driver\Connection as DriverConnection,
27
44class Connection implements DriverConnection
45{
50
55
60
65
71 protected $_conn;
72
76 protected $_config;
77
81 protected $_eventManager;
82
88 private $_isConnected = false;
89
95 private $_transactionNestingLevel = 0;
96
102 private $_transactionIsolationLevel;
103
109 private $_nestTransactionsWithSavepoints;
110
116 private $_params = array();
117
124 protected $_platform;
125
132
138 protected $_driver;
139
145 private $_isRollbackOnly = false;
146
155 public function __construct(array $params, Driver $driver, Configuration $config = null,
156 EventManager $eventManager = null)
157 {
158 $this->_driver = $driver;
159 $this->_params = $params;
160
161 if (isset($params['pdo'])) {
162 $this->_conn = $params['pdo'];
163 $this->_isConnected = true;
164 }
165
166 // Create default config and event manager if none given
167 if ( ! $config) {
168 $config = new Configuration();
169 }
170
171 if ( ! $eventManager) {
172 $eventManager = new EventManager();
173 }
174
175 $this->_config = $config;
176 $this->_eventManager = $eventManager;
177 if ( ! isset($params['platform'])) {
178 $this->_platform = $driver->getDatabasePlatform();
179 } else if ($params['platform'] instanceof Platforms\AbstractPlatform) {
180 $this->_platform = $params['platform'];
181 } else {
183 }
184 $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
185 }
186
192 public function getParams()
193 {
194 return $this->_params;
195 }
196
202 public function getDatabase()
203 {
204 return $this->_driver->getDatabase($this);
205 }
206
212 public function getHost()
213 {
214 return isset($this->_params['host']) ? $this->_params['host'] : null;
215 }
216
222 public function getPort()
223 {
224 return isset($this->_params['port']) ? $this->_params['port'] : null;
225 }
226
232 public function getUsername()
233 {
234 return isset($this->_params['user']) ? $this->_params['user'] : null;
235 }
236
242 public function getPassword()
243 {
244 return isset($this->_params['password']) ? $this->_params['password'] : null;
245 }
246
252 public function getDriver()
253 {
254 return $this->_driver;
255 }
256
262 public function getConfiguration()
263 {
264 return $this->_config;
265 }
266
272 public function getEventManager()
273 {
275 }
276
282 public function getDatabasePlatform()
283 {
284 return $this->_platform;
285 }
286
293 public function connect()
294 {
295 if ($this->_isConnected) return false;
296
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;
302
303 $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);
304 $this->_isConnected = true;
305
306 if ($this->_eventManager->hasListeners(Events::postConnect)) {
307 $eventArgs = new Event\ConnectionEventArgs($this);
308 $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
309 }
310
311 return true;
312 }
313
322 public function fetchAssoc($statement, array $params = array())
323 {
324 return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC);
325 }
326
335 public function fetchArray($statement, array $params = array())
336 {
337 return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_NUM);
338 }
339
349 public function fetchColumn($statement, array $params = array(), $colnum = 0)
350 {
351 return $this->executeQuery($statement, $params)->fetchColumn($colnum);
352 }
353
359 public function isConnected()
360 {
361 return $this->_isConnected;
362 }
363
369 public function isTransactionActive()
370 {
371 return $this->_transactionNestingLevel > 0;
372 }
373
381 public function delete($tableName, array $identifier)
382 {
383 $this->connect();
384
385 $criteria = array();
386
387 foreach (array_keys($identifier) as $columnName) {
388 $criteria[] = $columnName . ' = ?';
389 }
390
391 $query = 'DELETE FROM ' . $tableName . ' WHERE ' . implode(' AND ', $criteria);
392
393 return $this->executeUpdate($query, array_values($identifier));
394 }
395
401 public function close()
402 {
403 unset($this->_conn);
404
405 $this->_isConnected = false;
406 }
407
413 public function setTransactionIsolation($level)
414 {
415 $this->_transactionIsolationLevel = $level;
416
417 return $this->executeUpdate($this->_platform->getSetTransactionIsolationSQL($level));
418 }
419
425 public function getTransactionIsolation()
426 {
427 return $this->_transactionIsolationLevel;
428 }
429
437 public function update($tableName, array $data, array $identifier)
438 {
439 $this->connect();
440 $set = array();
441 foreach ($data as $columnName => $value) {
442 $set[] = $columnName . ' = ?';
443 }
444
445 $params = array_merge(array_values($data), array_values($identifier));
446
447 $sql = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $set)
448 . ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
449 . ' = ?';
450
451 return $this->executeUpdate($sql, $params);
452 }
453
461 public function insert($tableName, array $data)
462 {
463 $this->connect();
464
465 // column names are specified as array keys
466 $cols = array();
467 $placeholders = array();
468
469 foreach ($data as $columnName => $value) {
470 $cols[] = $columnName;
471 $placeholders[] = '?';
472 }
473
474 $query = 'INSERT INTO ' . $tableName
475 . ' (' . implode(', ', $cols) . ')'
476 . ' VALUES (' . implode(', ', $placeholders) . ')';
477
478 return $this->executeUpdate($query, array_values($data));
479 }
480
486 public function setCharset($charset)
487 {
488 $this->executeUpdate($this->_platform->getSetCharsetSQL($charset));
489 }
490
504 public function quoteIdentifier($str)
505 {
506 return $this->_platform->quoteIdentifier($str);
507 }
508
516 public function quote($input, $type = null)
517 {
518 $this->connect();
519
520 return $this->_conn->quote($input, $type);
521 }
522
530 public function fetchAll($sql, array $params = array())
531 {
532 return $this->executeQuery($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
533 }
534
541 public function prepare($statement)
542 {
543 $this->connect();
544
545 return new Statement($statement, $this);
546 }
547
559 public function executeQuery($query, array $params = array(), $types = array())
560 {
561 $this->connect();
562
563 $hasLogger = $this->_config->getSQLLogger() !== null;
564 if ($hasLogger) {
565 $this->_config->getSQLLogger()->startQuery($query, $params, $types);
566 }
567
568 if ($params) {
569 $stmt = $this->_conn->prepare($query);
570 if ($types) {
571 $this->_bindTypedValues($stmt, $params, $types);
572 $stmt->execute();
573 } else {
574 $stmt->execute($params);
575 }
576 } else {
577 $stmt = $this->_conn->query($query);
578 }
579
580 if ($hasLogger) {
581 $this->_config->getSQLLogger()->stopQuery();
582 }
583
584 return $stmt;
585 }
586
598 public function project($query, array $params, Closure $function)
599 {
600 $result = array();
601 $stmt = $this->executeQuery($query, $params ?: array());
602
603 while ($row = $stmt->fetch()) {
604 $result[] = $function($row);
605 }
606
607 $stmt->closeCursor();
608
609 return $result;
610 }
611
619 public function query()
620 {
621 $this->connect();
622
623 return call_user_func_array(array($this->_conn, 'query'), func_get_args());
624 }
625
638 public function executeUpdate($query, array $params = array(), array $types = array())
639 {
640 $this->connect();
641
642 $hasLogger = $this->_config->getSQLLogger() !== null;
643 if ($hasLogger) {
644 $this->_config->getSQLLogger()->startQuery($query, $params, $types);
645 }
646
647 if ($params) {
648 $stmt = $this->_conn->prepare($query);
649 if ($types) {
650 $this->_bindTypedValues($stmt, $params, $types);
651 $stmt->execute();
652 } else {
653 $stmt->execute($params);
654 }
655 $result = $stmt->rowCount();
656 } else {
657 $result = $this->_conn->exec($query);
658 }
659
660 if ($hasLogger) {
661 $this->_config->getSQLLogger()->stopQuery();
662 }
663
664 return $result;
665 }
666
673 public function exec($statement)
674 {
675 $this->connect();
676 return $this->_conn->exec($statement);
677 }
678
685 {
686 return $this->_transactionNestingLevel;
687 }
688
694 public function errorCode()
695 {
696 $this->connect();
697 return $this->_conn->errorCode();
698 }
699
705 public function errorInfo()
706 {
707 $this->connect();
708 return $this->_conn->errorInfo();
709 }
710
722 public function lastInsertId($seqName = null)
723 {
724 $this->connect();
725 return $this->_conn->lastInsertId($seqName);
726 }
727
738 public function transactional(Closure $func)
739 {
740 $this->beginTransaction();
741 try {
742 $func($this);
743 $this->commit();
744 } catch (Exception $e) {
745 $this->rollback();
746 throw $e;
747 }
748 }
749
756 public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
757 {
758 if ($this->_transactionNestingLevel > 0) {
760 }
761
762 if (!$this->_platform->supportsSavepoints()) {
764 }
765
766 $this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
767 }
768
775 {
776 return $this->_nestTransactionsWithSavepoints;
777 }
778
786 {
787 return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
788 }
789
795 public function beginTransaction()
796 {
797 $this->connect();
798
799 ++$this->_transactionNestingLevel;
800
801 if ($this->_transactionNestingLevel == 1) {
802 $this->_conn->beginTransaction();
803 } else if ($this->_nestTransactionsWithSavepoints) {
805 }
806 }
807
815 public function commit()
816 {
817 if ($this->_transactionNestingLevel == 0) {
819 }
820 if ($this->_isRollbackOnly) {
822 }
823
824 $this->connect();
825
826 if ($this->_transactionNestingLevel == 1) {
827 $this->_conn->commit();
828 } else if ($this->_nestTransactionsWithSavepoints) {
830 }
831
832 --$this->_transactionNestingLevel;
833 }
834
843 public function rollback()
844 {
845 if ($this->_transactionNestingLevel == 0) {
847 }
848
849 $this->connect();
850
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;
858 } else {
859 $this->_isRollbackOnly = true;
860 --$this->_transactionNestingLevel;
861 }
862 }
863
871 public function createSavepoint($savepoint)
872 {
873 if (!$this->_platform->supportsSavepoints()) {
875 }
876
877 $this->_conn->exec($this->_platform->createSavePoint($savepoint));
878 }
879
887 public function releaseSavepoint($savepoint)
888 {
889 if (!$this->_platform->supportsSavepoints()) {
891 }
892
893 if ($this->_platform->supportsReleaseSavepoints()) {
894 $this->_conn->exec($this->_platform->releaseSavePoint($savepoint));
895 }
896 }
897
905 public function rollbackSavepoint($savepoint)
906 {
907 if (!$this->_platform->supportsSavepoints()) {
909 }
910
911 $this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
912 }
913
919 public function getWrappedConnection()
920 {
921 $this->connect();
922
923 return $this->_conn;
924 }
925
932 public function getSchemaManager()
933 {
934 if ( ! $this->_schemaManager) {
935 $this->_schemaManager = $this->_driver->getSchemaManager($this);
936 }
937
939 }
940
947 public function setRollbackOnly()
948 {
949 if ($this->_transactionNestingLevel == 0) {
951 }
952 $this->_isRollbackOnly = true;
953 }
954
961 public function isRollbackOnly()
962 {
963 if ($this->_transactionNestingLevel == 0) {
965 }
966 return $this->_isRollbackOnly;
967 }
968
977 public function convertToDatabaseValue($value, $type)
978 {
979 return Type::getType($type)->convertToDatabaseValue($value, $this->_platform);
980 }
981
990 public function convertToPHPValue($value, $type)
991 {
992 return Type::getType($type)->convertToPHPValue($value, $this->_platform);
993 }
994
1005 private function _bindTypedValues($stmt, array $params, array $types)
1006 {
1007 // Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
1008 if (is_int(key($params))) {
1009 // Positional parameters
1010 $typeOffset = array_key_exists(0, $types) ? -1 : 0;
1011 $bindIndex = 1;
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);
1018 }
1019 if ($type instanceof Type) {
1020 $value = $type->convertToDatabaseValue($value, $this->_platform);
1021 $bindingType = $type->getBindingType();
1022 } else {
1023 $bindingType = $type; // PDO::PARAM_* constants
1024 }
1025 $stmt->bindValue($bindIndex, $value, $bindingType);
1026 } else {
1027 $stmt->bindValue($bindIndex, $value);
1028 }
1029 ++$bindIndex;
1030 }
1031 } else {
1032 // Named parameters
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);
1038 }
1039 if ($type instanceof Type) {
1040 $value = $type->convertToDatabaseValue($value, $this->_platform);
1041 $bindingType = $type->getBindingType();
1042 } else {
1043 $bindingType = $type; // PDO::PARAM_* constants
1044 }
1045 $stmt->bindValue($name, $value, $bindingType);
1046 } else {
1047 $stmt->bindValue($name, $value);
1048 }
1049 }
1050 }
1051 }
1052}
insert($tableName, array $data)
Definição Connection.php:461
fetchAll($sql, array $params=array())
Definição Connection.php:530
setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
Definição Connection.php:756
lastInsertId($seqName=null)
Definição Connection.php:722
createSavepoint($savepoint)
Definição Connection.php:871
__construct(array $params, Driver $driver, Configuration $config=null, EventManager $eventManager=null)
Definição Connection.php:155
fetchAssoc($statement, array $params=array())
Definição Connection.php:322
releaseSavepoint($savepoint)
Definição Connection.php:887
project($query, array $params, Closure $function)
Definição Connection.php:598
convertToDatabaseValue($value, $type)
Definição Connection.php:977
transactional(Closure $func)
Definição Connection.php:738
fetchColumn($statement, array $params=array(), $colnum=0)
Definição Connection.php:349
quote($input, $type=null)
Definição Connection.php:516
convertToPHPValue($value, $type)
Definição Connection.php:990
fetchArray($statement, array $params=array())
Definição Connection.php:335
executeUpdate($query, array $params=array(), array $types=array())
Definição Connection.php:638
update($tableName, array $data, array $identifier)
Definição Connection.php:437
rollbackSavepoint($savepoint)
Definição Connection.php:905
executeQuery($query, array $params=array(), $types=array())
Definição Connection.php:559
$charset
Definição base.php:7