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.
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
23
* @see Zend_Db_Statement
25
require_once 'Zend/Db/Statement.php';
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
36
class Zend_Db_Statement_Oracle extends Zend_Db_Statement
40
* The connection_stmt object.
50
* Fetched result values.
55
* Prepares statement handle
59
* @throws Zend_Db_Statement_Oracle_Exception
61
protected function _prepare($sql)
63
$connection = $this->_adapter->getConnection();
64
$this->_stmt = oci_parse($connection, $sql);
67
* @see Zend_Db_Statement_Oracle_Exception
69
require_once 'Zend/Db/Statement/Oracle/Exception.php';
70
throw new Zend_Db_Statement_Oracle_Exception(oci_error($connection));
75
* Binds a parameter to the specified variable name.
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.
83
* @throws Zend_Db_Statement_Exception
85
protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
93
if ($length === NULL) {
97
$retval = @oci_bind_by_name($this->_stmt, $parameter, $variable, $length, $type);
98
if ($retval === false) {
100
* @see Zend_Db_Adapter_Oracle_Exception
102
require_once 'Zend/Db/Statement/Oracle/Exception.php';
103
throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
110
* Closes the cursor, allowing the statement to be executed again.
114
public function closeCursor()
120
oci_free_statement($this->_stmt);
121
$this->_stmt = false;
126
* Returns the number of columns in the result set.
127
* Returns null if the statement has no result set metadata.
129
* @return int The number of columns.
131
public function columnCount()
137
return oci_num_fields($this->_stmt);
142
* Retrieves the error code, if any, associated with the last operation on
143
* the statement handle.
145
* @return string error code.
147
public function errorCode()
153
$error = oci_error($this->_stmt);
159
return $error['code'];
164
* Retrieves an array of error information, if any, associated with the
165
* last operation on the statement handle.
169
public function errorInfo()
175
$error = oci_error($this->_stmt);
180
if (isset($error['sqltext'])) {
197
* Executes a prepared statement.
199
* @param array $params OPTIONAL Values to bind to parameter placeholders.
201
* @throws Zend_Db_Statement_Exception
203
public function _execute(array $params = null)
205
$connection = $this->_adapter->getConnection();
210
if (! $this->_stmt) {
212
* @see Zend_Db_Adapter_Oracle_Exception
214
require_once 'Zend/Db/Statement/Oracle/Exception.php';
215
throw new Zend_Db_Statement_Oracle_Exception(oci_error($connection));
218
if ($params !== null) {
219
if (!is_array($params)) {
220
$params = array($params);
223
foreach (array_keys($params) as $name) {
224
if (!@oci_bind_by_name($this->_stmt, $name, $params[$name], -1)) {
231
* @see Zend_Db_Adapter_Oracle_Exception
233
require_once 'Zend/Db/Statement/Oracle/Exception.php';
234
throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
238
$retval = @oci_execute($this->_stmt, $this->_adapter->_getExecuteMode());
239
if ($retval === false) {
241
* @see Zend_Db_Adapter_Oracle_Exception
243
require_once 'Zend/Db/Statement/Oracle/Exception.php';
244
throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
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;
255
$this->_values = Array();
257
$this->_values = array_fill(0, count($this->_keys), null);
264
* Fetches a row from the result set.
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
272
public function fetch($style = null, $cursor = null, $offset = null)
278
if ($style === null) {
279
$style = $this->_fetchMode;
283
case Zend_Db::FETCH_NUM:
284
$row = oci_fetch_row($this->_stmt);
286
case Zend_Db::FETCH_ASSOC:
287
$row = oci_fetch_assoc($this->_stmt);
289
case Zend_Db::FETCH_BOTH:
290
$row = oci_fetch_array($this->_stmt, OCI_BOTH);
292
case Zend_Db::FETCH_OBJ:
293
$row = oci_fetch_object($this->_stmt);
295
case Zend_Db::FETCH_BOUND:
296
$row = oci_fetch_array($this->_stmt, OCI_BOTH);
297
if ($row !== false) {
298
return $this->_fetchBound($row);
303
* @see Zend_Db_Adapter_Oracle_Exception
305
require_once 'Zend/Db/Statement/Oracle/Exception.php';
306
throw new Zend_Db_Statement_Oracle_Exception(
309
'message' => "Invalid fetch mode '$style' specified"
315
if (! $row && $error = oci_error($this->_stmt)) {
317
* @see Zend_Db_Adapter_Oracle_Exception
319
require_once 'Zend/Db/Statement/Oracle/Exception.php';
320
throw new Zend_Db_Statement_Oracle_Exception($error);
327
* Returns an array containing all of the result set rows.
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
334
public function fetchAll($style = null, $col = 0)
340
// make sure we have a fetch mode
341
if ($style === null) {
342
$style = $this->_fetchMode;
345
$flags = OCI_FETCHSTATEMENT_BY_ROW;
348
case Zend_Db::FETCH_BOTH:
350
* @see Zend_Db_Adapter_Oracle_Exception
352
require_once 'Zend/Db/Statement/Oracle/Exception.php';
353
throw new Zend_Db_Statement_Oracle_Exception(
356
'message' => "OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead"
363
case Zend_Db::FETCH_NUM:
366
case Zend_Db::FETCH_ASSOC:
369
case Zend_Db::FETCH_OBJ:
371
case Zend_Db::FETCH_COLUMN:
372
$flags = $flags &~ OCI_FETCHSTATEMENT_BY_ROW;
373
$flags |= OCI_FETCHSTATEMENT_BY_COLUMN;
378
* @see Zend_Db_Adapter_Oracle_Exception
380
require_once 'Zend/Db/Statement/Oracle/Exception.php';
381
throw new Zend_Db_Statement_Oracle_Exception(
384
'message' => "Invalid fetch mode '$style' specified"
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)) {
395
* @see Zend_Db_Adapter_Oracle_Exception
397
require_once 'Zend/Db/Statement/Oracle/Exception.php';
398
throw new Zend_Db_Statement_Oracle_Exception($error);
404
if ($style == Zend_Db::FETCH_COLUMN) {
405
$result = $result[$col];
408
while (($row = oci_fetch_object($this->_stmt)) !== false) {
411
if ($error = oci_error($this->_stmt)) {
413
* @see Zend_Db_Adapter_Oracle_Exception
415
require_once 'Zend/Db/Statement/Oracle/Exception.php';
416
throw new Zend_Db_Statement_Oracle_Exception($error);
425
* Returns a single column from the next row of a result set.
427
* @param int $col OPTIONAL Position of the column to fetch.
429
* @throws Zend_Db_Statement_Exception
431
public function fetchColumn($col = 0)
437
if (!oci_fetch($this->_stmt)) {
441
$data = oci_result($this->_stmt, $col+1); //1-based
442
if ($data === false) {
444
* @see Zend_Db_Adapter_Oracle_Exception
446
require_once 'Zend/Db/Statement/Oracle/Exception.php';
447
throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
454
* Fetches the next row and returns it as an object.
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
461
public function fetchObject($class = 'stdClass', array $config = array())
467
$obj = oci_fetch_object($this->_stmt);
469
if ($obj === false) {
471
* @see Zend_Db_Adapter_Oracle_Exception
473
require_once 'Zend/Db/Statement/Oracle/Exception.php';
474
throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
477
/* @todo XXX handle parameters */
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.
488
* @throws Zend_Db_Statement_Exception
490
public function nextRowset()
493
* @see Zend_Db_Statement_Oracle_Exception
495
require_once 'Zend/Db/Statement/Oracle/Exception.php';
496
throw new Zend_Db_Statement_Oracle_Exception(
499
'message' => 'Optional feature not implemented'
505
* Returns the number of rows affected by the execution of the
506
* last INSERT, DELETE, or UPDATE statement executed by this
509
* @return int The number of rows affected.
510
* @throws Zend_Db_Statement_Exception
512
public function rowCount()
518
$num_rows = oci_num_rows($this->_stmt);
520
if ($num_rows === false) {
522
* @see Zend_Db_Adapter_Oracle_Exception
524
require_once 'Zend/Db/Statement/Oracle/Exception.php';
525
throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));