SQLAnywhereConnection.php 5.7 KB

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