ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
14{
15
19 function __construct()
20 {
21 global $ilDB;
22
23 $this->manager = $ilDB->db->loadModule('Manager');
24 $this->reverse = $ilDB->db->loadModule('Reverse');
25 $this->il_db = $ilDB;
26 $this->allowed_attributes = $ilDB->getAllowedAttributes();
27 }
28
29
36 function getFieldInformation($a_table, $a_remove_not_allowed_attributes = false)
37 {
38//echo "<br>-".$a_table."-".$field."-";
39 $fields = $this->manager->listTableFields($a_table);
40 $inf = array();
41 foreach ($fields as $field)
42 {
43//echo "<br>-".$a_table."-".$field."-";
44 $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
45//var_dump($rdef);
46 // is this possible?
47 if ($rdef["type"] != $rdef["mdb2type"])
48 {
49 echo "ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field";
50 }
51
52 $best_alt = $this->getBestDefinitionAlternative($rdef);
53
54 // collect other alternatives
55 reset($rdef);
56 $alt_types = "";
57 foreach ($rdef as $k => $rd)
58 {
59 if ($k != $best_alt)
60 {
61 $alt_types.= $rdef[$k]["type"].$rdef[$k]["length"]." ";
62 }
63 }
64
65 $inf[$field] = array(
66 "notnull" => $rdef[$best_alt]["notnull"],
67 "nativetype" => $rdef[$best_alt]["nativetype"],
68 "length" => $rdef[$best_alt]["length"],
69 "unsigned" => $rdef[$best_alt]["unsigned"],
70 "default" => $rdef[$best_alt]["default"],
71 "fixed" => $rdef[$best_alt]["fixed"],
72 "autoincrement" => $rdef[$best_alt]["autoincrement"],
73 "type" => $rdef[$best_alt]["type"],
74 "alt_types" => $alt_types,
75 );
76
77 if ($a_remove_not_allowed_attributes)
78 {
79 foreach ($inf[$field] as $k => $v)
80 {
81 if ($k != "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]]))
82 {
83 unset($inf[$field][$k]);
84 }
85 }
86 }
87 }
88
89 return $inf;
90 }
91
93 {
94 // determine which type to choose
95 $car = array(
96 "boolean" => 10,
97 "integer" => 20,
98 "decimal" => 30,
99 "float" => 40,
100 "date" => 50,
101 "time" => 60,
102 "timestamp" => 70,
103 "text" => 80,
104 "clob" => 90,
105 "blob" => 100);
106
107 $cur_car = 0;
108 $best_alt = 0; // best alternatice
109 foreach ($a_def as $k => $rd)
110 {
111 if ($car[$rd["type"]] > $cur_car)
112 {
113 $cur_car = $car[$rd["type"]];
114 $best_alt = $k;
115 }
116 }
117
118 return $best_alt;
119 }
120
128 function getAutoIncrementField($a_table)
129 {
130 $fields = $this->manager->listTableFields($a_table);
131 $inf = array();
132
133 foreach ($fields as $field)
134 {
135 $rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
136 if ($rdef[0]["autoincrement"])
137 {
138 return $field;
139 }
140 }
141 return false;
142 }
143
150 function getPrimaryKeyInformation($a_table)
151 {
152 $constraints = $this->manager->listTableConstraints($a_table);
153 $pk = false;
154 foreach ($constraints as $c)
155 {
156 $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
157 if ($info["primary"])
158 {
159 $pk["name"] = $c;
160 foreach ($info["fields"] as $k => $f)
161 {
162 $pk["fields"][$k] = array(
163 "position" => $f["position"],
164 "sorting" => $f["sorting"]);
165 }
166 }
167 }
168
169 return $pk;
170 }
171
180 function getIndicesInformation($a_table, $a_abstract_table = false)
181 {
182 //$constraints = $this->manager->listTableConstraints($a_table);
183 $indexes = $this->manager->listTableIndexes($a_table);
184
185 // get additional information if database is MySQL
186 $mysql_info = array();
187 if ($this->il_db->getDBType() == "mysql")
188 {
189 $set = $this->il_db->query("SHOW INDEX FROM ".$a_table);
190 while ($rec = $this->il_db->fetchAssoc($set))
191 {
192 if (!empty ($rec["Key_name"]))
193 {
194 $mysql_info[$rec["Key_name"]] = $rec;
195 }
196 else
197 {
198 $mysql_info[$rec["key_name"]] = $rec;
199 }
200 }
201 }
202
203 $ind = array();
204 foreach ($indexes as $c)
205 {
206 $info = $this->reverse->getTableIndexDefinition($a_table, $c);
207
208 $i = array();
209 if (!$info["primary"])
210 {
211 $i["name"] = $c;
212 $i["fulltext"] = false;
213 $suffix = ($a_abstract_table)
214 ? "_idx"
215 : "";
216
217 if ($mysql_info[$i["name"]]["Index_type"] == "FULLTEXT" ||
218 $mysql_info[$i["name"]."_idx"]["Index_type"] == "FULLTEXT" ||
219 $mysql_info[$i["name"]]["index_type"] == "FULLTEXT" ||
220 $mysql_info[$i["name"]."_idx"]["index_type"] == "FULLTEXT")
221 {
222 $i["fulltext"] = true;
223 }
224 foreach ($info["fields"] as $k => $f)
225 {
226 $i["fields"][$k] = array(
227 "position" => $f["position"],
228 "sorting" => $f["sorting"]);
229 }
230 $ind[] = $i;
231 }
232 }
233
234 return $ind;
235 }
236
245 function getConstraintsInformation($a_table, $a_abstract_table = false)
246 {
247 $constraints = $this->manager->listTableConstraints($a_table);
248
249 $cons = array();
250 foreach ($constraints as $c)
251 {
252 $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
253//var_dump($info);
254 $i = array();
255 if ($info["unique"])
256 {
257 $i["name"] = $c;
258 $i["type"] = "unique";
259 foreach ($info["fields"] as $k => $f)
260 {
261 $i["fields"][$k] = array(
262 "position" => $f["position"],
263 "sorting" => $f["sorting"]);
264 }
265 $cons[] = $i;
266 }
267 }
268
269 return $cons;
270 }
271
278 function hasSequence($a_table)
279 {
280 $seq = $this->manager->listSequences();
281 if (is_array($seq) && in_array($a_table, $seq))
282 {
283 // sequence field is (only) primary key field of table
284 $pk = $this->getPrimaryKeyInformation($a_table);
285 if (is_array($pk["fields"]) && count($pk["fields"] == 1))
286 {
287 $seq_field = key($pk["fields"]);
288 }
289 else
290 {
291 die("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: ".$a_table.".");
292 }
293
294 $set = $this->il_db->query("SELECT MAX(`".$seq_field."`) ma FROM `".$a_table."`");
295 $rec = $this->il_db->fetchAssoc($set);
296 $next = $rec["ma"] + 1;
297
298 return $next;
299 }
300 return false;
301 }
302
303}
304?>
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()
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.
$info
Definition: example_052.php:80
global $ilDB