Skip to content

Commit

Permalink
Merge pull request #921 from MetadataConsulting/bug/infinite-recursio…
Browse files Browse the repository at this point in the history
…n-in-stored-function

check for processed catalogue elements
  • Loading branch information
davidmilward authored Feb 6, 2017
2 parents 8beec12 + a911ba1 commit b343676
Showing 1 changed file with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,60 +1,69 @@
-- DELIMITER $$
# DELIMITER $$

DROP FUNCTION IF EXISTS AddToSet $$

CREATE FUNCTION AddToSet(NextValues TEXT, CurrentSet TEXT) RETURNS TEXT
DETERMINISTIC
BEGIN
IF LENGTH(CurrentSet) = 0 OR CurrentSet is NULL THEN
RETURN NextValues;
ELSE
RETURN CONCAT(CurrentSet,',',NextValues);
END IF;
END $$

DROP FUNCTION IF EXISTS GetAllDestinations $$

CREATE FUNCTION GetAllDestinations (InitialQueue TEXT, DescendType LONG, LeafType LONG) RETURNS TEXT
DETERMINISTIC
BEGIN
BEGIN

DECLARE rv,q,queue,queue_children,leaves TEXT;
DECLARE queue_length,front_id,pos LONG;
DECLARE rv,q,queue,queue_children,leaves, processed TEXT;
DECLARE queue_length,front_id,pos LONG;

SET rv = '';
SET queue = InitialQueue;
SET queue_length = LENGTH(InitialQueue) - LENGTH(REPLACE(InitialQueue, ',', '')) + 1;
SET rv = '';
SET queue = InitialQueue;
SET queue_length = LENGTH(InitialQueue) - LENGTH(REPLACE(InitialQueue, ',', '')) + 1;

WHILE queue_length > 0 DO
SET front_id = ROUND(queue,0);
IF queue_length = 1 THEN
SET queue = '';
ELSE
SET pos = LOCATE(',',queue) + 1;
SET q = SUBSTR(queue,pos);
SET queue = q;
END IF;
WHILE queue_length > 0 DO
SET front_id = ROUND(queue,0);

SET queue_length = queue_length - 1;
IF queue_length = 1 THEN
SET queue = '';
ELSE
SET pos = LOCATE(',',queue) + 1;
SET q = SUBSTR(queue,pos);
SET queue = q;
END IF;

SELECT IFNULL(qc,'') INTO queue_children
FROM (SELECT GROUP_CONCAT(destination_id) qc
FROM relationship WHERE source_id = front_id AND relationship_type_id = DescendType) A;
SET queue_length = queue_length - 1;

SELECT IFNULL(l,'') INTO leaves
FROM (SELECT GROUP_CONCAT(destination_id) l
FROM relationship WHERE source_id = front_id AND relationship_type_id = LeafType) A;
IF find_in_set(front_id, processed) = 0 THEN
SET processed = AddToSet(front_id, processed);

IF LENGTH(queue_children) = 0 THEN
IF LENGTH(queue) = 0 THEN
SET queue_length = 0;
END IF;
ELSE
IF LENGTH(rv) = 0 THEN
SET rv = leaves;
ELSEIF LENGTH(leaves) > 0 THEN
SET rv = CONCAT(rv,',',leaves);
END IF;
IF LENGTH(queue) = 0 THEN
SET queue = queue_children;
ELSE
SET queue = CONCAT(queue,',',queue_children);
END IF;
SET queue_length = LENGTH(queue) - LENGTH(REPLACE(queue,',','')) + 1;
END IF;
END WHILE;
SELECT IFNULL(qc,'') INTO queue_children
FROM (SELECT GROUP_CONCAT(destination_id) qc
FROM relationship WHERE source_id = front_id AND relationship_type_id = DescendType) A;

RETURN rv;
SELECT IFNULL(l,'') INTO leaves
FROM (SELECT GROUP_CONCAT(destination_id) l
FROM relationship WHERE source_id = front_id AND relationship_type_id = LeafType) A;

END $$
IF LENGTH(queue_children) = 0 THEN
IF LENGTH(queue) = 0 THEN
SET queue_length = 0;
END IF;
ELSE
SET rv = AddToSet(rv, leaves);
SET queue = AddToSet(queue_children, queue);
SET queue_length = LENGTH(queue) - LENGTH(REPLACE(queue,',','')) + 1;
END IF;
END IF;
END WHILE;

RETURN rv;

END $$

DROP FUNCTION IF EXISTS GetTopLevelDataClasses $$
CREATE FUNCTION GetTopLevelDataClasses (DataModelId LONG, DescendType LONG) RETURNS TEXT
Expand All @@ -70,6 +79,5 @@ DETERMINISTIC
);
END $$


-- DELIMITER ;
# DELIMITER ;

0 comments on commit b343676

Please sign in to comment.