ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDBAnalyzer.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
16 class ilDBAnalyzer {
17 
21  protected $manager;
25  protected $reverse;
29  protected $il_db;
34 
35 
43  public function __construct(ilDBInterface $ilDBInterface = null) {
44  if (!$ilDBInterface instanceof ilDBInterface) {
45  global $ilDB;
46  $ilDBInterface = $ilDB;
47  }
48 
49  $this->manager = $ilDBInterface->loadModule(ilDBConstants::MODULE_MANAGER);
50  $this->reverse = $ilDBInterface->loadModule(ilDBConstants::MODULE_REVERSE);
51  $this->il_db = $ilDBInterface;
52  $this->allowed_attributes = $ilDBInterface->getAllowedAttributes();
53  }
54 
55 
62  public function getFieldInformation($a_table, $a_remove_not_allowed_attributes = false) {
63  $fields = $this->manager->listTableFields($a_table);
64  $inf = array();
65  foreach ($fields as $field) {
66  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
67  // is this possible?
68  if ($rdef["type"] != $rdef["mdb2type"]) {
69  throw new ilDatabaseException("ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field");
70  }
71 
72  $best_alt = $this->getBestDefinitionAlternative($rdef);
73 
74  // collect other alternatives
75  reset($rdef);
76  $alt_types = "";
77  foreach ($rdef as $k => $rd) {
78  if ($k != $best_alt) {
79  $alt_types .= $rdef[$k]["type"] . $rdef[$k]["length"] . " ";
80  }
81  }
82 
83  $inf[$field] = array(
84  "notnull" => $rdef[$best_alt]["notnull"],
85  "nativetype" => $rdef[$best_alt]["nativetype"],
86  "length" => $rdef[$best_alt]["length"],
87  "unsigned" => $rdef[$best_alt]["unsigned"],
88  "default" => $rdef[$best_alt]["default"],
89  "fixed" => $rdef[$best_alt]["fixed"],
90  "autoincrement" => $rdef[$best_alt]["autoincrement"],
91  "type" => $rdef[$best_alt]["type"],
92  "alt_types" => $alt_types,
93  );
94 
95  if ($a_remove_not_allowed_attributes) {
96  foreach ($inf[$field] as $k => $v) {
97  if ($k != "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]])) {
98  unset($inf[$field][$k]);
99  }
100  }
101  }
102  }
103 
104  return $inf;
105  }
106 
107 
112  public function getBestDefinitionAlternative($a_def) {
113  // determine which type to choose
114  $car = array(
115  "boolean" => 10,
116  "integer" => 20,
117  "decimal" => 30,
118  "float" => 40,
119  "date" => 50,
120  "time" => 60,
121  "timestamp" => 70,
122  "text" => 80,
123  "clob" => 90,
124  "blob" => 100,
125  );
126 
127  $cur_car = 0;
128  $best_alt = 0; // best alternatice
129  foreach ($a_def as $k => $rd) {
130  if ($car[$rd["type"]] > $cur_car) {
131  $cur_car = $car[$rd["type"]];
132  $best_alt = $k;
133  }
134  }
135 
136  return $best_alt;
137  }
138 
139 
147  public function getAutoIncrementField($a_table) {
148  $fields = $this->manager->listTableFields($a_table);
149  $inf = array();
150 
151  foreach ($fields as $field) {
152  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
153  if ($rdef[0]["autoincrement"]) {
154  return $field;
155  }
156  }
157 
158  return false;
159  }
160 
161 
168  public function getPrimaryKeyInformation($a_table) {
169  $constraints = $this->manager->listTableConstraints($a_table);
170 
171  $pk = false;
172  foreach ($constraints as $c) {
173 
174  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
175 
176  if ($info["primary"]) {
177  $pk["name"] = $c;
178  foreach ($info["fields"] as $k => $f) {
179  $pk["fields"][$k] = array(
180  "position" => $f["position"],
181  "sorting" => $f["sorting"],
182  );
183  }
184  }
185  }
186 
187  return $pk;
188  }
189 
190 
199  public function getIndicesInformation($a_table, $a_abstract_table = false) {
200  //$constraints = $this->manager->listTableConstraints($a_table);
201  $indexes = $this->manager->listTableIndexes($a_table);
202 
203  // get additional information if database is MySQL
204  $mysql_info = array();
205 
206  $set = $this->il_db->query("SHOW INDEX FROM " . $a_table);
207  while ($rec = $this->il_db->fetchAssoc($set)) {
208  if (!empty ($rec["Key_name"])) {
209  $mysql_info[$rec["Key_name"]] = $rec;
210  } else {
211  $mysql_info[$rec["key_name"]] = $rec;
212  }
213  }
214 
215 
216  $ind = array();
217  foreach ($indexes as $c) {
218  $info = $this->reverse->getTableIndexDefinition($a_table, $c);
219 
220  $i = array();
221  if (!$info["primary"]) {
222  $i["name"] = $c;
223  $i["fulltext"] = false;
224  $suffix = ($a_abstract_table) ? "_idx" : "";
225 
226  if ($mysql_info[$i["name"]]["Index_type"] == "FULLTEXT"
227  || $mysql_info[$i["name"] . "_idx"]["Index_type"] == "FULLTEXT"
228  || $mysql_info[$i["name"]]["index_type"] == "FULLTEXT"
229  || $mysql_info[$i["name"] . "_idx"]["index_type"] == "FULLTEXT"
230  ) {
231  $i["fulltext"] = true;
232  }
233  foreach ($info["fields"] as $k => $f) {
234  $i["fields"][$k] = array(
235  "position" => $f["position"],
236  "sorting" => $f["sorting"],
237  );
238  }
239  $ind[] = $i;
240  }
241  }
242 
243  return $ind;
244  }
245 
246 
255  public function getConstraintsInformation($a_table, $a_abstract_table = false) {
256  $constraints = $this->manager->listTableConstraints($a_table);
257 
258  $cons = array();
259  foreach ($constraints as $c) {
260  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
261  //var_dump($info);
262  $i = array();
263  if ($info["unique"]) {
264  $i["name"] = $c;
265  $i["type"] = "unique";
266  foreach ($info["fields"] as $k => $f) {
267  $i["fields"][$k] = array(
268  "position" => $f["position"],
269  "sorting" => $f["sorting"],
270  );
271  }
272  $cons[] = $i;
273  }
274  }
275 
276  return $cons;
277  }
278 
279 
289  public function hasSequence($a_table) {
290  $seq = $this->manager->listSequences();
291  if (is_array($seq) && in_array($a_table, $seq)) {
292  // sequence field is (only) primary key field of table
293  $pk = $this->getPrimaryKeyInformation($a_table);
294  if (is_array($pk["fields"]) && count($pk["fields"] == 1)) {
295  $seq_field = key($pk["fields"]);
296  } else {
297  throw new ilDatabaseException("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: "
298  . $a_table . ".");
299  }
300 
301  $set = $this->il_db->query("SELECT MAX(" .$this->il_db->quoteIdentifier($seq_field) . ") ma FROM " . $this->il_db->quoteIdentifier($a_table) . "");
302  $rec = $this->il_db->fetchAssoc($set);
303  $next = $rec["ma"] + 1;
304 
305  return $next;
306  }
307 
308  return false;
309  }
310 }
311 
312 ?>
getBestDefinitionAlternative($a_def)
hasSequence($a_table)
Check whether sequence is defined for current table (only works on "abstraced" tables) ...
getConstraintsInformation($a_table, $a_abstract_table=false)
Get information on constraints of a table.
getAutoIncrementField($a_table)
Gets the auto increment field of a table.
getIndicesInformation($a_table, $a_abstract_table=false)
Get information on indices of a table.
Class ilDatabaseException.
getPrimaryKeyInformation($a_table)
Get primary key of a table.
Interface ilDBInterface.
$info
Definition: example_052.php:80
Create styles array
The data for the language used.
getFieldInformation($a_table, $a_remove_not_allowed_attributes=false)
Get field information of a table.
global $ilDB
__construct(ilDBInterface $ilDBInterface=null)
ilDBAnalyzer constructor.
This class gives all kind of DB information using the MDB2 manager and reverse module.