ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 (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 MDB2 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 $ilDB, ilDBConstants\MODULE_MANAGER, and ilDBConstants\MODULE_REVERSE.

45  {
46  if (!$ilDBInterface instanceof ilDBInterface) {
47  global $ilDB;
48  $ilDBInterface = $ilDB;
49  }
50 
51  $this->manager = $ilDBInterface->loadModule(ilDBConstants::MODULE_MANAGER);
52  $this->reverse = $ilDBInterface->loadModule(ilDBConstants::MODULE_REVERSE);
53  $this->il_db = $ilDBInterface;
54  $this->allowed_attributes = $ilDBInterface->getAllowedAttributes();
55  }
Interface ilDBInterface.
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 151 of file class.ilDBAnalyzer.php.

References array.

152  {
153  $fields = $this->manager->listTableFields($a_table);
154  $inf = array();
155 
156  foreach ($fields as $field) {
157  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
158  if ($rdef[0]["autoincrement"]) {
159  return $field;
160  }
161  }
162 
163  return false;
164  }
Create styles array
The data for the language used.

◆ getBestDefinitionAlternative()

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

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

References array.

Referenced by getFieldInformation().

116  {
117  // determine which type to choose
118  $car = array(
119  "boolean" => 10,
120  "integer" => 20,
121  "decimal" => 30,
122  "float" => 40,
123  "date" => 50,
124  "time" => 60,
125  "timestamp" => 70,
126  "text" => 80,
127  "clob" => 90,
128  "blob" => 100,
129  );
130 
131  $cur_car = 0;
132  $best_alt = 0; // best alternatice
133  foreach ($a_def as $k => $rd) {
134  if ($car[$rd["type"]] > $cur_car) {
135  $cur_car = $car[$rd["type"]];
136  $best_alt = $k;
137  }
138  }
139 
140  return $best_alt;
141  }
Create styles array
The data for the language used.
+ 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 261 of file class.ilDBAnalyzer.php.

References $i, $info, and array.

262  {
263  $constraints = $this->manager->listTableConstraints($a_table);
264 
265  $cons = array();
266  foreach ($constraints as $c) {
267  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
268  //var_dump($info);
269  $i = array();
270  if ($info["unique"]) {
271  $i["name"] = $c;
272  $i["type"] = "unique";
273  foreach ($info["fields"] as $k => $f) {
274  $i["fields"][$k] = array(
275  "position" => $f["position"],
276  "sorting" => $f["sorting"],
277  );
278  }
279  $cons[] = $i;
280  }
281  }
282 
283  return $cons;
284  }
Create styles array
The data for the language used.
$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 64 of file class.ilDBAnalyzer.php.

References array, and getBestDefinitionAlternative().

65  {
66  $fields = $this->manager->listTableFields($a_table);
67  $inf = array();
68  foreach ($fields as $field) {
69  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
70  // is this possible?
71  if ($rdef["type"] != $rdef["mdb2type"]) {
72  throw new ilDatabaseException("ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field");
73  }
74 
75  $best_alt = $this->getBestDefinitionAlternative($rdef);
76 
77  // collect other alternatives
78  reset($rdef);
79  $alt_types = "";
80  foreach ($rdef as $k => $rd) {
81  if ($k != $best_alt) {
82  $alt_types .= $rdef[$k]["type"] . $rdef[$k]["length"] . " ";
83  }
84  }
85 
86  $inf[$field] = array(
87  "notnull" => $rdef[$best_alt]["notnull"],
88  "nativetype" => $rdef[$best_alt]["nativetype"],
89  "length" => $rdef[$best_alt]["length"],
90  "unsigned" => $rdef[$best_alt]["unsigned"],
91  "default" => $rdef[$best_alt]["default"],
92  "fixed" => $rdef[$best_alt]["fixed"],
93  "autoincrement" => $rdef[$best_alt]["autoincrement"],
94  "type" => $rdef[$best_alt]["type"],
95  "alt_types" => $alt_types,
96  );
97 
98  if ($a_remove_not_allowed_attributes) {
99  foreach ($inf[$field] as $k => $v) {
100  if ($k != "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]])) {
101  unset($inf[$field][$k]);
102  }
103  }
104  }
105  }
106 
107  return $inf;
108  }
getBestDefinitionAlternative($a_def)
Class ilDatabaseException.
Create styles array
The data for the language used.
+ 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 204 of file class.ilDBAnalyzer.php.

References $i, $info, and array.

205  {
206  //$constraints = $this->manager->listTableConstraints($a_table);
207  $indexes = $this->manager->listTableIndexes($a_table);
208 
209  // get additional information if database is MySQL
210  $mysql_info = array();
211 
212  $set = $this->il_db->query("SHOW INDEX FROM " . $a_table);
213  while ($rec = $this->il_db->fetchAssoc($set)) {
214  if (!empty($rec["Key_name"])) {
215  $mysql_info[$rec["Key_name"]] = $rec;
216  } else {
217  $mysql_info[$rec["key_name"]] = $rec;
218  }
219  }
220 
221 
222  $ind = array();
223  foreach ($indexes as $c) {
224  $info = $this->reverse->getTableIndexDefinition($a_table, $c);
225 
226  $i = array();
227  if (!$info["primary"]) {
228  $i["name"] = $c;
229  $i["fulltext"] = false;
230  $suffix = ($a_abstract_table) ? "_idx" : "";
231 
232  if ($mysql_info[$i["name"]]["Index_type"] == "FULLTEXT"
233  || $mysql_info[$i["name"] . "_idx"]["Index_type"] == "FULLTEXT"
234  || $mysql_info[$i["name"]]["index_type"] == "FULLTEXT"
235  || $mysql_info[$i["name"] . "_idx"]["index_type"] == "FULLTEXT"
236  ) {
237  $i["fulltext"] = true;
238  }
239  foreach ($info["fields"] as $k => $f) {
240  $i["fields"][$k] = array(
241  "position" => $f["position"],
242  "sorting" => $f["sorting"],
243  );
244  }
245  $ind[] = $i;
246  }
247  }
248 
249  return $ind;
250  }
Create styles array
The data for the language used.
$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 173 of file class.ilDBAnalyzer.php.

References $info, and array.

Referenced by hasSequence().

174  {
175  $constraints = $this->manager->listTableConstraints($a_table);
176 
177  $pk = false;
178  foreach ($constraints as $c) {
179  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
180 
181  if ($info["primary"]) {
182  $pk["name"] = $c;
183  foreach ($info["fields"] as $k => $f) {
184  $pk["fields"][$k] = array(
185  "position" => $f["position"],
186  "sorting" => $f["sorting"],
187  );
188  }
189  }
190  }
191 
192  return $pk;
193  }
Create styles array
The data for the language used.
$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 296 of file class.ilDBAnalyzer.php.

References getPrimaryKeyInformation().

297  {
298  $seq = $this->manager->listSequences();
299  if (is_array($seq) && in_array($a_table, $seq)) {
300  // sequence field is (only) primary key field of table
301  $pk = $this->getPrimaryKeyInformation($a_table);
302  if (is_array($pk["fields"]) && count($pk["fields"] == 1)) {
303  $seq_field = key($pk["fields"]);
304  } else {
305  throw new ilDatabaseException("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: "
306  . $a_table . ".");
307  }
308 
309  $set = $this->il_db->query("SELECT MAX(" . $this->il_db->quoteIdentifier($seq_field) . ") ma FROM " . $this->il_db->quoteIdentifier($a_table) . "");
310  $rec = $this->il_db->fetchAssoc($set);
311  $next = $rec["ma"] + 1;
312 
313  return $next;
314  }
315 
316  return false;
317  }
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: