ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilDBAnalyzer Class Reference

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V. More...

+ Collaboration diagram for ilDBAnalyzer:

Public Member Functions

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

Protected Attributes

ilDBManager $manager
 
ilDBReverse $reverse
 
ilDBInterface $il_db
 
array $allowed_attributes
 

Detailed Description

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V.

ILIAS is licensed with the GPL-3.0, see https://www.gnu.org/licenses/gpl-3.0.en.html You should have received a copy of said license along with the source code, too.

If this is not the case or you just want to try ILIAS, you'll find us at: https://www.ilias.de https://github.com/ILIAS-eLearning 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 32 of file class.ilDBAnalyzer.php.

Constructor & Destructor Documentation

◆ __construct()

ilDBAnalyzer::__construct ( ilDBInterface  $ilDBInterface = null)

ilDBAnalyzer constructor.

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

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

50  {
51  if (!$ilDBInterface instanceof ilDBInterface) {
52  global $DIC;
53  $ilDB = $DIC->database();
54  $ilDBInterface = $ilDB;
55  }
56 
57  $this->manager = $ilDBInterface->loadModule(ilDBConstants::MODULE_MANAGER);
58  $this->reverse = $ilDBInterface->loadModule(ilDBConstants::MODULE_REVERSE);
59  $this->il_db = $ilDBInterface;
60  $this->allowed_attributes = $ilDBInterface->getAllowedAttributes();
61  }
global $DIC
Definition: feed.php:28
loadModule(string $module)

Member Function Documentation

◆ getAutoIncrementField()

ilDBAnalyzer::getAutoIncrementField ( string  $a_table)

Gets the auto increment field of a table.

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

Returns
string|bool name of autoincrement field

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

155  {
156  $fields = $this->manager->listTableFields($a_table);
157 
158  foreach ($fields as $field) {
159  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
160  if ($rdef[0]["autoincrement"]) {
161  return $field;
162  }
163  }
164 
165  return false;
166  }

◆ getBestDefinitionAlternative()

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

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

Referenced by getFieldInformation().

120  {
121  // determine which type to choose
122  $car = array(
123  "boolean" => 10,
124  "integer" => 20,
125  "decimal" => 30,
126  "float" => 40,
127  "date" => 50,
128  "time" => 60,
129  "timestamp" => 70,
130  "text" => 80,
131  "clob" => 90,
132  "blob" => 100,
133  );
134 
135  $cur_car = 0;
136  $best_alt = 0; // best alternatice
137  foreach ($a_def as $k => $rd) {
138  if ($car[$rd["type"]] > $cur_car) {
139  $cur_car = $car[$rd["type"]];
140  $best_alt = $k;
141  }
142  }
143 
144  return $best_alt;
145  }
+ Here is the caller graph for this function:

◆ getConstraintsInformation()

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

Get information on constraints of a table.

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

Returns
array indices information array

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

References $c, Vendor\Package\$f, and $i.

259  : array
260  {
261  $constraints = $this->manager->listTableConstraints($a_table);
262 
263  $cons = array();
264  foreach ($constraints as $c) {
265  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
266  $i = array();
267  if ($info["unique"] ?? null) {
268  $i["name"] = $c;
269  $i["type"] = "unique";
270  foreach ($info["fields"] as $k => $f) {
271  $i["fields"][$k] = array(
272  "position" => $f["position"],
273  "sorting" => $f["sorting"],
274  );
275  }
276  $cons[] = $i;
277  }
278  }
279 
280  return $cons;
281  }
$c
Definition: cli.php:38
$i
Definition: metadata.php:41

◆ getFieldInformation()

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

Get field information of a table.

Returns
array field information array

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

References getBestDefinitionAlternative().

69  : array
70  {
71  $fields = $this->manager->listTableFields($a_table);
72  $inf = array();
73  foreach ($fields as $field) {
74  $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
75  // is this possible?
76  if (isset($rdef["mdb2type"], $rdef["type"]) && $rdef["type"] !== $rdef["mdb2type"]) {
77  throw new ilDatabaseException("ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field");
78  }
79 
80  $best_alt = $this->getBestDefinitionAlternative($rdef);
81 
82  // collect other alternatives
83  reset($rdef);
84  $alt_types = "";
85  foreach (array_keys($rdef) as $k) {
86  if ($k !== $best_alt) {
87  $alt_types .= ($rdef[$k]["type"] ?? "") . ($rdef[$k]["length"] ?? "") . " ";
88  }
89  }
90 
91  $inf[$field] = array(
92  "notnull" => $rdef[$best_alt]["notnull"] ?? null,
93  "nativetype" => $rdef[$best_alt]["nativetype"] ?? null,
94  "length" => $rdef[$best_alt]["length"] ?? null,
95  "unsigned" => $rdef[$best_alt]["unsigned"] ?? null,
96  "default" => $rdef[$best_alt]["default"] ?? null,
97  "fixed" => $rdef[$best_alt]["fixed"] ?? null,
98  "autoincrement" => $rdef[$best_alt]["autoincrement"] ?? null,
99  "type" => $rdef[$best_alt]["type"] ?? null,
100  "alt_types" => $alt_types,
101  );
102 
103  if ($a_remove_not_allowed_attributes) {
104  foreach (array_keys($inf[$field]) as $k) {
105  if ($k !== "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]])) {
106  unset($inf[$field][$k]);
107  }
108  }
109  }
110  }
111 
112  return $inf;
113  }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getBestDefinitionAlternative(array $a_def)
+ Here is the call graph for this function:

◆ getIndicesInformation()

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

Get information on indices of a table.

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

Returns
array indices information array

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

References $c, Vendor\Package\$f, and $i.

204  : 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 
231  if ($mysql_info[$i["name"]]["Index_type"] === "FULLTEXT"
232  || $mysql_info[$i["name"] . "_idx"]["Index_type"] === "FULLTEXT"
233  || $mysql_info[$i["name"]]["index_type"] === "FULLTEXT"
234  || $mysql_info[$i["name"] . "_idx"]["index_type"] === "FULLTEXT"
235  ) {
236  $i["fulltext"] = true;
237  }
238  foreach ($info["fields"] as $k => $f) {
239  $i["fields"][$k] = array(
240  "position" => $f["position"],
241  "sorting" => $f["sorting"],
242  );
243  }
244  $ind[] = $i;
245  }
246  }
247 
248  return $ind;
249  }
$c
Definition: cli.php:38
$i
Definition: metadata.php:41

◆ getPrimaryKeyInformation()

ilDBAnalyzer::getPrimaryKeyInformation ( string  $a_table)

Get primary key of a table.

Returns
array primary key information array

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

References $c, and Vendor\Package\$f.

Referenced by hasSequence().

174  : array
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  }
$c
Definition: cli.php:38
+ Here is the caller graph for this function:

◆ hasSequence()

ilDBAnalyzer::hasSequence ( string  $a_table)

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

Returns
float|int|bool false, if no sequence is defined, start number otherwise
Exceptions

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

References getPrimaryKeyInformation().

293  {
294  $seq = $this->manager->listSequences();
295  if (is_array($seq) && in_array($a_table, $seq)) {
296  // sequence field is (only) primary key field of table
297  $pk = $this->getPrimaryKeyInformation($a_table);
298  if (is_array($pk["fields"]) && count($pk["fields"]) === 1) {
299  $seq_field = key($pk["fields"]);
300  } else {
301  throw new ilDatabaseException("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: "
302  . $a_table . ".");
303  }
304 
305  $set = $this->il_db->query("SELECT MAX(" . $this->il_db->quoteIdentifier($seq_field) . ") ma FROM " . $this->il_db->quoteIdentifier($a_table));
306  $rec = $this->il_db->fetchAssoc($set);
307 
308  return $rec["ma"] + 1;
309  }
310 
311  return false;
312  }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getPrimaryKeyInformation(string $a_table)
Get primary key of a table.
+ Here is the call graph for this function:

Field Documentation

◆ $allowed_attributes

array ilDBAnalyzer::$allowed_attributes
protected

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

◆ $il_db

ilDBInterface ilDBAnalyzer::$il_db
protected

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

◆ $manager

ilDBManager ilDBAnalyzer::$manager
protected

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

◆ $reverse

ilDBReverse ilDBAnalyzer::$reverse
protected

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


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