38
38
import org .springframework .data .neo4j .core .mapping .NodeDescription ;
39
39
import org .springframework .data .neo4j .core .schema .Property ;
40
40
import org .springframework .lang .Nullable ;
41
+ import org .springframework .util .Assert ;
41
42
42
43
/**
43
44
* Collects the parts of a Cypher query to be handed over to the Cypher generator.
47
48
*/
48
49
@ API (status = API .Status .INTERNAL , since = "6.0.4" )
49
50
public final class QueryFragments {
51
+ // todo put me into something referencable for the bean creation
52
+ public static final CustomStatementKreator KREATOR = new CustomStatementKreator () {
53
+ @ Override
54
+ public Statement createStatement (Neo4jPersistentEntity <?> neo4jPersistentEntity , List <PatternElement > matchOn , Condition condition , Predicate <PropertyFilter .RelaxedPropertyPath > includeField , boolean isDistinctReturn , Collection <Expression > returnExpressions , Collection <SortItem > orderBy , Long skip , Number limit , Expression deleteExpression ) {
55
+
56
+ StatementBuilder .OngoingReadingWithoutWhere match = null ;
57
+
58
+ for (PatternElement patternElement : matchOn ) {
59
+ if (match == null ) {
60
+ match = Cypher .match (matchOn .get (0 ));
61
+ } else {
62
+ match = match .match (patternElement );
63
+ }
64
+ }
65
+
66
+ StatementBuilder .OngoingReadingWithWhere matchWithWhere = match .where (condition );
67
+
68
+ if (deleteExpression != null ) {
69
+ matchWithWhere = (StatementBuilder .OngoingReadingWithWhere ) matchWithWhere .detachDelete (deleteExpression );
70
+ }
71
+
72
+ StatementBuilder .OngoingReadingAndReturn returnPart = isDistinctReturn
73
+ ? matchWithWhere .returningDistinct (returnExpressions )
74
+ : matchWithWhere .returning (returnExpressions );
75
+
76
+ Statement statement = returnPart
77
+ .orderBy (orderBy )
78
+ .skip (skip )
79
+ .limit (limit ).build ();
80
+
81
+ statement .setRenderConstantsAsParameters (false );
82
+ return statement ;
83
+
84
+ }
85
+
86
+ @ Override
87
+ public boolean supports (String repositoryName , String methodName ) {
88
+ return true ;
89
+ }
90
+ };
91
+
92
+ private final CustomStatementKreator kreator ;
50
93
private List <PatternElement > matchOn = new ArrayList <>();
51
94
private Condition condition ;
52
95
private Collection <Expression > returnExpressions = new ArrayList <>();
@@ -56,12 +99,21 @@ public final class QueryFragments {
56
99
private ReturnTuple returnTuple ;
57
100
private boolean scalarValueReturn = false ;
58
101
private Expression deleteExpression ;
102
+
59
103
/**
60
104
* This flag becomes {@literal true} for backward scrolling keyset pagination. Any {@code AbstractNeo4jQuery} will in turn reverse the result list.
61
105
*/
62
106
private boolean requiresReverseSort = false ;
63
107
private Predicate <PropertyFilter .RelaxedPropertyPath > projectingPropertyFilter ;
64
108
109
+ public QueryFragments () {
110
+ this .kreator = KREATOR ;
111
+ }
112
+
113
+ public QueryFragments (CustomStatementKreator kreator ) {
114
+ this .kreator = kreator ;
115
+ }
116
+
65
117
public void addMatchOn (PatternElement match ) {
66
118
this .matchOn .add (match );
67
119
}
@@ -130,38 +182,19 @@ public void setRequiresReverseSort(boolean requiresReverseSort) {
130
182
}
131
183
132
184
public Statement toStatement () {
133
-
134
- StatementBuilder .OngoingReadingWithoutWhere match = null ;
135
-
136
- for (PatternElement patternElement : matchOn ) {
137
- if (match == null ) {
138
- match = Cypher .match (matchOn .get (0 ));
139
- } else {
140
- match = match .match (patternElement );
141
- }
142
- }
143
-
144
- StatementBuilder .OngoingReadingWithWhere matchWithWhere = match .where (condition );
145
-
146
- if (deleteExpression != null ) {
147
- matchWithWhere = (StatementBuilder .OngoingReadingWithWhere ) matchWithWhere .detachDelete (deleteExpression );
148
- }
149
-
150
- StatementBuilder .OngoingReadingAndReturn returnPart = isDistinctReturn ()
151
- ? matchWithWhere .returningDistinct (getReturnExpressions ())
152
- : matchWithWhere .returning (getReturnExpressions ());
153
-
154
- Statement statement = returnPart
155
- .orderBy (getOrderBy ())
156
- .skip (skip )
157
- .limit (limit ).build ();
158
-
159
- statement .setRenderConstantsAsParameters (false );
160
- return statement ;
161
- }
162
-
163
- public Statement toStatement (CustomStatementCreator customStatementCreator ) {
164
- return customStatementCreator .createStatement (matchOn , condition , this ::includeField , (Neo4jPersistentEntity <?>) returnTuple .nodeDescription , getReturnExpressions (), getOrderBy (), skip , limit );
185
+ Assert .notNull (kreator , "Missing kreator bean. This _should_ not happen." );
186
+ return kreator .createStatement (this .returnTuple != null
187
+ ? (Neo4jPersistentEntity <?>) returnTuple .nodeDescription
188
+ : null ,
189
+ matchOn ,
190
+ condition ,
191
+ this ::includeField ,
192
+ isDistinctReturn (),
193
+ getReturnExpressions (),
194
+ getOrderBy (),
195
+ skip ,
196
+ limit ,
197
+ deleteExpression );
165
198
}
166
199
167
200
private Collection <Expression > getReturnExpressions () {
0 commit comments