EGOCMS  24.0
EGOTEC Content-Managament-System
Ego_Sql_oci.php
gehe zur Dokumentation dieser Datei
1 <?php
5 require_once('base/Ego_Sql_Abstract.php');
6 
16 {
18  private $_dbHandle;
19  private $_queryHandle;
20  private $_limit;
21  private $_lobStm;
22  private $_lobStmId;
23  private $_mode=OCI_DEFAULT;
24  private $_user;
26  function __destruct()
27  {
28  if ($this->_lobStm)
29  {
30  //echo " Dlob ";
31  @oci_free_statement($this->_lobStm);
32  unset($this->_lobStm);
33  }
34  if ($this->_queryHandle)
35  {
36  //echo " Dquery ";
37  @oci_free_statement($this->_queryHandle);
38  unset($this->_queryHandle);
39  }
40  }
41 
45  function connect($database='', $host='', $user='', $password='', $charset='AL32UTF8')
46  {
47  if (func_num_args()==0)
48  {
49  if ($GLOBALS['egotec_conf']['oci']) {
50  $GLOBALS['egotec_conf']['db'] = $GLOBALS['egotec_conf']['oci'];
51  }
53  { // Nur die EGOTEC Standardverbindung wird global gespeichert.
54  $this->_dbHandle = Ego_Sql_oci::$_staticOciHandle;
55  $this->_user = strtoupper($GLOBALS['egotec_conf']['db']['user']);
56  } else {
57  $this->_dbHandle = oci_pconnect( // Der pconnect erscheint instabil.
58  $GLOBALS['egotec_conf']['db']['user'],
59  $GLOBALS['egotec_conf']['db']['password'],
60  $GLOBALS['egotec_conf']['db']['database'],
61  'AL32UTF8' // UTF8 ist bei Oracle eigentlich CESU8 und soll nicht verwendet werden.
62  );
63  Ego_Sql_oci::$_staticOciHandle = $this->_dbHandle; // Nur die EGOTEC Standardverbindung wird global gespeichert.
64  $this->_user = strtoupper($GLOBALS['egotec_conf']['db']['user']);
65  }
66  } else {
67  $this->_dbHandle = oci_connect($user, $password, $database, $charset);
68  $this->_user = strtoupper($user);
69  if ( !$this->_dbHandle )
70  { // abwärtskompatibilität bis 4.2.25, die Parameter wurden in der falschen Reihenfolge übergeben.
71  $this->_dbHandle = oci_connect($host, $user, $password, $database);
72  $this->_user = strtoupper($host);
73  }
74  if ( !$this->_dbHandle )
75  { // Wenn der DB Handle nicht besteht wird eine Exception geworfen.
76  throw new Ego_Sql_Exception( 'Konnte keine Datenbankverbindung herstellen.', Ego_Sql_Exception::CONNECT_ERROR );
77  }
78  }
79  }
80 
87  function query($query, $appendCurrent = false)
88  {
89  // SQL compatibility
90  if (strpos($query, 'LEFT(')!==false) {
91  $query = preg_replace('#left\‍((.*?),(.*?)\‍)#i', 'substr($1,0,$2)', $query);
92  }
93 
94  set_time_limit(0);
95  if ($appendCurrent) {
96  $this->currentQuery .= $query;
97  } else {
98  $this->currentQuery = $query;
99  }
100  $this->currentBind = array();
101  $this->_queryHandle = oci_parse($this->_dbHandle, $query);
102  if ($this->_queryHandle===false || !@oci_execute($this->_queryHandle, $this->_mode))
103  { // Die Abfrage ist fehlgeschlagen.
104  $this->_halt();
105  }
106  oci_set_prefetch($this->_queryHandle, 100); // So viele Ergebnisse wie möglich übertragen.
107  if (!$this->_transaction) {
108  oci_commit($this->_dbHandle);
109  }
110  $this->_currentRow = -1;
111  return $this->_queryHandle;
112  }
113 
120  private function _prepare($query)
121  {
122  $this->currentBind = array();
123  $index = 0;
124  while (($pos=strpos($query, '?'))!==false) // Das erste Fragezeichen suchen
125  {
126  $query = substr($query, 0, $pos).':'.($index++).substr($query, $pos+1); // und ersetzen.
127  }
128  $this->currentQuery = $query;
129  return oci_parse($this->_dbHandle, $query);
130  }
131 
139  private function _execute($queryHandle, $values, $blobs=array())
140  {
141  $this->currentBind = $values;
142  $index = 0;
143  foreach ($values as $key => $val)
144  { // Da der letzte Parameter als Referenz übergeben wird, kann nicht $val angegeben werden.
145  oci_bind_by_name($queryHandle, ':'.$index++, $values[$key]);
146  }
147  if (!$blobs)
148  {
149  if (!@oci_execute($queryHandle, $this->_mode))
150  {
151  $this->_halt();
152  }
153  if (!$this->_transaction)
154  {
155  oci_commit($this->_dbHandle);
156  }
157  } else {
158  $descriptors = array();
159  foreach ($blobs as $key => $val)
160  { // LOBs binden.
161  $descriptors[$key] = oci_new_descriptor($this->_dbHandle, OCI_D_LOB);
162  oci_bind_by_name($queryHandle, ':'.$index++, $descriptors[$key], -1, OCI_B_CLOB);
163  }
164  if (!oci_execute($queryHandle, OCI_DEFAULT)) // Noch kein Commit, deswegen wird OCI_DEFAULT übergeben.
165  {
166  $this->_halt();
167  }
168  foreach ($blobs as $key => $val)
169  { // LOBs Inhalte übertragen.
170  $descriptors[$key]->write($val);
171  }
172  if (!$this->_transaction)
173  {
174  oci_commit($this->_dbHandle);
175  }
176  foreach ($descriptors as $descriptor)
177  { // LOBs freigeben.
178  $descriptor->free();
179  }
180  }
181  $error = oci_error($queryHandle);
182  if (!$queryHandle || $error)
183  { // Die Abfrage ist fehlgeschlagen.
184  $this->_halt();
185  }
186  $this->_currentRow = -1;
187  return $queryHandle;
188  }
189 
202  function dropTable($table)
203  {
204  $this->query('DROP TABLE "'.strtoupper($table).'"');
205  }
206 
212  protected function _nextRecord()
213  {
214  if (!$this->_queryHandle)
215  {
216  return $this->Record = null;
217  }
218  $this->_currentRow++;
219  if ($this->_limit<0 || $this->_limit--)
220  { // Ein Limit beachten.
221  $this->Record = oci_fetch_assoc($this->_queryHandle);
222  if ($this->Record)
223  {
224  foreach ($this->Record as $key => $val)
225  {
226  if (is_object($val))
227  {
228  $this->Record[$key] = $val->load();
229  }
230  }
231  if ($this->_lobStm)
232  {
233  oci_bind_by_name($this->_lobStm, ':'.$this->_lobStmId, $this->Record[strtoupper($this->_lobStmId)]);
234  oci_execute($this->_lobStm, $this->_mode);
235  $lob_record = oci_fetch_assoc($this->_lobStm);
236  if (!$lob_record)
237  {
238  return $this->Record = array();
239  }
240  foreach ($lob_record as $key=>$val)
241  {
242  if (is_object($val))
243  {
244  $this->Record[$key] = $val->load();
245  }
246  }
247  }
248  return $this->Record = array_change_key_case($this->Record);
249  }
250  }
251  return $this->Record = null;
252  }
253 
259  protected function _numRecords()
260  {
261  if ($this->_queryHandle)
262  { // Oracle kennt kein num_rows. Diese Funktion muss daher simuliert werden.
263  $GLOBALS['stats']['db_select']++;
264  $query = 'SELECT COUNT(*) FROM ('.$this->currentQuery.')';
265  $statement = oci_parse($this->_dbHandle, $query);
266  if ($this->currentBind)
267  {
268  foreach ($this->currentBind as $key => $val)
269  {
270  oci_bind_by_name($statement, $key, $this->currentBind[$key]);
271  }
272  }
273  oci_execute($statement, $this->_mode);
274  $row = oci_fetch_row($statement);
275  oci_free_statement($statement);
276  return (int)$row[0]; // unter Oracle wird sonst ein String ausgegeben
277  } else {
278  return 0;
279  }
280  }
281 
290  protected function _selectString($query)
291  {
292  if ($query['bitand']) {
293  if (empty($query['where'])) {
294  $query['where'] = '1=1';
295  }
296  foreach ($query['bitand'] as $bit) {
297  $query['where'] .= ' AND BITAND('.$bit[0].', '.$bit[1].') = '.$bit[2];
298  }
299  }
300  return 'SELECT '.($query['distinct']?'DISTINCT ':'')
301  .($query['fields']?:'*').(isset($query['fields2'])&&!empty($query['fields2'])?','.(is_array($query['fields2'])?implode(',',$query['fields2']):$query['fields2']):'')
302  ."\nFROM ".($query['table']?:$query['from'])
303  .($query['table2']?($query['table']?',':'').$query['table2']:'')
304  .($query['inner']?' INNER JOIN '.(is_array($query['inner'])?
305  implode(' INNER JOIN ', $query['inner']):$query['inner']):'')
306  .($query['join']?"\nLEFT JOIN ".(is_array($query['join'])?
307  implode("\nLEFT JOIN ", $query['join']):$query['join']):'')
308  .($query['where']?"\nWHERE ".$query['where']:'')
309  .($query['group']?"\nGROUP BY ".$query['group']:'')
310  .($query['having']?' HAVING '.$query['having']:'')
311  .($query['order']?"\nORDER BY ".$query['order']:'');
312  }
313 
322  protected function _select($query)
323  {
324  if ($query['field_as'])
325  {
326  foreach ($query['field_as'] as $k=>$v)
327  {
328  if ($query['field_as_bitand'][$k]) {
329  foreach ($query['field_as_bitand'][$k] as $bit) {
330  $v .= ' AND BITAND('.$bit[0].','.$bit[1].')='.$bit[2];
331  }
332  }
333  $query['fields2'][] = '('.$v.') AS '.$k;
334  }
335  unset($query['field_as']);
336  }
337 
338  unset($this->_lobStm);
339  if (($query['fields']=='id' || substr($query['fields'], 0, 3)=='id,') && $query['table'])
340  { // LOBs Umgehung.
341  $tables = explode(',', $query['table']);
342  $query['fields'] = $tables[sizeof($tables)-1].'.*';
343  }
344  if ($query['fields'] && $query['fields'][strlen($query['fields'])-5]=='_'
345  && substr($query['fields'], strlen($query['fields'])-2)=='.*')
346  {
347  $lob_table = substr($query['fields'], 0, strlen($query['fields'])-2);
348  $query['fields'] = $lob_table.'.id,'.$lob_table.'.name,'.$lob_table.'.title,'.$lob_table.'.url,'.$lob_table.'.short,'.
349  $lob_table.'.a_date,'.$lob_table.'.c_date,'.$lob_table.'.m_date,'.$lob_table.'.a_user,'.$lob_table.'.c_user,'.$lob_table.'.m_user,'.
350  $lob_table.'.type,'.$lob_table.'.children_order,'.$lob_table.'.order_field,'.
351  $lob_table.'.nav_hide,'.$lob_table.'.inactive,'.$lob_table.'.cache,'.$lob_table.'.release_from,'.$lob_table.'.release_until,'.
352  $lob_table.'.workflow,'.$lob_table.'.workflow_state,'.$lob_table.'.deleted';
353  $lob_query = 'select content, extra from '.$lob_table.' where id=:id';
354  $this->_lobStm = oci_parse($this->_dbHandle, $lob_query);
355  $this->_lobStmId = 'id';
356  }
357  if (
358  strpos($query['fields'], 'egotec_user.*')===0 ||
359  ( $query['union'] && $query['union'][0]['fields']=='egotec_user.*' )
360  ) {
361  $lob_table='egotec_user';
362  if ($query['union'])
363  {
364  for($i=0; $i<sizeof($query['union']); $i++)
365  {
366  $query['union'][$i]['fields'] = $lob_table.'.user_id,'.$lob_table.'.username,'.
367  $lob_table.'.email,'.$lob_table.'.sid,'.
368  $lob_table.'.multiple_login,'.$lob_table.'.last_login,'.$lob_table.'.no_admin,'.$lob_table.'.user_c_date';
369  }
370  } else {
371  $add_fields = substr($query['fields'], strlen('egotec_user.*'));
372  $query['fields'] = $lob_table.'.user_id,'.$lob_table.'.username,'.
373  $lob_table.'.email,'.$lob_table.'.sid,'.
374  $lob_table.'.multiple_login,'.$lob_table.'.last_login,'.$lob_table.'.no_admin,'.$lob_table.'.user_c_date'.
375  $add_fields;
376  }
377  $lob_query = 'select extra from '.$lob_table.' where user_id=:user_id';
378  $this->_lobStm = oci_parse($this->_dbHandle, $lob_query);
379  $this->_lobStmId = 'user_id';
380  }
381 
382  $query['where'] = str_replace("\\'", "''", $query['where']); // @todo Diese Zeile wird mit prepared Statements unnötig.
383 
384  $this->_cached = false;
385  if ($query['union'])
386  {
387  $unions = array();
388  $order = $query['order'];
389  unset($query['order']); // Oracle erlaubt kein order in den selects.
390  if (!$query['fields'] || $query['fields']=='*' || $query['fields']==$query['table'].'.*')
391  {
392  $order = str_replace(
393  array('id', 'name', 'a_date', 'c_date', 'type', 'order_field', 'workflow_state'),
394  array(0, 2, 6, 7, 10, 12, 19),
395  $order
396  );
397  } elseif ($order) {
398  $fields = $query['fields'].($query['fields2']?(is_array($query['fields2'])?implode(',',$query['fields2']):','.$query['fields2']):'');
399  $fields = preg_replace('#\‍(SELECT.*?\‍) AS ([^,]+)#ims', "\\1", $fields);
400  $fields = explode(',', $fields);
401  array_unshift($fields, '#####'); // Zählung beginnt bei 1 und Indizes in Arrays bei 0.
402  $order = str_replace(array_values($fields), array_keys($fields), $order);
403  }
404  foreach ($query['union'] as $union)
405  {
406  $union_join = $union['join']; // Falls union[join] leer ist,
407  $union = array_merge($query, $union); // steht nach dem array_merge das query[join] drin und dann wird das verdoppelt. Das darf nicht sein.
408  $union['join'] = $union_join?$union_join:array();
409  $union['where'] = $query['where']?'('.$query['where'].') AND ('.$union['where'].')':$union['where'];
410  $union['join'] = $query['join']?array_merge_recursive($query['join'], $union['join']):$union['join'];
411  $unions[] = $this->_selectString($union);
412  }
413  $s = implode("\nUNION\n", $unions).($order?"\nORDER BY ".$order:'');
414  } else {
415  $s = $this->_selectString($query);
416  }
417 
418  if ($query['bind'])
419  {
420  foreach ($query['bind'] as $k=>$v)
421  {
422  $s = str_replace(':'.$k, (!in_array($k, ['name','path','s','u','username','dir']) && is_numeric($v))?$v:'\''.str_replace(array('\\', '\''), array('\\\\', '\'\''), $v).'\'', $s);
423  }
424  $return = $this->query($s);
425  } else {
426  $return = $this->query($s);
427  }
428 
429  if ($query['limit'])
430  { // Limitbehandlung
431  $limit = explode(',', $query['limit']);
432  if (sizeof($limit)>1)
433  {
434  $offset = $limit[0];
435  while ($offset>0)
436  { // Die ersten $offset Einträge ignorieren.
437  if (!oci_fetch($this->_queryHandle))
438  {
439  unset($this->_queryHandle);
440  $return = false;
441  break;
442  }
443  $offset--;
444  }
445  $this->_limit = $limit[1];
446  } else {
447  $this->_limit = $limit[0];
448  }
449  } else {
450  $this->_limit = -1;
451  }
452  return $return;
453  }
454 
455  function delete($query)
456  {
457  assert($query['table']||$query['from']);
458  $s = 'DELETE '
459  .($query['fields']?$query['fields'].' ':'')
460  .'FROM '
461  .($query['table']?$query['table']:$query['from'])
462  .($query['inner']?' INNER JOIN '.(is_array($query['inner'])?
463  implode(' INNER JOIN ', $query['inner']):$query['inner']):'')
464  .($query['join']?' LEFT JOIN '.(is_array($query['join'])?
465  implode(' LEFT JOIN ', $query['join']):$query['join']):'')
466  .($query['where']?' WHERE '.$query['where']:'');
467  if ($query['bind'])
468  {
469  foreach ($query['bind'] as $k=>$v)
470  {
471  $s = str_replace(':'.$k, ($k!='name' && $k!='u' && $k!='s' && $k != 'dir' && is_numeric($v))?$v:'\''.str_replace(array('\\', '\''), array('\\\\', '\'\''), $v).'\'', $s);
472  }
473  }
474  return $this->query($s);
475  }
476 
495  function insert($query)
496  {
497  if (isset($query['key']))
498  {
499  $query['set'] = array_combine($query['key'], $query['val']);
500  }
501  $keys = array();
502  $values = array();
503  $names = array();
504  if (isset($query['set']))
505  {
506  foreach ($query['set'] as $k => $v)
507  {
508  switch ($k)
509  {
510  case 'content':
511  case 'extra':
512  $keys[] = strtoupper($k);
513  $names[] = 'EMPTY_CLOB()';
514  $blobs[$k] = $v;
515  break;
516  default:
517  $names[] = '?';
518  $keys[] = strtoupper($k);
519  $values[] = $v;
520  }
521  }
522  }
523  if ($query['replace'])
524  { // Einen eventuell vorhandenen Eintrag löschen.
525  if (empty($query['primary'])) {
526  $query['primary'] = array_keys($this->getPrimary($query['table']));
527  }
528  $where = array();
529  foreach ($keys as $key)
530  {
531  if (in_array(strtolower($key), $query['primary']))
532  {
533  $val = $query['set'][strtolower($key)];
534  $where[] = $key.'='.(strtolower($key)!='dir' && is_numeric($val)?$val:"'$val'");
535  }
536  }
537  $this->delete(array('table' => $query['table'], 'where' => implode(' AND ', $where)));
538  }
539  $insert_table = $query['table'].'-'.sizeof($keys).'-'.$query['replace'];
540  if ($this->_insertTable != $insert_table) {
541  $this->currentQuery = $query;
542  $s = 'INSERT INTO '.$query['table'].' ("'.implode('","', $keys).'") VALUES ('.implode(',', $names).')';
543  if ($blobs) {
544  $s.= ' RETURNING '.implode(',', array_keys($blobs)).' INTO ?'.str_repeat(',?', sizeof($blobs)-1);
545  }
546  $this->_insertstmt = $this->_prepare($s);
547  $this->_insertTable = $insert_table;
548  }
549  $this->currentQuery = print_r($query, true);
550  return $this->_execute($this->_insertstmt, $values, $blobs);
551  }
552 
559  function update($query)
560  {
561  if (isset($query['key']))
562  {
563  $query['set'] = array_combine($query['key'], $query['val']);
564  }
565  $set = array();
566  $set2 = array();
567  $values = array();
568  if (isset($query['set']))
569  {
570  foreach ($query['set'] as $k => $v)
571  {
572  switch ($k)
573  {
574  case 'content':
575  case 'extra':
576  $set2[] = $k.'=EMPTY_CLOB()';
577  $blobs[$k] = $v;
578  break;
579  default:
580  $set[] = $k.'=?';
581  $values[] = $v;
582  }
583  }
584  }
585  if (isset($query['set_f']))
586  {
587  foreach ($query['set_f'] as $k => $v)
588  {
589  $set[] = $k.'='.$v;
590  }
591  }
592  $set = array_merge($set, $set2);
593  $s = 'UPDATE '.$query['table'].' SET '.implode(',', $set).($query['where']?' WHERE '.$query['where']:'');
594  if ($blobs)
595  {
596  $s.= ' RETURNING '.implode(',', array_keys($blobs)).' INTO ?'.str_repeat(',?', sizeof($blobs)-1);
597  }
598  if ($query['bind'])
599  {
600  foreach ($query['bind'] as $k=>$v)
601  {
602  $s = str_replace(':'.$k, ($k!='name' && $k!='u' && $k!='s' && $k!='dir' && is_numeric($v))?$v:'\''.str_replace(array('\\', '\''), array('\\\\', '\'\''), $v).'\'', $s);
603  }
604  }
605  $this->_queryHandle = $this->_prepare($s);
606  return $this->_execute($this->_queryHandle, $values, $blobs);
607  }
608 
609  private function _changeType($type)
610  {
611  $type = str_replace('/*bin*/', '', $type);
612  $type_array = explode(' ', $type);
613 
614  switch ($type_array[0])
615  { // Typkonvertierung
616  case 'bigint':
617  case 'tinyint':
618  $type = 'int';
619  break;
620  case 'datetime':
621  $type = 'char(19)';
622  break;
623  case 'enum':
624  $type = 'varchar2(255)';
625  break;
626  case 'varchar(255)':
627  $type = 'varchar2(500)'; // Oracle zählt auch in UTF-8 in Bytes!
628  break;
629  case 'smalltext':
630  $type = 'varchar2(3900)';
631  break;
632  case 'text':
633  $type = 'clob';
634  }
635  return $type;
636  }
637 
651  function createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
652  {
653  if ($drop_flag && $this->tableExists($table)) // Wenn die Tabelle existiert,
654  {
655  $this->query('DROP TABLE '.strtoupper($table)); // dann wird diese gelöscht.
656  }
657 
658  if ($notexists_flag && $this->tableExists($table)) // Wenn die Tabelle existiert,
659  {
660  return; // dann wird nichts gemacht.
661  }
662 
663  $query = array();
664  $index = array();
665  foreach ($struct as $name => $type)
666  {
667  $key = explode(' ', $name);
668  $type = $this->_changeType($type);
669  if (sizeof($key)==1)
670  { // Ein Feld erzeugen.
671  $query[] = '"'.$name.'" '.$type;
672  } else {
673  $val = preg_replace('/\‍([^)]+\‍)/', '', $type);
674 
675  if ($table=='egotec_url' && $key[0]=='PRIMARY') {
676  $key[0] = 'KEY'; // Bei der egotec_url kann bei Oracle kein Primary Key gesetzt werden, da dir auch leer sein kann.
677  }
678 
679  switch ($key[0])
680  {
681  case 'PRIMARY':
682  $query[] = 'PRIMARY KEY ("'.str_replace(',', '","', $val).'")';
683  break;
684  case 'KEY':
685  $index_name = $table.'_'.$key[1];
686  if (strlen($index_name)>24)
687  { // Namen von Indizes dürfen in Oracle höchstens 24 Zeichen lang sein.
688  $index_name_array = explode('_', $index_name);
689  $index_name_short = '';
690  $part_length = (integer)(24/sizeof($index_name_array));
691  foreach ($index_name_array as $index_name_part)
692  {
693  $index_name_short.= substr($index_name_part, 0, $part_length);
694  }
695  $index_name = $index_name_short.substr(md5($index_name), 0, 24-strlen($index_name_short));
696  }
697  $index[] = 'CREATE INDEX '.$index_name.' ON '.$table.' ("'.str_replace(',', '","', $val).'") NOLOGGING'; // testImport 18.6s > 16.3s
698  break;
699  case 'UNIQUE':
700  $query[] = 'UNIQUE ("'.$type.'")';
701  }
702  }
703  }
704  $s = strtoupper('create table '.$table.' ('.implode(',', $query).')');
705  $this->query($s);
706  foreach ($index as $query)
707  {
708  $this->query(strtoupper($query));
709  }
710  }
711 
712  function alterTable($table, $struct)
713  {
714  $query = array();
715  foreach ($struct as $name => $type)
716  {
717  $key = explode(' ', $name);
718  $type = $this->_changeType($type);
719  if (sizeof($key)==1)
720  { // Ein Feld erzeugen.
721  $query[] = 'ADD '.$name.' '.$type;
722  } else {
723  switch ($key[0])
724  {
725  case 'PRIMARY':
726  $query[] = 'ADD PRIMARY KEY ('.$type.')';
727  break;
728  case 'KEY':
729  $query[] = 'ADD KEY '.$key[1].' ('.$type.')';
730  break;
731  case 'UNIQUE':
732  $query[] = 'ADD UNIQUE KEY '.$key[1].' ('.$type.')';
733  break;
734  case 'CHANGE':
735  if ($key[1] == $key[2]) {
736  $query[] = 'MODIFY '.$key[1].' ('.$type.')';
737  } else {
738  $query[] = 'RENAME '.$key[1].' TO '.$key[2];
739  }
740  }
741  }
742  }
743  foreach ($query as $q) {
744  $q = 'ALTER TABLE '.$table.' '.$q;
745  $this->query($q);
746  }
747  return true;
748  }
749 
753  private function _halt()
754  {
755  if ($this->_queryHandle)
756  {
757  $error = oci_error($this->_queryHandle);
758  } else {
759  $error = oci_error();
760  }
761  $error_str = print_r($error, true);
762  throw new Ego_Sql_Exception($this->currentQuery."\n".$error_str, Ego_Sql_Exception::QUERY_ERROR);
763  }
764 
768  function begin()
769  {
770  oci_commit($this->_dbHandle);
771  $this->_transaction = true;
772  }
773 
777  function commit()
778  {
779  $this->_transaction = false;
780  oci_commit($this->_dbHandle);
781  }
782 
786  function rollback()
787  {
788  if ($this->_transaction) { // Die Transaktion nur zurücksetzen wenn eine gestartet wurde.
789  oci_rollback($this->_dbHandle);
790  $this->_transaction = false;
791  }
792  }
793 
806  public function showColumns($table)
807  {
808  if (!$table) return array();
809  $table = strtoupper($table);
810  $stmt = $this->query("SELECT COLUMN_NAME, DATA_TYPE FROM user_tab_columns WHERE TABLE_NAME ='$table'");
811  $cols = array();
812  while($record = oci_fetch_assoc($stmt))
813  {
814  $type = strtolower($record['DATA_TYPE']);
815  switch ($type) {
816  case 'number':
817  $type = 'int(11)';
818  break;
819  case 'varchar2':
820  $type = 'varchar(255)';
821  break;
822  case 'char':
823  $type = 'char(32)';
824  break;
825  }
826  $cols[] = array(
827  'field' => strtolower($record['COLUMN_NAME']),
828  'type' => $type
829  );
830  }
831  oci_free_statement($stmt);
832  return $cols;
833  }
834 
850  public function tableExists($table)
851  {
852  if (!$table) {
853  return false;
854  }
855  /*$this->select(array(
856  'table' => 'all_tables',
857  'where' => 'table_name=:table AND owner=:owner',
858  'bind' => array(
859  'table' => strtoupper($table),
860  'owner' => $this->_user
861  )
862  ));*/
863  $this->select(array(
864  'table' => 'all_tables',
865  'where' => 'table_name=:table',
866  'bind' => array(
867  'table' => strtoupper($table)
868  )
869  ));
870  return (bool)$this->nextRecord();
871  }
872 
879  function getPrimary($table)
880  {
881  $db = clone $this;
882  $db->select(array(
883  'fields' => 'cols.column_name,cols2.data_type',
884  'table' => 'all_constraints cons, all_cons_columns cols, all_tab_columns cols2',
885  'where' => 'cols.table_name=\''.strtoupper($table).'\' '.
886  'AND cons.constraint_type=\'P\' '.
887  'AND cons.constraint_name = cols.constraint_name '.
888  'AND cons.owner = cols.owner '.
889  'AND cols2.table_name=cols.table_name AND cols2.column_name=cols.column_name',
890  'order' => 'cols.position'
891  ));
892  $primary = array();
893  while ($db->nextRecord())
894  {
895  if (strpos($db->Record['data_type'], 'INT')!==false || $db->Record['data_type']=='NUMBER')
896  {
897  $type = 'int';
898  } else {
899  $type = 'string';
900  }
901  $primary[strtolower($db->Record['column_name'])] = $type;
902  }
903  return $primary;
904  }
905 
911  function optimize($table)
912  {
913  require_once('base/Ego_System.php');
915  $s = 'ANALYZE TABLE '.$table.' COMPUTE STATISTICS';
916  $handle = oci_parse($this->_dbHandle, $s);
917  oci_execute($handle);
919  }
920 
921 
938  public function getTables($like = '') {
939  if ($like) {
940  /*$this->select(array(
941  'table' => 'all_tables',
942  'where' => "TABLE_NAME LIKE :whatnames AND owner=:owner",
943  'bind' => array(
944  'owner' => $this->_user,
945  'whatnames' => strtoupper($like.'%')
946  )
947  ));*/
948  $this->select(array(
949  'table' => 'all_tables',
950  'where' => "TABLE_NAME LIKE :whatnames",
951  'bind' => array(
952  'whatnames' => strtoupper($like.'%')
953  )
954  ));
955  } else {
956  /*$this->select(array(
957  'table' => 'all_tables',
958  'where' => "owner=:owner",
959  'bind' => array(
960  'owner' => $this->_user
961  )
962  ));*/
963  $this->select(array(
964  'table' => 'all_tables'
965  ));
966  }
967 
968  $all_table = array();
969  $count = 0;
970 
971  while($table = $this->nextRecord()) {
972  $all_table[$count++] = array_pop($this->Record);
973  }
974  return $all_table;
975  }
976 
980  protected function _rewind()
981  {
982  $this->query($this->currentQuery);
983  }
984 
989  function getVersion() {
990  return oci_server_version($this->_dbHandle);
991  }
992 }
993 ?>
select($query, $cache=false)
alterTable($table, $struct)
createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
getTables($like='')
dropTable($table)
optimize($table)
_select($query)
insert($query)
connect($database='', $host='', $user='', $password='', $charset='AL32UTF8')
Definition: Ego_Sql_oci.php:45
query($query, $appendCurrent=false)
Definition: Ego_Sql_oci.php:87
_selectString($query)
update($query)
getPrimary($table)
static $_staticOciHandle
Definition: Ego_Sql_oci.php:17
tableExists($table)
showColumns($table)
static eternalCache($active, $clear=true)