-
Notifications
You must be signed in to change notification settings - Fork 863
/
StatementBuilder.java
149 lines (129 loc) · 4.64 KB
/
StatementBuilder.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
/*
* 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.api.core.cql;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.config.DriverConfigProfile;
import com.datastax.oss.driver.api.core.metadata.token.Token;
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;
import net.jcip.annotations.NotThreadSafe;
/**
* Handle options common to all statement builders.
*
* @see SimpleStatement#builder(String)
* @see BatchStatement#builder(BatchType)
* @see PreparedStatement#boundStatementBuilder()
*/
@NotThreadSafe
public abstract class StatementBuilder<T extends StatementBuilder<T, S>, S extends Statement<S>> {
@SuppressWarnings("unchecked")
private final T self = (T) this;
protected String configProfileName;
protected DriverConfigProfile configProfile;
protected CqlIdentifier routingKeyspace;
protected ByteBuffer routingKey;
protected Token routingToken;
private NullAllowingImmutableMap.Builder<String, ByteBuffer> customPayloadBuilder;
protected Boolean idempotent;
protected boolean tracing;
protected long timestamp = Long.MIN_VALUE;
protected ByteBuffer pagingState;
protected StatementBuilder() {
// nothing to do
}
protected StatementBuilder(S template) {
this.configProfileName = template.getConfigProfileName();
this.configProfile = template.getConfigProfile();
this.customPayloadBuilder =
NullAllowingImmutableMap.<String, ByteBuffer>builder().putAll(template.getCustomPayload());
this.idempotent = template.isIdempotent();
this.tracing = template.isTracing();
this.timestamp = template.getTimestamp();
this.pagingState = template.getPagingState();
}
/** @see Statement#setConfigProfileName(String) */
public T withConfigProfileName(String configProfileName) {
this.configProfileName = configProfileName;
return self;
}
/** @see Statement#setConfigProfile(DriverConfigProfile) */
public T withConfigProfile(DriverConfigProfile configProfile) {
this.configProfile = configProfile;
this.configProfileName = null;
return self;
}
/** @see Statement#setRoutingKeyspace(CqlIdentifier) */
public T withRoutingKeyspace(CqlIdentifier routingKeyspace) {
this.routingKeyspace = routingKeyspace;
return self;
}
/**
* Shortcut for {@link #withRoutingKeyspace(CqlIdentifier)
* withRoutingKeyspace(CqlIdentifier.fromCql(routingKeyspaceName))}.
*/
public T withRoutingKeyspace(String routingKeyspaceName) {
return withRoutingKeyspace(CqlIdentifier.fromCql(routingKeyspaceName));
}
/** @see Statement#setRoutingKey(ByteBuffer) */
public T withRoutingKey(ByteBuffer routingKey) {
this.routingKey = routingKey;
return self;
}
/** @see Statement#setRoutingToken(Token) */
public T withRoutingToken(Token routingToken) {
this.routingToken = routingToken;
return self;
}
/** @see Statement#setCustomPayload(Map) */
public T addCustomPayload(String key, ByteBuffer value) {
if (customPayloadBuilder == null) {
customPayloadBuilder = NullAllowingImmutableMap.builder();
}
customPayloadBuilder.put(key, value);
return self;
}
/** @see Statement#setCustomPayload(Map) */
public T clearCustomPayload() {
customPayloadBuilder = null;
return self;
}
/** @see Statement#setIdempotent(Boolean) */
public T withIdempotence(boolean idempotent) {
this.idempotent = idempotent;
return self;
}
/** @see Statement#setTracing(boolean) */
public T withTracing() {
this.tracing = true;
return self;
}
/** @see Statement#setTimestamp(long) */
public T withTimestamp(long timestamp) {
this.timestamp = timestamp;
return self;
}
/** @see Statement#setPagingState(ByteBuffer) */
public T withPagingState(ByteBuffer pagingState) {
this.pagingState = pagingState;
return self;
}
protected Map<String, ByteBuffer> buildCustomPayload() {
return (customPayloadBuilder == null) ? Collections.emptyMap() : customPayloadBuilder.build();
}
public abstract S build();
}