-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdeep-genres.sql
126 lines (123 loc) · 3.64 KB
/
deep-genres.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- DEBUG => this.dbc._native.prepare(cds.utils.fs.readFileSync(__dirname + '/deep-genres.sql','utf-8')).exec([JSON.stringify(query.UPDATE.data)])
DO (IN JSON NCLOB => ?) BEGIN
-- Extract genres with a depth of 3 (like: '$.children[*].children[*]')
Genres = SELECT
NEW.name,
NEW."$.NAME",
NEW.descr,
NEW."$.DESCR",
NEW.ID,
NEW."$.ID",
NEW.parent_ID,
NEW."$.PARENT_ID",
NEW."$.CHILDREN"
FROM
JSON_TABLE(
:JSON,
'$' COLUMNS(
name NVARCHAR(1020) PATH '$.name',
"$.NAME" NVARCHAR(2147483647) FORMAT JSON PATH '$.name',
descr NVARCHAR(4000) PATH '$.descr',
"$.DESCR" NVARCHAR(2147483647) FORMAT JSON PATH '$.descr',
ID INT PATH '$.ID',
"$.ID" NVARCHAR(2147483647) FORMAT JSON PATH '$.ID',
parent_ID INT PATH '$.parent_ID',
"$.PARENT_ID" NVARCHAR(2147483647) FORMAT JSON PATH '$.parent_ID',
"$.CHILDREN" NVARCHAR(2147483647) FORMAT JSON PATH '$.children'
)
ERROR ON ERROR
) AS NEW
UNION ALL
SELECT
NEW.name,
NEW."$.NAME",
NEW.descr,
NEW."$.DESCR",
NEW.ID,
NEW."$.ID",
NEW.parent_ID,
NEW."$.PARENT_ID",
NEW."$.CHILDREN"
FROM
JSON_TABLE(
:JSON,
'$.children[*]' COLUMNS(
name NVARCHAR(1020) PATH '$.name',
"$.NAME" NVARCHAR(2147483647) FORMAT JSON PATH '$.name',
descr NVARCHAR(4000) PATH '$.descr',
"$.DESCR" NVARCHAR(2147483647) FORMAT JSON PATH '$.descr',
ID INT PATH '$.ID',
"$.ID" NVARCHAR(2147483647) FORMAT JSON PATH '$.ID',
parent_ID INT PATH '$.parent_ID',
"$.PARENT_ID" NVARCHAR(2147483647) FORMAT JSON PATH '$.parent_ID',
"$.CHILDREN" NVARCHAR(2147483647) FORMAT JSON PATH '$.children'
)
ERROR ON ERROR
) AS NEW
UNION ALL
SELECT
NEW.name,
NEW."$.NAME",
NEW.descr,
NEW."$.DESCR",
NEW.ID,
NEW."$.ID",
NEW.parent_ID,
NEW."$.PARENT_ID",
NEW."$.CHILDREN"
FROM
JSON_TABLE(
:JSON,
'$.children[*].children[*]' COLUMNS(
name NVARCHAR(1020) PATH '$.name',
"$.NAME" NVARCHAR(2147483647) FORMAT JSON PATH '$.name',
descr NVARCHAR(4000) PATH '$.descr',
"$.DESCR" NVARCHAR(2147483647) FORMAT JSON PATH '$.descr',
ID INT PATH '$.ID',
"$.ID" NVARCHAR(2147483647) FORMAT JSON PATH '$.ID',
parent_ID INT PATH '$.parent_ID',
"$.PARENT_ID" NVARCHAR(2147483647) FORMAT JSON PATH '$.parent_ID',
"$.CHILDREN" NVARCHAR(2147483647) FORMAT JSON PATH '$.children'
)
ERROR ON ERROR
) AS NEW;
-- DELETE all children of parents that are no longer in the dataset
DELETE FROM TestService_Genres WHERE
(parent_ID) IN (SELECT ID FROM :Genres WHERE "$.CHILDREN" IS NOT NULL)
AND
(ID) NOT IN (SELECT ID FROM :Genres);
-- UPSERT new deep genres entries
UPSERT sap_capire_bookshop_Genres (name, descr, ID, parent_ID)
SELECT
CASE
WHEN OLD.ID IS NULL THEN NEW.name
ELSE (
CASE
WHEN "$.NAME" IS NULL THEN OLD.name
ELSE NEW.name
END
)
END as name,
CASE
WHEN OLD.ID IS NULL THEN NEW.descr
ELSE (
CASE
WHEN "$.DESCR" IS NULL THEN OLD.descr
ELSE NEW.descr
END
)
END as descr,
NEW.ID as ID,
CASE
WHEN OLD.ID IS NULL THEN NEW.parent_ID
ELSE (
CASE
WHEN "$.PARENT_ID" IS NULL THEN OLD.parent_ID
ELSE NEW.parent_ID
END
)
END as parent_ID
FROM
:Genres AS NEW
LEFT JOIN sap_capire_bookshop_Genres AS OLD ON NEW.ID = OLD.ID;
END;