SQLSrvConnection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\SQLSrv;
  20. use Doctrine\DBAL\Driver\Connection;
  21. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  22. /**
  23. * SQL Server implementation for the Connection interface.
  24. *
  25. * @since 2.3
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class SQLSrvConnection implements Connection, ServerInfoAwareConnection
  29. {
  30. /**
  31. * @var resource
  32. */
  33. protected $conn;
  34. /**
  35. * @var \Doctrine\DBAL\Driver\SQLSrv\LastInsertId
  36. */
  37. protected $lastInsertId;
  38. /**
  39. * @param string $serverName
  40. * @param array $connectionOptions
  41. *
  42. * @throws \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
  43. */
  44. public function __construct($serverName, $connectionOptions)
  45. {
  46. if ( ! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
  47. throw SQLSrvException::fromSqlSrvErrors();
  48. }
  49. $this->conn = sqlsrv_connect($serverName, $connectionOptions);
  50. if ( ! $this->conn) {
  51. throw SQLSrvException::fromSqlSrvErrors();
  52. }
  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. return false;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function prepare($sql)
  74. {
  75. return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function query()
  81. {
  82. $args = func_get_args();
  83. $sql = $args[0];
  84. $stmt = $this->prepare($sql);
  85. $stmt->execute();
  86. return $stmt;
  87. }
  88. /**
  89. * {@inheritDoc}
  90. * @license New BSD, code from Zend Framework
  91. */
  92. public function quote($value, $type=\PDO::PARAM_STR)
  93. {
  94. if (is_int($value)) {
  95. return $value;
  96. } elseif (is_float($value)) {
  97. return sprintf('%F', $value);
  98. }
  99. return "'" . str_replace("'", "''", $value) . "'";
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function exec($statement)
  105. {
  106. $stmt = $this->prepare($statement);
  107. $stmt->execute();
  108. return $stmt->rowCount();
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function lastInsertId($name = null)
  114. {
  115. if ($name !== null) {
  116. $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
  117. $stmt->execute(array($name));
  118. return $stmt->fetchColumn();
  119. }
  120. return $this->lastInsertId->getId();
  121. }
  122. /**
  123. * {@inheritDoc}
  124. */
  125. public function beginTransaction()
  126. {
  127. if ( ! sqlsrv_begin_transaction($this->conn)) {
  128. throw SQLSrvException::fromSqlSrvErrors();
  129. }
  130. }
  131. /**
  132. * {@inheritDoc}
  133. */
  134. public function commit()
  135. {
  136. if ( ! sqlsrv_commit($this->conn)) {
  137. throw SQLSrvException::fromSqlSrvErrors();
  138. }
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function rollBack()
  144. {
  145. if ( ! sqlsrv_rollback($this->conn)) {
  146. throw SQLSrvException::fromSqlSrvErrors();
  147. }
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function errorCode()
  153. {
  154. $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
  155. if ($errors) {
  156. return $errors[0]['code'];
  157. }
  158. return false;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public function errorInfo()
  164. {
  165. return sqlsrv_errors(SQLSRV_ERR_ERRORS);
  166. }
  167. }