Skip to content

Commit c7f041a

Browse files
committed
Create MP DELETE Subtrees.md
1 parent d10813f commit c7f041a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

MP DELETE Subtrees.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: default
3+
title: DELETE Subtrees
4+
nav_order: 7
5+
parent: Materialized Paths
6+
permalink: /mat-paths/delete
7+
---
8+
9+
The following query performs a simple deletion via the database DELETE statement and associated cascades. This query contains a [modify-style prologue](modify#prologue), and the *targets* block prepares a list of deleted record IDs (the DELETE statement, as opposed to UPDATE, does not support joins).
10+
11+
~~~sql
12+
WITH
13+
json_ops(ops) AS (
14+
VALUES
15+
(json(
16+
'[' ||
17+
'{"op":"delete", "path_old":"tcl/compat/zlib1/"},' ||
18+
'{"op":"delete", "path_old":"BAZ/dev/git4win/x32/mingw32/share/"}' ||
19+
']'
20+
))
21+
),
22+
base_ops AS (
23+
SELECT
24+
"key" + 1 AS opid,
25+
json_extract(value, '$.op') AS op,
26+
json_extract(value, '$.path_old') AS path_old,
27+
json_extract(value, '$.path_new') AS path_new
28+
FROM json_ops AS jo, json_each(jo.ops) AS terms
29+
),
30+
targets AS (SELECT path FROM categories, base_ops WHERE path like path_old || '%')
31+
DELETE FROM categories WHERE path IN targets;
32+
~~~

0 commit comments

Comments
 (0)