简体   繁体   中英

Testing for Leaf Node with MySQL

Been stuck on this for about 2 hours, I have this procedure which takes an edge and a set and returns a bool leaf to test if a certain node is a leaf. The test only works for the left node though, once I go into the second loop it breaks down, I cannot figure out why:

CREATE  PROCEDURE `is_leaf`(IN `edge` VARCHAR(12), IN `eset` VARCHAR(512), OUT `leaf` INT)
BEGIN

declare lnode char(6) default substring_index(edge, ':', 1); -- left node of edge.
  declare rnode char(6) default substring_index(edge, ':', -1); -- right node of edge.
  declare cedge char(12);
  declare lnode_in_cedge char(6);
  declare rnode_in_cedge char(6);
  declare found int default 1;
   declare OGset varchar(512) default '';
  SET OGset= concat('',eset);

--CHECKS LEFT NODE OF EDGE AGAINST ALL EDGES IN SET
WHILE length(eset)!=0 AND found!=2 DO
set cedge = substring_index(eset, ',', 1);
SET lnode_in_cedge= substring_index(cedge, ':', 1);
SET rnode_in_cedge= substring_index(cedge, ':', -1);

--IF LEFT NODE IS FOUND TO HAVE COMMON VERTEX, THE SAME TEST IS RUN FOR THE RIGHT ONE
IF lnode= lnode_in_cedge OR lnode= rnode_in_cedge THEN
    BEGIN
        SET leaf=false;

    SET eset= OGset;
    WHILE length(eset)!=0 AND found !=2 DO
    set cedge = substring_index(eset, ',', 1);
    SET lnode_in_cedge= substring_index(cedge, ':', 1);
    SET rnode_in_cedge= substring_index(cedge, ':', -1);
        IF rnode= lnode_in_cedge OR rnode= rnode_in_cedge THEN
            BEGIN
            SET leaf= false;
            SET found= 2;
            END;
         END IF;
         SET eset  = REMOVE_FIRST(eset);
     END WHILE;
     END;
END IF;

SET eset  = REMOVE_FIRST(eset);
END WHILE;



IF found=1 THEN
SET leaf=true;
END IF;


END

I got it working by breaking up the loops, if anybody finds a reason why the previous code didn't work let me know. I don't know too much about nested loops in MySQL. Here's the working code:

CREATE PROCEDURE `is_leaf`(IN `edge` VARCHAR(12), IN `eset` VARCHAR(512), OUT `leaf` INT)
BEGIN

declare lnode char(6) default substring_index(edge, ':', 1); -- left node of edge.
  declare rnode char(6) default substring_index(edge, ':', -1); -- right node of edge.
  declare cedge char(12);
  declare lnode_in_cedge char(6);
  declare rnode_in_cedge char(6);
  declare found int default 1;
  declare OGset varchar(512) default '';
  SET OGset= concat('',eset);

WHILE length(eset)!=0  DO
set cedge = substring_index(eset, ',', 1);
SET lnode_in_cedge= substring_index(cedge, ':', 1);
SET rnode_in_cedge= substring_index(cedge, ':', -1);
IF lnode= lnode_in_cedge OR lnode= rnode_in_cedge THEN
    set found=0;
END IF;

SET eset  = REMOVE_FIRST(eset);
END WHILE;

IF found=0 THEN
BEGIN
    SET eset= OGset;
    WHILE length(eset)!=0  DO
    set cedge = substring_index(eset, ',', 1);
    SET lnode_in_cedge= substring_index(cedge, ':', 1);
    SET rnode_in_cedge= substring_index(cedge, ':', -1);
        IF rnode= lnode_in_cedge OR rnode= rnode_in_cedge THEN
            BEGIN
            SET leaf= false;
            SET found= 2;
            END;
         END IF;
         SET eset  = REMOVE_FIRST(eset);
     END WHILE;
END;
END IF;


IF found!=2 THEN
SET leaf=true;
END IF;


END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM