SQLSrvConnection.php 3.8 KB

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