ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilDBAnalyzer Class Reference

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

+ Collaboration diagram for ilDBAnalyzer:

Public Member Functions

 __construct (ilDBInterface $ilDBInterface=null)
 ilDBAnalyzer 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...
 

Protected Attributes

 $manager
 
 $reverse
 
 $il_db
 
 $allowed_attributes
 

Detailed Description

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

Author
Alex Killing alex..nosp@m.kill.nosp@m.ing@g.nosp@m.mx.d.nosp@m.e
Fabian Schmid fs@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Deprecated:
Use global ilDB only. If something is missing there, please contact fs@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch

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

Constructor & Destructor Documentation

◆ __construct()

ilDBAnalyzer::__construct ( ilDBInterface  $ilDBInterface = null)

ilDBAnalyzer constructor.

Parameters
\ilDBInterface$ilDBInterface
Deprecated:
Use global ilDB only. If something is missing there, please contact fs@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch

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

References $DIC, $ilDB, ilDBConstants\MODULE_MANAGER, and ilDBConstants\MODULE_REVERSE.

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  }
global $DIC
Definition: saml.php:7
global $ilDB
loadModule($module)

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 152 of file class.ilDBAnalyzer.php.

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  }

◆ getBestDefinitionAlternative()

ilDBAnalyzer::getBestDefinitionAlternative (   $a_def)
Parameters
$a_def
Returns
int|string

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

Referenced by getFieldInformation().

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  }
+ 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 262 of file class.ilDBAnalyzer.php.

References $c, $f, $i, and $info.

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  }
$i
Definition: disco.tpl.php:19
$info
Definition: index.php:5

◆ 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 65 of file class.ilDBAnalyzer.php.

References getBestDefinitionAlternative().

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  }
getBestDefinitionAlternative($a_def)
Class ilDatabaseException.
+ Here is the call 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 205 of file class.ilDBAnalyzer.php.

References $c, $f, $i, and $info.

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  }
$i
Definition: disco.tpl.php:19
$info
Definition: index.php:5

◆ getPrimaryKeyInformation()

ilDBAnalyzer::getPrimaryKeyInformation (   $a_table)

Get primary key of a table.

Parameters
stringtable name
Returns
array primary key information array

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

References $c, $f, and $info.

Referenced by hasSequence().

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  }
$info
Definition: index.php:5
+ 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
Exceptions

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

References getPrimaryKeyInformation().

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  }
Class ilDatabaseException.
getPrimaryKeyInformation($a_table)
Get primary key of a table.
+ Here is the call graph for this function:

Field Documentation

◆ $allowed_attributes

ilDBAnalyzer::$allowed_attributes
protected

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

◆ $il_db

ilDBAnalyzer::$il_db
protected

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

◆ $manager

ilDBAnalyzer::$manager
protected

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

◆ $reverse

ilDBAnalyzer::$reverse
protected

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


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