]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/datadict/datadict-oci8.inc.php
Upgrade adodb
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / datadict / datadict-oci8.inc.php
1 <?php
2
3 /**
4   V5.18 3 Sep 2012  (c) 2000-2012 John Lim (jlim#natsoft.com). 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 'TIMESTAMP':
59                         return 'TS';
60                         
61                 case 'DATE': 
62                         return 'T';
63                 
64                 case 'INT': 
65                 case 'SMALLINT':
66                 case 'INTEGER': 
67                         return 'I';
68                         
69                 default:
70                         return 'N';
71                 }
72         }
73         
74         function ActualType($meta)
75         {
76                 switch($meta) {
77                 case 'C': return 'VARCHAR';
78                 case 'X': return $this->typeX;
79                 case 'XL': return $this->typeXL;
80                 
81                 case 'C2': return 'NVARCHAR2';
82                 case 'X2': return 'NVARCHAR2(4000)';
83                 
84                 case 'B': return 'BLOB';
85                 
86                 case 'TS':
87                                 return 'TIMESTAMP';
88                                 
89                 case 'D': 
90                 case 'T': return 'DATE';
91                 case 'L': return 'NUMBER(1)';
92                 case 'I1': return 'NUMBER(3)';
93                 case 'I2': return 'NUMBER(5)';
94                 case 'I':
95                 case 'I4': return 'NUMBER(10)';
96                 
97                 case 'I8': return 'NUMBER(20)';
98                 case 'F': return 'NUMBER';
99                 case 'N': return 'NUMBER';
100                 case 'R': return 'NUMBER(20)';
101                 default:
102                         return $meta;
103                 }       
104         }
105         
106         function CreateDatabase($dbname, $options=false)
107         {
108                 $options = $this->_Options($options);
109                 $password = isset($options['PASSWORD']) ? $options['PASSWORD'] : 'tiger';
110                 $tablespace = isset($options["TABLESPACE"]) ? " DEFAULT TABLESPACE ".$options["TABLESPACE"] : '';
111                 $sql[] = "CREATE USER ".$dbname." IDENTIFIED BY ".$password.$tablespace;
112                 $sql[] = "GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO $dbname";
113                 
114                 return $sql;
115         }
116         
117         function AddColumnSQL($tabname, $flds)
118         {
119                 $f = array();
120                 list($lines,$pkey) = $this->_GenFields($flds);
121                 $s = "ALTER TABLE $tabname ADD (";
122                 foreach($lines as $v) {
123                         $f[] = "\n $v";
124                 }
125                 
126                 $s .= implode(', ',$f).')';
127                 $sql[] = $s;
128                 return $sql;
129         }
130         
131         function AlterColumnSQL($tabname, $flds)
132         {
133                 $f = array();
134                 list($lines,$pkey) = $this->_GenFields($flds);
135                 $s = "ALTER TABLE $tabname MODIFY(";
136                 foreach($lines as $v) {
137                         $f[] = "\n $v";
138                 }
139                 $s .= implode(', ',$f).')';
140                 $sql[] = $s;
141                 return $sql;
142         }
143         
144         function DropColumnSQL($tabname, $flds)
145         {
146                 if (!is_array($flds)) $flds = explode(',',$flds);
147                 foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
148                 
149                 $sql = array();
150                 $s = "ALTER TABLE $tabname DROP(";
151                 $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
152                 $sql[] = $s;
153                 return $sql;
154         }
155         
156         function _DropAutoIncrement($t)
157         {
158                 if (strpos($t,'.') !== false) {
159                         $tarr = explode('.',$t);
160                         return "drop sequence ".$tarr[0].".seq_".$tarr[1];
161                 }
162                 return "drop sequence seq_".$t;
163         }
164         
165         // return string must begin with space
166         function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
167         {
168                 $suffix = '';
169                 
170                 if ($fdefault == "''" && $fnotnull) {// this is null in oracle
171                         $fnotnull = false;
172                         if ($this->debug) ADOConnection::outp("NOT NULL and DEFAULT='' illegal in Oracle");
173                 }
174                 
175                 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
176                 if ($fnotnull) $suffix .= ' NOT NULL';
177                 
178                 if ($fautoinc) $this->seqField = $fname;
179                 if ($fconstraint) $suffix .= ' '.$fconstraint;
180                 
181                 return $suffix;
182         }
183         
184 /*
185 CREATE or replace TRIGGER jaddress_insert
186 before insert on jaddress
187 for each row
188 begin
189 select seqaddress.nextval into :new.A_ID from dual;
190 end;
191 */
192         function _Triggers($tabname,$tableoptions)
193         {
194                 if (!$this->seqField) return array();
195                 
196                 if ($this->schema) {
197                         $t = strpos($tabname,'.');
198                         if ($t !== false) $tab = substr($tabname,$t+1);
199                         else $tab = $tabname;
200                         $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
201                         $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab;
202                 } else {
203                         $seqname = $this->seqPrefix.$tabname;
204                         $trigname = $this->trigPrefix.$seqname;
205                 }
206                 
207                 if (strlen($seqname) > 30) {
208                         $seqname = $this->seqPrefix.uniqid('');
209                 } // end if
210                 if (strlen($trigname) > 30) {
211                         $trigname = $this->trigPrefix.uniqid('');
212                 } // end if
213
214                 if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
215                 $seqCache = '';
216                 if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
217                 $seqIncr = '';
218                 if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
219                 $seqStart = '';
220                 if (isset($tableoptions['SEQUENCE_START'])){$seqIncr = ' START WITH '.$tableoptions['SEQUENCE_START'];}
221                 $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache";
222                 $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";
223                 
224                 $this->seqField = false;
225                 return $sql;
226         }
227         
228         /*
229         CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
230                 [table_options] [select_statement]
231                 create_definition:
232                 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
233                 [PRIMARY KEY] [reference_definition]
234                 or PRIMARY KEY (index_col_name,...)
235                 or KEY [index_name] (index_col_name,...)
236                 or INDEX [index_name] (index_col_name,...)
237                 or UNIQUE [INDEX] [index_name] (index_col_name,...)
238                 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
239                 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
240                 [reference_definition]
241                 or CHECK (expr)
242         */
243         
244
245         
246         function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
247         {
248                 $sql = array();
249                 
250                 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
251                         $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
252                         if ( isset($idxoptions['DROP']) )
253                                 return $sql;
254                 }
255                 
256                 if ( empty ($flds) ) {
257                         return $sql;
258                 }
259                 
260                 if (isset($idxoptions['BITMAP'])) {
261                         $unique = ' BITMAP'; 
262                 } elseif (isset($idxoptions['UNIQUE'])) {
263                         $unique = ' UNIQUE';
264                 } else {
265                         $unique = '';
266                 }
267                 
268                 if ( is_array($flds) )
269                         $flds = implode(', ',$flds);
270                 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
271                 
272                 if ( isset($idxoptions[$this->upperName]) )
273                         $s .= $idxoptions[$this->upperName];
274                 
275                 if (isset($idxoptions['oci8']))
276                         $s .= $idxoptions['oci8'];
277                 
278
279                 $sql[] = $s;
280                 
281                 return $sql;
282         }
283         
284         function GetCommentSQL($table,$col)
285         {
286                 $table = $this->connection->qstr($table);
287                 $col = $this->connection->qstr($col);   
288                 return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
289         }
290         
291         function SetCommentSQL($table,$col,$cmt)
292         {
293                 $cmt = $this->connection->qstr($cmt);
294                 return  "COMMENT ON COLUMN $table.$col IS $cmt";
295         }
296 }
297 ?>