ILIAS  release_4-4 Revision
ilDBAnalyzer Class Reference

This class gives all kind of DB information using the MDB2 manager and reverse module. More...

+ Collaboration diagram for ilDBAnalyzer:

Public Member Functions

 __construct ()
 Constructor. More...
 
 getFieldInformation ($a_table, $a_remove_not_allowed_attributes=false)
 Get field information of a table. More...
 
 getBestDefinitionAlternative ($a_def)
 
 getAutoIncrementField ($a_table)
 Gets the auto increment field of a table. More...
 
 getPrimaryKeyInformation ($a_table)
 Get primary key of a table. More...
 
 getIndicesInformation ($a_table, $a_abstract_table=false)
 Get information on indices of a table. More...
 
 getConstraintsInformation ($a_table, $a_abstract_table=false)
 Get information on constraints of a table. More...
 
 hasSequence ($a_table)
 Check whether sequence is defined for current table (only works on "abstraced" tables) More...
 

Detailed Description

This class gives all kind of DB information using the MDB2 manager and reverse module.

Author
Alex Killing alex..nosp@m.kill.nosp@m.ing@g.nosp@m.mx.d.nosp@m.e
Version
Id
class.ilDBUpdate.php 18649 2009-01-21 09:59:23Z akill

Definition at line 13 of file class.ilDBAnalyzer.php.

Constructor & Destructor Documentation

◆ __construct()

ilDBAnalyzer::__construct ( )

Constructor.

Definition at line 19 of file class.ilDBAnalyzer.php.

20  {
21  global $ilDB;
22 
23  $this->manager = $ilDB->db->loadModule('Manager');
24  $this->reverse = $ilDB->db->loadModule('Reverse');
25  $this->il_db = $ilDB;
26  $this->allowed_attributes = $ilDB->getAllowedAttributes();
27  }

Member Function Documentation

◆ getAutoIncrementField()

ilDBAnalyzer::getAutoIncrementField (   $a_table)

Gets the auto increment field of a table.

This should be used on ILIAS 3.10.x "MySQL" tables only.

Parameters
stringtable name
Returns
string name of autoincrement field

Definition at line 128 of file class.ilDBAnalyzer.php.

129  {
130  $fields = $this->manager->listTableFields($a_table);
131  $inf = array();
132 
133  foreach ($fields as $field)
134  {
135  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
136  if ($rdef[0]["autoincrement"])
137  {
138  return $field;
139  }
140  }
141  return false;
142  }

◆ getBestDefinitionAlternative()

ilDBAnalyzer::getBestDefinitionAlternative (   $a_def)

Definition at line 92 of file class.ilDBAnalyzer.php.

Referenced by getFieldInformation().

93  {
94  // determine which type to choose
95  $car = array(
96  "boolean" => 10,
97  "integer" => 20,
98  "decimal" => 30,
99  "float" => 40,
100  "date" => 50,
101  "time" => 60,
102  "timestamp" => 70,
103  "text" => 80,
104  "clob" => 90,
105  "blob" => 100);
106 
107  $cur_car = 0;
108  $best_alt = 0; // best alternatice
109  foreach ($a_def as $k => $rd)
110  {
111  if ($car[$rd["type"]] > $cur_car)
112  {
113  $cur_car = $car[$rd["type"]];
114  $best_alt = $k;
115  }
116  }
117 
118  return $best_alt;
119  }
+ Here is the caller graph for this function:

◆ getConstraintsInformation()

ilDBAnalyzer::getConstraintsInformation (   $a_table,
  $a_abstract_table = false 
)

Get information on constraints of a table.

Primary key is NOT included! Fulltext indices are included and marked.

Parameters
stringtable name
Returns
array indices information array

Definition at line 245 of file class.ilDBAnalyzer.php.

246  {
247  $constraints = $this->manager->listTableConstraints($a_table);
248 
249  $cons = array();
250  foreach ($constraints as $c)
251  {
252  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
253 //var_dump($info);
254  $i = array();
255  if ($info["unique"])
256  {
257  $i["name"] = $c;
258  $i["type"] = "unique";
259  foreach ($info["fields"] as $k => $f)
260  {
261  $i["fields"][$k] = array(
262  "position" => $f["position"],
263  "sorting" => $f["sorting"]);
264  }
265  $cons[] = $i;
266  }
267  }
268 
269  return $cons;
270  }

◆ getFieldInformation()

ilDBAnalyzer::getFieldInformation (   $a_table,
  $a_remove_not_allowed_attributes = false 
)

Get field information of a table.

Parameters
stringtable name
Returns
array field information array

Definition at line 36 of file class.ilDBAnalyzer.php.

References getBestDefinitionAlternative().

Referenced by ilSetting\_getValueType().

37  {
38 //echo "<br>-".$a_table."-".$field."-";
39  $fields = $this->manager->listTableFields($a_table);
40  $inf = array();
41  foreach ($fields as $field)
42  {
43 //echo "<br>-".$a_table."-".$field."-";
44  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
45 //var_dump($rdef);
46  // is this possible?
47  if ($rdef["type"] != $rdef["mdb2type"])
48  {
49  echo "ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field";
50  }
51 
52  $best_alt = $this->getBestDefinitionAlternative($rdef);
53 
54  // collect other alternatives
55  reset($rdef);
56  $alt_types = "";
57  foreach ($rdef as $k => $rd)
58  {
59  if ($k != $best_alt)
60  {
61  $alt_types.= $rdef[$k]["type"].$rdef[$k]["length"]." ";
62  }
63  }
64 
65  $inf[$field] = array(
66  "notnull" => $rdef[$best_alt]["notnull"],
67  "nativetype" => $rdef[$best_alt]["nativetype"],
68  "length" => $rdef[$best_alt]["length"],
69  "unsigned" => $rdef[$best_alt]["unsigned"],
70  "default" => $rdef[$best_alt]["default"],
71  "fixed" => $rdef[$best_alt]["fixed"],
72  "autoincrement" => $rdef[$best_alt]["autoincrement"],
73  "type" => $rdef[$best_alt]["type"],
74  "alt_types" => $alt_types,
75  );
76 
77  if ($a_remove_not_allowed_attributes)
78  {
79  foreach ($inf[$field] as $k => $v)
80  {
81  if ($k != "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]]))
82  {
83  unset($inf[$field][$k]);
84  }
85  }
86  }
87  }
88 
89  return $inf;
90  }
getBestDefinitionAlternative($a_def)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getIndicesInformation()

ilDBAnalyzer::getIndicesInformation (   $a_table,
  $a_abstract_table = false 
)

Get information on indices of a table.

Primary key is NOT included! Fulltext indices are included and marked.

Parameters
stringtable name
Returns
array indices information array

Definition at line 180 of file class.ilDBAnalyzer.php.

181  {
182  //$constraints = $this->manager->listTableConstraints($a_table);
183  $indexes = $this->manager->listTableIndexes($a_table);
184 
185  // get additional information if database is MySQL
186  $mysql_info = array();
187  if ($this->il_db->getDBType() == "mysql")
188  {
189  $set = $this->il_db->query("SHOW INDEX FROM ".$a_table);
190  while ($rec = $this->il_db->fetchAssoc($set))
191  {
192  if (!empty ($rec["Key_name"]))
193  {
194  $mysql_info[$rec["Key_name"]] = $rec;
195  }
196  else
197  {
198  $mysql_info[$rec["key_name"]] = $rec;
199  }
200  }
201  }
202 
203  $ind = array();
204  foreach ($indexes as $c)
205  {
206  $info = $this->reverse->getTableIndexDefinition($a_table, $c);
207 
208  $i = array();
209  if (!$info["primary"])
210  {
211  $i["name"] = $c;
212  $i["fulltext"] = false;
213  $suffix = ($a_abstract_table)
214  ? "_idx"
215  : "";
216 
217  if ($mysql_info[$i["name"]]["Index_type"] == "FULLTEXT" ||
218  $mysql_info[$i["name"]."_idx"]["Index_type"] == "FULLTEXT" ||
219  $mysql_info[$i["name"]]["index_type"] == "FULLTEXT" ||
220  $mysql_info[$i["name"]."_idx"]["index_type"] == "FULLTEXT")
221  {
222  $i["fulltext"] = true;
223  }
224  foreach ($info["fields"] as $k => $f)
225  {
226  $i["fields"][$k] = array(
227  "position" => $f["position"],
228  "sorting" => $f["sorting"]);
229  }
230  $ind[] = $i;
231  }
232  }
233 
234  return $ind;
235  }

◆ getPrimaryKeyInformation()

ilDBAnalyzer::getPrimaryKeyInformation (   $a_table)

Get primary key of a table.

Parameters
stringtable name
Returns
array primary key information array

Definition at line 150 of file class.ilDBAnalyzer.php.

Referenced by hasSequence().

151  {
152  $constraints = $this->manager->listTableConstraints($a_table);
153  $pk = false;
154  foreach ($constraints as $c)
155  {
156  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
157  if ($info["primary"])
158  {
159  $pk["name"] = $c;
160  foreach ($info["fields"] as $k => $f)
161  {
162  $pk["fields"][$k] = array(
163  "position" => $f["position"],
164  "sorting" => $f["sorting"]);
165  }
166  }
167  }
168 
169  return $pk;
170  }
+ Here is the caller graph for this function:

◆ hasSequence()

ilDBAnalyzer::hasSequence (   $a_table)

Check whether sequence is defined for current table (only works on "abstraced" tables)

Parameters
stringtable name
Returns
mixed false, if no sequence is defined, start number otherwise

Definition at line 278 of file class.ilDBAnalyzer.php.

References getPrimaryKeyInformation().

279  {
280  $seq = $this->manager->listSequences();
281  if (is_array($seq) && in_array($a_table, $seq))
282  {
283  // sequence field is (only) primary key field of table
284  $pk = $this->getPrimaryKeyInformation($a_table);
285  if (is_array($pk["fields"]) && count($pk["fields"] == 1))
286  {
287  $seq_field = key($pk["fields"]);
288  }
289  else
290  {
291  die("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: ".$a_table.".");
292  }
293 
294  $set = $this->il_db->query("SELECT MAX(`".$seq_field."`) ma FROM `".$a_table."`");
295  $rec = $this->il_db->fetchAssoc($set);
296  $next = $rec["ma"] + 1;
297 
298  return $next;
299  }
300  return false;
301  }
getPrimaryKeyInformation($a_table)
Get primary key of a table.
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: