PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/cubi/openbiz/others/Zend/Db/Statement/Db2.php

http://openbiz-cubi.googlecode.com/
PHP | 360 lines | 169 code | 37 blank | 154 comment | 25 complexity | 7fbeda4d3ba144ba9dfd49e392b122d1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  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: Db2.php 18951 2009-11-12 16:26:19Z alexander $
  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-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_Db2 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 = @db2_prepare($connection, $sql);
  57. if (!$this->_stmt) {
  58. /**
  59. * @see Zend_Db_Statement_Db2_Exception
  60. */
  61. // require_once 'Zend/Db/Statement/Db2/Exception.php';
  62. throw new Zend_Db_Statement_Db2_Exception(
  63. db2_stmt_errormsg(),
  64. db2_stmt_error()
  65. );
  66. }
  67. }
  68. /**
  69. * Binds a parameter to the specified variable name.
  70. *
  71. * @param mixed $parameter Name the parameter, either integer or string.
  72. * @param mixed $variable Reference to PHP variable containing the value.
  73. * @param mixed $type OPTIONAL Datatype of SQL parameter.
  74. * @param mixed $length OPTIONAL Length of SQL parameter.
  75. * @param mixed $options OPTIONAL Other options.
  76. * @return bool
  77. * @throws Zend_Db_Statement_Db2_Exception
  78. */
  79. public function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  80. {
  81. if ($type === null) {
  82. $type = DB2_PARAM_IN;
  83. }
  84. if (isset($options['data-type'])) {
  85. $datatype = $options['data-type'];
  86. } else {
  87. $datatype = DB2_CHAR;
  88. }
  89. if (!db2_bind_param($this->_stmt, $position, "variable", $type, $datatype)) {
  90. /**
  91. * @see Zend_Db_Statement_Db2_Exception
  92. */
  93. // require_once 'Zend/Db/Statement/Db2/Exception.php';
  94. throw new Zend_Db_Statement_Db2_Exception(
  95. db2_stmt_errormsg(),
  96. db2_stmt_error()
  97. );
  98. }
  99. return true;
  100. }
  101. /**
  102. * Closes the cursor, allowing the statement to be executed again.
  103. *
  104. * @return bool
  105. */
  106. public function closeCursor()
  107. {
  108. if (!$this->_stmt) {
  109. return false;
  110. }
  111. db2_free_stmt($this->_stmt);
  112. $this->_stmt = false;
  113. return true;
  114. }
  115. /**
  116. * Returns the number of columns in the result set.
  117. * Returns null if the statement has no result set metadata.
  118. *
  119. * @return int The number of columns.
  120. */
  121. public function columnCount()
  122. {
  123. if (!$this->_stmt) {
  124. return false;
  125. }
  126. return db2_num_fields($this->_stmt);
  127. }
  128. /**
  129. * Retrieves the error code, if any, associated with the last operation on
  130. * the statement handle.
  131. *
  132. * @return string error code.
  133. */
  134. public function errorCode()
  135. {
  136. if (!$this->_stmt) {
  137. return false;
  138. }
  139. $error = db2_stmt_error();
  140. if ($error === '') {
  141. return false;
  142. }
  143. return $error;
  144. }
  145. /**
  146. * Retrieves an array of error information, if any, associated with the
  147. * last operation on the statement handle.
  148. *
  149. * @return array
  150. */
  151. public function errorInfo()
  152. {
  153. $error = $this->errorCode();
  154. if ($error === false){
  155. return false;
  156. }
  157. /*
  158. * Return three-valued array like PDO. But DB2 does not distinguish
  159. * between SQLCODE and native RDBMS error code, so repeat the SQLCODE.
  160. */
  161. return array(
  162. $error,
  163. $error,
  164. db2_stmt_errormsg()
  165. );
  166. }
  167. /**
  168. * Executes a prepared statement.
  169. *
  170. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  171. * @return bool
  172. * @throws Zend_Db_Statement_Db2_Exception
  173. */
  174. public function _execute(array $params = null)
  175. {
  176. if (!$this->_stmt) {
  177. return false;
  178. }
  179. $retval = true;
  180. if ($params !== null) {
  181. $retval = @db2_execute($this->_stmt, $params);
  182. } else {
  183. $retval = @db2_execute($this->_stmt);
  184. }
  185. if ($retval === false) {
  186. /**
  187. * @see Zend_Db_Statement_Db2_Exception
  188. */
  189. // require_once 'Zend/Db/Statement/Db2/Exception.php';
  190. throw new Zend_Db_Statement_Db2_Exception(
  191. db2_stmt_errormsg(),
  192. db2_stmt_error());
  193. }
  194. $this->_keys = array();
  195. if ($field_num = $this->columnCount()) {
  196. for ($i = 0; $i < $field_num; $i++) {
  197. $name = db2_field_name($this->_stmt, $i);
  198. $this->_keys[] = $name;
  199. }
  200. }
  201. $this->_values = array();
  202. if ($this->_keys) {
  203. $this->_values = array_fill(0, count($this->_keys), null);
  204. }
  205. return $retval;
  206. }
  207. /**
  208. * Fetches a row from the result set.
  209. *
  210. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  211. * @param int $cursor OPTIONAL Absolute, relative, or other.
  212. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  213. * @return mixed Array, object, or scalar depending on fetch mode.
  214. * @throws Zend_Db_Statement_Db2_Exception
  215. */
  216. public function fetch($style = null, $cursor = null, $offset = null)
  217. {
  218. if (!$this->_stmt) {
  219. return false;
  220. }
  221. if ($style === null) {
  222. $style = $this->_fetchMode;
  223. }
  224. switch ($style) {
  225. case Zend_Db::FETCH_NUM :
  226. $row = db2_fetch_array($this->_stmt);
  227. break;
  228. case Zend_Db::FETCH_ASSOC :
  229. $row = db2_fetch_assoc($this->_stmt);
  230. break;
  231. case Zend_Db::FETCH_BOTH :
  232. $row = db2_fetch_both($this->_stmt);
  233. break;
  234. case Zend_Db::FETCH_OBJ :
  235. $row = db2_fetch_object($this->_stmt);
  236. break;
  237. case Zend_Db::FETCH_BOUND:
  238. $row = db2_fetch_both($this->_stmt);
  239. if ($row !== false) {
  240. return $this->_fetchBound($row);
  241. }
  242. break;
  243. default:
  244. /**
  245. * @see Zend_Db_Statement_Db2_Exception
  246. */
  247. // require_once 'Zend/Db/Statement/Db2/Exception.php';
  248. throw new Zend_Db_Statement_Db2_Exception("Invalid fetch mode '$style' specified");
  249. break;
  250. }
  251. return $row;
  252. }
  253. /**
  254. * Fetches the next row and returns it as an object.
  255. *
  256. * @param string $class OPTIONAL Name of the class to create.
  257. * @param array $config OPTIONAL Constructor arguments for the class.
  258. * @return mixed One object instance of the specified class.
  259. */
  260. public function fetchObject($class = 'stdClass', array $config = array())
  261. {
  262. $obj = $this->fetch(Zend_Db::FETCH_OBJ);
  263. return $obj;
  264. }
  265. /**
  266. * Retrieves the next rowset (result set) for a SQL statement that has
  267. * multiple result sets. An example is a stored procedure that returns
  268. * the results of multiple queries.
  269. *
  270. * @return bool
  271. * @throws Zend_Db_Statement_Db2_Exception
  272. */
  273. public function nextRowset()
  274. {
  275. /**
  276. * @see Zend_Db_Statement_Db2_Exception
  277. */
  278. // require_once 'Zend/Db/Statement/Db2/Exception.php';
  279. throw new Zend_Db_Statement_Db2_Exception(__FUNCTION__ . '() is not implemented');
  280. }
  281. /**
  282. * Returns the number of rows affected by the execution of the
  283. * last INSERT, DELETE, or UPDATE statement executed by this
  284. * statement object.
  285. *
  286. * @return int The number of rows affected.
  287. */
  288. public function rowCount()
  289. {
  290. if (!$this->_stmt) {
  291. return false;
  292. }
  293. $num = @db2_num_rows($this->_stmt);
  294. if ($num === false) {
  295. return 0;
  296. }
  297. return $num;
  298. }
  299. /**
  300. * Returns an array containing all of the result set rows.
  301. *
  302. * @param int $style OPTIONAL Fetch mode.
  303. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  304. * @return array Collection of rows, each in a format by the fetch mode.
  305. *
  306. * Behaves like parent, but if limit()
  307. * is used, the final result removes the extra column
  308. * 'zend_db_rownum'
  309. */
  310. public function fetchAll($style = null, $col = null)
  311. {
  312. $data = parent::fetchAll($style, $col);
  313. $results = array();
  314. $remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM');
  315. foreach ($data as $row) {
  316. if (is_array($row) && array_key_exists($remove, $row)) {
  317. unset($row[$remove]);
  318. }
  319. $results[] = $row;
  320. }
  321. return $results;
  322. }
  323. }