ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
oci8.php
Go to the documentation of this file.
1 <?php
2 // vim: set et ts=4 sw=4 fdm=marker:
3 // +----------------------------------------------------------------------+
4 // | PHP versions 4 and 5 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
7 // | Stig. S. Bakken, Lukas Smith |
8 // | All rights reserved. |
9 // +----------------------------------------------------------------------+
10 // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
11 // | API as well as database abstraction for PHP applications. |
12 // | This LICENSE is in the BSD license style. |
13 // | |
14 // | Redistribution and use in source and binary forms, with or without |
15 // | modification, are permitted provided that the following conditions |
16 // | are met: |
17 // | |
18 // | Redistributions of source code must retain the above copyright |
19 // | notice, this list of conditions and the following disclaimer. |
20 // | |
21 // | Redistributions in binary form must reproduce the above copyright |
22 // | notice, this list of conditions and the following disclaimer in the |
23 // | documentation and/or other materials provided with the distribution. |
24 // | |
25 // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
26 // | Lukas Smith nor the names of his contributors may be used to endorse |
27 // | or promote products derived from this software without specific prior|
28 // | written permission. |
29 // | |
30 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
33 // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
34 // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
35 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
36 // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
37 // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
38 // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
39 // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
40 // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
41 // | POSSIBILITY OF SUCH DAMAGE. |
42 // +----------------------------------------------------------------------+
43 // | Author: Lukas Smith <smith@pooteeweet.org> |
44 // +----------------------------------------------------------------------+
45 
46 // $Id: oci8.php,v 1.192 2007/03/04 22:27:11 quipo Exp $
47 
56 {
57  // {{{ properties
58  var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => "'", 'escape_pattern' => '@');
59 
60  var $identifier_quoting = array('start' => '"', 'end' => '"', 'escape' => '"');
61 
63  // }}}
64  // {{{ constructor
65 
69  function __construct()
70  {
72 
73  $this->phptype = 'oci8';
74  $this->dbsyntax = 'oci8';
75 
76  $this->supported['sequences'] = true;
77  $this->supported['indexes'] = true;
78  $this->supported['summary_functions'] = true;
79  $this->supported['order_by_text'] = true;
80  $this->supported['current_id'] = true;
81  $this->supported['affected_rows'] = true;
82  $this->supported['transactions'] = true;
83  $this->supported['savepoints'] = true;
84  $this->supported['limit_queries'] = true;
85  $this->supported['LOBs'] = true;
86  $this->supported['replace'] = 'emulated';
87  $this->supported['sub_selects'] = true;
88  $this->supported['auto_increment'] = false; // implementation is broken
89  $this->supported['primary_key'] = true;
90  $this->supported['result_introspection'] = true;
91  $this->supported['prepared_statements'] = true;
92  $this->supported['identifier_quoting'] = true;
93  $this->supported['pattern_escaping'] = true;
94  $this->supported['new_link'] = true;
95 
96  $this->options['DBA_username'] = false;
97  $this->options['DBA_password'] = false;
98  $this->options['database_name_prefix'] = false;
99  $this->options['emulate_database'] = true;
100  $this->options['default_tablespace'] = false;
101  $this->options['default_text_field_length'] = 2000;
102  $this->options['result_prefetching'] = false;
103  }
104 
105  // }}}
106  // {{{ errorInfo()
107 
115  function errorInfo($error = null)
116  {
117  if (is_resource($error)) {
118  $error_data = @OCIError($error);
119  $error = null;
120  } elseif ($this->connection) {
121  $error_data = @OCIError($this->connection);
122  } else {
123  $error_data = @OCIError();
124  }
125  $native_code = $error_data['code'];
126  $native_msg = $error_data['message'];
127  if (is_null($error)) {
128  static $ecode_map;
129  if (empty($ecode_map)) {
130  $ecode_map = array(
132  900 => MDB2_ERROR_SYNTAX,
133  904 => MDB2_ERROR_NOSUCHFIELD,
134  911 => MDB2_ERROR_SYNTAX, //invalid character
136  921 => MDB2_ERROR_SYNTAX,
137  923 => MDB2_ERROR_SYNTAX,
138  942 => MDB2_ERROR_NOSUCHTABLE,
141  1401 => MDB2_ERROR_INVALID,
143  1418 => MDB2_ERROR_NOT_FOUND,
144  1476 => MDB2_ERROR_DIVZERO,
146  2289 => MDB2_ERROR_NOSUCHTABLE,
147  2291 => MDB2_ERROR_CONSTRAINT,
148  2292 => MDB2_ERROR_CONSTRAINT,
149  2449 => MDB2_ERROR_CONSTRAINT,
150  24344 => MDB2_ERROR_SYNTAX, //success with compilation error
151  );
152  }
153  if (isset($ecode_map[$native_code])) {
154  $error = $ecode_map[$native_code];
155  }
156  }
157  return array($error, $native_code, $native_msg);
158  }
159 
160  // }}}
161  // {{{ beginTransaction()
162 
171  function beginTransaction($savepoint = null)
172  {
173  $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
174  if (!is_null($savepoint)) {
175  if (!$this->in_transaction) {
176  return $this->raiseError(MDB2_ERROR_INVALID, null, null,
177  'savepoint cannot be released when changes are auto committed', __FUNCTION__);
178  }
179  $query = 'SAVEPOINT '.$savepoint;
180  return $this->_doQuery($query, true);
181  } elseif ($this->in_transaction) {
182  return MDB2_OK; //nothing to do
183  }
184  if (!$this->destructor_registered && $this->opened_persistent) {
185  $this->destructor_registered = true;
186  register_shutdown_function('MDB2_closeOpenTransactions');
187  }
188  $this->in_transaction = true;
190  return MDB2_OK;
191  }
192 
193  // }}}
194  // {{{ commit()
195 
207  function commit($savepoint = null)
208  {
209  $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
210  if (!$this->in_transaction) {
211  return $this->raiseError(MDB2_ERROR_INVALID, null, null,
212  'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
213  }
214  if (!is_null($savepoint)) {
215  return MDB2_OK;
216  }
217 
218  if ($this->uncommitedqueries) {
219  $connection = $this->getConnection();
220  if (PEAR::isError($connection)) {
221  return $connection;
222  }
223  if (!@OCICommit($connection)) {
224  return $this->raiseError(null, null, null,
225  'Unable to commit transaction', __FUNCTION__);
226  }
227  $this->uncommitedqueries = 0;
228  }
229  $this->in_transaction = false;
230  return MDB2_OK;
231  }
232 
233  // }}}
234  // {{{ rollback()
235 
247  function rollback($savepoint = null)
248  {
249  $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
250  if (!$this->in_transaction) {
251  return $this->raiseError(MDB2_ERROR_INVALID, null, null,
252  'rollback cannot be done changes are auto committed', __FUNCTION__);
253  }
254  if (!is_null($savepoint)) {
255  $query = 'ROLLBACK TO SAVEPOINT '.$savepoint;
256  return $this->_doQuery($query, true);
257  }
258 
259  if ($this->uncommitedqueries) {
260  $connection = $this->getConnection();
261  if (PEAR::isError($connection)) {
262  return $connection;
263  }
264  if (!@OCIRollback($connection)) {
265  return $this->raiseError(null, null, null,
266  'Unable to rollback transaction', __FUNCTION__);
267  }
268  $this->uncommitedqueries = 0;
269  }
270  $this->in_transaction = false;
271  return MDB2_OK;
272  }
273 
274  // }}}
275  // {{{ function setTransactionIsolation()
276 
290  function setTransactionIsolation($isolation)
291  {
292  $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
293  switch ($isolation) {
294  case 'READ UNCOMMITTED':
295  $isolation = 'READ COMMITTED';
296  case 'READ COMMITTED':
297  case 'REPEATABLE READ':
298  $isolation = 'SERIALIZABLE';
299  case 'SERIALIZABLE':
300  break;
301  default:
302  return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
303  'isolation level is not supported: '.$isolation, __FUNCTION__);
304  }
305 
306  $query = "ALTER SESSION ISOLATION LEVEL $isolation";
307  return $this->_doQuery($query, true);
308  }
309 
310  // }}}
311  // {{{ _doConnect()
312 
319  function _doConnect($username, $password, $persistent = false)
320  {
321  if (!PEAR::loadExtension($this->phptype)) {
322  return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
323  'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
324  }
325 
326  $sid = '';
327 
328  if (!empty($this->dsn['service']) && $this->dsn['hostspec']) {
329  //oci8://username:password@foo.example.com[:port]/?service=service
330  // service name is given, it is assumed that hostspec is really a
331  // hostname, we try to construct an oracle connection string from this
332  $port = $this->dsn['port'] ? $this->dsn['port'] : 1521;
333  $sid = sprintf("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
334  (HOST=%s) (PORT=%s)))
335  (CONNECT_DATA=(SERVICE_NAME=%s)))",
336  $this->dsn['hostspec'],
337  $port,
338  $this->dsn['service']
339  );
340  } elseif ($this->dsn['hostspec']) {
341  // we are given something like 'oci8://username:password@foo/'
342  // we have hostspec but not a service name, now we assume that
343  // hostspec is a tnsname defined in tnsnames.ora
344  $sid = $this->dsn['hostspec'];
345  } else {
346  // oci://username:password@
347  // if everything fails, we have to rely on environment variables
348  // not before a check to 'emulate_database'
349  if (!$this->options['emulate_database'] && $this->database_name) {
350  $sid = $this->database_name;
351  } elseif (getenv('ORACLE_SID')) {
352  $sid = getenv('ORACLE_SID');
353  } elseif ($sid = getenv('TWO_TASK')) {
354  $sid = getenv('TWO_TASK');
355  } else {
356  return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
357  'not a valid connection string or environment variable [ORACLE_SID|TWO_TASK] not set',
358  __FUNCTION__);
359  }
360  }
361 
362  if (function_exists('oci_connect')) {
363  if (isset($this->dsn['new_link'])
364  && ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true)
365  ) {
366  $connect_function = 'oci_new_connect';
367  } else {
368  $connect_function = $persistent ? 'oci_pconnect' : 'oci_connect';
369  }
370 
371  $charset = empty($this->dsn['charset']) ? null : $this->dsn['charset'];
372  $connection = @$connect_function($username, $password, $sid, $charset);
373  $error = @OCIError();
374  if (isset($error['code']) && $error['code'] == 12541) {
375  // Couldn't find TNS listener. Try direct connection.
376  $connection = @$connect_function($username, $password, null, $charset);
377  }
378  } else {
379  $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
380  $connection = @$connect_function($username, $password, $sid);
381 
382  if (!empty($this->dsn['charset'])) {
383  $result = $this->setCharset($this->dsn['charset'], $connection);
384  if (PEAR::isError($result)) {
385  return $result;
386  }
387  }
388  }
389 
390  if (!$connection) {
391  return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
392  'unable to establish a connection', __FUNCTION__);
393  }
394 
395  if (empty($this->dsn['disable_iso_date'])) {
396  $query = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
397  $err =& $this->_doQuery($query, true, $connection);
398  if (PEAR::isError($err)) {
399  $this->disconnect(false);
400  return $err;
401  }
402  }
403 
404  $query = "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
405  $err =& $this->_doQuery($query, true, $connection);
406  if (PEAR::isError($err)) {
407  $this->disconnect(false);
408  return $err;
409  }
410 
411  return $connection;
412  }
413 
414  // }}}
415  // {{{ connect()
416 
423  function connect()
424  {
425  if ($this->database_name && $this->options['emulate_database']) {
426  $this->dsn['username'] = $this->options['database_name_prefix'].$this->database_name;
427  }
428  if (is_resource($this->connection)) {
429  if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
430  && $this->connected_database_name == $this->database_name
431  && $this->opened_persistent == $this->options['persistent']
432  ) {
433  return MDB2_OK;
434  }
435  $this->disconnect(false);
436  }
437 
438  $connection = $this->_doConnect(
439  $this->dsn['username'],
440  $this->dsn['password'],
441  $this->options['persistent']
442  );
443  if (PEAR::isError($connection)) {
444  return $connection;
445  }
446  $this->connection = $connection;
447  $this->connected_dsn = $this->dsn;
448  $this->connected_database_name = $this->database_name;
449  $this->opened_persistent = $this->options['persistent'];
450  $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
451 
452  $this->as_keyword = ' ';
453  $server_info = $this->getServerVersion();
454  if (is_array($server_info)) {
455  if ($server_info['major'] >= '10') {
456  $this->as_keyword = ' AS ';
457  }
458  }
459  return MDB2_OK;
460  }
461 
462  // }}}
463  // {{{ disconnect()
464 
474  function disconnect($force = true)
475  {
476  if (is_resource($this->connection)) {
477  if ($this->in_transaction) {
478  $dsn = $this->dsn;
480  $persistent = $this->options['persistent'];
481  $this->dsn = $this->connected_dsn;
482  $this->database_name = $this->connected_database_name;
483  $this->options['persistent'] = $this->opened_persistent;
484  $this->rollback();
485  $this->dsn = $dsn;
486  $this->database_name = $database_name;
487  $this->options['persistent'] = $persistent;
488  }
489 
490  if (!$this->opened_persistent || $force) {
491  if (function_exists('oci_close')) {
492  @oci_close($this->connection);
493  } else {
494  @OCILogOff($this->connection);
495  }
496  }
497  $this->uncommitedqueries = 0;
498  }
499  return parent::disconnect($force);
500  }
501 
502  // }}}
503  // {{{ standaloneExec()
504 
513  {
514  $connection = $this->_doConnect(
515  $this->options['DBA_username'],
516  $this->options['DBA_password'],
517  $this->options['persistent']
518  );
519  if (PEAR::isError($connection)) {
520  return $connection;
521  }
522 
525  $this->offset = $this->limit = 0;
526  $query = $this->_modifyQuery($query, false, $limit, $offset);
527 
528  $result =& $this->_doQuery($query, false, $connection, false);
529  if (PEAR::isError($result)) {
530  @OCILogOff($connection);
531  return $result;
532  }
533 
535  @OCILogOff($connection);
536  return $ret;
537  }
538 
539  // }}}
540  // {{{ standaloneQuery()
541 
552  function &standaloneQuery($query, $types = null, $is_manip = false)
553  {
554  $connection = $this->_doConnect(
555  $this->options['DBA_username'],
556  $this->options['DBA_password'],
557  $this->options['persistent']
558  );
559  if (PEAR::isError($connection)) {
560  return $connection;
561  }
562 
565  $this->offset = $this->limit = 0;
566  $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
567 
568  $result =& $this->_doQuery($query, $is_manip, $connection, false);
569  @OCILogOff($connection);
570  if (PEAR::isError($result)) {
571  return $result;
572  }
573 
574  if ($is_manip) {
575  $affected_rows = $this->_affectedRows($connection, $result);
576  return $affected_rows;
577  }
578  $return =& $this->_wrapResult($result, $types, true, false, $limit, $offset);
579  return $return;
580  }
581 
582  // }}}
583  // {{{ _modifyQuery()
584 
595  function _modifyQuery($query, $is_manip, $limit, $offset)
596  {
597  if (preg_match('/^\s*SELECT/i', $query)) {
598  if (!preg_match('/\sFROM\s/i', $query)) {
599  $query.= " FROM dual";
600  }
601  if ($limit > 0) {
602  // taken from http://svn.ez.no/svn/ezcomponents/packages/Database
603  $max = $offset + $limit;
604  if ($offset > 0) {
605  $min = $offset + 1;
606  $query = "SELECT * FROM (SELECT a.*, ROWNUM mdb2rn FROM ($query) a WHERE ROWNUM <= $max) WHERE mdb2rn >= $min";
607  } else {
608  $query = "SELECT a.* FROM ($query) a WHERE ROWNUM <= $max";
609  }
610  }
611  }
612  return $query;
613  }
614 
615  // }}}
616  // {{{ _doQuery()
617 
627  function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
628  {
629  $this->last_query = $query;
630  $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
631  if ($result) {
632  if (PEAR::isError($result)) {
633  return $result;
634  }
635  $query = $result;
636  }
637  if ($this->getOption('disable_query')) {
638  if ($is_manip) {
639  return 0;
640  }
641  return null;
642  }
643 
644  if (is_null($connection)) {
645  $connection = $this->getConnection();
646  if (PEAR::isError($connection)) {
647  return $connection;
648  }
649  }
650 
651  $result = @OCIParse($connection, $query);
652  if (!$result) {
653  $err = $this->raiseError(null, null, null,
654  'Could not create statement', __FUNCTION__);
655  return $err;
656  }
657 
658  $mode = $this->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
659  if (!@OCIExecute($result, $mode)) {
660  $err =& $this->raiseError($result, null, null,
661  'Could not execute statement', __FUNCTION__);
662  return $err;
663  }
664 
665  if (is_numeric($this->options['result_prefetching'])) {
666  @ocisetprefetch($result, $this->options['result_prefetching']);
667  }
668 
669  $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
670  return $result;
671  }
672 
673  // }}}
674  // {{{ _affectedRows()
675 
685  {
686  if (is_null($connection)) {
687  $connection = $this->getConnection();
688  if (PEAR::isError($connection)) {
689  return $connection;
690  }
691  }
692  return @OCIRowCount($result);
693  }
694 
695  // }}}
696  // {{{ getServerVersion()
697 
705  function getServerVersion($native = false)
706  {
707  $connection = $this->getConnection();
708  if (PEAR::isError($connection)) {
709  return $connection;
710  }
711  if ($this->connected_server_info) {
712  $server_info = $this->connected_server_info;
713  } else {
714  $server_info = @ociserverversion($connection);
715  }
716  if (!$server_info) {
717  return $this->raiseError(null, null, null,
718  'Could not get server information', __FUNCTION__);
719  }
720  // cache server_info
721  $this->connected_server_info = $server_info;
722  if (!$native) {
723  if (!preg_match('/ (\d+)\.(\d+)\.(\d+)\.([\d\.]+) /', $server_info, $tmp)) {
724  return $this->raiseError(MDB2_ERROR_INVALID, null, null,
725  'Could not parse version information:'.$server_info, __FUNCTION__);
726  }
727  $server_info = array(
728  'major' => $tmp[1],
729  'minor' => $tmp[2],
730  'patch' => $tmp[3],
731  'extra' => $tmp[4],
732  'native' => $server_info,
733  );
734  }
735  return $server_info;
736  }
737 
738  // }}}
739  // {{{ prepare()
740 
761  function &prepare($query, $types = null, $result_types = null, $lobs = array())
762  {
763  if ($this->options['emulate_prepared']) {
764  $obj =& parent::prepare($query, $types, $result_types, $lobs);
765  return $obj;
766  }
767  $is_manip = ($result_types === MDB2_PREPARE_MANIP);
770  $this->offset = $this->limit = 0;
771  $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
772  if ($result) {
773  if (PEAR::isError($result)) {
774  return $result;
775  }
776  $query = $result;
777  }
778  $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
779  $placeholder_type_guess = $placeholder_type = null;
780  $question = '?';
781  $colon = ':';
782  $positions = array();
783  $position = 0;
784  $parameter = -1;
785  while ($position < strlen($query)) {
786  $q_position = strpos($query, $question, $position);
787  $c_position = strpos($query, $colon, $position);
788  if ($q_position && $c_position) {
789  $p_position = min($q_position, $c_position);
790  } elseif ($q_position) {
791  $p_position = $q_position;
792  } elseif ($c_position) {
793  $p_position = $c_position;
794  } else {
795  break;
796  }
797  if (is_null($placeholder_type)) {
798  $placeholder_type_guess = $query[$p_position];
799  }
800 
801  $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
802  if (PEAR::isError($new_pos)) {
803  return $new_pos;
804  }
805  if ($new_pos != $position) {
806  $position = $new_pos;
807  continue; //evaluate again starting from the new position
808  }
809 
810  if ($query[$position] == $placeholder_type_guess) {
811  if (is_null($placeholder_type)) {
812  $placeholder_type = $query[$p_position];
813  $question = $colon = $placeholder_type;
814  if (!empty($types) && is_array($types)) {
815  if ($placeholder_type == ':') {
816  if (is_int(key($types))) {
817  $types_tmp = $types;
818  $types = array();
819  $count = -1;
820  }
821  } else {
822  $types = array_values($types);
823  }
824  }
825  }
826  if ($placeholder_type == ':') {
827  $parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query);
828  if ($parameter === '') {
829  $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
830  'named parameter with an empty name', __FUNCTION__);
831  return $err;
832  }
833  // use parameter name in type array
834  if (isset($count) && isset($types_tmp[++$count])) {
835  $types[$parameter] = $types_tmp[$count];
836  }
837  $length = strlen($parameter) + 1;
838  } else {
839  ++$parameter;
840  $length = strlen($parameter);
841  }
842  if (!in_array($parameter, $positions)) {
843  $positions[] = $parameter;
844  }
845  if (isset($types[$parameter])
846  && ($types[$parameter] == 'clob' || $types[$parameter] == 'blob')
847  ) {
848  if (!isset($lobs[$parameter])) {
849  $lobs[$parameter] = $parameter;
850  }
851  $value = $this->quote(true, $types[$parameter]);
852  $query = substr_replace($query, $value, $p_position, $length);
853  $position = $p_position + strlen($value) - 1;
854  } elseif ($placeholder_type == '?') {
855  $query = substr_replace($query, ':'.$parameter, $p_position, 1);
856  $position = $p_position + $length;
857  } else {
858  $position = $p_position + 1;
859  }
860  } else {
861  $position = $p_position;
862  }
863  }
864  if (is_array($lobs)) {
865  $columns = $variables = '';
866  foreach ($lobs as $parameter => $field) {
867  $columns.= ($columns ? ', ' : ' RETURNING ').$field;
868  $variables.= ($variables ? ', ' : ' INTO ').':'.$parameter;
869  }
870  $query.= $columns.$variables;
871  }
872  $connection = $this->getConnection();
873  if (PEAR::isError($connection)) {
874  return $connection;
875  }
876  $statement = @OCIParse($connection, $query);
877  if (!$statement) {
878  $err =& $this->raiseError(null, null, null,
879  'Could not create statement', __FUNCTION__);
880  return $err;
881  }
882 
883  $class_name = 'MDB2_Statement_'.$this->phptype;
884  $obj =& new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
885  $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
886  return $obj;
887  }
888 
889  // }}}
890  // {{{ nextID()
891 
902  function nextID($seq_name, $ondemand = true)
903  {
904  $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
905  $query = "SELECT $sequence_name.nextval FROM DUAL";
907  $result = $this->queryOne($query, 'integer');
908  $this->popExpect();
909  if (PEAR::isError($result)) {
910  if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
911  $this->loadModule('Manager', null, true);
912  $result = $this->manager->createSequence($seq_name);
913  if (PEAR::isError($result)) {
914  return $result;
915  }
916  return $this->nextId($seq_name, false);
917  }
918  }
919  return $result;
920  }
921 
922  // }}}
923  // {{{ lastInsertID()
924 
934  function lastInsertID($table = null, $field = null)
935  {
936  $seq = $table.(empty($field) ? '' : '_'.$field);
937  $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq), true);
938  return $this->queryOne("SELECT $sequence_name.currval", 'integer');
939  }
940 
941  // }}}
942  // {{{ currId()
943 
951  function currId($seq_name)
952  {
953  $sequence_name = $this->getSequenceName($seq_name);
954  $query = 'SELECT (last_number-1) FROM user_sequences';
955  $query.= ' WHERE sequence_name='.$this->quote($sequence_name, 'text');
956  $query.= ' OR sequence_name='.$this->quote(strtoupper($sequence_name), 'text');
957  return $this->queryOne($query, 'integer');
958  }
959 }
960 
969 {
970  // }}}
971  // {{{ fetchRow()
972 
981  function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
982  {
983  if (!is_null($rownum)) {
984  $seek = $this->seek($rownum);
985  if (PEAR::isError($seek)) {
986  return $seek;
987  }
988  }
989  if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
990  $fetchmode = $this->db->fetchmode;
991  }
992  if ($fetchmode & MDB2_FETCHMODE_ASSOC) {
993  @OCIFetchInto($this->result, $row, OCI_ASSOC+OCI_RETURN_NULLS);
994  if (is_array($row)
995  && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE
996  ) {
997  $row = array_change_key_case($row, $this->db->options['field_case']);
998  }
999  } else {
1000  @OCIFetchInto($this->result, $row, OCI_RETURN_NULLS);
1001  }
1002  if (!$row) {
1003  if ($this->result === false) {
1004  $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
1005  'resultset has already been freed', __FUNCTION__);
1006  return $err;
1007  }
1008  $null = null;
1009  return $null;
1010  }
1011  // remove additional column at the end
1012  if ($this->offset > 0) {
1013  array_pop($row);
1014  }
1015  $mode = 0;
1016  $rtrim = false;
1017  if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
1018  if (empty($this->types)) {
1019  $mode += MDB2_PORTABILITY_RTRIM;
1020  } else {
1021  $rtrim = true;
1022  }
1023  }
1024  if ($mode) {
1025  $this->db->_fixResultArrayValues($row, $mode);
1026  }
1027  if (!empty($this->types)) {
1028  $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
1029  }
1030  if (!empty($this->values)) {
1031  $this->_assignBindColumns($row);
1032  }
1033  if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
1034  $object_class = $this->db->options['fetch_class'];
1035  if ($object_class == 'stdClass') {
1036  $row = (object) $row;
1037  } else {
1038  $row = &new $object_class($row);
1039  }
1040  }
1041  ++$this->rownum;
1042  return $row;
1043  }
1044 
1045  // }}}
1046  // {{{ _getColumnNames()
1047 
1057  function _getColumnNames()
1058  {
1059  $columns = array();
1060  $numcols = $this->numCols();
1061  if (PEAR::isError($numcols)) {
1062  return $numcols;
1063  }
1064  for ($column = 0; $column < $numcols; $column++) {
1065  $column_name = @OCIColumnName($this->result, $column + 1);
1066  $columns[$column_name] = $column;
1067  }
1068  if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
1069  $columns = array_change_key_case($columns, $this->db->options['field_case']);
1070  }
1071  return $columns;
1072  }
1073 
1074  // }}}
1075  // {{{ numCols()
1076 
1084  function numCols()
1085  {
1086  $cols = @OCINumCols($this->result);
1087  if (is_null($cols)) {
1088  if ($this->result === false) {
1089  return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
1090  'resultset has already been freed', __FUNCTION__);
1091  } elseif (is_null($this->result)) {
1092  return count($this->types);
1093  }
1094  return $this->db->raiseError(null, null, null,
1095  'Could not get column count', __FUNCTION__);
1096  }
1097  if ($this->offset > 0) {
1098  --$cols;
1099  }
1100  return $cols;
1101  }
1102 
1103  // }}}
1104  // {{{ free()
1105 
1112  function free()
1113  {
1114  if (is_resource($this->result) && $this->db->connection) {
1115  $free = @OCIFreeCursor($this->result);
1116  if ($free === false) {
1117  return $this->db->raiseError(null, null, null,
1118  'Could not free result', __FUNCTION__);
1119  }
1120  }
1121  $this->result = false;
1122  return MDB2_OK;
1123 
1124  }
1125 }
1126 
1135 {
1136  var $buffer;
1137  var $buffer_rownum = - 1;
1138 
1139  // {{{ _fillBuffer()
1140 
1149  function _fillBuffer($rownum = null)
1150  {
1151  if (isset($this->buffer) && is_array($this->buffer)) {
1152  if (is_null($rownum)) {
1153  if (!end($this->buffer)) {
1154  return false;
1155  }
1156  } elseif (isset($this->buffer[$rownum])) {
1157  return (bool)$this->buffer[$rownum];
1158  }
1159  }
1160 
1161  $row = true;
1162  while ((is_null($rownum) || $this->buffer_rownum < $rownum)
1163  && ($row = @OCIFetchInto($this->result, $buffer, OCI_RETURN_NULLS))
1164  ) {
1166  // remove additional column at the end
1167  if ($this->offset > 0) {
1168  array_pop($buffer);
1169  }
1170  if (empty($this->types)) {
1171  foreach (array_keys($buffer) as $key) {
1172  if (is_a($buffer[$key], 'oci-lob')) {
1173  $buffer[$key] = $buffer[$key]->load();
1174  }
1175  }
1176  }
1177  $this->buffer[$this->buffer_rownum] = $buffer;
1178  }
1179 
1180  if (!$row) {
1182  $this->buffer[$this->buffer_rownum] = false;
1183  return false;
1184  }
1185  return true;
1186  }
1187 
1188  // }}}
1189  // {{{ fetchRow()
1190 
1199  function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
1200  {
1201  if ($this->result === false) {
1202  $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
1203  'resultset has already been freed', __FUNCTION__);
1204  return $err;
1205  } elseif (is_null($this->result)) {
1206  return null;
1207  }
1208  if (!is_null($rownum)) {
1209  $seek = $this->seek($rownum);
1210  if (PEAR::isError($seek)) {
1211  return $seek;
1212  }
1213  }
1214  $target_rownum = $this->rownum + 1;
1215  if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
1216  $fetchmode = $this->db->fetchmode;
1217  }
1218  if (!$this->_fillBuffer($target_rownum)) {
1219  $null = null;
1220  return $null;
1221  }
1222  $row = $this->buffer[$target_rownum];
1223  if ($fetchmode & MDB2_FETCHMODE_ASSOC) {
1224  $column_names = $this->getColumnNames();
1225  foreach ($column_names as $name => $i) {
1226  $column_names[$name] = $row[$i];
1227  }
1228  $row = $column_names;
1229  }
1230  $mode = 0;
1231  $rtrim = false;
1232  if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
1233  if (empty($this->types)) {
1234  $mode += MDB2_PORTABILITY_RTRIM;
1235  } else {
1236  $rtrim = true;
1237  }
1238  }
1239  if ($mode) {
1240  $this->db->_fixResultArrayValues($row, $mode);
1241  }
1242  if (!empty($this->types)) {
1243  $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
1244  }
1245  if (!empty($this->values)) {
1246  $this->_assignBindColumns($row);
1247  }
1248  if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
1249  $object_class = $this->db->options['fetch_class'];
1250  if ($object_class == 'stdClass') {
1251  $row = (object) $row;
1252  } else {
1253  $row = &new $object_class($row);
1254  }
1255  }
1256  ++$this->rownum;
1257  return $row;
1258  }
1259 
1260  // }}}
1261  // {{{ seek()
1262 
1270  function seek($rownum = 0)
1271  {
1272  if ($this->result === false) {
1273  return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
1274  'resultset has already been freed', __FUNCTION__);
1275  }
1276  $this->rownum = $rownum - 1;
1277  return MDB2_OK;
1278  }
1279 
1280  // }}}
1281  // {{{ valid()
1282 
1289  function valid()
1290  {
1291  if ($this->result === false) {
1292  return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
1293  'resultset has already been freed', __FUNCTION__);
1294  } elseif (is_null($this->result)) {
1295  return true;
1296  }
1297  if ($this->_fillBuffer($this->rownum + 1)) {
1298  return true;
1299  }
1300  return false;
1301  }
1302 
1303  // }}}
1304  // {{{ numRows()
1305 
1312  function numRows()
1313  {
1314  if ($this->result === false) {
1315  return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
1316  'resultset has already been freed', __FUNCTION__);
1317  } elseif (is_null($this->result)) {
1318  return 0;
1319  }
1320  $this->_fillBuffer();
1321  return $this->buffer_rownum;
1322  }
1323 
1324  // }}}
1325  // {{{ free()
1326 
1333  function free()
1334  {
1335  $this->buffer = null;
1336  $this->buffer_rownum = null;
1337  return parent::free();
1338  }
1339 }
1340 
1349 {
1350  // }}}
1351  // {{{ _execute()
1352 
1361  function &_execute($result_class = true, $result_wrap_class = false)
1362  {
1363  if (is_null($this->statement)) {
1364  $result =& parent::_execute($result_class, $result_wrap_class);
1365  return $result;
1366  }
1367  $this->db->last_query = $this->query;
1368  $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values));
1369  if ($this->db->getOption('disable_query')) {
1370  $result = $this->is_manip ? 0 : null;
1371  return $result;
1372  }
1373 
1374  $connection = $this->db->getConnection();
1375  if (PEAR::isError($connection)) {
1376  return $connection;
1377  }
1378 
1379  $result = MDB2_OK;
1380  $lobs = $quoted_values = array();
1381  $i = 0;
1382  foreach ($this->positions as $parameter) {
1383  if (!array_key_exists($parameter, $this->values)) {
1384  return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
1385  'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
1386  }
1387  $value = $this->values[$parameter];
1388  $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;
1389  if ($type == 'clob' || $type == 'blob') {
1390  $lobs[$i]['file'] = false;
1391  if (is_resource($value)) {
1392  $fp = $value;
1393  $value = '';
1394  while (!feof($fp)) {
1395  $value.= fread($fp, 8192);
1396  }
1397  } elseif (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
1398 // ILIAS patch. We need interpretation as string not as file handle. alex 13.10.2009
1399 // See ILIAS Mantis Bug #4636
1400 // $lobs[$i]['file'] = true;
1401 // if ($match[1] == 'file://') {
1402 // $value = $match[2];
1403 // }
1404  }
1405  $lobs[$i]['value'] = $value;
1406  $lobs[$i]['descriptor'] = @OCINewDescriptor($connection, OCI_D_LOB);
1407  if (!is_object($lobs[$i]['descriptor'])) {
1408  $result = $this->db->raiseError(null, null, null,
1409  'Unable to create descriptor for LOB in parameter: '.$parameter, __FUNCTION__);
1410  break;
1411  }
1412  $lob_type = ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
1413  if (!@OCIBindByName($this->statement, ':'.$parameter, $lobs[$i]['descriptor'], -1, $lob_type)) {
1414  $result = $this->db->raiseError($this->statement, null, null,
1415  'could not bind LOB parameter', __FUNCTION__);
1416  break;
1417  }
1418  } else {
1419  $quoted_values[$i] = $this->db->quote($value, $type, false);
1420  if (PEAR::isError($quoted_values[$i])) {
1421  return $quoted_values[$i];
1422  }
1423  if (!@OCIBindByName($this->statement, ':'.$parameter, $quoted_values[$i])) {
1424  $result = $this->db->raiseError($this->statement, null, null,
1425  'could not bind non LOB parameter', __FUNCTION__);
1426  break;
1427  }
1428  }
1429  ++$i;
1430  }
1431 
1432  $lob_keys = array_keys($lobs);
1433  if (!PEAR::isError($result)) {
1434  $mode = (!empty($lobs) || $this->db->in_transaction) ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
1435  if (!@OCIExecute($this->statement, $mode)) {
1436  $err =& $this->db->raiseError($this->statement, null, null,
1437  'could not execute statement', __FUNCTION__);
1438  return $err;
1439  }
1440 
1441  if (!empty($lobs)) {
1442  foreach ($lob_keys as $i) {
1443  if (!is_null($lobs[$i]['value']) && $lobs[$i]['value'] !== '') {
1444  if ($lobs[$i]['file']) {
1445  $result = $lobs[$i]['descriptor']->savefile($lobs[$i]['value']);
1446  } else {
1447  $result = $lobs[$i]['descriptor']->save($lobs[$i]['value']);
1448  }
1449  if (!$result) {
1450  $result = $this->db->raiseError(null, null, null,
1451  'Unable to save descriptor contents', __FUNCTION__);
1452  break;
1453  }
1454  }
1455  }
1456 
1457  if (!PEAR::isError($result)) {
1458  if (!$this->db->in_transaction) {
1459  if (!@OCICommit($connection)) {
1460  $result = $this->db->raiseError(null, null, null,
1461  'Unable to commit transaction', __FUNCTION__);
1462  }
1463  } else {
1464  ++$this->db->uncommitedqueries;
1465  }
1466  }
1467  }
1468  }
1469 
1470  $lob_keys = array_keys($lobs);
1471  foreach ($lob_keys as $i) {
1472  $lobs[$i]['descriptor']->free();
1473  }
1474 
1475  if (PEAR::isError($result)) {
1476  return $result;
1477  }
1478 
1479  if ($this->is_manip) {
1480  $affected_rows = $this->db->_affectedRows($connection, $this->statement);
1481  return $affected_rows;
1482  }
1483 
1484  $result =& $this->db->_wrapResult($this->statement, $this->result_types,
1485  $result_class, $result_wrap_class, $this->limit, $this->offset);
1486  $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result));
1487  return $result;
1488  }
1489 
1490  // }}}
1491  // {{{ free()
1492 
1499  function free()
1500  {
1501  if (is_null($this->positions)) {
1502  return $this->db->raiseError(MDB2_ERROR, null, null,
1503  'Prepared statement has already been freed', __FUNCTION__);
1504  }
1505  $result = MDB2_OK;
1506 
1507  if (!is_null($this->statement) && !@OCIFreeStatement($this->statement)) {
1508  $result = $this->db->raiseError(null, null, null,
1509  'Could not free statement', __FUNCTION__);
1510  }
1511 
1512  parent::free();
1513  return $result;
1514  }
1515 }
1516 ?>