Optimized nested recipe resolving / prevent infinite nested recipes

This commit is contained in:
Bernd Bestel 2019-10-04 10:30:30 +02:00
parent 5d98140843
commit 039ed54a58
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300

View File

@ -11,9 +11,31 @@ AS (
SELECT rn.recipe_id, r1.includes_recipe_id, rn.servings
FROM recipes_nestings rn, r1 r1
WHERE rn.includes_recipe_id = r1.recipe_id
LIMIT 1000000000 -- This is just a safety limit to prevent infinite loops due to infinite nested recipes
)
SELECT
*,
1 AS id -- Dummy, LessQL needs an id column
FROM r1;
CREATE TRIGGER prevent_self_nested_recipes_INS BEFORE INSERT ON recipes_nestings
BEGIN
SELECT CASE WHEN((
SELECT 1
FROM recipes_nestings
WHERE NEW.recipe_id = NEW.includes_recipe_id
)
NOTNULL) THEN RAISE(ABORT, "Recursive nested recipe detected") END;
END;
CREATE TRIGGER prevent_self_nested_recipes_UPD BEFORE UPDATE ON recipes_nestings
BEGIN
SELECT CASE WHEN((
SELECT 1
FROM recipes_nestings
WHERE NEW.recipe_id = NEW.includes_recipe_id
)
NOTNULL) THEN RAISE(ABORT, "Recursive nested recipe detected") END;
END;
DELETE FROM recipes_nestings
WHERE recipe_id = includes_recipe_id;