~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Db/Statement/Db2.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * @package    Zend_Db
 
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
 
19
 */
 
20
 
 
21
/**
 
22
 * @see Zend_Db_Statement
 
23
 */
 
24
require_once 'Zend/Db/Statement.php';
 
25
 
 
26
/**
 
27
 * Extends for DB2 native adapter.
 
28
 *
 
29
 * @package    Zend_Db
 
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
 
33
 */
 
34
class Zend_Db_Statement_Db2 extends Zend_Db_Statement
 
35
{
 
36
    /**
 
37
     * Statement resource handle.
 
38
     */
 
39
    protected $_stmt = null;
 
40
 
 
41
    /**
 
42
     * Column names.
 
43
     */
 
44
    protected $_keys;
 
45
 
 
46
    /**
 
47
     * Fetched result values.
 
48
     */
 
49
    protected $_values;
 
50
 
 
51
    /**
 
52
     * Prepare a statement handle.
 
53
     *
 
54
     * @param string $sql
 
55
     * @return void
 
56
     * @throws Zend_Db_Statement_Db2_Exception
 
57
     */
 
58
    public function _prepare($sql)
 
59
    {
 
60
        $connection = $this->_adapter->getConnection();
 
61
 
 
62
        $this->_stmt = db2_prepare($connection, $sql);
 
63
 
 
64
        if (!$this->_stmt) {
 
65
            /**
 
66
             * @see Zend_Db_Statement_Db2_Exception
 
67
             */
 
68
            require_once 'Zend/Db/Statement/Db2/Exception.php';
 
69
            throw new Zend_Db_Statement_Db2_Exception(
 
70
                db2_stmt_errormsg(),
 
71
                db2_stmt_error()
 
72
            );
 
73
        }
 
74
    }
 
75
 
 
76
    /**
 
77
     * Binds a parameter to the specified variable name.
 
78
     *
 
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.
 
84
     * @return bool
 
85
     * @throws Zend_Db_Statement_Db2_Exception
 
86
     */
 
87
    public function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
 
88
    {
 
89
        if ($type === null) {
 
90
            $type = DB2_PARAM_IN;
 
91
        }
 
92
 
 
93
        if (isset($options['data-type'])) {
 
94
            $datatype = $options['data-type'];
 
95
        } else {
 
96
            $datatype = DB2_CHAR;
 
97
        }
 
98
 
 
99
        if (!db2_bind_param($this->_stmt, $position, "variable", $type, $datatype)) {
 
100
            /**
 
101
             * @see Zend_Db_Statement_Db2_Exception
 
102
             */
 
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)
 
107
            );
 
108
        }
 
109
 
 
110
        return true;
 
111
    }
 
112
 
 
113
    /**
 
114
     * Closes the cursor, allowing the statement to be executed again.
 
115
     *
 
116
     * @return bool
 
117
     */
 
118
    public function closeCursor()
 
119
    {
 
120
        if (!$this->_stmt) {
 
121
            return false;
 
122
        }
 
123
        db2_free_stmt($this->_stmt);
 
124
        $this->_stmt = false;
 
125
        return true;
 
126
    }
 
127
 
 
128
 
 
129
    /**
 
130
     * Returns the number of columns in the result set.
 
131
     * Returns null if the statement has no result set metadata.
 
132
     *
 
133
     * @return int The number of columns.
 
134
     */
 
135
    public function columnCount()
 
136
    {
 
137
        if (!$this->_stmt) {
 
138
            return false;
 
139
        }
 
140
        return db2_num_fields($this->_stmt);
 
141
    }
 
142
 
 
143
    /**
 
144
     * Retrieves the error code, if any, associated with the last operation on
 
145
     * the statement handle.
 
146
     *
 
147
     * @return string error code.
 
148
     */
 
149
    public function errorCode()
 
150
    {
 
151
        if (!$this->_stmt) {
 
152
            return '0000';
 
153
        }
 
154
 
 
155
        return db2_stmt_error($this->_stmt);
 
156
    }
 
157
 
 
158
    /**
 
159
     * Retrieves an array of error information, if any, associated with the
 
160
     * last operation on the statement handle.
 
161
     *
 
162
     * @return array
 
163
     */
 
164
    public function errorInfo()
 
165
    {
 
166
        if (!$this->_stmt) {
 
167
            return array(false, 0, '');
 
168
        }
 
169
 
 
170
        /*
 
171
         * Return three-valued array like PDO.  But DB2 does not distinguish
 
172
         * between SQLCODE and native RDBMS error code, so repeat the SQLCODE.
 
173
         */
 
174
        return array(
 
175
            db2_stmt_error($this->_stmt),
 
176
            db2_stmt_error($this->_stmt),
 
177
            db2_stmt_errormsg($this->_stmt)
 
178
        );
 
179
    }
 
180
 
 
181
    /**
 
182
     * Executes a prepared statement.
 
183
     *
 
184
     * @param array $params OPTIONAL Values to bind to parameter placeholders.
 
185
     * @return bool
 
186
     * @throws Zend_Db_Statement_Db2_Exception
 
187
     */
 
188
    public function _execute(array $params = null)
 
189
    {
 
190
        if (!$this->_stmt) {
 
191
            return false;
 
192
        }
 
193
 
 
194
        $retval = true;
 
195
        if ($params !== null) {
 
196
            $retval = @db2_execute($this->_stmt, $params);
 
197
        } else {
 
198
            $retval = @db2_execute($this->_stmt);
 
199
        }
 
200
 
 
201
        if ($retval === false) {
 
202
            /**
 
203
             * @see Zend_Db_Statement_Db2_Exception
 
204
             */
 
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));
 
209
        }
 
210
 
 
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;
 
216
            }
 
217
        }
 
218
 
 
219
        $this->_values = array();
 
220
        if ($this->_keys) {
 
221
            $this->_values = array_fill(0, count($this->_keys), null);
 
222
        }
 
223
 
 
224
        return $retval;
 
225
    }
 
226
 
 
227
    /**
 
228
     * Fetches a row from the result set.
 
229
     *
 
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
 
235
     */
 
236
    public function fetch($style = null, $cursor = null, $offset = null)
 
237
    {
 
238
        if (!$this->_stmt) {
 
239
            return false;
 
240
        }
 
241
 
 
242
        if ($style === null) {
 
243
            $style = $this->_fetchMode;
 
244
        }
 
245
 
 
246
        switch ($style) {
 
247
            case Zend_Db::FETCH_NUM :
 
248
                $row = db2_fetch_array($this->_stmt);
 
249
                break;
 
250
            case Zend_Db::FETCH_ASSOC :
 
251
                $row = db2_fetch_assoc($this->_stmt);
 
252
                break;
 
253
            case Zend_Db::FETCH_BOTH :
 
254
                $row = db2_fetch_both($this->_stmt);
 
255
                break;
 
256
            case Zend_Db::FETCH_OBJ :
 
257
                $row = db2_fetch_object($this->_stmt);
 
258
                break;
 
259
            case Zend_Db::FETCH_BOUND:
 
260
                $row = db2_fetch_both($this->_stmt);
 
261
                if ($row !== false) {
 
262
                    return $this->_fetchBound($row);
 
263
                }
 
264
                break;
 
265
            default:
 
266
                /**
 
267
                 * @see Zend_Db_Statement_Db2_Exception
 
268
                 */
 
269
                require_once 'Zend/Db/Statement/Db2/Exception.php';
 
270
                throw new Zend_Db_Statement_Db2_Exception("Invalid fetch mode '$style' specified");
 
271
                break;
 
272
        }
 
273
 
 
274
        return $row;
 
275
    }
 
276
 
 
277
    /**
 
278
     * Fetches the next row and returns it as an object.
 
279
     *
 
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.
 
283
     */
 
284
    public function fetchObject($class = 'stdClass', array $config = array())
 
285
    {
 
286
        $obj = $this->fetch(Zend_Db::FETCH_OBJ);
 
287
        return $obj;
 
288
    }
 
289
 
 
290
    /**
 
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.
 
294
     *
 
295
     * @return bool
 
296
     * @throws Zend_Db_Statement_Db2_Exception
 
297
     */
 
298
    public function nextRowset()
 
299
    {
 
300
        /**
 
301
         * @see Zend_Db_Statement_Db2_Exception
 
302
         */
 
303
        require_once 'Zend/Db/Statement/Db2/Exception.php';
 
304
        throw new Zend_Db_Statement_Db2_Exception(__FUNCTION__ . '() is not implemented');
 
305
    }
 
306
 
 
307
    /**
 
308
     * Returns the number of rows affected by the execution of the
 
309
     * last INSERT, DELETE, or UPDATE statement executed by this
 
310
     * statement object.
 
311
     *
 
312
     * @return int     The number of rows affected.
 
313
     */
 
314
    public function rowCount()
 
315
    {
 
316
        if (!$this->_stmt) {
 
317
            return false;
 
318
        }
 
319
 
 
320
        $num = @db2_num_rows($this->_stmt);
 
321
 
 
322
        if ($num === false) {
 
323
            return 0;
 
324
        }
 
325
 
 
326
        return $num;
 
327
    }
 
328
 
 
329
     /**
 
330
     * Returns an array containing all of the result set rows.
 
331
     *
 
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.
 
335
     *
 
336
     * Behaves like parent, but if limit()
 
337
     * is used, the final result removes the extra column
 
338
     * 'zend_db_rownum'
 
339
     */
 
340
    public function fetchAll($style = null, $col = null)
 
341
    {
 
342
        $data = parent::fetchAll($style, $col);
 
343
        $results = array();
 
344
        $remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM');
 
345
 
 
346
        foreach ($data as $row) {
 
347
            if (is_array($row) && array_key_exists($remove, $row)) {
 
348
                unset($row[$remove]);
 
349
            }
 
350
            $results[] = $row;
 
351
        }
 
352
        return $results;
 
353
    }
 
354
}