метод для проверки целостности таблицы дерева(не оптимизирован):
Код:
function checkTree()
{
$db = new MySql(null, "mport4", null, null);
$db->MySql_Connect();
$db->MySql_SelectDb();
//left key always less then right key
$selectQuery = "select cat_ID from categories
where cat_LEFT >= cat_RIGHT";
$db->MySql_QueryDb($selectQuery);
if ($db->dbResult != null)
{
$this->errors[] = "Левый ключ всегда меньше правого ключа.";
//echo "error 1";
//return -1;
}
//smallest key always equal 1 and biggest key always equal 2*n-elemetns
$selectQuery = "SELECT min( cat_LEFT )
as minKey, max( cat_RIGHT )
as maxKey, count( * )
as nElements
FROM categories";
$db->MySql_QueryDb($selectQuery);
if ($db->dbResult != null)
{
while($row = mysql_fetch_assoc($db->dbResult))
{
if ($row["minKey"] != 1 || ($row["maxKey"] % $row["nElements"]) != 0)
{
$this->errors[] = "Ключи корневой ветки неправильные.";
}
}
}
// cat_RIGHT-cat_LEFT/2 always not equal 0
$selectQuery = "SELECT mod(cat_RIGHT-cat_LEFT,2) as os
FROM `categories`
GROUP BY cat_ID
HAVING os = 0";
$db->MySql_QueryDb($selectQuery);
if ($db->dbResult != null)
{
$this->errors[] = "Разница между правым и левым ключами должна быть отрицательна.";
}
//need fix?
$selectQuery = "SELECT mod((cat_LEFT-cat_LEVEL+2),2) as os
FROM categories
HAVING os = 0";
$db->MySql_QueryDb($selectQuery);
if ($db->dbResult != null)
{
$this->errors[] = "Ошибка 4.";
}
//all keys must be unique
$keys = array();
$selectQuery = "SELECT cat_LEFT, cat_RIGHT
FROM categories";
$db->MySql_QueryDb($selectQuery);
if ($db->dbResult != null)
{
while($row = mysql_fetch_assoc($db->dbResult))
{
$keys[] = $row["cat_LEFT"];
$keys[] = $row["cat_RIGHT"];
}
//return "error 5";
}
//$keys[] = 1;
if ($keys !== array_unique($keys))
{
$this->errors[] = "Все ключи должны быть уникальны.";
}
if (!empty($this->errors))
{
return -1;
}
return 0;
}