SQLSrvConnection.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Doctrine\DBAL\Driver\SQLSrv;
  3. use Doctrine\DBAL\Driver\Connection;
  4. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  5. use Doctrine\DBAL\ParameterType;
  6. use const SQLSRV_ERR_ERRORS;
  7. use function func_get_args;
  8. use function is_float;
  9. use function is_int;
  10. use function sprintf;
  11. use function sqlsrv_begin_transaction;
  12. use function sqlsrv_commit;
  13. use function sqlsrv_configure;
  14. use function sqlsrv_connect;
  15. use function sqlsrv_errors;
  16. use function sqlsrv_query;
  17. use function sqlsrv_rollback;
  18. use function sqlsrv_rows_affected;
  19. use function sqlsrv_server_info;
  20. use function str_replace;
  21. /**
  22. * SQL Server implementation for the Connection interface.
  23. */
  24. class SQLSrvConnection implements Connection, ServerInfoAwareConnection
  25. {
  26. /** @var resource */
  27. protected $conn;
  28. /** @var LastInsertId */
  29. protected $lastInsertId;
  30. /**
  31. * @param string $serverName
  32. * @param mixed[] $connectionOptions
  33. *
  34. * @throws SQLSrvException
  35. */
  36. public function __construct($serverName, $connectionOptions)
  37. {
  38. if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
  39. throw SQLSrvException::fromSqlSrvErrors();
  40. }
  41. $this->conn = sqlsrv_connect($serverName, $connectionOptions);
  42. if (! $this->conn) {
  43. throw SQLSrvException::fromSqlSrvErrors();
  44. }
  45. $this->lastInsertId = new LastInsertId();
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getServerVersion()
  51. {
  52. $serverInfo = sqlsrv_server_info($this->conn);
  53. return $serverInfo['SQLServerVersion'];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function requiresQueryForServerVersion()
  59. {
  60. return false;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function prepare($sql)
  66. {
  67. return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function query()
  73. {
  74. $args = func_get_args();
  75. $sql = $args[0];
  76. $stmt = $this->prepare($sql);
  77. $stmt->execute();
  78. return $stmt;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function quote($value, $type = ParameterType::STRING)
  84. {
  85. if (is_int($value)) {
  86. return $value;
  87. } elseif (is_float($value)) {
  88. return sprintf('%F', $value);
  89. }
  90. return "'" . str_replace("'", "''", $value) . "'";
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function exec($statement)
  96. {
  97. $stmt = sqlsrv_query($this->conn, $statement);
  98. if ($stmt === false) {
  99. throw SQLSrvException::fromSqlSrvErrors();
  100. }
  101. return sqlsrv_rows_affected($stmt);
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function lastInsertId($name = null)
  107. {
  108. if ($name !== null) {
  109. $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
  110. $stmt->execute([$name]);
  111. } else {
  112. $stmt = $this->query('SELECT @@IDENTITY');
  113. }
  114. return $stmt->fetchColumn();
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function beginTransaction()
  120. {
  121. if (! sqlsrv_begin_transaction($this->conn)) {
  122. throw SQLSrvException::fromSqlSrvErrors();
  123. }
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function commit()
  129. {
  130. if (! sqlsrv_commit($this->conn)) {
  131. throw SQLSrvException::fromSqlSrvErrors();
  132. }
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public function rollBack()
  138. {
  139. if (! sqlsrv_rollback($this->conn)) {
  140. throw SQLSrvException::fromSqlSrvErrors();
  141. }
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function errorCode()
  147. {
  148. $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
  149. if ($errors) {
  150. return $errors[0]['code'];
  151. }
  152. return false;
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public function errorInfo()
  158. {
  159. return sqlsrv_errors(SQLSRV_ERR_ERRORS);
  160. }
  161. }