ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 ()
 Constructor.
 getFieldInformation ($a_table, $a_remove_not_allowed_attributes=false)
 Get field information of a table.
 getBestDefinitionAlternative ($a_def)
 getAutoIncrementField ($a_table)
 Gets the auto increment field of a table.
 getPrimaryKeyInformation ($a_table)
 Get primary key of a table.
 getIndicesInformation ($a_table, $a_abstract_table=false)
 Get information on indices of a table.
 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)

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
Version
Id:
class.ilDBUpdate.php 18649 2009-01-21 09:59:23Z akill

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

Constructor & Destructor Documentation

ilDBAnalyzer::__construct ( )

Constructor.

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

References $ilDB.

{
global $ilDB;
$this->manager = $ilDB->db->loadModule('Manager');
$this->reverse = $ilDB->db->loadModule('Reverse');
$this->il_db = $ilDB;
$this->allowed_attributes = $ilDB->getAllowedAttributes();
}

Member Function Documentation

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

{
$fields = $this->manager->listTableFields($a_table);
$inf = array();
foreach ($fields as $field)
{
$rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
if ($rdef[0]["autoincrement"])
{
return $field;
}
}
return false;
}
ilDBAnalyzer::getBestDefinitionAlternative (   $a_def)

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

Referenced by getFieldInformation().

{
// determine which type to choose
$car = array(
"boolean" => 10,
"integer" => 20,
"decimal" => 30,
"float" => 40,
"date" => 50,
"time" => 60,
"timestamp" => 70,
"text" => 80,
"clob" => 90,
"blob" => 100);
$cur_car = 0;
$best_alt = 0; // best alternatice
foreach ($a_def as $k => $rd)
{
if ($car[$rd["type"]] > $cur_car)
{
$cur_car = $car[$rd["type"]];
$best_alt = $k;
}
}
return $best_alt;
}

+ Here is the caller graph for this function:

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

References $f.

{
$constraints = $this->manager->listTableConstraints($a_table);
$cons = array();
foreach ($constraints as $c)
{
$info = $this->reverse->getTableConstraintDefinition($a_table, $c);
//var_dump($info);
$i = array();
if ($info["unique"])
{
$i["name"] = $c;
$i["type"] = "unique";
foreach ($info["fields"] as $k => $f)
{
$i["fields"][$k] = array(
"position" => $f["position"],
"sorting" => $f["sorting"]);
}
$cons[] = $i;
}
}
return $cons;
}
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 36 of file class.ilDBAnalyzer.php.

References getBestDefinitionAlternative().

{
//echo "<br>-".$a_table."-".$field."-";
$fields = $this->manager->listTableFields($a_table);
$inf = array();
foreach ($fields as $field)
{
//echo "<br>-".$a_table."-".$field."-";
$rdef = $this->reverse->getTableFieldDefinition($a_table, $field);
//var_dump($rdef);
// is this possible?
if ($rdef["type"] != $rdef["mdb2type"])
{
echo "ilDBAnalyzer::getFielInformation: Found type != mdb2type: $a_table, $field";
}
$best_alt = $this->getBestDefinitionAlternative($rdef);
// collect other alternatives
reset($rdef);
$alt_types = "";
foreach ($rdef as $k => $rd)
{
if ($k != $best_alt)
{
$alt_types.= $rdef[$k]["type"].$rdef[$k]["length"]." ";
}
}
$inf[$field] = array(
"notnull" => $rdef[$best_alt]["notnull"],
"nativetype" => $rdef[$best_alt]["nativetype"],
"length" => $rdef[$best_alt]["length"],
"unsigned" => $rdef[$best_alt]["unsigned"],
"default" => $rdef[$best_alt]["default"],
"fixed" => $rdef[$best_alt]["fixed"],
"autoincrement" => $rdef[$best_alt]["autoincrement"],
"type" => $rdef[$best_alt]["type"],
"alt_types" => $alt_types,
);
if ($a_remove_not_allowed_attributes)
{
foreach ($inf[$field] as $k => $v)
{
if ($k != "type" && !in_array($k, $this->allowed_attributes[$inf[$field]["type"]]))
{
unset($inf[$field][$k]);
}
}
}
}
return $inf;
}

+ Here is the call graph for this function:

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

References $f.

{
//$constraints = $this->manager->listTableConstraints($a_table);
$indexes = $this->manager->listTableIndexes($a_table);
// get additional information if database is MySQL
$mysql_info = array();
if ($this->il_db->getDBType() == "mysql")
{
$set = $this->il_db->query("SHOW INDEX FROM ".$a_table);
while ($rec = $this->il_db->fetchAssoc($set))
{
if (!empty ($rec["Key_name"]))
{
$mysql_info[$rec["Key_name"]] = $rec;
}
else
{
$mysql_info[$rec["key_name"]] = $rec;
}
}
}
$ind = array();
foreach ($indexes as $c)
{
$info = $this->reverse->getTableIndexDefinition($a_table, $c);
$i = array();
if (!$info["primary"])
{
$i["name"] = $c;
$i["fulltext"] = false;
$suffix = ($a_abstract_table)
? "_idx"
: "";
if ($mysql_info[$i["name"]]["Index_type"] == "FULLTEXT" ||
$mysql_info[$i["name"]."_idx"]["Index_type"] == "FULLTEXT" ||
$mysql_info[$i["name"]]["index_type"] == "FULLTEXT" ||
$mysql_info[$i["name"]."_idx"]["index_type"] == "FULLTEXT")
{
$i["fulltext"] = true;
}
foreach ($info["fields"] as $k => $f)
{
$i["fields"][$k] = array(
"position" => $f["position"],
"sorting" => $f["sorting"]);
}
$ind[] = $i;
}
}
return $ind;
}
ilDBAnalyzer::getPrimaryKeyInformation (   $a_table)

Get primary key of a table.

Parameters
stringtable name
Returns
array primary key information array

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

References $f.

Referenced by hasSequence().

{
$constraints = $this->manager->listTableConstraints($a_table);
$pk = false;
foreach ($constraints as $c)
{
$info = $this->reverse->getTableConstraintDefinition($a_table, $c);
if ($info["primary"])
{
$pk["name"] = $c;
foreach ($info["fields"] as $k => $f)
{
$pk["fields"][$k] = array(
"position" => $f["position"],
"sorting" => $f["sorting"]);
}
}
}
return $pk;
}

+ Here is the caller graph for this function:

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

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

References getPrimaryKeyInformation().

{
$seq = $this->manager->listSequences();
if (is_array($seq) && in_array($a_table, $seq))
{
// sequence field is (only) primary key field of table
$pk = $this->getPrimaryKeyInformation($a_table);
if (is_array($pk["fields"]) && count($pk["fields"] == 1))
{
$seq_field = key($pk["fields"]);
}
else
{
die("ilDBAnalyzer::hasSequence: Error, sequence defined, but no one-field primary key given. Table: ".$a_table.".");
}
$set = $this->il_db->query("SELECT MAX(`".$seq_field."`) ma FROM `".$a_table."`");
$rec = $this->il_db->fetchAssoc($set);
$next = $rec["ma"] + 1;
return $next;
}
return false;
}

+ Here is the call graph for this function:


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