SQLSrvStatement.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 PDO;
  21. use IteratorAggregate;
  22. use Doctrine\DBAL\Driver\Statement;
  23. /**
  24. * SQL Server Statement
  25. *
  26. * @since 2.3
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class SQLSrvStatement implements IteratorAggregate, Statement
  30. {
  31. /**
  32. * SQLSRV Resource
  33. *
  34. * @var resource
  35. */
  36. private $conn;
  37. /**
  38. * SQL Statement to execute
  39. *
  40. * @var string
  41. */
  42. private $sql;
  43. /**
  44. * SQLSRV Statement Resource
  45. *
  46. * @var resource
  47. */
  48. private $stmt;
  49. /**
  50. * Parameters to bind
  51. *
  52. * @var array
  53. */
  54. private $params = array();
  55. /**
  56. * Translations
  57. *
  58. * @var array
  59. */
  60. private static $fetchMap = array(
  61. PDO::FETCH_BOTH => SQLSRV_FETCH_BOTH,
  62. PDO::FETCH_ASSOC => SQLSRV_FETCH_ASSOC,
  63. PDO::FETCH_NUM => SQLSRV_FETCH_NUMERIC,
  64. );
  65. /**
  66. * Fetch Style
  67. *
  68. * @param int
  69. */
  70. private $defaultFetchMode = PDO::FETCH_BOTH;
  71. /**
  72. * @var int|null
  73. */
  74. private $lastInsertId;
  75. /**
  76. * Append to any INSERT query to retrieve the last insert id.
  77. *
  78. * @var string
  79. */
  80. const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;';
  81. public function __construct($conn, $sql, $lastInsertId = null)
  82. {
  83. $this->conn = $conn;
  84. $this->sql = $sql;
  85. if (stripos($sql, 'INSERT INTO ') === 0) {
  86. $this->sql .= self::LAST_INSERT_ID_SQL;
  87. $this->lastInsertId = $lastInsertId;
  88. }
  89. }
  90. public function bindValue($param, $value, $type = null)
  91. {
  92. return $this->bindParam($param, $value, $type,null);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function bindParam($column, &$variable, $type = null, $length = null)
  98. {
  99. if (!is_numeric($column)) {
  100. throw new SQLSrvException("sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead.");
  101. }
  102. if ($type === \PDO::PARAM_LOB) {
  103. $this->params[$column-1] = array($variable, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max'));
  104. } else {
  105. $this->params[$column-1] = $variable;
  106. }
  107. }
  108. public function closeCursor()
  109. {
  110. if ($this->stmt) {
  111. sqlsrv_free_stmt($this->stmt);
  112. }
  113. }
  114. public function columnCount()
  115. {
  116. return sqlsrv_num_fields($this->stmt);
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function errorCode()
  122. {
  123. $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
  124. if ($errors) {
  125. return $errors[0]['code'];
  126. }
  127. return false;
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public function errorInfo()
  133. {
  134. return sqlsrv_errors(SQLSRV_ERR_ERRORS);
  135. }
  136. public function execute($params = null)
  137. {
  138. if ($params) {
  139. $hasZeroIndex = array_key_exists(0, $params);
  140. foreach ($params as $key => $val) {
  141. $key = ($hasZeroIndex && is_numeric($key)) ? $key + 1 : $key;
  142. $this->bindValue($key, $val);
  143. }
  144. }
  145. $this->stmt = sqlsrv_query($this->conn, $this->sql, $this->params);
  146. if ( ! $this->stmt) {
  147. throw SQLSrvException::fromSqlSrvErrors();
  148. }
  149. if ($this->lastInsertId) {
  150. sqlsrv_next_result($this->stmt);
  151. sqlsrv_fetch($this->stmt);
  152. $this->lastInsertId->setId( sqlsrv_get_field($this->stmt, 0) );
  153. }
  154. }
  155. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  156. {
  157. $this->defaultFetchMode = $fetchMode;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getIterator()
  163. {
  164. $data = $this->fetchAll();
  165. return new \ArrayIterator($data);
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function fetch($fetchMode = null)
  171. {
  172. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  173. if (isset(self::$fetchMap[$fetchMode])) {
  174. return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]);
  175. } else if ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) {
  176. $className = null;
  177. $ctorArgs = null;
  178. if (func_num_args() >= 2) {
  179. $args = func_get_args();
  180. $className = $args[1];
  181. $ctorArgs = (isset($args[2])) ? $args[2] : array();
  182. }
  183. return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs);
  184. }
  185. throw new SQLSrvException("Fetch mode is not supported!");
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function fetchAll($fetchMode = null)
  191. {
  192. $className = null;
  193. $ctorArgs = null;
  194. if (func_num_args() >= 2) {
  195. $args = func_get_args();
  196. $className = $args[1];
  197. $ctorArgs = (isset($args[2])) ? $args[2] : array();
  198. }
  199. $rows = array();
  200. while ($row = $this->fetch($fetchMode, $className, $ctorArgs)) {
  201. $rows[] = $row;
  202. }
  203. return $rows;
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. public function fetchColumn($columnIndex = 0)
  209. {
  210. $row = $this->fetch(PDO::FETCH_NUM);
  211. return $row[$columnIndex];
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function rowCount()
  217. {
  218. return sqlsrv_rows_affected($this->stmt);
  219. }
  220. }