DB2Connection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Driver\IBMDB2;
  22. class DB2Connection implements \Doctrine\DBAL\Driver\Connection
  23. {
  24. private $_conn = null;
  25. public function __construct(array $params, $username, $password, $driverOptions = array())
  26. {
  27. $isPersistant = (isset($params['persistent']) && $params['persistent'] == true);
  28. if ($isPersistant) {
  29. $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
  30. } else {
  31. $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
  32. }
  33. if (!$this->_conn) {
  34. throw new DB2Exception(db2_conn_errormsg());
  35. }
  36. }
  37. public function prepare($sql)
  38. {
  39. $stmt = @db2_prepare($this->_conn, $sql);
  40. if (!$stmt) {
  41. throw new DB2Exception(db2_stmt_errormsg());
  42. }
  43. return new DB2Statement($stmt);
  44. }
  45. public function query()
  46. {
  47. $args = func_get_args();
  48. $sql = $args[0];
  49. $stmt = $this->prepare($sql);
  50. $stmt->execute();
  51. return $stmt;
  52. }
  53. public function quote($input, $type=\PDO::PARAM_STR)
  54. {
  55. $input = db2_escape_string($input);
  56. if ($type == \PDO::PARAM_INT ) {
  57. return $input;
  58. } else {
  59. return "'".$input."'";
  60. }
  61. }
  62. public function exec($statement)
  63. {
  64. $stmt = $this->prepare($statement);
  65. $stmt->execute();
  66. return $stmt->rowCount();
  67. }
  68. public function lastInsertId($name = null)
  69. {
  70. return db2_last_insert_id($this->_conn);
  71. }
  72. public function beginTransaction()
  73. {
  74. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_OFF);
  75. }
  76. public function commit()
  77. {
  78. if (!db2_commit($this->_conn)) {
  79. throw new DB2Exception(db2_conn_errormsg($this->_conn));
  80. }
  81. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON);
  82. }
  83. public function rollBack()
  84. {
  85. if (!db2_rollback($this->_conn)) {
  86. throw new DB2Exception(db2_conn_errormsg($this->_conn));
  87. }
  88. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON);
  89. }
  90. public function errorCode()
  91. {
  92. return db2_conn_error($this->_conn);
  93. }
  94. public function errorInfo()
  95. {
  96. return array(
  97. 0 => db2_conn_errormsg($this->_conn),
  98. 1 => $this->errorCode(),
  99. );
  100. }
  101. }