Last active
August 29, 2015 13:56
-
-
Save akalongman/9108753 to your computer and use it in GitHub Desktop.
Nested check function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function diagnosticTree() | |
{ | |
$errors = array(); | |
$this->_lock(); | |
// Test 0 | |
$sql = ' SELECT MIN(`left_key`) ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' LIMIT 1 '; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadResult(); | |
if ($result != 1) | |
{ | |
$errors[0] = 'Minimal left_key is not 1!'; | |
} | |
// Test 1 | |
$sql = ' SELECT `id`,`left_key`,`right_key`, `level` ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' WHERE `left_key` >=`right_key` '; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadObjectList(); | |
if ($result) | |
{ | |
$errors[1] = $result; | |
} | |
// Test 2 | |
$sql = ' SELECT `id`, MOD((`right_key` - `left_key`) / 2) AS `summ` ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' WHERE `summ` = "0" '; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadObjectList(); | |
if ($result) | |
{ | |
$errors[2] = $result; | |
} | |
// Test 3 | |
$sql = ' SELECT `id`, MOD((`left_key` - `level` + 2) / 2) AS `summ` ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' WHERE `summ` = "1" '; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadObjectList(); | |
if ($result) | |
{ | |
$errors[3] = $result; | |
} | |
// Test 4 | |
$sql = ' SELECT `t1`.`id`, COUNT(`t1`.`id`) AS `rep`, MAX(`t3`.`right_key`) AS `max_right` ' | |
.' FROM `#__wsshoppingcart_categories` AS `t1`, `#__wsshoppingcart_categories` AS `t2`, `#__wsshoppingcart_categories` AS `t3` ' | |
.' WHERE `t1`.`left_key` <> `t2`.`left_key` AND `t1`.`left_key` <> `t2`.`right_key` AND `t1`.`right_key` <> `t2`.`left_key` AND `t1`.`right_key` <> `t2`.`right_key` ' | |
.' GROUP BY `t1`.`id` ' | |
.' HAVING `max_right` <> SQRT(4 * `rep` + 1) + 1 '; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadObjectList(); | |
if ($result) | |
{ | |
$errors[4] = $result; | |
} | |
// Test 5 | |
$sql = ' SELECT * ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' ORDER BY `left_key` ' | |
.' LIMIT 1 ' | |
; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadObject(); | |
if (!$result || $result->level != 0) | |
{ | |
$errors[5] = 'Invalid level for the top item!'; | |
} | |
// Tests 6 - 14 | |
$sql = ' SELECT * ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' ORDER BY `left_key` ' | |
; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadAssocList(); | |
$a = array(); | |
$path = array(); | |
$count = count($result); | |
for($i=0; $i<$count; $i++) | |
{ | |
$f = $result[$i]; | |
if ($i && !count($path)) | |
{ | |
$errors[6] = "Out of top level node at id=".$f['id']." "; | |
break; | |
} | |
if ($f['left_key'] >= $f['right_key']) | |
{ | |
$errors[7] = "left_key [".$f['left_key']."] >= right_key [".$f['right_key']."] in ID: ".$f['id'].""; | |
break; | |
} | |
if (($f['right_key'] - $f['left_key']) % 2 != 1) | |
{ | |
$errors[8] = "not a valid number of children for left_key [".$f['left_key']."] and right_key [".$f['right_key']."] "; | |
break; | |
} | |
if (!empty($a[$f['left_key']])) | |
{ | |
$errors[9] = "Repeatable left_key [".$f['left_key']."] "; | |
break; | |
} | |
$a[$f['left_key']] = true; | |
if (!empty($a[$f['right_key']])) | |
{ | |
$errors[10] = "Repeatable right_key [".$f['right_key']."] "; | |
break; | |
} | |
$cntpath = count($path); | |
if ($f['level'] != $cntpath) | |
{ | |
$errors[11] = "Level is invalid at id=".$f['id'].". ".$cntpath." expected."; | |
break; | |
} | |
if ($i && !$f['level']) | |
{ | |
$errors[12] = "Zero level at id=".$f['id']." "; | |
break; | |
} | |
if (!$i) | |
{ | |
$path[] = $f; | |
} | |
else | |
{ | |
$c = count($path) - 1; | |
if ($f['right_key'] > $f['left_key'] + 1 && $path[$c]['left_key'] < $f['left_key'] && $path[$c]['right_key'] > $f['right_key']) | |
{ | |
$path[] = $f; | |
} | |
else | |
{ | |
$right_key = $f['right_key']; | |
while(count($path) && $path[$c]['right_key'] == $right_key + 1) | |
{ | |
array_pop($path); | |
$c--; | |
$right_key++; | |
} | |
} | |
} | |
$a[$f['right_key']] = true; | |
} | |
$sql = ' SELECT * ' | |
.' FROM `#__wsshoppingcart_categories` ' | |
.' ORDER BY `left_key` ' | |
.' LIMIT 1 ' | |
; | |
$this->_db->setQuery($sql); | |
$result = $this->_db->loadObject(); | |
for($i = $result->left_key; $i <= $result->right_key; $i++) | |
{ | |
if (empty($a[$i])) | |
{ | |
$errors[13] = "Number missing [".$i."] "; | |
break; | |
} | |
} | |
if (count($a) != $result->right_key - $result->left_key + 1) | |
{ | |
$errors[14] = "Invalid number of items"; | |
} | |
$this->_unlock(); | |
return $errors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment