Sqlsrv.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Statement
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Statement
  24. */
  25. require_once 'Zend/Db/Statement.php';
  26. /**
  27. * Extends for Microsoft SQL Server Driver for PHP
  28. *
  29. * @category Zend
  30. * @package Zend_Db
  31. * @subpackage Statement
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Db_Statement_Sqlsrv extends Zend_Db_Statement
  36. {
  37. /**
  38. * The connection_stmt object.
  39. */
  40. protected $_stmt;
  41. /**
  42. * The connection_stmt object original string.
  43. */
  44. protected $_originalSQL;
  45. /**
  46. * Column names.
  47. */
  48. protected $_keys;
  49. /**
  50. * Query executed
  51. */
  52. protected $_executed = false;
  53. /**
  54. * Prepares statement handle
  55. *
  56. * @param string $sql
  57. * @return void
  58. * @throws Zend_Db_Statement_Sqlsrv_Exception
  59. */
  60. protected function _prepare($sql)
  61. {
  62. $connection = $this->_adapter->getConnection();
  63. $this->_stmt = sqlsrv_prepare($connection, $sql);
  64. if (!$this->_stmt) {
  65. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  66. throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
  67. }
  68. $this->_originalSQL = $sql;
  69. }
  70. /**
  71. * Binds a parameter to the specified variable name.
  72. *
  73. * @param mixed $parameter Name the parameter, either integer or string.
  74. * @param mixed $variable Reference to PHP variable containing the value.
  75. * @param mixed $type OPTIONAL Datatype of SQL parameter.
  76. * @param mixed $length OPTIONAL Length of SQL parameter.
  77. * @param mixed $options OPTIONAL Other options.
  78. * @return bool
  79. * @throws Zend_Db_Statement_Exception
  80. */
  81. protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  82. {
  83. //Sql server doesn't support bind by name
  84. return true;
  85. }
  86. /**
  87. * Closes the cursor, allowing the statement to be executed again.
  88. *
  89. * @return bool
  90. */
  91. public function closeCursor()
  92. {
  93. if (!$this->_stmt) {
  94. return false;
  95. }
  96. sqlsrv_free_stmt($this->_stmt);
  97. $this->_stmt = false;
  98. return true;
  99. }
  100. /**
  101. * Returns the number of columns in the result set.
  102. * Returns null if the statement has no result set metadata.
  103. *
  104. * @return int The number of columns.
  105. */
  106. public function columnCount()
  107. {
  108. if ($this->_stmt && $this->_executed) {
  109. return sqlsrv_num_fields($this->_stmt);
  110. }
  111. return 0;
  112. }
  113. /**
  114. * Retrieves the error code, if any, associated with the last operation on
  115. * the statement handle.
  116. *
  117. * @return string error code.
  118. */
  119. public function errorCode()
  120. {
  121. if (!$this->_stmt) {
  122. return false;
  123. }
  124. $error = sqlsrv_errors();
  125. if (!$error) {
  126. return false;
  127. }
  128. return $error[0]['code'];
  129. }
  130. /**
  131. * Retrieves an array of error information, if any, associated with the
  132. * last operation on the statement handle.
  133. *
  134. * @return array
  135. */
  136. public function errorInfo()
  137. {
  138. if (!$this->_stmt) {
  139. return false;
  140. }
  141. $error = sqlsrv_errors();
  142. if (!$error) {
  143. return false;
  144. }
  145. return array(
  146. $error[0]['code'],
  147. $error[0]['message'],
  148. );
  149. }
  150. /**
  151. * Executes a prepared statement.
  152. *
  153. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  154. * @return bool
  155. * @throws Zend_Db_Statement_Exception
  156. */
  157. public function _execute(array $params = null)
  158. {
  159. $connection = $this->_adapter->getConnection();
  160. if (!$this->_stmt) {
  161. return false;
  162. }
  163. if ($params !== null) {
  164. if (!is_array($params)) {
  165. $params = array($params);
  166. }
  167. $error = false;
  168. // make all params passed by reference
  169. $params_ = array();
  170. $temp = array();
  171. $i = 1;
  172. foreach ($params as $param) {
  173. $temp[$i] = $param;
  174. $params_[] = &$temp[$i];
  175. $i++;
  176. }
  177. $params = $params_;
  178. }
  179. $this->_stmt = sqlsrv_query($connection, $this->_originalSQL, $params);
  180. if (!$this->_stmt) {
  181. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  182. throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
  183. }
  184. $this->_executed = true;
  185. return (!$this->_stmt);
  186. }
  187. /**
  188. * Fetches a row from the result set.
  189. *
  190. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  191. * @param int $cursor OPTIONAL Absolute, relative, or other.
  192. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  193. * @return mixed Array, object, or scalar depending on fetch mode.
  194. * @throws Zend_Db_Statement_Exception
  195. */
  196. public function fetch($style = null, $cursor = null, $offset = null)
  197. {
  198. if (!$this->_stmt) {
  199. return false;
  200. }
  201. if (null === $style) {
  202. $style = $this->_fetchMode;
  203. }
  204. $values = sqlsrv_fetch_array($this->_stmt, SQLSRV_FETCH_ASSOC);
  205. if (!$values && (null !== $error = sqlsrv_errors())) {
  206. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  207. throw new Zend_Db_Statement_Sqlsrv_Exception($error);
  208. }
  209. if (null === $values) {
  210. return null;
  211. }
  212. if (!$this->_keys) {
  213. foreach ($values as $key => $value) {
  214. $this->_keys[] = $this->_adapter->foldCase($key);
  215. }
  216. }
  217. $values = array_values($values);
  218. $row = false;
  219. switch ($style) {
  220. case Zend_Db::FETCH_NUM:
  221. $row = $values;
  222. break;
  223. case Zend_Db::FETCH_ASSOC:
  224. $row = array_combine($this->_keys, $values);
  225. break;
  226. case Zend_Db::FETCH_BOTH:
  227. $assoc = array_combine($this->_keys, $values);
  228. $row = array_merge($values, $assoc);
  229. break;
  230. case Zend_Db::FETCH_OBJ:
  231. $row = (object) array_combine($this->_keys, $values);
  232. break;
  233. case Zend_Db::FETCH_BOUND:
  234. $assoc = array_combine($this->_keys, $values);
  235. $row = array_merge($values, $assoc);
  236. $row = $this->_fetchBound($row);
  237. break;
  238. default:
  239. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  240. throw new Zend_Db_Statement_Sqlsrv_Exception("Invalid fetch mode '$style' specified");
  241. break;
  242. }
  243. return $row;
  244. }
  245. /**
  246. * Returns a single column from the next row of a result set.
  247. *
  248. * @param int $col OPTIONAL Position of the column to fetch.
  249. * @return string
  250. * @throws Zend_Db_Statement_Exception
  251. */
  252. public function fetchColumn($col = 0)
  253. {
  254. if (!$this->_stmt) {
  255. return false;
  256. }
  257. if (!sqlsrv_fetch($this->_stmt)) {
  258. if (null !== $error = sqlsrv_errors()) {
  259. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  260. throw new Zend_Db_Statement_Sqlsrv_Exception($error);
  261. }
  262. // If no error, there is simply no record
  263. return false;
  264. }
  265. $data = sqlsrv_get_field($this->_stmt, $col); //0-based
  266. if ($data === false) {
  267. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  268. throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
  269. }
  270. return $data;
  271. }
  272. /**
  273. * Fetches the next row and returns it as an object.
  274. *
  275. * @param string $class OPTIONAL Name of the class to create.
  276. * @param array $config OPTIONAL Constructor arguments for the class.
  277. * @return mixed One object instance of the specified class.
  278. * @throws Zend_Db_Statement_Exception
  279. */
  280. public function fetchObject($class = 'stdClass', array $config = array())
  281. {
  282. if (!$this->_stmt) {
  283. return false;
  284. }
  285. $obj = sqlsrv_fetch_object($this->_stmt);
  286. if ($error = sqlsrv_errors()) {
  287. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  288. throw new Zend_Db_Statement_Sqlsrv_Exception($error);
  289. }
  290. /* @todo XXX handle parameters */
  291. if (null === $obj) {
  292. return false;
  293. }
  294. return $obj;
  295. }
  296. /**
  297. * Returns metadata for a column in a result set.
  298. *
  299. * @param int $column
  300. * @return mixed
  301. * @throws Zend_Db_Statement_Sqlsrv_Exception
  302. */
  303. public function getColumnMeta($column)
  304. {
  305. $fields = sqlsrv_field_metadata($this->_stmt);
  306. if (!$fields) {
  307. throw new Zend_Db_Statement_Sqlsrv_Exception('Column metadata can not be fetched');
  308. }
  309. if (!isset($fields[$column])) {
  310. throw new Zend_Db_Statement_Sqlsrv_Exception('Column index does not exist in statement');
  311. }
  312. return $fields[$column];
  313. }
  314. /**
  315. * Retrieves the next rowset (result set) for a SQL statement that has
  316. * multiple result sets. An example is a stored procedure that returns
  317. * the results of multiple queries.
  318. *
  319. * @return bool
  320. * @throws Zend_Db_Statement_Exception
  321. */
  322. public function nextRowset()
  323. {
  324. if (sqlsrv_next_result($this->_stmt) === false) {
  325. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  326. throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
  327. }
  328. //else - moved to next (or there are no more rows)
  329. }
  330. /**
  331. * Returns the number of rows affected by the execution of the
  332. * last INSERT, DELETE, or UPDATE statement executed by this
  333. * statement object.
  334. *
  335. * @return int The number of rows affected.
  336. * @throws Zend_Db_Statement_Exception
  337. */
  338. public function rowCount()
  339. {
  340. if (!$this->_stmt) {
  341. return false;
  342. }
  343. if (!$this->_executed) {
  344. return 0;
  345. }
  346. $num_rows = sqlsrv_rows_affected($this->_stmt);
  347. // Strict check is necessary; 0 is a valid return value
  348. if ($num_rows === false) {
  349. require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
  350. throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
  351. }
  352. return $num_rows;
  353. }
  354. }