SQLSrvConnection.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Doctrine\DBAL\Driver\SQLSrv;
  3. use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
  4. use Doctrine\DBAL\Driver\Result;
  5. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  6. use Doctrine\DBAL\Driver\SQLSrv\Exception\Error;
  7. use Doctrine\DBAL\ParameterType;
  8. use Doctrine\Deprecations\Deprecation;
  9. use function func_get_args;
  10. use function is_float;
  11. use function is_int;
  12. use function sprintf;
  13. use function sqlsrv_begin_transaction;
  14. use function sqlsrv_commit;
  15. use function sqlsrv_configure;
  16. use function sqlsrv_connect;
  17. use function sqlsrv_errors;
  18. use function sqlsrv_query;
  19. use function sqlsrv_rollback;
  20. use function sqlsrv_rows_affected;
  21. use function sqlsrv_server_info;
  22. use function str_replace;
  23. use const SQLSRV_ERR_ERRORS;
  24. /**
  25. * SQL Server implementation for the Connection interface.
  26. *
  27. * @deprecated Use {@link Connection} instead
  28. */
  29. class SQLSrvConnection implements ConnectionInterface, ServerInfoAwareConnection
  30. {
  31. /** @var resource */
  32. protected $conn;
  33. /** @var LastInsertId */
  34. protected $lastInsertId;
  35. /**
  36. * @internal The connection can be only instantiated by its driver.
  37. *
  38. * @param string $serverName
  39. * @param mixed[] $connectionOptions
  40. *
  41. * @throws SQLSrvException
  42. */
  43. public function __construct($serverName, $connectionOptions)
  44. {
  45. if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
  46. throw Error::new();
  47. }
  48. $conn = sqlsrv_connect($serverName, $connectionOptions);
  49. if ($conn === false) {
  50. throw Error::new();
  51. }
  52. $this->conn = $conn;
  53. $this->lastInsertId = new LastInsertId();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getServerVersion()
  59. {
  60. $serverInfo = sqlsrv_server_info($this->conn);
  61. return $serverInfo['SQLServerVersion'];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function requiresQueryForServerVersion()
  67. {
  68. Deprecation::triggerIfCalledFromOutside(
  69. 'doctrine/dbal',
  70. 'https://github.com/doctrine/dbal/pull/4114',
  71. 'ServerInfoAwareConnection::requiresQueryForServerVersion() is deprecated and removed in DBAL 3.'
  72. );
  73. return false;
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function prepare($sql)
  79. {
  80. return new Statement($this->conn, $sql, $this->lastInsertId);
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function query()
  86. {
  87. $args = func_get_args();
  88. $sql = $args[0];
  89. $stmt = $this->prepare($sql);
  90. $stmt->execute();
  91. return $stmt;
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function quote($value, $type = ParameterType::STRING)
  97. {
  98. if (is_int($value)) {
  99. return $value;
  100. }
  101. if (is_float($value)) {
  102. return sprintf('%F', $value);
  103. }
  104. return "'" . str_replace("'", "''", $value) . "'";
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public function exec($sql)
  110. {
  111. $stmt = sqlsrv_query($this->conn, $sql);
  112. if ($stmt === false) {
  113. throw Error::new();
  114. }
  115. $rowsAffected = sqlsrv_rows_affected($stmt);
  116. if ($rowsAffected === false) {
  117. throw Error::new();
  118. }
  119. return $rowsAffected;
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function lastInsertId($name = null)
  125. {
  126. if ($name !== null) {
  127. $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
  128. $stmt->execute([$name]);
  129. } else {
  130. $stmt = $this->query('SELECT @@IDENTITY');
  131. }
  132. if ($stmt instanceof Result) {
  133. return $stmt->fetchOne();
  134. }
  135. return $stmt->fetchColumn();
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. public function beginTransaction()
  141. {
  142. if (! sqlsrv_begin_transaction($this->conn)) {
  143. throw Error::new();
  144. }
  145. return true;
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. public function commit()
  151. {
  152. if (! sqlsrv_commit($this->conn)) {
  153. throw Error::new();
  154. }
  155. return true;
  156. }
  157. /**
  158. * {@inheritDoc}
  159. */
  160. public function rollBack()
  161. {
  162. if (! sqlsrv_rollback($this->conn)) {
  163. throw Error::new();
  164. }
  165. return true;
  166. }
  167. /**
  168. * {@inheritDoc}
  169. *
  170. * @deprecated The error information is available via exceptions.
  171. */
  172. public function errorCode()
  173. {
  174. $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
  175. if ($errors) {
  176. return $errors[0]['code'];
  177. }
  178. return null;
  179. }
  180. /**
  181. * {@inheritDoc}
  182. *
  183. * @deprecated The error information is available via exceptions.
  184. */
  185. public function errorInfo()
  186. {
  187. return (array) sqlsrv_errors(SQLSRV_ERR_ERRORS);
  188. }
  189. }