1 /*
2 * Copyright 2018-2020 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.springframework.data.jpa.repository.query;
17
18 import java.util.List;
19
20 import org.springframework.lang.Nullable;
21 import org.springframework.util.StringUtils;
22
23 /**
24 * A wrapper for a String representation of a query offering information about the query.
25 *
26 * @author Jens Schauder
27 * @since 2.0.3
28 */
29 interface DeclaredQuery {
30
31 /**
32 * Creates a {@literal DeclaredQuery} from a query {@literal String}.
33 *
34 * @param query might be {@literal null} or empty.
35 * @return a {@literal DeclaredQuery} instance even for a {@literal null} or empty argument.
36 */
37 static DeclaredQuery of(@Nullable String query) {
38 return StringUtils.isEmpty(query) ? EmptyDeclaredQuery.EMPTY_QUERY : new StringQuery(query);
39 }
40
41 /**
42 * @return whether the underlying query has at least one named parameter.
43 */
44 boolean hasNamedParameter();
45
46 /**
47 * Returns the query string.
48 */
49 String getQueryString();
50
51 /**
52 * Returns the main alias used in the query.
53 *
54 * @return the alias
55 */
56 @Nullable
57 String getAlias();
58
59 /**
60 * Returns whether the query is using a constructor expression.
61 *
62 * @since 1.10
63 */
64 boolean hasConstructorExpression();
65
66 /**
67 * Returns whether the query uses the default projection, i.e. returns the main alias defined for the query.
68 */
69 boolean isDefaultProjection();
70
71 /**
72 * Returns the {@link StringQuery.ParameterBinding}s registered.
73 */
74 List<StringQuery.ParameterBinding> getParameterBindings();
75
76 /**
77 * Creates a new {@literal DeclaredQuery} representing a count query, i.e. a query returning the number of rows to be
78 * expected from the original query, either derived from the query wrapped by this instance or from the information
79 * passed as arguments.
80 *
81 * @param countQuery an optional query string to be used if present.
82 * @param countQueryProjection an optional return type for the query.
83 * @return a new {@literal DeclaredQuery} instance.
84 */
85 DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection);
86
87 /**
88 * @return whether paging is implemented in the query itself, e.g. using SpEL expressions.
89 * @since 2.0.6
90 */
91 default boolean usesPaging() {
92 return false;
93 }
94
95 /**
96 * Returns whether the query uses JDBC style parameters, i.e. parameters denoted by a simple ? without any index or
97 * name.
98 *
99 * @return Whether the query uses JDBC style parameters.
100 * @since 2.0.6
101 */
102 boolean usesJdbcStyleParameters();
103 }
104