DB2Connection.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\IBMDB2;
  20. class DB2Connection implements \Doctrine\DBAL\Driver\Connection
  21. {
  22. /**
  23. * @var resource
  24. */
  25. private $_conn = null;
  26. /**
  27. * @param array $params
  28. * @param string $username
  29. * @param string $password
  30. * @param array $driverOptions
  31. *
  32. * @throws \Doctrine\DBAL\Driver\IBMDB2\DB2Exception
  33. */
  34. public function __construct(array $params, $username, $password, $driverOptions = array())
  35. {
  36. $isPersistant = (isset($params['persistent']) && $params['persistent'] == true);
  37. if ($isPersistant) {
  38. $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
  39. } else {
  40. $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
  41. }
  42. if ( ! $this->_conn) {
  43. throw new DB2Exception(db2_conn_errormsg());
  44. }
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function prepare($sql)
  50. {
  51. $stmt = @db2_prepare($this->_conn, $sql);
  52. if ( ! $stmt) {
  53. throw new DB2Exception(db2_stmt_errormsg());
  54. }
  55. return new DB2Statement($stmt);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function query()
  61. {
  62. $args = func_get_args();
  63. $sql = $args[0];
  64. $stmt = $this->prepare($sql);
  65. $stmt->execute();
  66. return $stmt;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function quote($input, $type=\PDO::PARAM_STR)
  72. {
  73. $input = db2_escape_string($input);
  74. if ($type == \PDO::PARAM_INT ) {
  75. return $input;
  76. } else {
  77. return "'".$input."'";
  78. }
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function exec($statement)
  84. {
  85. $stmt = $this->prepare($statement);
  86. $stmt->execute();
  87. return $stmt->rowCount();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function lastInsertId($name = null)
  93. {
  94. return db2_last_insert_id($this->_conn);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function beginTransaction()
  100. {
  101. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_OFF);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function commit()
  107. {
  108. if (!db2_commit($this->_conn)) {
  109. throw new DB2Exception(db2_conn_errormsg($this->_conn));
  110. }
  111. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function rollBack()
  117. {
  118. if (!db2_rollback($this->_conn)) {
  119. throw new DB2Exception(db2_conn_errormsg($this->_conn));
  120. }
  121. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function errorCode()
  127. {
  128. return db2_conn_error($this->_conn);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function errorInfo()
  134. {
  135. return array(
  136. 0 => db2_conn_errormsg($this->_conn),
  137. 1 => $this->errorCode(),
  138. );
  139. }
  140. }