1
1
package streams.service.sink.strategy
2
2
3
+ import org.neo4j.caniuse.CanIUse.canIUse
4
+ import org.neo4j.caniuse.Cypher
5
+ import org.neo4j.caniuse.Neo4j
3
6
import streams.events.EntityType
4
7
import streams.extensions.quote
5
8
import streams.utils.JSONUtils
@@ -25,7 +28,7 @@ data class CUDNode(override val op: CUDOperations,
25
28
val detach : Boolean = true ,
26
29
val labels : List <String > = emptyList()): CUD() {
27
30
override val type = EntityType .node
28
-
31
+
29
32
fun toMap (): Map <String , Any > {
30
33
return when (op) {
31
34
CUDOperations .delete -> mapOf (" ids" to ids)
@@ -59,7 +62,8 @@ data class CUDRelationship(override val op: CUDOperations,
59
62
}
60
63
61
64
62
- class CUDIngestionStrategy : IngestionStrategy {
65
+ class CUDIngestionStrategy (private val neo4j : Neo4j ): IngestionStrategy {
66
+ private val cypherPrefix = if (canIUse(Cypher .explicitCypher5Selection()).withNeo4j(neo4j)) " CYPHER 5 " else " "
63
67
64
68
companion object {
65
69
@JvmStatic val ID_KEY = " ids"
@@ -87,14 +91,14 @@ class CUDIngestionStrategy: IngestionStrategy {
87
91
}
88
92
89
93
private fun buildNodeCreateStatement (labels : List <String >): String = """
90
- |${StreamsUtils .UNWIND }
94
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
91
95
|CREATE (n${getLabelsAsString(labels)} )
92
96
|SET n = event.properties
93
97
""" .trimMargin()
94
98
95
99
private fun buildRelCreateStatement (from : NodeRelMetadata , to : NodeRelMetadata ,
96
100
rel_type : String ): String = """
97
- |${StreamsUtils .UNWIND }
101
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
98
102
|${buildNodeLookupByIds(keyword = from.getOperation(), ids = from.ids, labels = from.labels, identifier = FROM_KEY , field = FROM_KEY )}
99
103
|${StreamsUtils .WITH_EVENT_FROM }
100
104
|${buildNodeLookupByIds(keyword = to.getOperation(), ids = to.ids, labels = to.labels, identifier = TO_KEY , field = TO_KEY )}
@@ -103,14 +107,14 @@ class CUDIngestionStrategy: IngestionStrategy {
103
107
""" .trimMargin()
104
108
105
109
private fun buildNodeMergeStatement (labels : List <String >, ids : Set <String >): String = """
106
- |${StreamsUtils .UNWIND }
110
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
107
111
|${buildNodeLookupByIds(keyword = " MERGE" , ids = ids, labels = labels)}
108
112
|SET n += event.properties
109
113
""" .trimMargin()
110
114
111
115
private fun buildRelMergeStatement (from : NodeRelMetadata , to : NodeRelMetadata ,
112
116
rel_type : String ): String = """
113
- |${StreamsUtils .UNWIND }
117
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
114
118
|${buildNodeLookupByIds(keyword = from.getOperation(), ids = from.ids, labels = from.labels, identifier = FROM_KEY , field = FROM_KEY )}
115
119
|${StreamsUtils .WITH_EVENT_FROM }
116
120
|${buildNodeLookupByIds(keyword = to.getOperation(), ids = to.ids, labels = to.labels, identifier = TO_KEY , field = TO_KEY )}
@@ -119,29 +123,29 @@ class CUDIngestionStrategy: IngestionStrategy {
119
123
""" .trimMargin()
120
124
121
125
private fun buildNodeUpdateStatement (labels : List <String >, ids : Set <String >): String = """
122
- |${StreamsUtils .UNWIND }
126
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
123
127
|${buildNodeLookupByIds(ids = ids, labels = labels)}
124
128
|SET n += event.properties
125
129
""" .trimMargin()
126
130
127
131
private fun buildRelUpdateStatement (from : NodeRelMetadata , to : NodeRelMetadata ,
128
132
rel_type : String ): String = """
129
- |${StreamsUtils .UNWIND }
133
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
130
134
|${buildNodeLookupByIds(ids = from.ids, labels = from.labels, identifier = FROM_KEY , field = FROM_KEY )}
131
135
|${buildNodeLookupByIds(ids = to.ids, labels = to.labels, identifier = TO_KEY , field = TO_KEY )}
132
136
|MATCH ($FROM_KEY )-[r:${rel_type.quote()} ]->($TO_KEY )
133
137
|SET r += event.properties
134
138
""" .trimMargin()
135
139
136
140
private fun buildDeleteStatement (labels : List <String >, ids : Set <String >, detach : Boolean ): String = """
137
- |${StreamsUtils .UNWIND }
141
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
138
142
|${buildNodeLookupByIds(ids = ids, labels = labels)}
139
143
|${if (detach) " DETACH " else " " } DELETE n
140
144
""" .trimMargin()
141
145
142
146
private fun buildRelDeleteStatement (from : NodeRelMetadata , to : NodeRelMetadata ,
143
147
rel_type : String ): String = """
144
- |${StreamsUtils .UNWIND }
148
+ |${cypherPrefix}${ StreamsUtils .UNWIND }
145
149
|${buildNodeLookupByIds(ids = from.ids, labels = from.labels, identifier = FROM_KEY , field = FROM_KEY )}
146
150
|${buildNodeLookupByIds(ids = to.ids, labels = to.labels, identifier = TO_KEY , field = TO_KEY )}
147
151
|MATCH ($FROM_KEY )-[r:${rel_type.quote()} ]->($TO_KEY )
0 commit comments