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