TYPO3 CMS  TYPO3_6-2
datadict-oci8.inc.php
Go to the documentation of this file.
1 <?php
2 
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, $fieldobj=false)
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  $tabname = $this->TableName ($tabname);
120  $f = array();
121  list($lines,$pkey) = $this->_GenFields($flds);
122  $s = "ALTER TABLE $tabname ADD (";
123  foreach($lines as $v) {
124  $f[] = "\n $v";
125  }
126 
127  $s .= implode(', ',$f).')';
128  $sql[] = $s;
129  return $sql;
130  }
131 
132  function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
133  {
134  $tabname = $this->TableName ($tabname);
135  $f = array();
136  list($lines,$pkey) = $this->_GenFields($flds);
137  $s = "ALTER TABLE $tabname MODIFY(";
138  foreach($lines as $v) {
139  $f[] = "\n $v";
140  }
141  $s .= implode(', ',$f).')';
142  $sql[] = $s;
143  return $sql;
144  }
145 
146  function DropColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
147  {
148  $tabname = $this->TableName ($tabname);
149  if (!is_array($flds)) $flds = explode(',',$flds);
150  foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v);
151 
152  $sql = array();
153  $s = "ALTER TABLE $tabname DROP(";
154  $s .= implode(', ',$flds).') CASCADE CONSTRAINTS';
155  $sql[] = $s;
156  return $sql;
157  }
158 
159  function _DropAutoIncrement($t)
160  {
161  if (strpos($t,'.') !== false) {
162  $tarr = explode('.',$t);
163  return "drop sequence ".$tarr[0].".seq_".$tarr[1];
164  }
165  return "drop sequence seq_".$t;
166  }
167 
168  // return string must begin with space
169  function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
170  {
171  $suffix = '';
172 
173  if ($fdefault == "''" && $fnotnull) {// this is null in oracle
174  $fnotnull = false;
175  if ($this->debug) ADOConnection::outp("NOT NULL and DEFAULT='' illegal in Oracle");
176  }
177 
178  if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
179  if ($fnotnull) $suffix .= ' NOT NULL';
180 
181  if ($fautoinc) $this->seqField = $fname;
182  if ($fconstraint) $suffix .= ' '.$fconstraint;
183 
184  return $suffix;
185  }
186 
187 /*
188 CREATE or replace TRIGGER jaddress_insert
189 before insert on jaddress
190 for each row
191 begin
192 select seqaddress.nextval into :new.A_ID from dual;
193 end;
194 */
195  function _Triggers($tabname,$tableoptions)
196  {
197  if (!$this->seqField) return array();
198 
199  if ($this->schema) {
200  $t = strpos($tabname,'.');
201  if ($t !== false) $tab = substr($tabname,$t+1);
202  else $tab = $tabname;
203  $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
204  $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab;
205  } else {
206  $seqname = $this->seqPrefix.$tabname;
207  $trigname = $this->trigPrefix.$seqname;
208  }
209 
210  if (strlen($seqname) > 30) {
211  $seqname = $this->seqPrefix.uniqid('');
212  } // end if
213  if (strlen($trigname) > 30) {
214  $trigname = $this->trigPrefix.uniqid('');
215  } // end if
216 
217  if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname";
218  $seqCache = '';
219  if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];}
220  $seqIncr = '';
221  if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];}
222  $seqStart = '';
223  if (isset($tableoptions['SEQUENCE_START'])){$seqIncr = ' START WITH '.$tableoptions['SEQUENCE_START'];}
224  $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache";
225  $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;";
226 
227  $this->seqField = false;
228  return $sql;
229  }
230 
231  /*
232  CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
233  [table_options] [select_statement]
234  create_definition:
235  col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
236  [PRIMARY KEY] [reference_definition]
237  or PRIMARY KEY (index_col_name,...)
238  or KEY [index_name] (index_col_name,...)
239  or INDEX [index_name] (index_col_name,...)
240  or UNIQUE [INDEX] [index_name] (index_col_name,...)
241  or FULLTEXT [INDEX] [index_name] (index_col_name,...)
242  or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
243  [reference_definition]
244  or CHECK (expr)
245  */
246 
247 
248 
249  function _IndexSQL($idxname, $tabname, $flds,$idxoptions)
250  {
251  $sql = array();
252 
253  if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
254  $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
255  if ( isset($idxoptions['DROP']) )
256  return $sql;
257  }
258 
259  if ( empty ($flds) ) {
260  return $sql;
261  }
262 
263  if (isset($idxoptions['BITMAP'])) {
264  $unique = ' BITMAP';
265  } elseif (isset($idxoptions['UNIQUE'])) {
266  $unique = ' UNIQUE';
267  } else {
268  $unique = '';
269  }
270 
271  if ( is_array($flds) )
272  $flds = implode(', ',$flds);
273  $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
274 
275  if ( isset($idxoptions[$this->upperName]) )
276  $s .= $idxoptions[$this->upperName];
277 
278  if (isset($idxoptions['oci8']))
279  $s .= $idxoptions['oci8'];
280 
281 
282  $sql[] = $s;
283 
284  return $sql;
285  }
286 
287  function GetCommentSQL($table,$col)
288  {
289  $table = $this->connection->qstr($table);
290  $col = $this->connection->qstr($col);
291  return "select comments from USER_COL_COMMENTS where TABLE_NAME=$table and COLUMN_NAME=$col";
292  }
293 
294  function SetCommentSQL($table,$col,$cmt)
295  {
296  $cmt = $this->connection->qstr($cmt);
297  return "COMMENT ON COLUMN $table.$col IS $cmt";
298  }
299 }
_GenFields($flds, $widespacing=false)
$sql
Definition: server.php:82
AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
SetCommentSQL($table, $col, $cmt)
_IndexSQL($idxname, $tabname, $flds, $idxoptions)
die
Definition: index.php:6
_Triggers($tabname, $tableoptions)
GetCommentSQL($table, $col)
CreateDatabase($dbname, $options=false)
MetaType($t, $len=-1, $fieldobj=false)
debug($variable='', $name=' *variable *', $line=' *line *', $file=' *file *', $recursiveDepth=3, $debugLevel=E_DEBUG)
AddColumnSQL($tabname, $flds)
DropColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
_CreateSuffix($fname, &$ftype, $fnotnull, $fdefault, $fautoinc, $fconstraint, $funsigned)
NameQuote($name=NULL, $allowBrackets=false)