DB2Connection.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Doctrine\DBAL\Driver\IBMDB2;
  3. use Doctrine\DBAL\Driver\Connection;
  4. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  5. use Doctrine\DBAL\ParameterType;
  6. use stdClass;
  7. use const DB2_AUTOCOMMIT_OFF;
  8. use const DB2_AUTOCOMMIT_ON;
  9. use function db2_autocommit;
  10. use function db2_commit;
  11. use function db2_conn_error;
  12. use function db2_conn_errormsg;
  13. use function db2_connect;
  14. use function db2_escape_string;
  15. use function db2_exec;
  16. use function db2_last_insert_id;
  17. use function db2_num_rows;
  18. use function db2_pconnect;
  19. use function db2_prepare;
  20. use function db2_rollback;
  21. use function db2_server_info;
  22. use function db2_stmt_errormsg;
  23. use function func_get_args;
  24. class DB2Connection implements Connection, ServerInfoAwareConnection
  25. {
  26. /** @var resource */
  27. private $conn = null;
  28. /**
  29. * @param mixed[] $params
  30. * @param string $username
  31. * @param string $password
  32. * @param mixed[] $driverOptions
  33. *
  34. * @throws DB2Exception
  35. */
  36. public function __construct(array $params, $username, $password, $driverOptions = [])
  37. {
  38. $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
  39. if ($isPersistent) {
  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. /** @var stdClass $serverInfo */
  54. $serverInfo = db2_server_info($this->conn);
  55. return $serverInfo->DBMS_VER;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function requiresQueryForServerVersion()
  61. {
  62. return false;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function prepare($sql)
  68. {
  69. $stmt = @db2_prepare($this->conn, $sql);
  70. if (! $stmt) {
  71. throw new DB2Exception(db2_stmt_errormsg());
  72. }
  73. return new DB2Statement($stmt);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function query()
  79. {
  80. $args = func_get_args();
  81. $sql = $args[0];
  82. $stmt = $this->prepare($sql);
  83. $stmt->execute();
  84. return $stmt;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function quote($input, $type = ParameterType::STRING)
  90. {
  91. $input = db2_escape_string($input);
  92. if ($type === ParameterType::INTEGER) {
  93. return $input;
  94. }
  95. return "'" . $input . "'";
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function exec($statement)
  101. {
  102. $stmt = @db2_exec($this->conn, $statement);
  103. if ($stmt === false) {
  104. throw new DB2Exception(db2_stmt_errormsg());
  105. }
  106. return db2_num_rows($stmt);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function lastInsertId($name = null)
  112. {
  113. return db2_last_insert_id($this->conn);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function beginTransaction()
  119. {
  120. db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function commit()
  126. {
  127. if (! db2_commit($this->conn)) {
  128. throw new DB2Exception(db2_conn_errormsg($this->conn));
  129. }
  130. db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function rollBack()
  136. {
  137. if (! db2_rollback($this->conn)) {
  138. throw new DB2Exception(db2_conn_errormsg($this->conn));
  139. }
  140. db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function errorCode()
  146. {
  147. return db2_conn_error($this->conn);
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function errorInfo()
  153. {
  154. return [
  155. 0 => db2_conn_errormsg($this->conn),
  156. 1 => $this->errorCode(),
  157. ];
  158. }
  159. }