DB2Connection.php 4.3 KB

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