SQLAnywhereConnection.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Doctrine\DBAL\Driver\SQLAnywhere;
  3. use Doctrine\DBAL\Driver\Connection;
  4. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  5. use Doctrine\DBAL\ParameterType;
  6. use function assert;
  7. use function func_get_args;
  8. use function is_float;
  9. use function is_int;
  10. use function is_resource;
  11. use function is_string;
  12. use function sasql_affected_rows;
  13. use function sasql_commit;
  14. use function sasql_connect;
  15. use function sasql_error;
  16. use function sasql_errorcode;
  17. use function sasql_escape_string;
  18. use function sasql_insert_id;
  19. use function sasql_pconnect;
  20. use function sasql_real_query;
  21. use function sasql_rollback;
  22. use function sasql_set_option;
  23. /**
  24. * SAP Sybase SQL Anywhere implementation of the Connection interface.
  25. */
  26. class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
  27. {
  28. /** @var resource The SQL Anywhere connection resource. */
  29. private $connection;
  30. /**
  31. * Connects to database with given connection string.
  32. *
  33. * @param string $dsn The connection string.
  34. * @param bool $persistent Whether or not to establish a persistent connection.
  35. *
  36. * @throws SQLAnywhereException
  37. */
  38. public function __construct($dsn, $persistent = false)
  39. {
  40. $this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn);
  41. if (! is_resource($this->connection)) {
  42. throw SQLAnywhereException::fromSQLAnywhereError();
  43. }
  44. // Disable PHP warnings on error.
  45. if (! sasql_set_option($this->connection, 'verbose_errors', false)) {
  46. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  47. }
  48. // Enable auto committing by default.
  49. if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
  50. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  51. }
  52. }
  53. /**
  54. * {@inheritdoc}
  55. *
  56. * @throws SQLAnywhereException
  57. */
  58. public function beginTransaction()
  59. {
  60. if (! sasql_set_option($this->connection, 'auto_commit', 'off')) {
  61. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  62. }
  63. return true;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @throws SQLAnywhereException
  69. */
  70. public function commit()
  71. {
  72. if (! sasql_commit($this->connection)) {
  73. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  74. }
  75. $this->endTransaction();
  76. return true;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function errorCode()
  82. {
  83. return sasql_errorcode($this->connection);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function errorInfo()
  89. {
  90. return sasql_error($this->connection);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function exec($statement)
  96. {
  97. if (sasql_real_query($this->connection, $statement) === false) {
  98. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  99. }
  100. return sasql_affected_rows($this->connection);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getServerVersion()
  106. {
  107. $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
  108. assert(is_string($version));
  109. return $version;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function lastInsertId($name = null)
  115. {
  116. if ($name === null) {
  117. return sasql_insert_id($this->connection);
  118. }
  119. return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function prepare($prepareString)
  125. {
  126. return new SQLAnywhereStatement($this->connection, $prepareString);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function query()
  132. {
  133. $args = func_get_args();
  134. $stmt = $this->prepare($args[0]);
  135. $stmt->execute();
  136. return $stmt;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function quote($input, $type = ParameterType::STRING)
  142. {
  143. if (is_int($input) || is_float($input)) {
  144. return $input;
  145. }
  146. return "'" . sasql_escape_string($this->connection, $input) . "'";
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function requiresQueryForServerVersion()
  152. {
  153. return true;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. *
  158. * @throws SQLAnywhereException
  159. */
  160. public function rollBack()
  161. {
  162. if (! sasql_rollback($this->connection)) {
  163. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  164. }
  165. $this->endTransaction();
  166. return true;
  167. }
  168. /**
  169. * Ends transactional mode and enables auto commit again.
  170. *
  171. * @return bool Whether or not ending transactional mode succeeded.
  172. *
  173. * @throws SQLAnywhereException
  174. */
  175. private function endTransaction()
  176. {
  177. if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
  178. throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
  179. }
  180. return true;
  181. }
  182. }