4 @version v5.20.1 06-Dec-2015
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)';
29 function MetaType($t, $len=-1, $fieldobj=false)
34 $len = $fieldobj->max_length
;
36 switch (strtoupper($t)) {
42 if (isset($this) && $len <= $this->blobSize
) return 'C';
48 if (isset($this) && $len <= $this->blobSize
) return 'C2';
56 case 'LONG VARBINARY':
76 function ActualType($meta)
79 case 'C': return 'VARCHAR';
80 case 'X': return $this->typeX
;
81 case 'XL': return $this->typeXL
;
83 case 'C2': return 'NVARCHAR2';
84 case 'X2': return 'NVARCHAR2(4000)';
86 case 'B': return 'BLOB';
92 case 'T': return 'DATE';
93 case 'L': return 'NUMBER(1)';
94 case 'I1': return 'NUMBER(3)';
95 case 'I2': return 'NUMBER(5)';
97 case 'I4': return 'NUMBER(10)';
99 case 'I8': return 'NUMBER(20)';
100 case 'F': return 'NUMBER';
101 case 'N': return 'NUMBER';
102 case 'R': return 'NUMBER(20)';
108 function CreateDatabase($dbname, $options=false)
110 $options = $this->_Options($options);
111 $password = isset($options['PASSWORD']) ?
$options['PASSWORD'] : 'tiger';
112 $tablespace = isset($options["TABLESPACE"]) ?
" DEFAULT TABLESPACE ".$options["TABLESPACE"] : '';
113 $sql[] = "CREATE USER ".$dbname." IDENTIFIED BY ".$password.$tablespace;
114 $sql[] = "GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO $dbname";
119 function AddColumnSQL($tabname, $flds)
121 $tabname = $this->TableName($tabname);
123 list($lines,$pkey) = $this->_GenFields($flds);
124 $s = "ALTER TABLE $tabname ADD (";
125 foreach($lines as $v) {
129 $s .= implode(', ',$f).')';
134 function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
136 $tabname = $this->TableName($tabname);
138 list($lines,$pkey) = $this->_GenFields($flds);
139 $s = "ALTER TABLE $tabname MODIFY(";
140 foreach($lines as $v) {
143 $s .= implode(', ',$f).')';
148 function DropColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
150 if (!is_array($flds)) $flds = explode(',',$flds);
151 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
154 $s = "ALTER TABLE $tabname DROP(";
155 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
160 function _DropAutoIncrement($t)
162 if (strpos($t,'.') !== false) {
163 $tarr = explode('.',$t);
164 return "drop sequence ".$tarr[0].".seq_".$tarr[1];
166 return "drop sequence seq_".$t;
169 // return string must begin with space
170 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
174 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
176 if ($this->debug
) ADOConnection
::outp("NOT NULL and DEFAULT='' illegal in Oracle");
179 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
180 if ($fnotnull) $suffix .= ' NOT NULL';
182 if ($fautoinc) $this->seqField
= $fname;
183 if ($fconstraint) $suffix .= ' '.$fconstraint;
189 CREATE or replace TRIGGER jaddress_insert
190 before insert on jaddress
193 select seqaddress.nextval into :new.A_ID from dual;
196 function _Triggers($tabname,$tableoptions)
198 if (!$this->seqField
) return array();
201 $t = strpos($tabname,'.');
202 if ($t !== false) $tab = substr($tabname,$t+
1);
203 else $tab = $tabname;
204 $seqname = $this->schema
.'.'.$this->seqPrefix
.$tab;
205 $trigname = $this->schema
.'.'.$this->trigPrefix
.$this->seqPrefix
.$tab;
207 $seqname = $this->seqPrefix
.$tabname;
208 $trigname = $this->trigPrefix
.$seqname;
211 if (strlen($seqname) > 30) {
212 $seqname = $this->seqPrefix
.uniqid('');
214 if (strlen($trigname) > 30) {
215 $trigname = $this->trigPrefix
.uniqid('');
218 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
220 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
222 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
224 if (isset($tableoptions['SEQUENCE_START'])){$seqIncr = ' START WITH '.$tableoptions['SEQUENCE_START'];}
225 $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache";
226 $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;";
228 $this->seqField
= false;
233 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
234 [table_options] [select_statement]
236 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
237 [PRIMARY KEY] [reference_definition]
238 or PRIMARY KEY (index_col_name,...)
239 or KEY [index_name] (index_col_name,...)
240 or INDEX [index_name] (index_col_name,...)
241 or UNIQUE [INDEX] [index_name] (index_col_name,...)
242 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
243 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
244 [reference_definition]
250 function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
254 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
255 $sql[] = sprintf ($this->dropIndex
, $idxname, $tabname);
256 if ( isset($idxoptions['DROP']) )
260 if ( empty ($flds) ) {
264 if (isset($idxoptions['BITMAP'])) {
266 } elseif (isset($idxoptions['UNIQUE'])) {
272 if ( is_array($flds) )
273 $flds = implode(', ',$flds);
274 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
276 if ( isset($idxoptions[$this->upperName
]) )
277 $s .= $idxoptions[$this->upperName
];
279 if (isset($idxoptions['oci8']))
280 $s .= $idxoptions['oci8'];
288 function GetCommentSQL($table,$col)
290 $table = $this->connection
->qstr($table);
291 $col = $this->connection
->qstr($col);
292 return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
295 function SetCommentSQL($table,$col,$cmt)
297 $cmt = $this->connection
->qstr($cmt);
298 return "COMMENT ON COLUMN $table.$col IS $cmt";