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)';
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)
44 $len = $fieldobj->max_length
;
46 switch (strtoupper($t)) {
52 if (isset($this) && $len <= $this->blobSize
) return 'C';
58 if (isset($this) && $len <= $this->blobSize
) return 'C2';
66 case 'LONG VARBINARY':
82 return ADODB_DEFAULT_METATYPE
;
86 function ActualType($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';
102 case 'T': return 'DATE';
103 case 'L': return 'NUMBER(1)';
104 case 'I1': return 'NUMBER(3)';
105 case 'I2': return 'NUMBER(5)';
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)';
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";
129 function AddColumnSQL($tabname, $flds)
131 $tabname = $this->TableName($tabname);
133 list($lines,$pkey) = $this->_GenFields($flds);
134 $s = "ALTER TABLE $tabname ADD (";
135 foreach($lines as $v) {
139 $s .= implode(', ',$f).')';
144 function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
146 $tabname = $this->TableName($tabname);
148 list($lines,$pkey) = $this->_GenFields($flds);
149 $s = "ALTER TABLE $tabname MODIFY(";
150 foreach($lines as $v) {
153 $s .= implode(', ',$f).')';
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);
164 $s = "ALTER TABLE $tabname DROP(";
165 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
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)
184 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
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;
199 * Creates an insert trigger to emulate an auto-increment column
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();
214 $t = strpos($tabname,'.');
216 $tab = substr($tabname,$t+
1);
220 if ($this->connection
->useCompactAutoIncrements
)
221 $id = sprintf('%u',crc32(strtolower($tab)));
225 $seqname = $this->schema
.'.'.$this->seqPrefix
.$tab;
226 $trigname = $this->schema
.'.'.$this->trigPrefix
.$this->seqPrefix
.$tab;
231 if ($this->connection
->useCompactAutoIncrements
)
232 $id = sprintf('%u',crc32(strtolower($tabname)));
236 $seqname = $this->seqPrefix
.$id;
237 $trigname = $this->trigPrefix
.$id;
240 if (strlen($seqname) > 30) {
241 $seqname = $this->seqPrefix
.uniqid('');
244 if (strlen($trigname) > 30) {
245 $trigname = $this->trigPrefix
.uniqid('');
248 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
250 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
252 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
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;
263 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
264 [table_options] [select_statement]
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]
280 function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
284 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
285 $sql[] = sprintf ($this->dropIndex
, $idxname, $tabname);
286 if ( isset($idxoptions['DROP']) )
290 if ( empty ($flds) ) {
294 if (isset($idxoptions['BITMAP'])) {
296 } elseif (isset($idxoptions['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'];
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";