ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
17 {
18 
22  protected $manager;
26  protected $reverse;
30  protected $il_db;
35 
36 
44  public function __construct(ilDBInterface $ilDBInterface = null)
45  {
46  if (!$ilDBInterface instanceof ilDBInterface) {
47  global $DIC;
48  $ilDB = $DIC->database();
49  $ilDBInterface = $ilDB;
50  }
51 
52  $this->manager = $ilDBInterface->loadModule(ilDBConstants::MODULE_MANAGER);
53  $this->reverse = $ilDBInterface->loadModule(ilDBConstants::MODULE_REVERSE);
54  $this->il_db = $ilDBInterface;
55  $this->allowed_attributes = $ilDBInterface->getAllowedAttributes();
56  }
57 
58 
65  public function getFieldInformation($a_table, $a_remove_not_allowed_attributes = false)
66  {
67  $fields = $this->manager->listTableFields($a_table);
68  $inf = array();
69  foreach ($fields as $field) {
70  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
71  // is this possible?
72  if ($rdef["type"] != $rdef["mdb2type"]) {
73  throw new ilDatabaseException("ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field");
74  }
75 
76  $best_alt = $this->getBestDefinitionAlternative($rdef);
77 
78  // collect other alternatives
79  reset($rdef);
80  $alt_types = "";
81  foreach ($rdef as $k => $rd) {
82  if ($k != $best_alt) {
83  $alt_types .= $rdef[$k]["type"] . $rdef[$k]["length"] . " ";
84  }
85  }
86 
87  $inf[$field] = array(
88  "notnull" => $rdef[$best_alt]["notnull"],
89  "nativetype" => $rdef[$best_alt]["nativetype"],
90  "length" => $rdef[$best_alt]["length"],
91  "unsigned" => $rdef[$best_alt]["unsigned"],
92  "default" => $rdef[$best_alt]["default"],
93  "fixed" => $rdef[$best_alt]["fixed"],
94  "autoincrement" => $rdef[$best_alt]["autoincrement"],
95  "type" => $rdef[$best_alt]["type"],
96  "alt_types" => $alt_types,
97  );
98 
99  if ($a_remove_not_allowed_attributes) {
100  foreach ($inf[$field] as $k => $v) {
101  if ($k != "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]])) {
102  unset($inf[$field][$k]);
103  }
104  }
105  }
106  }
107 
108  return $inf;
109  }
110 
111 
116  public function getBestDefinitionAlternative($a_def)
117  {
118  // determine which type to choose
119  $car = array(
120  "boolean" => 10,
121  "integer" => 20,
122  "decimal" => 30,
123  "float" => 40,
124  "date" => 50,
125  "time" => 60,
126  "timestamp" => 70,
127  "text" => 80,
128  "clob" => 90,
129  "blob" => 100,
130  );
131 
132  $cur_car = 0;
133  $best_alt = 0; // best alternatice
134  foreach ($a_def as $k => $rd) {
135  if ($car[$rd["type"]] > $cur_car) {
136  $cur_car = $car[$rd["type"]];
137  $best_alt = $k;
138  }
139  }
140 
141  return $best_alt;
142  }
143 
144 
152  public function getAutoIncrementField($a_table)
153  {
154  $fields = $this->manager->listTableFields($a_table);
155  $inf = array();
156 
157  foreach ($fields as $field) {
158  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
159  if ($rdef[0]["autoincrement"]) {
160  return $field;
161  }
162  }
163 
164  return false;
165  }
166 
167 
174  public function getPrimaryKeyInformation($a_table)
175  {
176  $constraints = $this->manager->listTableConstraints($a_table);
177 
178  $pk = false;
179  foreach ($constraints as $c) {
180  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
181 
182  if ($info["primary"]) {
183  $pk["name"] = $c;
184  foreach ($info["fields"] as $k => $f) {
185  $pk["fields"][$k] = array(
186  "position" => $f["position"],
187  "sorting" => $f["sorting"],
188  );
189  }
190  }
191  }
192 
193  return $pk;
194  }
195 
196 
205  public function getIndicesInformation($a_table, $a_abstract_table = false)
206  {
207  //$constraints = $this->manager->listTableConstraints($a_table);
208  $indexes = $this->manager->listTableIndexes($a_table);
209 
210  // get additional information if database is MySQL
211  $mysql_info = array();
212 
213  $set = $this->il_db->query("SHOW INDEX FROM " . $a_table);
214  while ($rec = $this->il_db->fetchAssoc($set)) {
215  if (!empty($rec["Key_name"])) {
216  $mysql_info[$rec["Key_name"]] = $rec;
217  } else {
218  $mysql_info[$rec["key_name"]] = $rec;
219  }
220  }
221 
222 
223  $ind = array();
224  foreach ($indexes as $c) {
225  $info = $this->reverse->getTableIndexDefinition($a_table, $c);
226 
227  $i = array();
228  if (!$info["primary"]) {
229  $i["name"] = $c;
230  $i["fulltext"] = false;
231  $suffix = ($a_abstract_table) ? "_idx" : "";
232 
233  if ($mysql_info[$i["name"]]["Index_type"] == "FULLTEXT"
234  || $mysql_info[$i["name"] . "_idx"]["Index_type"] == "FULLTEXT"
235  || $mysql_info[$i["name"]]["index_type"] == "FULLTEXT"
236  || $mysql_info[$i["name"] . "_idx"]["index_type"] == "FULLTEXT"
237  ) {
238  $i["fulltext"] = true;
239  }
240  foreach ($info["fields"] as $k => $f) {
241  $i["fields"][$k] = array(
242  "position" => $f["position"],
243  "sorting" => $f["sorting"],
244  );
245  }
246  $ind[] = $i;
247  }
248  }
249 
250  return $ind;
251  }
252 
253 
262  public function getConstraintsInformation($a_table, $a_abstract_table = false)
263  {
264  $constraints = $this->manager->listTableConstraints($a_table);
265 
266  $cons = array();
267  foreach ($constraints as $c) {
268  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
269  //var_dump($info);
270  $i = array();
271  if ($info["unique"]) {
272  $i["name"] = $c;
273  $i["type"] = "unique";
274  foreach ($info["fields"] as $k => $f) {
275  $i["fields"][$k] = array(
276  "position" => $f["position"],
277  "sorting" => $f["sorting"],
278  );
279  }
280  $cons[] = $i;
281  }
282  }
283 
284  return $cons;
285  }
286 
287 
297  public function hasSequence($a_table)
298  {
299  $seq = $this->manager->listSequences();
300  if (is_array($seq) && in_array($a_table, $seq)) {
301  // sequence field is (only) primary key field of table
302  $pk = $this->getPrimaryKeyInformation($a_table);
303  if (is_array($pk["fields"]) && count($pk["fields"] == 1)) {
304  $seq_field = key($pk["fields"]);
305  } else {
306  throw new ilDatabaseException("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: "
307  . $a_table . ".");
308  }
309 
310  $set = $this->il_db->query("SELECT MAX(" . $this->il_db->quoteIdentifier($seq_field) . ") ma FROM " . $this->il_db->quoteIdentifier($a_table) . "");
311  $rec = $this->il_db->fetchAssoc($set);
312  $next = $rec["ma"] + 1;
313 
314  return $next;
315  }
316 
317  return false;
318  }
319 }
getBestDefinitionAlternative($a_def)
hasSequence($a_table)
Check whether sequence is defined for current table (only works on "abstraced" tables) ...
global $DIC
Definition: saml.php:7
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.
getFieldInformation($a_table, $a_remove_not_allowed_attributes=false)
Get field information of a table.
global $ilDB
__construct(ilDBInterface $ilDBInterface=null)
ilDBAnalyzer constructor.
$i
Definition: disco.tpl.php:19
This class gives all kind of DB information using the database manager and reverse module...
$info
Definition: index.php:5