ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $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 }
56
57
64 public function getFieldInformation($a_table, $a_remove_not_allowed_attributes = false)
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 }
109
110
115 public function getBestDefinitionAlternative($a_def)
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 }
142
143
151 public function getAutoIncrementField($a_table)
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 }
165
166
173 public function getPrimaryKeyInformation($a_table)
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 }
194
195
204 public function getIndicesInformation($a_table, $a_abstract_table = false)
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 }
251
252
261 public function getConstraintsInformation($a_table, $a_abstract_table = false)
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 }
285
286
296 public function hasSequence($a_table)
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 }
318}
An exception for terminatinating execution or to throw for unit testing.
This class gives all kind of DB information using the MDB2 manager and reverse module.
getFieldInformation($a_table, $a_remove_not_allowed_attributes=false)
Get field information of a table.
getPrimaryKeyInformation($a_table)
Get primary key of a table.
getBestDefinitionAlternative($a_def)
getConstraintsInformation($a_table, $a_abstract_table=false)
Get information on constraints of a table.
hasSequence($a_table)
Check whether sequence is defined for current table (only works on "abstraced" tables)
__construct(ilDBInterface $ilDBInterface=null)
ilDBAnalyzer constructor.
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.
$i
Definition: disco.tpl.php:19
Interface ilDBInterface.
$info
Definition: index.php:5
global $ilDB