Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

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