ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
Common.php
Go to the documentation of this file.
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP versions 4 and 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
6 // | Stig. S. Bakken, Lukas Smith |
7 // | All rights reserved. |
8 // +----------------------------------------------------------------------+
9 // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
10 // | API as well as database abstraction for PHP applications. |
11 // | This LICENSE is in the BSD license style. |
12 // | |
13 // | Redistribution and use in source and binary forms, with or without |
14 // | modification, are permitted provided that the following conditions |
15 // | are met: |
16 // | |
17 // | Redistributions of source code must retain the above copyright |
18 // | notice, this list of conditions and the following disclaimer. |
19 // | |
20 // | Redistributions in binary form must reproduce the above copyright |
21 // | notice, this list of conditions and the following disclaimer in the |
22 // | documentation and/or other materials provided with the distribution. |
23 // | |
24 // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
25 // | Lukas Smith nor the names of his contributors may be used to endorse |
26 // | or promote products derived from this software without specific prior|
27 // | written permission. |
28 // | |
29 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
30 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
31 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
32 // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
33 // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
34 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
35 // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
36 // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
37 // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
38 // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
39 // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
40 // | POSSIBILITY OF SUCH DAMAGE. |
41 // +----------------------------------------------------------------------+
42 // | Author: Lukas Smith <smith@pooteeweet.org> |
43 // +----------------------------------------------------------------------+
44 //
45 // $Id: Common.php,v 1.35 2007/02/25 11:14:34 quipo Exp $
46 //
47 
59 define('MDB2_TABLEINFO_ORDER', 1);
60 define('MDB2_TABLEINFO_ORDERTABLE', 2);
61 define('MDB2_TABLEINFO_FULL', 3);
62 
71 {
72  // }}}
73  // {{{ getTableFieldDefinition()
74 
86  function getTableFieldDefinition($table, $field)
87  {
88  $db =& $this->getDBInstance();
89  if (PEAR::isError($db)) {
90  return $db;
91  }
92 
93  return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
94  'method not implemented', __FUNCTION__);
95  }
96 
97  // }}}
98  // {{{ getTableIndexDefinition()
99 
120  function getTableIndexDefinition($table, $index)
121  {
122  $db =& $this->getDBInstance();
123  if (PEAR::isError($db)) {
124  return $db;
125  }
126 
127  return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
128  'method not implemented', __FUNCTION__);
129  }
130 
131  // }}}
132  // {{{ getTableConstraintDefinition()
133 
155  function getTableConstraintDefinition($table, $index)
156  {
157  $db =& $this->getDBInstance();
158  if (PEAR::isError($db)) {
159  return $db;
160  }
161 
162  return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
163  'method not implemented', __FUNCTION__);
164  }
165 
166  // }}}
167  // {{{ getSequenceDefinition()
168 
182  function getSequenceDefinition($sequence)
183  {
184  $db =& $this->getDBInstance();
185  if (PEAR::isError($db)) {
186  return $db;
187  }
188 
189  $start = $db->currId($sequence);
190  if (PEAR::isError($start)) {
191  return $start;
192  }
193  if ($db->supports('current_id')) {
194  $start++;
195  } else {
196  $db->warnings[] = 'database does not support getting current
197  sequence value, the sequence value was incremented';
198  }
199  $definition = array();
200  if ($start != 1) {
201  $definition = array('start' => $start);
202  }
203  return $definition;
204  }
205 
206  // }}}
207  // {{{ getTriggerDefinition()
208 
235  function getTriggerDefinition($trigger)
236  {
237  $db =& $this->getDBInstance();
238  if (PEAR::isError($db)) {
239  return $db;
240  }
241 
242  return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
243  'method not implemented', __FUNCTION__);
244  }
245 
246  // }}}
247  // {{{ tableInfo()
248 
369  function tableInfo($result, $mode = null)
370  {
371  $db =& $this->getDBInstance();
372  if (PEAR::isError($db)) {
373  return $db;
374  }
375 
376  if (!is_string($result)) {
377  return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
378  'method not implemented', __FUNCTION__);
379  }
380 
381  $db->loadModule('Manager', null, true);
382  $fields = $db->manager->listTableFields($result);
383  if (PEAR::isError($fields)) {
384  return $fields;
385  }
386 
387  $flags = array();
388 
389  $idxname_format = $db->getOption('idxname_format');
390  $db->setOption('idxname_format', '%s');
391 
392  $indexes = $db->manager->listTableIndexes($result);
393  if (PEAR::isError($indexes)) {
394  $db->setOption('idxname_format', $idxname_format);
395  return $indexes;
396  }
397 
398  foreach ($indexes as $index) {
399  $definition = $this->getTableIndexDefinition($result, $index);
400  if (PEAR::isError($definition)) {
401  $db->setOption('idxname_format', $idxname_format);
402  return $definition;
403  }
404  if (count($definition['fields']) > 1) {
405  foreach ($definition['fields'] as $field => $sort) {
406  $flags[$field] = 'multiple_key';
407  }
408  }
409  }
410 
411  $constraints = $db->manager->listTableConstraints($result);
412  if (PEAR::isError($constraints)) {
413  return $constraints;
414  }
415 
416  foreach ($constraints as $constraint) {
417  $definition = $this->getTableConstraintDefinition($result, $constraint);
418  if (PEAR::isError($definition)) {
419  $db->setOption('idxname_format', $idxname_format);
420  return $definition;
421  }
422  $flag = !empty($definition['primary'])
423  ? 'primary_key' : (!empty($definition['unique'])
424  ? 'unique_key' : false);
425  if ($flag) {
426  foreach ($definition['fields'] as $field => $sort) {
427  if (empty($flags[$field]) || $flags[$field] != 'primary_key') {
428  $flags[$field] = $flag;
429  }
430  }
431  }
432  }
433 
434  if ($mode) {
435  $res['num_fields'] = count($fields);
436  }
437 
438  foreach ($fields as $i => $field) {
439  $definition = $this->getTableFieldDefinition($result, $field);
440  if (PEAR::isError($definition)) {
441  $db->setOption('idxname_format', $idxname_format);
442  return $definition;
443  }
444  $res[$i] = $definition[0];
445  $res[$i]['name'] = $field;
446  $res[$i]['table'] = $result;
447  $res[$i]['type'] = preg_replace('/^([a-z]+).*$/i', '\\1', trim($definition[0]['nativetype']));
448  // 'primary_key', 'unique_key', 'multiple_key'
449  $res[$i]['flags'] = empty($flags[$field]) ? '' : $flags[$field];
450  // not_null', 'unsigned', 'auto_increment', 'default_[rawencodedvalue]'
451  if (!empty($res[$i]['notnull'])) {
452  $res[$i]['flags'].= ' not_null';
453  }
454  if (!empty($res[$i]['unsigned'])) {
455  $res[$i]['flags'].= ' unsigned';
456  }
457  if (!empty($res[$i]['auto_increment'])) {
458  $res[$i]['flags'].= ' autoincrement';
459  }
460  if (!empty($res[$i]['default'])) {
461  $res[$i]['flags'].= ' default_'.rawurlencode($res[$i]['default']);
462  }
463 
464  if ($mode & MDB2_TABLEINFO_ORDER) {
465  $res['order'][$res[$i]['name']] = $i;
466  }
467  if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
468  $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
469  }
470  }
471 
472  $db->setOption('idxname_format', $idxname_format);
473  return $res;
474  }
475 }
476 ?>