Update to upstream version 4.94
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / datadict / datadict-oci8.inc.php
1 <?php
2
3 /**
4 V4.94 23 Jan 2007 (c) 2000-2007 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.
8
9 Set tabs to 4 for best viewing.
10
11 */
12
13 // security - hide paths
14 if (!defined('ADODB_DIR')) die();
15
16 class ADODB2_oci8 extends ADODB_DataDict {
17
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)';
25 var $typeXL = 'CLOB';
26
27 function MetaType($t,$len=-1)
28 {
29 if (is_object($t)) {
30 $fieldobj = $t;
31 $t = $fieldobj->type;
32 $len = $fieldobj->max_length;
33 }
34 switch (strtoupper($t)) {
35 case 'VARCHAR':
36 case 'VARCHAR2':
37 case 'CHAR':
38 case 'VARBINARY':
39 case 'BINARY':
40 if (isset($this) && $len <= $this->blobSize) return 'C';
41 return 'X';
42
43 case 'NCHAR':
44 case 'NVARCHAR2':
45 case 'NVARCHAR':
46 if (isset($this) && $len <= $this->blobSize) return 'C2';
47 return 'X2';
48
49 case 'NCLOB':
50 case 'CLOB':
51 return 'XL';
52
53 case 'LONG RAW':
54 case 'LONG VARBINARY':
55 case 'BLOB':
56 return 'B';
57
58 case 'DATE':
59 return 'T';
60
61 case 'INT':
62 case 'SMALLINT':
63 case 'INTEGER':
64 return 'I';
65
66 default:
67 return 'N';
68 }
69 }
70
71 function ActualType($meta)
72 {
73 switch($meta) {
74 case 'C': return 'VARCHAR';
75 case 'X': return $this->typeX;
76 case 'XL': return $this->typeXL;
77
78 case 'C2': return 'NVARCHAR2';
79 case 'X2': return 'NVARCHAR2(4000)';
80
81 case 'B': return 'BLOB';
82
83 case 'D':
84 case 'T': return 'DATE';
85 case 'L': return 'DECIMAL(1)';
86 case 'I1': return 'DECIMAL(3)';
87 case 'I2': return 'DECIMAL(5)';
88 case 'I':
89 case 'I4': return 'DECIMAL(10)';
90
91 case 'I8': return 'DECIMAL(20)';
92 case 'F': return 'DECIMAL';
93 case 'N': return 'DECIMAL';
94 default:
95 return $meta;
96 }
97 }
98
99 function CreateDatabase($dbname, $options=false)
100 {
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";
106
107 return $sql;
108 }
109
110 function AddColumnSQL($tabname, $flds)
111 {
112 $tabname = $this->TableName ($tabname);
113 $f = array();
114 list($lines,$pkey) = $this->_GenFields($flds);
115 $s = "ALTER TABLE $tabname ADD (";
116 foreach($lines as $v) {
117 $f[] = "\n $v";
118 }
119
120 $s .= implode(', ',$f).')';
121 $sql[] = $s;
122 return $sql;
123 }
124
125 function AlterColumnSQL($tabname, $flds)
126 {
127 $tabname = $this->TableName ($tabname);
128 $f = array();
129 list($lines,$pkey) = $this->_GenFields($flds);
130 $s = "ALTER TABLE $tabname MODIFY(";
131 foreach($lines as $v) {
132 $f[] = "\n $v";
133 }
134 $s .= implode(', ',$f).')';
135 $sql[] = $s;
136 return $sql;
137 }
138
139 function DropColumnSQL($tabname, $flds)
140 {
141 $tabname = $this->TableName ($tabname);
142 if (!is_array($flds)) $flds = explode(',',$flds);
143 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
144
145 $sql = array();
146 $s = "ALTER TABLE $tabname DROP(";
147 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
148 $sql[] = $s;
149 return $sql;
150 }
151
152 function _DropAutoIncrement($t)
153 {
154 if (strpos($t,'.') !== false) {
155 $tarr = explode('.',$t);
156 return "drop sequence ".$tarr[0].".seq_".$tarr[1];
157 }
158 return "drop sequence seq_".$t;
159 }
160
161 // return string must begin with space
162 function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
163 {
164 $suffix = '';
165
166 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
167 $fnotnull = false;
168 if ($this->debug) ADOConnection::outp("NOT NULL and DEFAULT='' illegal in Oracle");
169 }
170
171 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
172 if ($fnotnull) $suffix .= ' NOT NULL';
173
174 if ($fautoinc) $this->seqField = $fname;
175 if ($fconstraint) $suffix .= ' '.$fconstraint;
176
177 return $suffix;
178 }
179
180 /*
181 CREATE or replace TRIGGER jaddress_insert
182 before insert on jaddress
183 for each row
184 begin
185 select seqaddress.nextval into :new.A_ID from dual;
186 end;
187 */
188 function _Triggers($tabname,$tableoptions)
189 {
190 if (!$this->seqField) return array();
191
192 if ($this->schema) {
193 $t = strpos($tabname,'.');
194 if ($t !== false) $tab = substr($tabname,$t+1);
195 else $tab = $tabname;
196 $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
197 $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab;
198 } else {
199 $seqname = $this->seqPrefix.$tabname;
200 $trigname = $this->trigPrefix.$seqname;
201 }
202
203 if (strlen($seqname) > 30) {
204 $seqname = $this->seqPrefix.uniqid('');
205 } // end if
206 if (strlen($trigname) > 30) {
207 $trigname = $this->trigPrefix.uniqid('');
208 } // end if
209
210 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
211 $seqCache = '';
212 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
213 $seqIncr = '';
214 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
215 $seqStart = '';
216 if (isset($tableoptions['SEQUENCE_START'])){$seqIncr = ' START WITH '.$tableoptions['SEQUENCE_START'];}
217 $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache";
218 $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;";
219
220 $this->seqField = false;
221 return $sql;
222 }
223
224 /*
225 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
226 [table_options] [select_statement]
227 create_definition:
228 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
229 [PRIMARY KEY] [reference_definition]
230 or PRIMARY KEY (index_col_name,...)
231 or KEY [index_name] (index_col_name,...)
232 or INDEX [index_name] (index_col_name,...)
233 or UNIQUE [INDEX] [index_name] (index_col_name,...)
234 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
235 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
236 [reference_definition]
237 or CHECK (expr)
238 */
239
240
241
242 function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
243 {
244 $sql = array();
245
246 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
247 $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
248 if ( isset($idxoptions['DROP']) )
249 return $sql;
250 }
251
252 if ( empty ($flds) ) {
253 return $sql;
254 }
255
256 if (isset($idxoptions['BITMAP'])) {
257 $unique = ' BITMAP';
258 } elseif (isset($idxoptions['UNIQUE'])) {
259 $unique = ' UNIQUE';
260 } else {
261 $unique = '';
262 }
263
264 if ( is_array($flds) )
265 $flds = implode('", "',$flds);
266 $s = 'CREATE' . $unique . ' INDEX "' . $idxname . '" ON "' .$tabname . '" ("' . $flds . '")';
267
268 if ( isset($idxoptions[$this->upperName]) )
269 $s .= $idxoptions[$this->upperName];
270
271 if (isset($idxoptions['oci8']))
272 $s .= $idxoptions['oci8'];
273
274
275 $sql[] = $s;
276
277 return $sql;
278 }
279
280 function GetCommentSQL($table,$col)
281 {
282 $table = $this->connection->qstr($table);
283 $col = $this->connection->qstr($col);
284 return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
285 }
286
287 function SetCommentSQL($table,$col,$cmt)
288 {
289 $cmt = $this->connection->qstr($cmt);
290 return "COMMENT ON COLUMN $table.$col IS $cmt";
291 }
292 }
293 ?>