Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  /**
   4    @version   v5.21.0  2021-02-27
   5    @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   6    @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   7    Released under both BSD license and Lesser GPL library license.
   8    Whenever there is any discrepancy between the two licenses,
   9    the BSD license will take precedence.
  10  
  11    Set tabs to 4 for best viewing.
  12  
  13  */
  14  
  15  // security - hide paths
  16  if (!defined('ADODB_DIR')) die();
  17  
  18  class ADODB2_oci8 extends ADODB_DataDict {
  19  
  20  	 var $databaseType = 'oci8';
  21  	 var $seqField = false;
  22  	 var $seqPrefix = 'SEQ_';
  23  	 var $dropTable = "DROP TABLE %s CASCADE CONSTRAINTS";
  24  	 var $trigPrefix = 'TRIG_';
  25  	 var $alterCol = ' MODIFY ';
  26  	 var $typeX = 'VARCHAR(4000)';
  27  	 var $typeXL = 'CLOB';
  28  	 
  29  	 /**
  30  	  * Legacy compatibility for sequence names for emulated auto-increments.
  31  	  *
  32  	  * If set to true, creates sequences and triggers as TRIG_394545594
  33  	  * instead of TRIG_possibly_too_long_tablename
  34  	  *
  35  	  * @var bool $useCompactAutoIncrements
  36  	  */
  37  	 public $useCompactAutoIncrements = false;
  38  
  39  	function metaType($t, $len=-1, $fieldobj=false)
  40  	 {
  41  	 	 if (is_object($t)) {
  42  	 	 	 $fieldobj = $t;
  43  	 	 	 $t = $fieldobj->type;
  44  	 	 	 $len = $fieldobj->max_length;
  45  	 	 }
  46  	 	 switch (strtoupper($t)) {
  47  	  	 case 'VARCHAR':
  48  	  	 case 'VARCHAR2':
  49  	 	 case 'CHAR':
  50  	 	 case 'VARBINARY':
  51  	 	 case 'BINARY':
  52  	 	 	 if (isset($this) && $len <= $this->blobSize) return 'C';
  53  	 	 	 return 'X';
  54  
  55  	 	 case 'NCHAR':
  56  	 	 case 'NVARCHAR2':
  57  	 	 case 'NVARCHAR':
  58  	 	 	 if (isset($this) && $len <= $this->blobSize) return 'C2';
  59  	 	 	 return 'X2';
  60  
  61  	 	 case 'NCLOB':
  62  	 	 case 'CLOB':
  63  	 	 	 return 'XL';
  64  
  65  	 	 case 'LONG RAW':
  66  	 	 case 'LONG VARBINARY':
  67  	 	 case 'BLOB':
  68  	 	 	 return 'B';
  69  
  70  	 	 case 'TIMESTAMP':
  71  	 	 	 return 'TS';
  72  
  73  	 	 case 'DATE':
  74  	 	 	 return 'T';
  75  
  76  	 	 case 'INT':
  77  	 	 case 'SMALLINT':
  78  	 	 case 'INTEGER':
  79  	 	 	 return 'I';
  80  
  81  	 	 default:
  82  	 	 	 return ADODB_DEFAULT_METATYPE;
  83  	 	 }
  84  	 }
  85  
  86   	function ActualType($meta)
  87  	 {
  88  	 	 switch($meta) {
  89  	 	 case 'C': return 'VARCHAR';
  90  	 	 case 'X': return $this->typeX;
  91  	 	 case 'XL': return $this->typeXL;
  92  
  93  	 	 case 'C2': return 'NVARCHAR2';
  94  	 	 case 'X2': return 'NVARCHAR2(4000)';
  95  
  96  	 	 case 'B': return 'BLOB';
  97  
  98  	 	 case 'TS':
  99  	 	 	 	 return 'TIMESTAMP';
 100  
 101  	 	 case 'D':
 102  	 	 case 'T': return 'DATE';
 103  	 	 case 'L': return 'NUMBER(1)';
 104  	 	 case 'I1': return 'NUMBER(3)';
 105  	 	 case 'I2': return 'NUMBER(5)';
 106  	 	 case 'I':
 107  	 	 case 'I4': return 'NUMBER(10)';
 108  
 109  	 	 case 'I8': return 'NUMBER(20)';
 110  	 	 case 'F': return 'NUMBER';
 111  	 	 case 'N': return 'NUMBER';
 112  	 	 case 'R': return 'NUMBER(20)';
 113  	 	 default:
 114  	 	 	 return $meta;
 115  	 	 }
 116  	 }
 117  
 118  	function CreateDatabase($dbname, $options=false)
 119  	 {
 120  	 	 $options = $this->_Options($options);
 121  	 	 $password = isset($options['PASSWORD']) ? $options['PASSWORD'] : 'tiger';
 122  	 	 $tablespace = isset($options["TABLESPACE"]) ? " DEFAULT TABLESPACE ".$options["TABLESPACE"] : '';
 123  	 	 $sql[] = "CREATE USER ".$dbname." IDENTIFIED BY ".$password.$tablespace;
 124  	 	 $sql[] = "GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO $dbname";
 125  
 126  	 	 return $sql;
 127  	 }
 128  
 129  	function AddColumnSQL($tabname, $flds)
 130  	 {
 131  	 	 $tabname = $this->TableName($tabname);
 132  	 	 $f = array();
 133  	 	 list($lines,$pkey) = $this->_GenFields($flds);
 134  	 	 $s = "ALTER TABLE $tabname ADD (";
 135  	 	 foreach($lines as $v) {
 136  	 	 	 $f[] = "\n $v";
 137  	 	 }
 138  
 139  	 	 $s .= implode(', ',$f).')';
 140  	 	 $sql[] = $s;
 141  	 	 return $sql;
 142  	 }
 143  
 144  	function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
 145  	 {
 146  	 	 $tabname = $this->TableName($tabname);
 147  	 	 $f = array();
 148  	 	 list($lines,$pkey) = $this->_GenFields($flds);
 149  	 	 $s = "ALTER TABLE $tabname MODIFY(";
 150  	 	 foreach($lines as $v) {
 151  	 	 	 $f[] = "\n $v";
 152  	 	 }
 153  	 	 $s .= implode(', ',$f).')';
 154  	 	 $sql[] = $s;
 155  	 	 return $sql;
 156  	 }
 157  
 158  	function DropColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
 159  	 {
 160  	 	 if (!is_array($flds)) $flds = explode(',',$flds);
 161  	 	 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
 162  
 163  	 	 $sql = array();
 164  	 	 $s = "ALTER TABLE $tabname DROP(";
 165  	 	 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
 166  	 	 $sql[] = $s;
 167  	 	 return $sql;
 168  	 }
 169  
 170  	function _DropAutoIncrement($t)
 171  	 {
 172  	 	 if (strpos($t,'.') !== false) {
 173  	 	 	 $tarr = explode('.',$t);
 174  	 	 	 return "drop sequence ".$tarr[0].".seq_".$tarr[1];
 175  	 	 }
 176  	 	 return "drop sequence seq_".$t;
 177  	 }
 178  
 179  	 // return string must begin with space
 180  	function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 181  	 {
 182  	 	 $suffix = '';
 183  
 184  	 	 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
 185  	 	 	 $fnotnull = false;
 186  	 	 	 if ($this->debug) ADOConnection::outp("NOT NULL and DEFAULT='' illegal in Oracle");
 187  	 	 }
 188  
 189  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 190  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
 191  
 192  	 	 if ($fautoinc) $this->seqField = $fname;
 193  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
 194  
 195  	 	 return $suffix;
 196  	 }
 197  
 198  	 /**
 199  	  * Creates an insert trigger to emulate an auto-increment column
 200  	  * in a table
 201  	  *
 202  	  * @param string   $tabname       The name of the table
 203  	  * @param string[] $tableoptions  Optional configuration items
 204  	  *
 205  	  * @return string[] The SQL statements to create the trigger
 206  	  */
 207  	function _Triggers($tabname,$tableoptions)
 208  	 {
 209  	 	 
 210  	 	 if (!$this->seqField) return array();
 211  
 212  	 	 if ($this->schema) 
 213  	 	 {
 214  	 	 	 $t = strpos($tabname,'.');
 215  	 	 	 if ($t !== false) 
 216  	 	 	 	 $tab = substr($tabname,$t+1);
 217  	 	 	 else 
 218  	 	 	 	 $tab = $tabname;
 219  	 	 	 
 220  	 	 	 if ($this->connection->useCompactAutoIncrements)
 221  	 	 	 	 $id = sprintf('%u',crc32(strtolower($tab)));
 222  	 	 	 else
 223  	 	 	 	 $id = $tab;
 224  	 	 	 
 225  	 	 	 $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
 226  	 	 	 $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab;
 227  	 	 	 
 228  	 	 } 
 229  	 	 else 
 230  	 	 {
 231  	 	 	 if ($this->connection->useCompactAutoIncrements)
 232  	 	 	 	 $id = sprintf('%u',crc32(strtolower($tabname)));
 233  	 	 	 else
 234  	 	 	 	 $id = $tabname;
 235  	 	 	 
 236  	 	 	 $seqname = $this->seqPrefix.$id;
 237  	 	 	 $trigname = $this->trigPrefix.$id;
 238  	 	 }
 239  
 240  	 	 if (strlen($seqname) > 30) {
 241  	 	 	 $seqname = $this->seqPrefix.uniqid('');
 242  	 	 } // end if
 243  	 	 
 244  	 	 if (strlen($trigname) > 30) {
 245  	 	 	 $trigname = $this->trigPrefix.uniqid('');
 246  	 	 } // end if
 247  
 248  	 	 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
 249  	 	 $seqCache = '';
 250  	 	 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
 251  	 	 $seqIncr = '';
 252  	 	 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
 253  	 	 $seqStart = '';
 254  	 	 if (isset($tableoptions['SEQUENCE_START'])){$seqStart = ' START WITH '.$tableoptions['SEQUENCE_START'];}
 255  	 	 $sql[] = "CREATE SEQUENCE $seqname MINVALUE 1 $seqStart $seqIncr $seqCache";
 256  	 	 $sql[] = "CREATE OR REPLACE TRIGGER $trigname BEFORE insert ON $tabname FOR EACH ROW WHEN (NEW.$this->seqField IS NULL OR NEW.$this->seqField = 0) BEGIN select $seqname.nextval into :new.$this->seqField from dual; END;";
 257  
 258  	 	 $this->seqField = false;
 259  	 	 return $sql;
 260  	 }
 261  
 262  	 /*
 263  	 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
 264  	 	 [table_options] [select_statement]
 265  	 	 create_definition:
 266  	 	 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
 267  	 	 [PRIMARY KEY] [reference_definition]
 268  	 	 or PRIMARY KEY (index_col_name,...)
 269  	 	 or KEY [index_name] (index_col_name,...)
 270  	 	 or INDEX [index_name] (index_col_name,...)
 271  	 	 or UNIQUE [INDEX] [index_name] (index_col_name,...)
 272  	 	 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
 273  	 	 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
 274  	 	 [reference_definition]
 275  	 	 or CHECK (expr)
 276  	 */
 277  
 278  
 279  
 280  	function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
 281  	 {
 282  	 	 $sql = array();
 283  
 284  	 	 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 285  	 	 	 $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
 286  	 	 	 if ( isset($idxoptions['DROP']) )
 287  	 	 	 	 return $sql;
 288  	 	 }
 289  
 290  	 	 if ( empty ($flds) ) {
 291  	 	 	 return $sql;
 292  	 	 }
 293  
 294  	 	 if (isset($idxoptions['BITMAP'])) {
 295  	 	 	 $unique = ' BITMAP';
 296  	 	 } elseif (isset($idxoptions['UNIQUE'])) {
 297  	 	 	 $unique = ' UNIQUE';
 298  	 	 } else {
 299  	 	 	 $unique = '';
 300  	 	 }
 301  
 302  	 	 if ( is_array($flds) )
 303  	 	 	 $flds = implode(', ',$flds);
 304  	 	 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
 305  
 306  	 	 if ( isset($idxoptions[$this->upperName]) )
 307  	 	 	 $s .= $idxoptions[$this->upperName];
 308  
 309  	 	 if (isset($idxoptions['oci8']))
 310  	 	 	 $s .= $idxoptions['oci8'];
 311  
 312  
 313  	 	 $sql[] = $s;
 314  
 315  	 	 return $sql;
 316  	 }
 317  
 318  	function GetCommentSQL($table,$col)
 319  	 {
 320  	 	 $table = $this->connection->qstr($table);
 321  	 	 $col = $this->connection->qstr($col);
 322  	 	 return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
 323  	 }
 324  
 325  	function SetCommentSQL($table,$col,$cmt)
 326  	 {
 327  	 	 $cmt = $this->connection->qstr($cmt);
 328  	 	 return  "COMMENT ON COLUMN $table.$col IS $cmt";
 329  	 }
 330  }