SQLSrvConnection.php 4.0 KB

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