ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 43 of file class.ilDBAnalyzer.php.

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

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

References array.

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

◆ getBestDefinitionAlternative()

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

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

References array.

Referenced by getFieldInformation().

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

References $info, and array.

255  {
256  $constraints = $this->manager->listTableConstraints($a_table);
257 
258  $cons = array();
259  foreach ($constraints as $c) {
260  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
261  //var_dump($info);
262  $i = array();
263  if ($info["unique"]) {
264  $i["name"] = $c;
265  $i["type"] = "unique";
266  foreach ($info["fields"] as $k => $f) {
267  $i["fields"][$k] = array(
268  "position" => $f["position"],
269  "sorting" => $f["sorting"],
270  );
271  }
272  $cons[] = $i;
273  }
274  }
275 
276  return $cons;
277  }
$info
Definition: example_052.php:80
Create styles array
The data for the language used.

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

References array, and getBestDefinitionAlternative().

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

References $info, and array.

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

◆ getPrimaryKeyInformation()

ilDBAnalyzer::getPrimaryKeyInformation (   $a_table)

Get primary key of a table.

Parameters
stringtable name
Returns
array primary key information array

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

References $info, and array.

Referenced by hasSequence().

168  {
169  $constraints = $this->manager->listTableConstraints($a_table);
170 
171  $pk = false;
172  foreach ($constraints as $c) {
173 
174  $info = $this->reverse->getTableConstraintDefinition($a_table, $c);
175 
176  if ($info["primary"]) {
177  $pk["name"] = $c;
178  foreach ($info["fields"] as $k => $f) {
179  $pk["fields"][$k] = array(
180  "position" => $f["position"],
181  "sorting" => $f["sorting"],
182  );
183  }
184  }
185  }
186 
187  return $pk;
188  }
$info
Definition: example_052.php:80
Create styles array
The data for the language used.
+ 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 289 of file class.ilDBAnalyzer.php.

References getPrimaryKeyInformation().

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

◆ $il_db

ilDBAnalyzer::$il_db
protected

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

◆ $manager

ilDBAnalyzer::$manager
protected

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

◆ $reverse

ilDBAnalyzer::$reverse
protected

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


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