weekly release 3.11.8+
[moodle.git] / lib / adodb / datadict / datadict-oci8.inc.php
blobb330951a7b501603e6579b85f7f6512f04b082a3
1 <?php
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.
11 Set tabs to 4 for best viewing.
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
18 class ADODB2_oci8 extends ADODB_DataDict {
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';
29 /**
30 * Legacy compatibility for sequence names for emulated auto-increments.
32 * If set to true, creates sequences and triggers as TRIG_394545594
33 * instead of TRIG_possibly_too_long_tablename
35 * @var bool $useCompactAutoIncrements
37 public $useCompactAutoIncrements = false;
39 function metaType($t, $len=-1, $fieldobj=false)
41 if (is_object($t)) {
42 $fieldobj = $t;
43 $t = $fieldobj->type;
44 $len = $fieldobj->max_length;
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';
55 case 'NCHAR':
56 case 'NVARCHAR2':
57 case 'NVARCHAR':
58 if (isset($this) && $len <= $this->blobSize) return 'C2';
59 return 'X2';
61 case 'NCLOB':
62 case 'CLOB':
63 return 'XL';
65 case 'LONG RAW':
66 case 'LONG VARBINARY':
67 case 'BLOB':
68 return 'B';
70 case 'TIMESTAMP':
71 return 'TS';
73 case 'DATE':
74 return 'T';
76 case 'INT':
77 case 'SMALLINT':
78 case 'INTEGER':
79 return 'I';
81 default:
82 return ADODB_DEFAULT_METATYPE;
86 function ActualType($meta)
88 switch($meta) {
89 case 'C': return 'VARCHAR';
90 case 'X': return $this->typeX;
91 case 'XL': return $this->typeXL;
93 case 'C2': return 'NVARCHAR2';
94 case 'X2': return 'NVARCHAR2(4000)';
96 case 'B': return 'BLOB';
98 case 'TS':
99 return 'TIMESTAMP';
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)';
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;
118 function CreateDatabase($dbname, $options=false)
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";
126 return $sql;
129 function AddColumnSQL($tabname, $flds)
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";
139 $s .= implode(', ',$f).')';
140 $sql[] = $s;
141 return $sql;
144 function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
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";
153 $s .= implode(', ',$f).')';
154 $sql[] = $s;
155 return $sql;
158 function DropColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
160 if (!is_array($flds)) $flds = explode(',',$flds);
161 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
163 $sql = array();
164 $s = "ALTER TABLE $tabname DROP(";
165 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
166 $sql[] = $s;
167 return $sql;
170 function _DropAutoIncrement($t)
172 if (strpos($t,'.') !== false) {
173 $tarr = explode('.',$t);
174 return "drop sequence ".$tarr[0].".seq_".$tarr[1];
176 return "drop sequence seq_".$t;
179 // return string must begin with space
180 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
182 $suffix = '';
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");
189 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
190 if ($fnotnull) $suffix .= ' NOT NULL';
192 if ($fautoinc) $this->seqField = $fname;
193 if ($fconstraint) $suffix .= ' '.$fconstraint;
195 return $suffix;
199 * Creates an insert trigger to emulate an auto-increment column
200 * in a table
202 * @param string $tabname The name of the table
203 * @param string[] $tableoptions Optional configuration items
205 * @return string[] The SQL statements to create the trigger
207 function _Triggers($tabname,$tableoptions)
210 if (!$this->seqField) return array();
212 if ($this->schema)
214 $t = strpos($tabname,'.');
215 if ($t !== false)
216 $tab = substr($tabname,$t+1);
217 else
218 $tab = $tabname;
220 if ($this->connection->useCompactAutoIncrements)
221 $id = sprintf('%u',crc32(strtolower($tab)));
222 else
223 $id = $tab;
225 $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
226 $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab;
229 else
231 if ($this->connection->useCompactAutoIncrements)
232 $id = sprintf('%u',crc32(strtolower($tabname)));
233 else
234 $id = $tabname;
236 $seqname = $this->seqPrefix.$id;
237 $trigname = $this->trigPrefix.$id;
240 if (strlen($seqname) > 30) {
241 $seqname = $this->seqPrefix.uniqid('');
242 } // end if
244 if (strlen($trigname) > 30) {
245 $trigname = $this->trigPrefix.uniqid('');
246 } // end if
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;";
258 $this->seqField = false;
259 return $sql;
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)
280 function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
282 $sql = array();
284 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
285 $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
286 if ( isset($idxoptions['DROP']) )
287 return $sql;
290 if ( empty ($flds) ) {
291 return $sql;
294 if (isset($idxoptions['BITMAP'])) {
295 $unique = ' BITMAP';
296 } elseif (isset($idxoptions['UNIQUE'])) {
297 $unique = ' UNIQUE';
298 } else {
299 $unique = '';
302 if ( is_array($flds) )
303 $flds = implode(', ',$flds);
304 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
306 if ( isset($idxoptions[$this->upperName]) )
307 $s .= $idxoptions[$this->upperName];
309 if (isset($idxoptions['oci8']))
310 $s .= $idxoptions['oci8'];
313 $sql[] = $s;
315 return $sql;
318 function GetCommentSQL($table,$col)
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";
325 function SetCommentSQL($table,$col,$cmt)
327 $cmt = $this->connection->qstr($cmt);
328 return "COMMENT ON COLUMN $table.$col IS $cmt";