-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcypher-recipes.cql
123 lines (107 loc) · 4.94 KB
/
cypher-recipes.cql
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
// Find a node labeled SCHEMA, with attribute "name" set to FOO.
MATCH (n:SCHEMA {name:"FOO"})
RETURN n
// Show number of dangling nodes.
MATCH (n)
WHERE NOT (n)-[]-()
RETURN count(n)
// Show number of nodes without OWNS relation.
MATCH (n)
WHERE NOT (n)<-[:OWNS]-()
RETURN count(n)
// Show number of nodes without REFERENCES relation.
MATCH (n)
WHERE NOT (n)<-[:REFERENCES]-()
RETURN count(n)
// Total destruction, will delete all nodes and relations.
MATCH (n) DETACH DELETE n
// Show count of objects per label.
MATCH (n) WITH DISTINCT labels(n) AS temp, count(n) AS tempCnt UNWIND temp AS type
RETURN type, sum(tempCnt) AS count ORDER BY count DESC
// Find a schema by name and traverse its relations.
MATCH (sourceSchema:SCHEMA {name:"FOO"})-[:OWNS]->(objectOwnedBySource)-[:REFERENCES*..]->(objectOwnedByTarget)<-[:OWNS]-(targetSchema:SCHEMA)
WHERE sourceSchema.name <> targetSchema.name
RETURN sourceSchema,objectOwnedBySource,objectOwnedByTarget,targetSchema
// Show count of object types owned per schema, ignore PUBLIC and SYS.
MATCH (object)<-[:OWNS]-(schema:SCHEMA)
WHERE NOT schema.name="PUBLIC" AND NOT schema.name="SYS"
WITH DISTINCT filter(x IN (labels(object)) WHERE NOT x="ENTITY") AS temp,
schema.name AS schema,
COUNT(object) AS tempCnt
UNWIND temp AS type RETURN schema,type, SUM(tempCnt) AS count ORDER BY count DESC
// Find (indirect) relationships (dependencies) from a schema to any other schema (and remove PUBLIC + SYS from dependsOn).
MATCH (sourceSchema:SCHEMA)-[:OWNS]->(objectOwnedBySource)-[:REFERENCES*..]->(objectOwnedByTarget)<-[:OWNS]-(targetSchema:SCHEMA)
WHERE sourceSchema.name <> targetSchema.name
WITH sourceSchema, targetSchema,
filter(x IN (collect(distinct targetSchema.name)) WHERE NOT x="PUBLIC" AND NOT x="SYS")[0] AS dependsOn
ORDER BY targetSchema.name
WHERE dependsOn <> []
RETURN DISTINCT sourceSchema.name AS schema,
collect(distinct dependsOn)
ORDER BY sourceSchema.name
// Find (indirect) relationships (dependencies) from a schema to any other schema and show object from+to information.
MATCH (sourceSchema:SCHEMA)-[:OWNS]->(objectOwnedBySource)-[:REFERENCES*..]->(objectOwnedByTarget)<-[:OWNS]-(targetSchema:SCHEMA)
WHERE sourceSchema.name <> targetSchema.name
WITH sourceSchema, targetSchema, objectOwnedBySource, objectOwnedByTarget,
filter(x IN (labels(objectOwnedBySource)) WHERE NOT x="ENTITY")[0] AS sourceType,
filter(x IN (labels(objectOwnedByTarget)) WHERE NOT x="ENTITY")[0] AS targetType
RETURN DISTINCT sourceSchema.name AS sourceSchema,
objectOwnedBySource.name AS sourceObject,
sourceType,
targetSchema.name AS targetSchema,
objectOwnedByTarget.name AS targetObject,
targetType,
count(*) AS references
ORDER BY sourceSchema,targetSchema
// Create a list of schemas with a complexity indication by counting objects of a certain type.
MATCH
(schema:SCHEMA)-[:OWNS]->(object)
WITH
schema.name as schema,
object.name as object,
filter(label IN (labels(object))
WHERE label="FUNCTION"
OR label="PACKAGE"
OR label="PROCEDURE"
OR label="TABLE"
OR label="TRIGGER"
OR label="TYPE"
OR label="VIEW"
) AS temp UNWIND temp AS type
RETURN
schema,
count(object) AS score
ORDER BY
score DESC
// Show business domain to schema relations.
// Note: requires you to load and relate DOMAIN type objects in addition to objects loaded from the database.
MATCH (sourceDomain:DOMAIN)-[:USES]->(objectOwnedBySource)-[:REFERENCES*..]->(objectOwnedByTarget)<-[:OWNS]-(targetSchema:SCHEMA)
WITH sourceDomain, targetSchema,
filter(x IN (collect(distinct targetSchema.name)) WHERE NOT x="PUBLIC" AND NOT x="SYS")[0] AS dependsOn
ORDER BY targetSchema.name
WHERE dependsOn <> []
RETURN DISTINCT sourceDomain.name AS domain,
collect(distinct dependsOn) AS schemas
ORDER BY sourceDomain.name
// Show schema to business domain relations.
// Note: requires you to load and relate DOMAIN type objects in addition to objects loaded from the database.
MATCH (sourceSchema:SCHEMA)-[:OWNS]->(objectOwnedBySource)-[:REFERENCES*..]->(objectOwnedByTarget)<-[:USES]-(targetDomain:DOMAIN)
WITH sourceSchema, targetDomain,
filter(x IN (collect(distinct targetDomain.name)) WHERE NOT x="PUBLIC" AND NOT x="SYS")[0] AS dependsOn
ORDER BY sourceSchema.name
WHERE dependsOn <> []
RETURN DISTINCT sourceSchema.name AS schema,
collect(distinct dependsOn) AS domains
ORDER BY sourceSchema.name
// Show domain to domain dependencies
MATCH (sourceDomain:DOMAIN)-[:USES {quantifier:"owner"}]->(objectUsedBySource)-[:REFERENCES*..]->(objectUsedByTarget)<-[:USES]-(targetDomain:DOMAIN)
WHERE sourceDomain.name <> targetDomain.name
WITH sourceDomain, targetDomain,
filter(x IN (collect(distinct targetDomain.name)) WHERE NOT x="PUBLIC" AND NOT x="SYS")[0] AS dependsOn
ORDER BY targetDomain.name
WHERE dependsOn <> []
RETURN DISTINCT sourceDomain.name AS domain,
collect(distinct dependsOn) as dependsOn
ORDER BY sourceDomain.name
//Add a quantifier attribute with a value to a relation.
MATCH (a:ENTITY)-[r:USES]->(b:ENTITY) WHERE NOT exists(r.quantifier) SET r.quantifier = 'owner'