PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/OpenIview/Db/Statement/I5.php

https://github.com/sirshurf/phpi5
PHP | 367 lines | 177 code | 41 blank | 149 comment | 25 complexity | eba84c6b595594fb3242866a0bafa1c3 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: I5.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Db_Statement
  24. */
  25. require_once 'Zend/Db/Statement.php';
  26. /**
  27. * Extends for DB2 native adapter.
  28. *
  29. * @package Zend_Db
  30. * @subpackage Statement
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Openiview_Db_Statement_I5 extends Zend_Db_Statement
  35. {
  36. /**
  37. * Column names.
  38. */
  39. protected $_keys;
  40. /**
  41. * Fetched result values.
  42. */
  43. protected $_values;
  44. /**
  45. * Prepare a statement handle.
  46. *
  47. * @param string $sql
  48. * @return void
  49. * @throws Zend_Db_Statement_Db2_Exception
  50. */
  51. public function _prepare($sql)
  52. {
  53. $connection = $this->_adapter->getConnection();
  54. // db2_prepare on i5 emits errors, these need to be
  55. // suppressed so that proper exceptions can be thrown
  56. $this->_stmt = @i5_prepare($sql, $connection);
  57. if (!$this->_stmt) {
  58. /**
  59. * @see Openiview_Db_Statement_I5_Exception
  60. */
  61. $arrError = i5_error();
  62. require_once 'Openiview/Db/Statement/I5/Exception.php';
  63. throw new Openiview_Db_Statement_I5_Exception(
  64. $arrError['desc'],
  65. i5_errno()
  66. );
  67. }
  68. }
  69. /**
  70. * Binds a parameter to the specified variable name.
  71. *
  72. * @param mixed $parameter Name the parameter, either integer or string.
  73. * @param mixed $variable Reference to PHP variable containing the value.
  74. * @param mixed $type OPTIONAL Datatype of SQL parameter.
  75. * @param mixed $length OPTIONAL Length of SQL parameter.
  76. * @param mixed $options OPTIONAL Other options.
  77. * @return bool
  78. * @throws Openiview_Db_Statement_I5_Exception
  79. */
  80. public function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  81. {
  82. if ($type === null) {
  83. $type = DB2_PARAM_IN;
  84. }
  85. if (isset($options['data-type'])) {
  86. $datatype = $options['data-type'];
  87. } else {
  88. $datatype = DB2_CHAR;
  89. }
  90. //if (!db2_bind_param($this->_stmt, $position, "variable", $type, $datatype)) {
  91. if (!i5_bind_result ($this->_stmt, "variable")) {
  92. /**
  93. * @see Openiview_Db_Statement_I5_Exception
  94. */
  95. require_once 'Openiview/Db/Statement/I5/Exception.php';
  96. throw new Openiview_Db_Statement_I5_Exception(
  97. i5_errormsg(),
  98. i5_errno()
  99. );
  100. }
  101. return true;
  102. }
  103. /**
  104. * Closes the cursor, allowing the statement to be executed again.
  105. *
  106. * @return bool
  107. */
  108. public function closeCursor()
  109. {
  110. if (!$this->_stmt) {
  111. return false;
  112. }
  113. i5_free_query($this->_stmt);
  114. $this->_stmt = false;
  115. return true;
  116. }
  117. /**
  118. * Returns the number of columns in the result set.
  119. * Returns null if the statement has no result set metadata.
  120. *
  121. * @return int The number of columns.
  122. */
  123. public function columnCount()
  124. {
  125. if (!$this->_stmt) {
  126. return false;
  127. }
  128. return i5_num_fields($this->_stmt);
  129. }
  130. /**
  131. * Retrieves the error code, if any, associated with the last operation on
  132. * the statement handle.
  133. *
  134. * @return string error code.
  135. */
  136. public function errorCode()
  137. {
  138. if (!$this->_stmt) {
  139. return false;
  140. }
  141. $error = i5_errno();
  142. if ($error === '') {
  143. return false;
  144. }
  145. return $error;
  146. }
  147. /**
  148. * Retrieves an array of error information, if any, associated with the
  149. * last operation on the statement handle.
  150. *
  151. * @return array
  152. */
  153. public function errorInfo()
  154. {
  155. $error = $this->errorCode();
  156. if ($error === false){
  157. return false;
  158. }
  159. /*
  160. * Return three-valued array like PDO. But DB2 does not distinguish
  161. * between SQLCODE and native RDBMS error code, so repeat the SQLCODE.
  162. */
  163. return array(
  164. $error,
  165. $error,
  166. i5_errormsg()
  167. );
  168. }
  169. /**
  170. * Executes a prepared statement.
  171. *
  172. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  173. * @return bool
  174. * @throws Openiview_Db_Statement_I5_Exception
  175. */
  176. public function _execute(array $params = null)
  177. {
  178. if (!$this->_stmt) {
  179. return false;
  180. }
  181. $retval = true;
  182. if ($params !== null) {
  183. $retval = @i5_execute($this->_stmt, $params);
  184. } else {
  185. $retval = @i5_execute($this->_stmt);
  186. }
  187. if ($retval === false) {
  188. /**
  189. * @see Openiview_Db_Statement_I5_Exception
  190. */
  191. $arrError = i5_error();
  192. require_once 'Openiview/Db/Statement/I5/Exception.php';
  193. throw new Openiview_Db_Statement_I5_Exception(
  194. $arrError['desc'],
  195. i5_errno());
  196. }
  197. $this->_keys = array();
  198. if ($field_num = $this->columnCount()) {
  199. for ($i = 0; $i < $field_num; $i++) {
  200. $name = i5_field_name($this->_stmt, $i);
  201. $this->_keys[] = $name;
  202. }
  203. }
  204. $this->_values = array();
  205. if ($this->_keys) {
  206. $this->_values = array_fill(0, count($this->_keys), null);
  207. }
  208. return $retval;
  209. }
  210. /**
  211. * Fetches a row from the result set.
  212. *
  213. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  214. * @param int $cursor OPTIONAL Absolute, relative, or other.
  215. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  216. * @return mixed Array, object, or scalar depending on fetch mode.
  217. * @throws Openiview_Db_Statement_I5_Exception
  218. */
  219. public function fetch($style = null, $cursor = null, $offset = null)
  220. {
  221. if (!$this->_stmt) {
  222. return false;
  223. }
  224. if ($style === null) {
  225. $style = $this->_fetchMode;
  226. }
  227. switch ($style) {
  228. case Zend_Db::FETCH_NUM :
  229. $row = i5_fetch_row($this->_stmt);
  230. break;
  231. case Zend_Db::FETCH_ASSOC :
  232. $row = i5_fetch_assoc($this->_stmt);
  233. break;
  234. case Zend_Db::FETCH_BOTH :
  235. $row = i5_fetch_array($this->_stmt);
  236. break;
  237. case Zend_Db::FETCH_OBJ :
  238. $row = i5_fetch_object($this->_stmt);
  239. break;
  240. case Zend_Db::FETCH_BOUND:
  241. $row = i5_fetch_array($this->_stmt);
  242. if ($row !== false) {
  243. return $this->_fetchBound($row);
  244. }
  245. break;
  246. default:
  247. /**
  248. * @see Openiview_Db_Statement_I5_Exception
  249. */
  250. require_once 'Openiview/Db/Statement/I5/Exception.php';
  251. throw new Openiview_Db_Statement_I5_Exception("Invalid fetch mode '$style' specified");
  252. break;
  253. }
  254. return $row;
  255. }
  256. /**
  257. * Fetches the next row and returns it as an object.
  258. *
  259. * @param string $class OPTIONAL Name of the class to create.
  260. * @param array $config OPTIONAL Constructor arguments for the class.
  261. * @return mixed One object instance of the specified class.
  262. */
  263. public function fetchObject($class = 'stdClass', array $config = array())
  264. {
  265. $obj = $this->fetch(Zend_Db::FETCH_OBJ);
  266. return $obj;
  267. }
  268. /**
  269. * Retrieves the next rowset (result set) for a SQL statement that has
  270. * multiple result sets. An example is a stored procedure that returns
  271. * the results of multiple queries.
  272. *
  273. * @return bool
  274. * @throws Openiview_Db_Statement_I5_Exception
  275. */
  276. public function nextRowset()
  277. {
  278. /**
  279. * @see Openiview_Db_Statement_I5_Exception
  280. */
  281. require_once 'Openiview/Db/Statement/I5/Exception.php';
  282. throw new Openiview_Db_Statement_I5_Exception(__FUNCTION__ . '() is not implemented');
  283. }
  284. /**
  285. * Returns the number of rows affected by the execution of the
  286. * last INSERT, DELETE, or UPDATE statement executed by this
  287. * statement object.
  288. *
  289. * @return int The number of rows affected.
  290. */
  291. public function rowCount()
  292. {
  293. if (!$this->_stmt) {
  294. return false;
  295. }
  296. $num = @db2_num_rows($this->_stmt);
  297. if ($num === false) {
  298. return 0;
  299. }
  300. return $num;
  301. }
  302. /**
  303. * Returns an array containing all of the result set rows.
  304. *
  305. * @param int $style OPTIONAL Fetch mode.
  306. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  307. * @return array Collection of rows, each in a format by the fetch mode.
  308. *
  309. * Behaves like parent, but if limit()
  310. * is used, the final result removes the extra column
  311. * 'zend_db_rownum'
  312. */
  313. public function fetchAll($style = null, $col = null)
  314. {
  315. $data = parent::fetchAll($style, $col);
  316. $results = array();
  317. $remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM');
  318. foreach ($data as $row) {
  319. if (is_array($row) && array_key_exists($remove, $row)) {
  320. unset($row[$remove]);
  321. }
  322. $results[] = $row;
  323. }
  324. return $results;
  325. }
  326. }