4 V4.92a 29 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.
5 Released under both BSD license and Lesser GPL library license.
6 Whenever there is any discrepancy between the two licenses,
7 the BSD license will take precedence.
9 Set tabs to 4 for best viewing.
13 // security - hide paths
14 if (!defined('ADODB_DIR')) die();
16 class ADODB2_oci8
extends ADODB_DataDict
{
18 var $databaseType = 'oci8';
19 var $seqField = false;
20 var $seqPrefix = 'SEQ_';
21 var $dropTable = "DROP TABLE %s CASCADE CONSTRAINTS";
22 var $trigPrefix = 'TRIG_';
23 var $alterCol = ' MODIFY ';
24 var $typeX = 'VARCHAR(4000)';
27 function MetaType($t,$len=-1)
32 $len = $fieldobj->max_length
;
34 switch (strtoupper($t)) {
40 if (isset($this) && $len <= $this->blobSize
) return 'C';
46 if (isset($this) && $len <= $this->blobSize
) return 'C2';
54 case 'LONG VARBINARY':
71 function ActualType($meta)
74 case 'C': return 'VARCHAR';
75 case 'X': return $this->typeX
;
76 case 'XL': return $this->typeXL
;
78 case 'C2': return 'NVARCHAR2';
79 case 'X2': return 'NVARCHAR2(4000)';
81 case 'B': return 'BLOB';
84 case 'T': return 'DATE';
85 case 'L': return 'DECIMAL(1)';
86 case 'I1': return 'DECIMAL(3)';
87 case 'I2': return 'DECIMAL(5)';
89 case 'I4': return 'DECIMAL(10)';
91 case 'I8': return 'DECIMAL(20)';
92 case 'F': return 'DECIMAL';
93 case 'N': return 'DECIMAL';
99 function CreateDatabase($dbname, $options=false)
101 $options = $this->_Options($options);
102 $password = isset($options['PASSWORD']) ?
$options['PASSWORD'] : 'tiger';
103 $tablespace = isset($options["TABLESPACE"]) ?
" DEFAULT TABLESPACE ".$options["TABLESPACE"] : '';
104 $sql[] = "CREATE USER ".$dbname." IDENTIFIED BY ".$password.$tablespace;
105 $sql[] = "GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO $dbname";
110 function AddColumnSQL($tabname, $flds)
113 list($lines,$pkey) = $this->_GenFields($flds);
114 $s = "ALTER TABLE $tabname ADD (";
115 foreach($lines as $v) {
119 $s .= implode(', ',$f).')';
124 function AlterColumnSQL($tabname, $flds)
127 list($lines,$pkey) = $this->_GenFields($flds);
128 $s = "ALTER TABLE $tabname MODIFY(";
129 foreach($lines as $v) {
132 $s .= implode(', ',$f).')';
137 function DropColumnSQL($tabname, $flds)
139 if (!is_array($flds)) $flds = explode(',',$flds);
140 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
143 $s = "ALTER TABLE $tabname DROP(";
144 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
149 function _DropAutoIncrement($t)
151 if (strpos($t,'.') !== false) {
152 $tarr = explode('.',$t);
153 return "drop sequence ".$tarr[0].".seq_".$tarr[1];
155 return "drop sequence seq_".$t;
158 // return string must begin with space
159 function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
163 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
165 if ($this->debug
) ADOConnection
::outp("NOT NULL and DEFAULT='' illegal in Oracle");
168 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
169 if ($fnotnull) $suffix .= ' NOT NULL';
171 if ($fautoinc) $this->seqField
= $fname;
172 if ($fconstraint) $suffix .= ' '.$fconstraint;
178 CREATE or replace TRIGGER jaddress_insert
179 before insert on jaddress
182 select seqaddress.nextval into :new.A_ID from dual;
185 function _Triggers($tabname,$tableoptions)
187 if (!$this->seqField
) return array();
190 $t = strpos($tabname,'.');
191 if ($t !== false) $tab = substr($tabname,$t+
1);
192 else $tab = $tabname;
193 $seqname = $this->schema
.'.'.$this->seqPrefix
.$tab;
194 $trigname = $this->schema
.'.'.$this->trigPrefix
.$this->seqPrefix
.$tab;
196 $seqname = $this->seqPrefix
.$tabname;
197 $trigname = $this->trigPrefix
.$seqname;
199 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
201 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
203 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
205 if (isset($tableoptions['SEQUENCE_START'])){$seqIncr = ' START WITH '.$tableoptions['SEQUENCE_START'];}
206 $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache";
207 $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;";
209 $this->seqField
= false;
214 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
215 [table_options] [select_statement]
217 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
218 [PRIMARY KEY] [reference_definition]
219 or PRIMARY KEY (index_col_name,...)
220 or KEY [index_name] (index_col_name,...)
221 or INDEX [index_name] (index_col_name,...)
222 or UNIQUE [INDEX] [index_name] (index_col_name,...)
223 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
224 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
225 [reference_definition]
231 function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
235 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
236 $sql[] = sprintf ($this->dropIndex
, $idxname, $tabname);
237 if ( isset($idxoptions['DROP']) )
241 if ( empty ($flds) ) {
245 if (isset($idxoptions['BITMAP'])) {
247 } elseif (isset($idxoptions['UNIQUE'])) {
253 if ( is_array($flds) )
254 $flds = implode(', ',$flds);
255 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
257 if ( isset($idxoptions[$this->upperName
]) )
258 $s .= $idxoptions[$this->upperName
];
260 if (isset($idxoptions['oci8']))
261 $s .= $idxoptions['oci8'];
269 function GetCommentSQL($table,$col)
271 $table = $this->connection
->qstr($table);
272 $col = $this->connection
->qstr($col);
273 return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
276 function SetCommentSQL($table,$col,$cmt)
278 $cmt = $this->connection
->qstr($cmt);
279 return "COMMENT ON COLUMN $table.$col IS $cmt";