Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [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  	 	 
  54  	 	 $t = strtoupper($t);
  55  	 	 
  56  	 	 if (array_key_exists($t,$this->connection->customActualTypes))
  57  	 	 	 return  $this->connection->customActualTypes[$t];
  58  
  59  	 	 switch ($t) {
  60  	  	 case 'VARCHAR':
  61  	  	 case 'VARCHAR2':
  62  	 	 case 'CHAR':
  63  	 	 case 'VARBINARY':
  64  	 	 case 'BINARY':
  65  	 	 	 if (isset($this) && $len <= $this->blobSize) return 'C';
  66  	 	 	 return 'X';
  67  
  68  	 	 case 'NCHAR':
  69  	 	 case 'NVARCHAR2':
  70  	 	 case 'NVARCHAR':
  71  	 	 	 if (isset($this) && $len <= $this->blobSize) return 'C2';
  72  	 	 	 return 'X2';
  73  
  74  	 	 case 'NCLOB':
  75  	 	 case 'CLOB':
  76  	 	 	 return 'XL';
  77  
  78  	 	 case 'LONG RAW':
  79  	 	 case 'LONG VARBINARY':
  80  	 	 case 'BLOB':
  81  	 	 	 return 'B';
  82  
  83  	 	 case 'TIMESTAMP':
  84  	 	 	 return 'TS';
  85  
  86  	 	 case 'DATE':
  87  	 	 	 return 'T';
  88  
  89  	 	 case 'INT':
  90  	 	 case 'SMALLINT':
  91  	 	 case 'INTEGER':
  92  	 	 	 return 'I';
  93  
  94  	 	 default:
  95  	 	 	 return ADODB_DEFAULT_METATYPE;
  96  	 	 }
  97  	 }
  98  
  99   	function ActualType($meta)
 100  	 {
 101  	 	 $meta = strtoupper($meta);
 102  	 	 
 103  	 	 /*
 104  	 	 * Add support for custom meta types. We do this
 105  	 	 * first, that allows us to override existing types
 106  	 	 */
 107  	 	 if (isset($this->connection->customMetaTypes[$meta]))
 108  	 	 	 return $this->connection->customMetaTypes[$meta]['actual'];
 109  	 	 
 110  	 	 switch($meta) {
 111  	 	 case 'C': return 'VARCHAR';
 112  	 	 case 'X': return $this->typeX;
 113  	 	 case 'XL': return $this->typeXL;
 114  
 115  	 	 case 'C2': return 'NVARCHAR2';
 116  	 	 case 'X2': return 'NVARCHAR2(4000)';
 117  
 118  	 	 case 'B': return 'BLOB';
 119  
 120  	 	 case 'TS':
 121  	 	 	 	 return 'TIMESTAMP';
 122  
 123  	 	 case 'D':
 124  	 	 case 'T': return 'DATE';
 125  	 	 case 'L': return 'NUMBER(1)';
 126  	 	 case 'I1': return 'NUMBER(3)';
 127  	 	 case 'I2': return 'NUMBER(5)';
 128  	 	 case 'I':
 129  	 	 case 'I4': return 'NUMBER(10)';
 130  
 131  	 	 case 'I8': return 'NUMBER(20)';
 132  	 	 case 'F': return 'NUMBER';
 133  	 	 case 'N': return 'NUMBER';
 134  	 	 case 'R': return 'NUMBER(20)';
 135  	 	 default:
 136  	 	 	 return $meta;
 137  	 	 }
 138  	 }
 139  
 140  	function CreateDatabase($dbname, $options=false)
 141  	 {
 142  	 	 $options = $this->_Options($options);
 143  	 	 $password = isset($options['PASSWORD']) ? $options['PASSWORD'] : 'tiger';
 144  	 	 $tablespace = isset($options["TABLESPACE"]) ? " DEFAULT TABLESPACE ".$options["TABLESPACE"] : '';
 145  	 	 $sql[] = "CREATE USER ".$dbname." IDENTIFIED BY ".$password.$tablespace;
 146  	 	 $sql[] = "GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO $dbname";
 147  
 148  	 	 return $sql;
 149  	 }
 150  
 151  	function AddColumnSQL($tabname, $flds)
 152  	 {
 153  	 	 $tabname = $this->TableName($tabname);
 154  	 	 $f = array();
 155  	 	 list($lines,$pkey) = $this->_GenFields($flds);
 156  	 	 $s = "ALTER TABLE $tabname ADD (";
 157  	 	 foreach($lines as $v) {
 158  	 	 	 $f[] = "\n $v";
 159  	 	 }
 160  
 161  	 	 $s .= implode(', ',$f).')';
 162  	 	 $sql[] = $s;
 163  	 	 return $sql;
 164  	 }
 165  
 166  	function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
 167  	 {
 168  	 	 $tabname = $this->TableName($tabname);
 169  	 	 $f = array();
 170  	 	 list($lines,$pkey) = $this->_GenFields($flds);
 171  	 	 $s = "ALTER TABLE $tabname MODIFY(";
 172  	 	 foreach($lines as $v) {
 173  	 	 	 $f[] = "\n $v";
 174  	 	 }
 175  	 	 $s .= implode(', ',$f).')';
 176  	 	 $sql[] = $s;
 177  	 	 return $sql;
 178  	 }
 179  
 180  	function DropColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
 181  	 {
 182  	 	 if (!is_array($flds)) $flds = explode(',',$flds);
 183  	 	 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
 184  
 185  	 	 $sql = array();
 186  	 	 $s = "ALTER TABLE $tabname DROP(";
 187  	 	 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
 188  	 	 $sql[] = $s;
 189  	 	 return $sql;
 190  	 }
 191  
 192  	function _DropAutoIncrement($t)
 193  	 {
 194  	 	 if (strpos($t,'.') !== false) {
 195  	 	 	 $tarr = explode('.',$t);
 196  	 	 	 return "drop sequence ".$tarr[0].".seq_".$tarr[1];
 197  	 	 }
 198  	 	 return "drop sequence seq_".$t;
 199  	 }
 200  
 201  	 // return string must begin with space
 202  	function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 203  	 {
 204  	 	 $suffix = '';
 205  
 206  	 	 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
 207  	 	 	 $fnotnull = false;
 208  	 	 	 if ($this->debug) ADOConnection::outp("NOT NULL and DEFAULT='' illegal in Oracle");
 209  	 	 }
 210  
 211  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 212  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
 213  
 214  	 	 if ($fautoinc) $this->seqField = $fname;
 215  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
 216  
 217  	 	 return $suffix;
 218  	 }
 219  
 220  	 /**
 221  	  * Creates an insert trigger to emulate an auto-increment column
 222  	  * in a table
 223  	  *
 224  	  * @param string   $tabname       The name of the table
 225  	  * @param string[] $tableoptions  Optional configuration items
 226  	  *
 227  	  * @return string[] The SQL statements to create the trigger
 228  	  */
 229  	function _Triggers($tabname,$tableoptions)
 230  	 {
 231  	 	 
 232  	 	 if (!$this->seqField) return array();
 233  
 234  	 	 if ($this->schema) 
 235  	 	 {
 236  	 	 	 $t = strpos($tabname,'.');
 237  	 	 	 if ($t !== false) 
 238  	 	 	 	 $tab = substr($tabname,$t+1);
 239  	 	 	 else 
 240  	 	 	 	 $tab = $tabname;
 241  	 	 	 
 242  	 	 	 if ($this->connection->useCompactAutoIncrements)
 243  	 	 	 	 $id = sprintf('%u',crc32(strtolower($tab)));
 244  	 	 	 else
 245  	 	 	 	 $id = $tab;
 246  	 	 	 
 247  	 	 	 $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
 248  	 	 	 $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab;
 249  	 	 	 
 250  	 	 } 
 251  	 	 else 
 252  	 	 {
 253  	 	 	 if ($this->connection->useCompactAutoIncrements)
 254  	 	 	 	 $id = sprintf('%u',crc32(strtolower($tabname)));
 255  	 	 	 else
 256  	 	 	 	 $id = $tabname;
 257  	 	 	 
 258  	 	 	 $seqname = $this->seqPrefix.$id;
 259  	 	 	 $trigname = $this->trigPrefix.$id;
 260  	 	 }
 261  
 262  	 	 if (strlen($seqname) > 30) {
 263  	 	 	 $seqname = $this->seqPrefix.uniqid('');
 264  	 	 } // end if
 265  	 	 
 266  	 	 if (strlen($trigname) > 30) {
 267  	 	 	 $trigname = $this->trigPrefix.uniqid('');
 268  	 	 } // end if
 269  
 270  	 	 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
 271  	 	 $seqCache = '';
 272  	 	 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
 273  	 	 $seqIncr = '';
 274  	 	 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
 275  	 	 $seqStart = '';
 276  	 	 if (isset($tableoptions['SEQUENCE_START'])){$seqStart = ' START WITH '.$tableoptions['SEQUENCE_START'];}
 277  	 	 $sql[] = "CREATE SEQUENCE $seqname MINVALUE 1 $seqStart $seqIncr $seqCache";
 278  	 	 $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;";
 279  
 280  	 	 $this->seqField = false;
 281  	 	 return $sql;
 282  	 }
 283  
 284  	 /*
 285  	 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
 286  	 	 [table_options] [select_statement]
 287  	 	 create_definition:
 288  	 	 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
 289  	 	 [PRIMARY KEY] [reference_definition]
 290  	 	 or PRIMARY KEY (index_col_name,...)
 291  	 	 or KEY [index_name] (index_col_name,...)
 292  	 	 or INDEX [index_name] (index_col_name,...)
 293  	 	 or UNIQUE [INDEX] [index_name] (index_col_name,...)
 294  	 	 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
 295  	 	 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
 296  	 	 [reference_definition]
 297  	 	 or CHECK (expr)
 298  	 */
 299  
 300  
 301  
 302  	function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
 303  	 {
 304  	 	 $sql = array();
 305  
 306  	 	 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 307  	 	 	 $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
 308  	 	 	 if ( isset($idxoptions['DROP']) )
 309  	 	 	 	 return $sql;
 310  	 	 }
 311  
 312  	 	 if ( empty ($flds) ) {
 313  	 	 	 return $sql;
 314  	 	 }
 315  
 316  	 	 if (isset($idxoptions['BITMAP'])) {
 317  	 	 	 $unique = ' BITMAP';
 318  	 	 } elseif (isset($idxoptions['UNIQUE'])) {
 319  	 	 	 $unique = ' UNIQUE';
 320  	 	 } else {
 321  	 	 	 $unique = '';
 322  	 	 }
 323  
 324  	 	 if ( is_array($flds) )
 325  	 	 	 $flds = implode(', ',$flds);
 326  	 	 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
 327  
 328  	 	 if ( isset($idxoptions[$this->upperName]) )
 329  	 	 	 $s .= $idxoptions[$this->upperName];
 330  
 331  	 	 if (isset($idxoptions['oci8']))
 332  	 	 	 $s .= $idxoptions['oci8'];
 333  
 334  
 335  	 	 $sql[] = $s;
 336  
 337  	 	 return $sql;
 338  	 }
 339  
 340  	function GetCommentSQL($table,$col)
 341  	 {
 342  	 	 $table = $this->connection->qstr($table);
 343  	 	 $col = $this->connection->qstr($col);
 344  	 	 return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
 345  	 }
 346  
 347  	function SetCommentSQL($table,$col,$cmt)
 348  	 {
 349  	 	 $cmt = $this->connection->qstr($cmt);
 350  	 	 return  "COMMENT ON COLUMN $table.$col IS $cmt";
 351  	 }
 352  }