PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Db/Statement/Db2.php

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