ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDBAnalyzer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
33{
35
37
39
40 protected array $allowed_attributes;
41
42
49 public function __construct(?ilDBInterface $ilDBInterface = null)
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 }
62
63
69 public function getFieldInformation(string $a_table, bool $a_remove_not_allowed_attributes = false): array
70 {
71 $fields = $this->manager->listTableFields($a_table);
72 $inf = [];
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] = [
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 }
114
115
119 public function getBestDefinitionAlternative(array $a_def): int|string
120 {
121 // determine which type to choose
122 $car = [
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 }
146
147
154 public function getAutoIncrementField(string $a_table)
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 }
167
168
174 public function getPrimaryKeyInformation(string $a_table): array
175 {
176 $constraints = $this->manager->listTableConstraints($a_table);
177
178 $pk = [];
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] = [
186 "position" => $f["position"],
187 "sorting" => $f["sorting"],
188 ];
189 }
190 }
191 }
192
193 return $pk;
194 }
195
196
204 public function getIndicesInformation(string $a_table, bool $a_abstract_table = false): 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 = [];
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 = [];
223 foreach ($indexes as $c) {
224 $info = $this->reverse->getTableIndexDefinition($a_table, $c);
225
226 $i = [];
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] = [
240 "position" => $f["position"],
241 "sorting" => $f["sorting"],
242 ];
243 }
244 $ind[] = $i;
245 }
246 }
247
248 return $ind;
249 }
250
251
259 public function getConstraintsInformation(string $a_table, bool $a_abstract_table = false): array
260 {
261 $constraints = $this->manager->listTableConstraints($a_table);
262
263 $cons = [];
264 foreach ($constraints as $c) {
265 $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
266 $i = [];
267 if ($info["unique"] ?? null) {
268 $i["name"] = $c;
269 $i["type"] = "unique";
270 foreach ($info["fields"] as $k => $f) {
271 $i["fields"][$k] = [
272 "position" => $f["position"],
273 "sorting" => $f["sorting"],
274 ];
275 }
276 $cons[] = $i;
277 }
278 }
279
280 return $cons;
281 }
282
283
292 public function hasSequence(string $a_table): int|float|false
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 (isset($pk['fields']) && 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 }
313}
This class gives all kind of DB information using the database manager and reverse module.
getPrimaryKeyInformation(string $a_table)
Get primary key of a table.
ilDBInterface $il_db
getFieldInformation(string $a_table, bool $a_remove_not_allowed_attributes=false)
Get field information of a table.
getBestDefinitionAlternative(array $a_def)
hasSequence(string $a_table)
Check whether sequence is defined for current table (only works on "abstraced" tables)
getIndicesInformation(string $a_table, bool $a_abstract_table=false)
Get information on indices of a table.
getConstraintsInformation(string $a_table, bool $a_abstract_table=false)
Get information on constraints of a table.
ilDBManager $manager
__construct(?ilDBInterface $ilDBInterface=null)
ilDBAnalyzer constructor.
getAutoIncrementField(string $a_table)
Gets the auto increment field of a table.
ilDBReverse $reverse
Class ilDatabaseException.
$c
Definition: deliver.php:25
$info
Definition: entry_point.php:21
Interface ilDBInterface.
Interface ilDBManager.
Interface ilDBReverse.
global $DIC
Definition: shib_login.php:26