@@ -89,17 +89,21 @@ public Object execute(Object[] parameters) {
89
89
return createQuery (parameterAccessor ).flatMapMany (it -> executeQuery (parameterAccessor , it ));
90
90
}
91
91
92
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
92
93
private Publisher <?> executeQuery (RelationalParameterAccessor parameterAccessor , BindableQuery it ) {
93
94
94
95
ResultProcessor processor = method .getResultProcessor ().withDynamicProjection (parameterAccessor );
95
96
DatabaseClient .GenericExecuteSpec boundQuery = it .bind (databaseClient .sql (it ));
96
97
97
- FetchSpec <?> fetchSpec ;
98
- if (requiresMapping ()) {
99
- EntityRowMapper <?> rowMapper = new EntityRowMapper <>(resolveResultType (processor ), converter );
98
+ FetchSpec <Object > fetchSpec ;
99
+
100
+ if (isExistsQuery ()) {
101
+ fetchSpec = (FetchSpec ) boundQuery .map (row -> true );
102
+ } else if (requiresMapping ()) {
103
+ EntityRowMapper rowMapper = new EntityRowMapper <>(resolveResultType (processor ), converter );
100
104
fetchSpec = new FetchSpecAdapter <>(boundQuery .map (rowMapper ));
101
105
} else {
102
- fetchSpec = boundQuery .fetch ();
106
+ fetchSpec = ( FetchSpec ) boundQuery .fetch ();
103
107
}
104
108
105
109
SqlIdentifier tableName = method .getEntityInformation ().getTableName ();
@@ -143,6 +147,14 @@ private R2dbcQueryExecution getExecutionToWrap(ReturnedType returnedType) {
143
147
return (q , t , c ) -> q .rowsUpdated ();
144
148
}
145
149
150
+ if (isCountQuery ()) {
151
+ return (q , t , c ) -> q .first ().defaultIfEmpty (0L );
152
+ }
153
+
154
+ if (isExistsQuery ()) {
155
+ return (q , t , c ) -> q .first ().defaultIfEmpty (false );
156
+ }
157
+
146
158
if (method .isCollectionQuery ()) {
147
159
return (q , t , c ) -> q .all ();
148
160
}
@@ -158,6 +170,22 @@ private R2dbcQueryExecution getExecutionToWrap(ReturnedType returnedType) {
158
170
*/
159
171
protected abstract boolean isModifyingQuery ();
160
172
173
+ /**
174
+ * Returns whether the query should get a count projection applied.
175
+ *
176
+ * @return
177
+ * @since 1.2
178
+ */
179
+ protected abstract boolean isCountQuery ();
180
+
181
+ /**
182
+ * Returns whether the query should get an exists projection applied.
183
+ *
184
+ * @return
185
+ * @since 1.2
186
+ */
187
+ protected abstract boolean isExistsQuery ();
188
+
161
189
/**
162
190
* Creates a {@link BindableQuery} instance using the given {@link ParameterAccessor}
163
191
*
0 commit comments