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.
16
* @subpackage Statement
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
22
* @see Zend_Db_Statement
24
require_once 'Zend/Db/Statement.php';
27
* Extends for DB2 native adapter.
30
* @subpackage Statement
31
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
32
* @license http://framework.zend.com/license/new-bsd New BSD License
34
class Zend_Db_Statement_Db2 extends Zend_Db_Statement
37
* Statement resource handle.
39
protected $_stmt = null;
47
* Fetched result values.
52
* Prepare a statement handle.
56
* @throws Zend_Db_Statement_Db2_Exception
58
public function _prepare($sql)
60
$connection = $this->_adapter->getConnection();
62
$this->_stmt = db2_prepare($connection, $sql);
66
* @see Zend_Db_Statement_Db2_Exception
68
require_once 'Zend/Db/Statement/Db2/Exception.php';
69
throw new Zend_Db_Statement_Db2_Exception(
77
* Binds a parameter to the specified variable name.
79
* @param mixed $parameter Name the parameter, either integer or string.
80
* @param mixed $variable Reference to PHP variable containing the value.
81
* @param mixed $type OPTIONAL Datatype of SQL parameter.
82
* @param mixed $length OPTIONAL Length of SQL parameter.
83
* @param mixed $options OPTIONAL Other options.
85
* @throws Zend_Db_Statement_Db2_Exception
87
public function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
93
if (isset($options['data-type'])) {
94
$datatype = $options['data-type'];
99
if (!db2_bind_param($this->_stmt, $position, "variable", $type, $datatype)) {
101
* @see Zend_Db_Statement_Db2_Exception
103
require_once 'Zend/Db/Statement/Db2/Exception.php';
104
throw new Zend_Db_Statement_Db2_Exception(
105
db2_stmt_errormsg($this->_stmt),
106
db2_stmt_error($this->_stmt)
114
* Closes the cursor, allowing the statement to be executed again.
118
public function closeCursor()
123
db2_free_stmt($this->_stmt);
124
$this->_stmt = false;
130
* Returns the number of columns in the result set.
131
* Returns null if the statement has no result set metadata.
133
* @return int The number of columns.
135
public function columnCount()
140
return db2_num_fields($this->_stmt);
144
* Retrieves the error code, if any, associated with the last operation on
145
* the statement handle.
147
* @return string error code.
149
public function errorCode()
155
return db2_stmt_error($this->_stmt);
159
* Retrieves an array of error information, if any, associated with the
160
* last operation on the statement handle.
164
public function errorInfo()
167
return array(false, 0, '');
171
* Return three-valued array like PDO. But DB2 does not distinguish
172
* between SQLCODE and native RDBMS error code, so repeat the SQLCODE.
175
db2_stmt_error($this->_stmt),
176
db2_stmt_error($this->_stmt),
177
db2_stmt_errormsg($this->_stmt)
182
* Executes a prepared statement.
184
* @param array $params OPTIONAL Values to bind to parameter placeholders.
186
* @throws Zend_Db_Statement_Db2_Exception
188
public function _execute(array $params = null)
195
if ($params !== null) {
196
$retval = @db2_execute($this->_stmt, $params);
198
$retval = @db2_execute($this->_stmt);
201
if ($retval === false) {
203
* @see Zend_Db_Statement_Db2_Exception
205
require_once 'Zend/Db/Statement/Db2/Exception.php';
206
throw new Zend_Db_Statement_Db2_Exception(
207
db2_stmt_errormsg($this->_stmt),
208
db2_stmt_error($this->_stmt));
211
$this->_keys = array();
212
if ($field_num = $this->columnCount()) {
213
for ($i = 0; $i < $field_num; $i++) {
214
$name = db2_field_name($this->_stmt, $i);
215
$this->_keys[] = $name;
219
$this->_values = array();
221
$this->_values = array_fill(0, count($this->_keys), null);
228
* Fetches a row from the result set.
230
* @param int $style OPTIONAL Fetch mode for this fetch operation.
231
* @param int $cursor OPTIONAL Absolute, relative, or other.
232
* @param int $offset OPTIONAL Number for absolute or relative cursors.
233
* @return mixed Array, object, or scalar depending on fetch mode.
234
* @throws Zend_Db_Statement_Db2_Exception
236
public function fetch($style = null, $cursor = null, $offset = null)
242
if ($style === null) {
243
$style = $this->_fetchMode;
247
case Zend_Db::FETCH_NUM :
248
$row = db2_fetch_array($this->_stmt);
250
case Zend_Db::FETCH_ASSOC :
251
$row = db2_fetch_assoc($this->_stmt);
253
case Zend_Db::FETCH_BOTH :
254
$row = db2_fetch_both($this->_stmt);
256
case Zend_Db::FETCH_OBJ :
257
$row = db2_fetch_object($this->_stmt);
259
case Zend_Db::FETCH_BOUND:
260
$row = db2_fetch_both($this->_stmt);
261
if ($row !== false) {
262
return $this->_fetchBound($row);
267
* @see Zend_Db_Statement_Db2_Exception
269
require_once 'Zend/Db/Statement/Db2/Exception.php';
270
throw new Zend_Db_Statement_Db2_Exception("Invalid fetch mode '$style' specified");
278
* Fetches the next row and returns it as an object.
280
* @param string $class OPTIONAL Name of the class to create.
281
* @param array $config OPTIONAL Constructor arguments for the class.
282
* @return mixed One object instance of the specified class.
284
public function fetchObject($class = 'stdClass', array $config = array())
286
$obj = $this->fetch(Zend_Db::FETCH_OBJ);
291
* Retrieves the next rowset (result set) for a SQL statement that has
292
* multiple result sets. An example is a stored procedure that returns
293
* the results of multiple queries.
296
* @throws Zend_Db_Statement_Db2_Exception
298
public function nextRowset()
301
* @see Zend_Db_Statement_Db2_Exception
303
require_once 'Zend/Db/Statement/Db2/Exception.php';
304
throw new Zend_Db_Statement_Db2_Exception(__FUNCTION__ . '() is not implemented');
308
* Returns the number of rows affected by the execution of the
309
* last INSERT, DELETE, or UPDATE statement executed by this
312
* @return int The number of rows affected.
314
public function rowCount()
320
$num = @db2_num_rows($this->_stmt);
322
if ($num === false) {
330
* Returns an array containing all of the result set rows.
332
* @param int $style OPTIONAL Fetch mode.
333
* @param int $col OPTIONAL Column number, if fetch mode is by column.
334
* @return array Collection of rows, each in a format by the fetch mode.
336
* Behaves like parent, but if limit()
337
* is used, the final result removes the extra column
340
public function fetchAll($style = null, $col = null)
342
$data = parent::fetchAll($style, $col);
344
$remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM');
346
foreach ($data as $row) {
347
if (is_array($row) && array_key_exists($remove, $row)) {
348
unset($row[$remove]);