Last active
March 9, 2016 21:23
-
-
Save kbrock/0a51ab150d0fac176d0a to your computer and use it in GitHub Desktop.
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
WITH RECURSIVE search_tree(id, path) AS ( | |
SELECT id, ARRAY[id] | |
FROM services | |
WHERE id = 42 | |
UNION ALL | |
SELECT services.id, path || services.id | |
FROM search_tree | |
JOIN services ON services.service_id = search_tree.id | |
WHERE NOT services.id = ANY(path) | |
) | |
SELECT id FROM search_tree; |
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
puts \ | |
Arel::SelectManager.new.with(:recursive, # TODO ADD: (id, path) | |
Arel::Nodes::As.new(search_tree, | |
Arel::SelectManager.new(arel_table) | |
.project(arel_table[primary_key], Arel::Nodes::NamedFunction.new("ARRAY", [arel_table[primary_key]])) # TODO change: ARRAY[id] | |
.where(base_case) | |
.union(Arel::SelectManager.new(arel_table) | |
.project(arel_table[primary_key], Arel::Nodes::InfixOperation.new("||", search_tree[:path], arel_table[primary_key])) | |
.join(search_tree).on(arel_table[ancestry_parent_column].eq(search_tree[primary_key])) | |
.where(arel_table[primary_key].eq(Arel::Nodes::NamedFunction.new("ANY", [search_tree[:path]])).not) | |
) | |
) | |
).from(search_tree).project(search_tree[primary_key]).to_sql |
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
WITH RECURSIVE "search_tree" AS ( | |
SELECT "services"."id", ARRAY("services"."id") | |
FROM "services" | |
WHERE "services"."id" = 42 | |
UNION | |
SELECT "services"."id", "search_tree"."path" || "services"."id" | |
FROM "services" | |
INNER JOIN "search_tree" ON "services"."service_id" = "search_tree"."id" | |
WHERE NOT ("services"."id" = ANY("search_tree"."path")) | |
) | |
SELECT "search_tree"."id" FROM "search_tree" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment