diff --git a/ModelCatalogueCorePluginTestApp/src/groovy/org/modelcatalogue/core/mysql-procedures.sql b/ModelCatalogueCorePluginTestApp/src/groovy/org/modelcatalogue/core/mysql-procedures.sql index a44506bc8e..6559b75039 100644 --- a/ModelCatalogueCorePluginTestApp/src/groovy/org/modelcatalogue/core/mysql-procedures.sql +++ b/ModelCatalogueCorePluginTestApp/src/groovy/org/modelcatalogue/core/mysql-procedures.sql @@ -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 @@ -70,6 +79,5 @@ DETERMINISTIC ); END $$ - --- DELIMITER ; +# DELIMITER ;