~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Db/Statement/Oracle.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
 * @category   Zend
 
16
 * @package    Zend_Db
 
17
 * @subpackage Statement
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
/**
 
23
 * @see Zend_Db_Statement
 
24
 */
 
25
require_once 'Zend/Db/Statement.php';
 
26
 
 
27
/**
 
28
 * Extends for Oracle.
 
29
 *
 
30
 * @category   Zend
 
31
 * @package    Zend_Db
 
32
 * @subpackage Statement
 
33
 * @copyright  Copyright (c) 2005-2008 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_Oracle extends Zend_Db_Statement
 
37
{
 
38
 
 
39
    /**
 
40
     * The connection_stmt object.
 
41
     */
 
42
    protected $_stmt;
 
43
 
 
44
    /**
 
45
     * Column names.
 
46
     */
 
47
    protected $_keys;
 
48
 
 
49
    /**
 
50
     * Fetched result values.
 
51
     */
 
52
    protected $_values;
 
53
 
 
54
    /**
 
55
     * Prepares statement handle
 
56
     *
 
57
     * @param string $sql
 
58
     * @return void
 
59
     * @throws Zend_Db_Statement_Oracle_Exception
 
60
     */
 
61
    protected function _prepare($sql)
 
62
    {
 
63
        $connection = $this->_adapter->getConnection();
 
64
        $this->_stmt = oci_parse($connection, $sql);
 
65
        if (!$this->_stmt) {
 
66
            /**
 
67
             * @see Zend_Db_Statement_Oracle_Exception
 
68
             */
 
69
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
70
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($connection));
 
71
        }
 
72
    }
 
73
 
 
74
    /**
 
75
     * Binds a parameter to the specified variable name.
 
76
     *
 
77
     * @param mixed $parameter Name the parameter, either integer or string.
 
78
     * @param mixed $variable  Reference to PHP variable containing the value.
 
79
     * @param mixed $type      OPTIONAL Datatype of SQL parameter.
 
80
     * @param mixed $length    OPTIONAL Length of SQL parameter.
 
81
     * @param mixed $options   OPTIONAL Other options.
 
82
     * @return bool
 
83
     * @throws Zend_Db_Statement_Exception
 
84
     */
 
85
    protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
 
86
    {
 
87
        // default value
 
88
        if ($type === NULL) {
 
89
            $type = SQLT_CHR;
 
90
        }
 
91
 
 
92
        // default value
 
93
        if ($length === NULL) {
 
94
            $length = -1;
 
95
        }
 
96
 
 
97
        $retval = @oci_bind_by_name($this->_stmt, $parameter, $variable, $length, $type);
 
98
        if ($retval === false) {
 
99
            /**
 
100
             * @see Zend_Db_Adapter_Oracle_Exception
 
101
             */
 
102
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
103
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
 
104
        }
 
105
 
 
106
        return true;
 
107
    }
 
108
 
 
109
    /**
 
110
     * Closes the cursor, allowing the statement to be executed again.
 
111
     *
 
112
     * @return bool
 
113
     */
 
114
    public function closeCursor()
 
115
    {
 
116
        if (!$this->_stmt) {
 
117
            return false;
 
118
        }
 
119
 
 
120
        oci_free_statement($this->_stmt);
 
121
        $this->_stmt = false;
 
122
        return true;
 
123
    }
 
124
 
 
125
    /**
 
126
     * Returns the number of columns in the result set.
 
127
     * Returns null if the statement has no result set metadata.
 
128
     *
 
129
     * @return int The number of columns.
 
130
     */
 
131
    public function columnCount()
 
132
    {
 
133
        if (!$this->_stmt) {
 
134
            return false;
 
135
        }
 
136
 
 
137
        return oci_num_fields($this->_stmt);
 
138
    }
 
139
 
 
140
 
 
141
    /**
 
142
     * Retrieves the error code, if any, associated with the last operation on
 
143
     * the statement handle.
 
144
     *
 
145
     * @return string error code.
 
146
     */
 
147
    public function errorCode()
 
148
    {
 
149
        if (!$this->_stmt) {
 
150
            return false;
 
151
        }
 
152
 
 
153
        $error = oci_error($this->_stmt);
 
154
 
 
155
        if (!$error) {
 
156
            return false;
 
157
        }
 
158
 
 
159
        return $error['code'];
 
160
    }
 
161
 
 
162
 
 
163
    /**
 
164
     * Retrieves an array of error information, if any, associated with the
 
165
     * last operation on the statement handle.
 
166
     *
 
167
     * @return array
 
168
     */
 
169
    public function errorInfo()
 
170
    {
 
171
        if (!$this->_stmt) {
 
172
            return false;
 
173
        }
 
174
 
 
175
        $error = oci_error($this->_stmt);
 
176
        if (!$error) {
 
177
            return false;
 
178
        }
 
179
 
 
180
        if (isset($error['sqltext'])) {
 
181
            return array(
 
182
                $error['code'],
 
183
                $error['message'],
 
184
                $error['offset'],
 
185
                $error['sqltext'],
 
186
            );
 
187
        } else {
 
188
            return array(
 
189
                $error['code'],
 
190
                $error['message'],
 
191
            );
 
192
        }
 
193
    }
 
194
 
 
195
 
 
196
    /**
 
197
     * Executes a prepared statement.
 
198
     *
 
199
     * @param array $params OPTIONAL Values to bind to parameter placeholders.
 
200
     * @return bool
 
201
     * @throws Zend_Db_Statement_Exception
 
202
     */
 
203
    public function _execute(array $params = null)
 
204
    {
 
205
        $connection = $this->_adapter->getConnection();
 
206
        if (!$this->_stmt) {
 
207
            return false;
 
208
        }
 
209
 
 
210
        if (! $this->_stmt) {
 
211
            /**
 
212
             * @see Zend_Db_Adapter_Oracle_Exception
 
213
             */
 
214
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
215
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($connection));
 
216
        }
 
217
 
 
218
        if ($params !== null) {
 
219
            if (!is_array($params)) {
 
220
                $params = array($params);
 
221
            }
 
222
            $error = false;
 
223
            foreach (array_keys($params) as $name) {
 
224
                if (!@oci_bind_by_name($this->_stmt, $name, $params[$name], -1)) {
 
225
                    $error = true;
 
226
                    break;
 
227
                }
 
228
            }
 
229
            if ($error) {
 
230
                /**
 
231
                 * @see Zend_Db_Adapter_Oracle_Exception
 
232
                 */
 
233
                require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
234
                throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
 
235
            }
 
236
        }
 
237
 
 
238
        $retval = @oci_execute($this->_stmt, $this->_adapter->_getExecuteMode());
 
239
        if ($retval === false) {
 
240
            /**
 
241
             * @see Zend_Db_Adapter_Oracle_Exception
 
242
             */
 
243
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
244
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
 
245
        }
 
246
 
 
247
        $this->_keys = Array();
 
248
        if ($field_num = oci_num_fields($this->_stmt)) {
 
249
            for ($i = 1; $i <= $field_num; $i++) {
 
250
                $name = oci_field_name($this->_stmt, $i);
 
251
                $this->_keys[] = $name;
 
252
            }
 
253
        }
 
254
 
 
255
        $this->_values = Array();
 
256
        if ($this->_keys) {
 
257
            $this->_values = array_fill(0, count($this->_keys), null);
 
258
        }
 
259
 
 
260
        return $retval;
 
261
    }
 
262
 
 
263
    /**
 
264
     * Fetches a row from the result set.
 
265
     *
 
266
     * @param int $style  OPTIONAL Fetch mode for this fetch operation.
 
267
     * @param int $cursor OPTIONAL Absolute, relative, or other.
 
268
     * @param int $offset OPTIONAL Number for absolute or relative cursors.
 
269
     * @return mixed Array, object, or scalar depending on fetch mode.
 
270
     * @throws Zend_Db_Statement_Exception
 
271
     */
 
272
    public function fetch($style = null, $cursor = null, $offset = null)
 
273
    {
 
274
        if (!$this->_stmt) {
 
275
            return false;
 
276
        }
 
277
 
 
278
        if ($style === null) {
 
279
            $style = $this->_fetchMode;
 
280
        }
 
281
 
 
282
        switch ($style) {
 
283
            case Zend_Db::FETCH_NUM:
 
284
                $row = oci_fetch_row($this->_stmt);
 
285
                break;
 
286
            case Zend_Db::FETCH_ASSOC:
 
287
                $row = oci_fetch_assoc($this->_stmt);
 
288
                break;
 
289
            case Zend_Db::FETCH_BOTH:
 
290
                $row = oci_fetch_array($this->_stmt, OCI_BOTH);
 
291
                break;
 
292
            case Zend_Db::FETCH_OBJ:
 
293
                $row = oci_fetch_object($this->_stmt);
 
294
                break;
 
295
            case Zend_Db::FETCH_BOUND:
 
296
                $row = oci_fetch_array($this->_stmt, OCI_BOTH);
 
297
                if ($row !== false) {
 
298
                    return $this->_fetchBound($row);
 
299
                }
 
300
                break;
 
301
            default:
 
302
                /**
 
303
                 * @see Zend_Db_Adapter_Oracle_Exception
 
304
                 */
 
305
                require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
306
                throw new Zend_Db_Statement_Oracle_Exception(
 
307
                    array(
 
308
                        'code'    => 'HYC00',
 
309
                        'message' => "Invalid fetch mode '$style' specified"
 
310
                    )
 
311
                );
 
312
                break;
 
313
        }
 
314
 
 
315
        if (! $row && $error = oci_error($this->_stmt)) {
 
316
            /**
 
317
             * @see Zend_Db_Adapter_Oracle_Exception
 
318
             */
 
319
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
320
            throw new Zend_Db_Statement_Oracle_Exception($error);
 
321
        }
 
322
 
 
323
        return $row;
 
324
    }
 
325
 
 
326
    /**
 
327
     * Returns an array containing all of the result set rows.
 
328
     *
 
329
     * @param int $style OPTIONAL Fetch mode.
 
330
     * @param int $col   OPTIONAL Column number, if fetch mode is by column.
 
331
     * @return array Collection of rows, each in a format by the fetch mode.
 
332
     * @throws Zend_Db_Statement_Exception
 
333
     */
 
334
    public function fetchAll($style = null, $col = 0)
 
335
    {
 
336
        if (!$this->_stmt) {
 
337
            return false;
 
338
        }
 
339
 
 
340
        // make sure we have a fetch mode
 
341
        if ($style === null) {
 
342
            $style = $this->_fetchMode;
 
343
        }
 
344
 
 
345
        $flags = OCI_FETCHSTATEMENT_BY_ROW;
 
346
 
 
347
        switch ($style) {
 
348
            case Zend_Db::FETCH_BOTH:
 
349
                /**
 
350
                 * @see Zend_Db_Adapter_Oracle_Exception
 
351
                 */
 
352
                require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
353
                throw new Zend_Db_Statement_Oracle_Exception(
 
354
                    array(
 
355
                        'code'    => 'HYC00',
 
356
                        'message' => "OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead"
 
357
                    )
 
358
                );
 
359
                // notreached
 
360
                $flags |= OCI_NUM;
 
361
                $flags |= OCI_ASSOC;
 
362
                break;
 
363
            case Zend_Db::FETCH_NUM:
 
364
                $flags |= OCI_NUM;
 
365
                break;
 
366
            case Zend_Db::FETCH_ASSOC:
 
367
                $flags |= OCI_ASSOC;
 
368
                break;
 
369
            case Zend_Db::FETCH_OBJ:
 
370
                break;
 
371
            case Zend_Db::FETCH_COLUMN:
 
372
                $flags = $flags &~ OCI_FETCHSTATEMENT_BY_ROW;
 
373
                $flags |= OCI_FETCHSTATEMENT_BY_COLUMN;
 
374
                $flags |= OCI_NUM;
 
375
                break;
 
376
            default:
 
377
                /**
 
378
                 * @see Zend_Db_Adapter_Oracle_Exception
 
379
                 */
 
380
                require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
381
                throw new Zend_Db_Statement_Oracle_Exception(
 
382
                    array(
 
383
                        'code'    => 'HYC00',
 
384
                        'message' => "Invalid fetch mode '$style' specified"
 
385
                    )
 
386
                );
 
387
                break;
 
388
        }
 
389
 
 
390
        $result = Array();
 
391
        if ($flags != OCI_FETCHSTATEMENT_BY_ROW) { /* not Zend_Db::FETCH_OBJ */
 
392
            if (! ($rows = oci_fetch_all($this->_stmt, $result, 0, -1, $flags) )) {
 
393
                if ($error = oci_error($this->_stmt)) {
 
394
                    /**
 
395
                     * @see Zend_Db_Adapter_Oracle_Exception
 
396
                     */
 
397
                    require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
398
                    throw new Zend_Db_Statement_Oracle_Exception($error);
 
399
                }
 
400
                if (!$rows) {
 
401
                    return array();
 
402
                }
 
403
            }
 
404
            if ($style == Zend_Db::FETCH_COLUMN) {
 
405
                $result = $result[$col];
 
406
            }
 
407
        } else {
 
408
            while (($row = oci_fetch_object($this->_stmt)) !== false) {
 
409
                $result [] = $row;
 
410
            }
 
411
            if ($error = oci_error($this->_stmt)) {
 
412
                /**
 
413
                 * @see Zend_Db_Adapter_Oracle_Exception
 
414
                 */
 
415
                require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
416
                throw new Zend_Db_Statement_Oracle_Exception($error);
 
417
            }
 
418
        }
 
419
 
 
420
        return $result;
 
421
    }
 
422
 
 
423
 
 
424
    /**
 
425
     * Returns a single column from the next row of a result set.
 
426
     *
 
427
     * @param int $col OPTIONAL Position of the column to fetch.
 
428
     * @return string
 
429
     * @throws Zend_Db_Statement_Exception
 
430
     */
 
431
    public function fetchColumn($col = 0)
 
432
    {
 
433
        if (!$this->_stmt) {
 
434
            return false;
 
435
        }
 
436
 
 
437
        if (!oci_fetch($this->_stmt)) {
 
438
            /* TODO ERROR */
 
439
        }
 
440
 
 
441
        $data = oci_result($this->_stmt, $col+1); //1-based
 
442
        if ($data === false) {
 
443
            /**
 
444
             * @see Zend_Db_Adapter_Oracle_Exception
 
445
             */
 
446
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
447
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
 
448
        }
 
449
        return $data;
 
450
    }
 
451
 
 
452
 
 
453
    /**
 
454
     * Fetches the next row and returns it as an object.
 
455
     *
 
456
     * @param string $class  OPTIONAL Name of the class to create.
 
457
     * @param array  $config OPTIONAL Constructor arguments for the class.
 
458
     * @return mixed One object instance of the specified class.
 
459
     * @throws Zend_Db_Statement_Exception
 
460
     */
 
461
    public function fetchObject($class = 'stdClass', array $config = array())
 
462
    {
 
463
        if (!$this->_stmt) {
 
464
            return false;
 
465
        }
 
466
 
 
467
        $obj = oci_fetch_object($this->_stmt);
 
468
 
 
469
        if ($obj === false) {
 
470
            /**
 
471
             * @see Zend_Db_Adapter_Oracle_Exception
 
472
             */
 
473
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
474
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
 
475
        }
 
476
 
 
477
        /* @todo XXX handle parameters */
 
478
 
 
479
        return $obj;
 
480
    }
 
481
 
 
482
    /**
 
483
     * Retrieves the next rowset (result set) for a SQL statement that has
 
484
     * multiple result sets.  An example is a stored procedure that returns
 
485
     * the results of multiple queries.
 
486
     *
 
487
     * @return bool
 
488
     * @throws Zend_Db_Statement_Exception
 
489
     */
 
490
    public function nextRowset()
 
491
    {
 
492
        /**
 
493
         * @see Zend_Db_Statement_Oracle_Exception
 
494
         */
 
495
        require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
496
        throw new Zend_Db_Statement_Oracle_Exception(
 
497
            array(
 
498
                'code'    => 'HYC00',
 
499
                'message' => 'Optional feature not implemented'
 
500
            )
 
501
        );
 
502
    }
 
503
 
 
504
    /**
 
505
     * Returns the number of rows affected by the execution of the
 
506
     * last INSERT, DELETE, or UPDATE statement executed by this
 
507
     * statement object.
 
508
     *
 
509
     * @return int     The number of rows affected.
 
510
     * @throws Zend_Db_Statement_Exception
 
511
     */
 
512
    public function rowCount()
 
513
    {
 
514
        if (!$this->_stmt) {
 
515
            return false;
 
516
        }
 
517
 
 
518
        $num_rows = oci_num_rows($this->_stmt);
 
519
 
 
520
        if ($num_rows === false) {
 
521
            /**
 
522
             * @see Zend_Db_Adapter_Oracle_Exception
 
523
             */
 
524
            require_once 'Zend/Db/Statement/Oracle/Exception.php';
 
525
            throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
 
526
        }
 
527
 
 
528
        return $num_rows;
 
529
    }
 
530
 
 
531
}