PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/store/lib/Zend/Db/Statement/Sqlsrv.php

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