PDOConnection.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver;
  20. use PDO;
  21. /**
  22. * PDO implementation of the Connection interface.
  23. * Used by all PDO-based drivers.
  24. *
  25. * @since 2.0
  26. */
  27. class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
  28. {
  29. /**
  30. * @param string $dsn
  31. * @param string|null $user
  32. * @param string|null $password
  33. * @param array|null $options
  34. *
  35. * @throws PDOException in case of an error.
  36. */
  37. public function __construct($dsn, $user = null, $password = null, array $options = null)
  38. {
  39. try {
  40. parent::__construct($dsn, $user, $password, $options);
  41. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
  42. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  43. } catch (\PDOException $exception) {
  44. throw new PDOException($exception);
  45. }
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function exec($statement)
  51. {
  52. try {
  53. return parent::exec($statement);
  54. } catch (\PDOException $exception) {
  55. throw new PDOException($exception);
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getServerVersion()
  62. {
  63. return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function prepare($prepareString, $driverOptions = array())
  69. {
  70. try {
  71. return parent::prepare($prepareString, $driverOptions);
  72. } catch (\PDOException $exception) {
  73. throw new PDOException($exception);
  74. }
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function query()
  80. {
  81. $args = func_get_args();
  82. $argsCount = count($args);
  83. try {
  84. if ($argsCount == 4) {
  85. return parent::query($args[0], $args[1], $args[2], $args[3]);
  86. }
  87. if ($argsCount == 3) {
  88. return parent::query($args[0], $args[1], $args[2]);
  89. }
  90. if ($argsCount == 2) {
  91. return parent::query($args[0], $args[1]);
  92. }
  93. return parent::query($args[0]);
  94. } catch (\PDOException $exception) {
  95. throw new PDOException($exception);
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function quote($input, $type = \PDO::PARAM_STR)
  102. {
  103. return parent::quote($input, $type);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function lastInsertId($name = null)
  109. {
  110. return parent::lastInsertId($name);
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function requiresQueryForServerVersion()
  116. {
  117. return false;
  118. }
  119. }