/
DaoBase.java
227 lines (195 loc) · 9.18 KB
/
DaoBase.java
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.oss.driver.internal.mapper;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.MappedAsyncPagingIterable;
import com.datastax.oss.driver.api.core.PagingIterable;
import com.datastax.oss.driver.api.core.cql.AsyncResultSet;
import com.datastax.oss.driver.api.core.cql.PrepareRequest;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.core.cql.Statement;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.mapper.annotations.Dao;
import com.datastax.oss.driver.api.mapper.annotations.Query;
import com.datastax.oss.driver.api.mapper.entity.EntityHelper;
import com.datastax.oss.driver.internal.core.cql.DefaultPrepareRequest;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
/** Base class for generated implementations of {@link Dao}-annotated interfaces. */
public class DaoBase {
/** The keyspace id placeholder in {@link Query#value()}. */
public static final String KEYSPACE_ID_PLACEHOLDER = "${keyspaceId}";
/** The table id placeholder in {@link Query#value()}. */
public static final String TABLE_ID_PLACEHOLDER = "${tableId}";
/** The qualified table id placeholder in {@link Query#value()}. */
public static final String QUALIFIED_TABLE_ID_PLACEHOLDER = "${qualifiedTableId}";
private static final CqlIdentifier APPLIED = CqlIdentifier.fromInternal("[applied]");
protected static CompletionStage<PreparedStatement> prepare(
SimpleStatement statement, MapperContext context) {
if (statement == null) {
// Can happen, see replaceKeyspaceAndTablePlaceholders. Simply propagate null, the generated
// methods will know how to deal with this.
return CompletableFuture.completedFuture(null);
} else {
return context
.getSession()
.execute(new DefaultPrepareRequest(statement), PrepareRequest.ASYNC);
}
}
/**
* Replaces {@link #KEYSPACE_ID_PLACEHOLDER}, {@link #TABLE_ID_PLACEHOLDER} and/or {@link
* #QUALIFIED_TABLE_ID_PLACEHOLDER} in a query string, and turns it into a statement.
*
* @param queryString the query string to process.
* @param context the context that contains the keyspace and table that the DAO was created with
* (if any).
* @param defaultEntityTableId the default table name for the entity that is returned from this
* query. Might be {@code null} if the query does not return entities.
* @return the statement, or {@code null} if we don't have enough information to fill the
* placeholders (the generated code will throw, we know it won't try to use the statement).
*/
protected static SimpleStatement replaceKeyspaceAndTablePlaceholders(
String queryString, MapperContext context, CqlIdentifier defaultEntityTableId) {
CqlIdentifier keyspaceId = context.getKeyspaceId();
CqlIdentifier tableId = context.getTableId();
if (tableId == null) {
tableId = defaultEntityTableId;
}
if (queryString.contains(KEYSPACE_ID_PLACEHOLDER)) {
if (keyspaceId == null) {
return null;
} else {
queryString = queryString.replace(KEYSPACE_ID_PLACEHOLDER, keyspaceId.asCql(false));
}
}
if (queryString.contains(TABLE_ID_PLACEHOLDER)) {
if (tableId == null) {
return null;
} else {
queryString = queryString.replace(TABLE_ID_PLACEHOLDER, tableId.asCql(false));
}
}
if (queryString.contains(QUALIFIED_TABLE_ID_PLACEHOLDER)) {
if (tableId == null) {
return null;
} else {
String qualifiedId =
(keyspaceId == null)
? tableId.asCql(false)
: keyspaceId.asCql(false) + '.' + tableId.asCql(false);
queryString = queryString.replace(QUALIFIED_TABLE_ID_PLACEHOLDER, qualifiedId);
}
}
return SimpleStatement.newInstance(queryString);
}
protected final MapperContext context;
protected DaoBase(MapperContext context) {
this.context = context;
}
protected ResultSet execute(Statement<?> statement) {
return context.getSession().execute(statement, Statement.SYNC);
}
protected boolean executeAndMapWasAppliedToBoolean(Statement<?> statement) {
ResultSet rs = execute(statement);
return rs.wasApplied();
}
protected long executeAndMapFirstColumnToLong(Statement<?> statement) {
Row row = executeAndExtractFirstRow(statement);
return extractCount(row);
}
private long extractCount(Row row) {
if (row == null) {
throw new IllegalArgumentException(
"Expected the query to return at least one row "
+ "(return type long is intended for COUNT queries)");
}
if (row.getColumnDefinitions().size() == 0
|| !row.getColumnDefinitions().get(0).getType().equals(DataTypes.BIGINT)) {
throw new IllegalStateException(
"Expected the query to return a column with CQL type BIGINT in first position "
+ "(return type long is intended for COUNT queries)");
}
return row.getLong(0);
}
protected Row executeAndExtractFirstRow(Statement<?> statement) {
return execute(statement).one();
}
protected <EntityT> EntityT executeAndMapToSingleEntity(
Statement<?> statement, EntityHelper<EntityT> entityHelper) {
ResultSet rs = execute(statement);
return asEntity(rs.one(), entityHelper);
}
private <EntityT> EntityT asEntity(Row row, EntityHelper<EntityT> entityHelper) {
return (row == null
// Special case for INSERT IF NOT EXISTS. If the row did not exists, the query returns
// only [applied], we want to return null to indicate there was no previous entity
|| (row.getColumnDefinitions().size() == 1
&& row.getColumnDefinitions().get(0).getName().equals(APPLIED)))
? null
: entityHelper.get(row);
}
protected <EntityT> Optional<EntityT> executeAndMapToOptionalEntity(
Statement<?> statement, EntityHelper<EntityT> entityHelper) {
return Optional.ofNullable(executeAndMapToSingleEntity(statement, entityHelper));
}
protected <EntityT> PagingIterable<EntityT> executeAndMapToEntityIterable(
Statement<?> statement, EntityHelper<EntityT> entityHelper) {
return execute(statement).map(entityHelper::get);
}
protected CompletableFuture<AsyncResultSet> executeAsync(Statement<?> statement) {
CompletionStage<AsyncResultSet> stage =
context.getSession().execute(statement, Statement.ASYNC);
// We use the generic execute which allows null results, but an async processor should always
// return a non-null stage
assert stage != null;
// We allow DAO interfaces to return CompletableFuture instead of CompletionStage. This method
// returns CompletableFuture, which makes the implementation code a bit simpler to generate.
// In practice this has no performance impact, because the default implementation of
// toCompletableFuture in the JDK is `return this`.
return stage.toCompletableFuture();
}
protected CompletableFuture<Void> executeAsyncAndMapToVoid(Statement<?> statement) {
return executeAsync(statement).thenApply(rs -> null);
}
protected CompletableFuture<Boolean> executeAsyncAndMapWasAppliedToBoolean(
Statement<?> statement) {
return executeAsync(statement).thenApply(AsyncResultSet::wasApplied);
}
protected CompletableFuture<Long> executeAsyncAndMapFirstColumnToLong(Statement<?> statement) {
return executeAsyncAndExtractFirstRow(statement).thenApply(this::extractCount);
}
protected CompletableFuture<Row> executeAsyncAndExtractFirstRow(Statement<?> statement) {
return executeAsync(statement).thenApply(AsyncResultSet::one);
}
protected <EntityT> CompletableFuture<EntityT> executeAsyncAndMapToSingleEntity(
Statement<?> statement, EntityHelper<EntityT> entityHelper) {
return executeAsync(statement).thenApply(rs -> asEntity(rs.one(), entityHelper));
}
protected <EntityT> CompletableFuture<Optional<EntityT>> executeAsyncAndMapToOptionalEntity(
Statement<?> statement, EntityHelper<EntityT> entityHelper) {
return executeAsync(statement)
.thenApply(rs -> Optional.ofNullable(asEntity(rs.one(), entityHelper)));
}
protected <EntityT>
CompletableFuture<MappedAsyncPagingIterable<EntityT>> executeAsyncAndMapToEntityIterable(
Statement<?> statement, EntityHelper<EntityT> entityHelper) {
return executeAsync(statement).thenApply(rs -> rs.map(entityHelper::get));
}
}