DB2Connection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Doctrine\DBAL\Driver\IBMDB2;
  3. use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
  4. use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionError;
  5. use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed;
  6. use Doctrine\DBAL\Driver\IBMDB2\Exception\PrepareFailed;
  7. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  8. use Doctrine\DBAL\ParameterType;
  9. use Doctrine\Deprecations\Deprecation;
  10. use stdClass;
  11. use function assert;
  12. use function db2_autocommit;
  13. use function db2_commit;
  14. use function db2_conn_error;
  15. use function db2_conn_errormsg;
  16. use function db2_connect;
  17. use function db2_escape_string;
  18. use function db2_exec;
  19. use function db2_last_insert_id;
  20. use function db2_num_rows;
  21. use function db2_pconnect;
  22. use function db2_prepare;
  23. use function db2_rollback;
  24. use function db2_server_info;
  25. use function error_get_last;
  26. use function func_get_args;
  27. use function is_bool;
  28. use const DB2_AUTOCOMMIT_OFF;
  29. use const DB2_AUTOCOMMIT_ON;
  30. /**
  31. * @deprecated Use {@link Connection} instead
  32. */
  33. class DB2Connection implements ConnectionInterface, ServerInfoAwareConnection
  34. {
  35. /** @var resource */
  36. private $conn;
  37. /**
  38. * @internal The connection can be only instantiated by its driver.
  39. *
  40. * @param mixed[] $params
  41. * @param string $username
  42. * @param string $password
  43. * @param mixed[] $driverOptions
  44. *
  45. * @throws DB2Exception
  46. */
  47. public function __construct(array $params, $username, $password, $driverOptions = [])
  48. {
  49. $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
  50. if ($isPersistent) {
  51. $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
  52. } else {
  53. $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
  54. }
  55. if ($conn === false) {
  56. throw ConnectionFailed::new();
  57. }
  58. $this->conn = $conn;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getServerVersion()
  64. {
  65. $serverInfo = db2_server_info($this->conn);
  66. assert($serverInfo instanceof stdClass);
  67. return $serverInfo->DBMS_VER;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function requiresQueryForServerVersion()
  73. {
  74. Deprecation::triggerIfCalledFromOutside(
  75. 'doctrine/dbal',
  76. 'https://github.com/doctrine/dbal/pull/4114',
  77. 'ServerInfoAwareConnection::requiresQueryForServerVersion() is deprecated and removed in DBAL 3.'
  78. );
  79. return false;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function prepare($sql)
  85. {
  86. $stmt = @db2_prepare($this->conn, $sql);
  87. if ($stmt === false) {
  88. throw PrepareFailed::new(error_get_last());
  89. }
  90. return new Statement($stmt);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function query()
  96. {
  97. $args = func_get_args();
  98. $sql = $args[0];
  99. $stmt = $this->prepare($sql);
  100. $stmt->execute();
  101. return $stmt;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function quote($value, $type = ParameterType::STRING)
  107. {
  108. $value = db2_escape_string($value);
  109. if ($type === ParameterType::INTEGER) {
  110. return $value;
  111. }
  112. return "'" . $value . "'";
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function exec($sql)
  118. {
  119. $stmt = @db2_exec($this->conn, $sql);
  120. if ($stmt === false) {
  121. throw ConnectionError::new($this->conn);
  122. }
  123. return db2_num_rows($stmt);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function lastInsertId($name = null)
  129. {
  130. return db2_last_insert_id($this->conn);
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function beginTransaction()
  136. {
  137. $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
  138. assert(is_bool($result));
  139. return $result;
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function commit()
  145. {
  146. if (! db2_commit($this->conn)) {
  147. throw ConnectionError::new($this->conn);
  148. }
  149. $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
  150. assert(is_bool($result));
  151. return $result;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function rollBack()
  157. {
  158. if (! db2_rollback($this->conn)) {
  159. throw ConnectionError::new($this->conn);
  160. }
  161. $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
  162. assert(is_bool($result));
  163. return $result;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. *
  168. * @deprecated The error information is available via exceptions.
  169. */
  170. public function errorCode()
  171. {
  172. return db2_conn_error($this->conn);
  173. }
  174. /**
  175. * {@inheritdoc}
  176. *
  177. * @deprecated The error information is available via exceptions.
  178. */
  179. public function errorInfo()
  180. {
  181. return [
  182. 0 => db2_conn_errormsg($this->conn),
  183. 1 => $this->errorCode(),
  184. ];
  185. }
  186. }