Skip to content

Commit b134be3

Browse files
authored
Merge pull request #40 from AlexanderYastrebov/bugfix/GH-37-immutable-stored-procedure
GH-37 Make stored procedure immutable
2 parents 4e9e5db + 2cf3398 commit b134be3

File tree

3 files changed

+117
-117
lines changed

3 files changed

+117
-117
lines changed

src/main/java/de/zalando/sprocwrapper/proxy/SProcCallHandler.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ public Map<Method, StoredProcedure> handle(Class c, SProcServiceAnnotationHandle
122122

123123
RowMapper<?> resultMapper = this.getRowMapper(scA);
124124

125-
boolean useValidation = this.isValidationActive(scA, handlerResult);
126-
final StoredProcedure storedProcedure = this.getStoredProcedure(scA, handlerResult, method, name, sprocStrategy, resultMapper, useValidation);
125+
final boolean useValidation = this.isValidationActive(scA, handlerResult);
126+
127+
final List<ShardKeyParameter> shardKeyParameters = new ArrayList<>();
128+
final List<StoredProcedureParameter> params = new ArrayList<>();
127129

128130
int pos = 0;
129131
for (final Annotation[] as : method.getParameterAnnotations()) {
@@ -140,7 +142,7 @@ public Map<Method, StoredProcedure> handle(Class c, SProcServiceAnnotationHandle
140142
}
141143

142144
if (a instanceof ShardKey) {
143-
storedProcedure.addShardKeyParameter(pos, clazz);
145+
shardKeyParameters.add(new ShardKeyParameter(pos, clazz));
144146
}
145147

146148
if (a instanceof SProcParam) {
@@ -149,7 +151,7 @@ public Map<Method, StoredProcedure> handle(Class c, SProcServiceAnnotationHandle
149151
final String dbTypeName = sParam.type();
150152

151153
try {
152-
storedProcedure.addParam(StoredProcedureParameter.createParameter(clazz, genericType,
154+
params.add(StoredProcedureParameter.createParameter(clazz, genericType,
153155
method, dbTypeName, sParam.sqlType(), pos, sParam.sensitive()));
154156
} catch (final InstantiationException | IllegalAccessException e) {
155157
LOG.error("Could not instantiate StoredProcedureParameter. ABORTING.", e);
@@ -160,22 +162,27 @@ public Map<Method, StoredProcedure> handle(Class c, SProcServiceAnnotationHandle
160162

161163
pos++;
162164
}
165+
final StoredProcedure storedProcedure = createStoredProcedure(scA, handlerResult, method, name, params, sprocStrategy, shardKeyParameters, resultMapper, useValidation);
166+
163167
result.put(method, storedProcedure);
164168
}
165169
return result;
166170
}
167171

168-
private StoredProcedure getStoredProcedure(SProcCall scA, SProcServiceAnnotationHandler.HandlerResult handlerResult, Method method, String name, VirtualShardKeyStrategy sprocStrategy, RowMapper<?> resultMapper, boolean useValidation) {
172+
private StoredProcedure createStoredProcedure(SProcCall scA, SProcServiceAnnotationHandler.HandlerResult handlerResult,
173+
Method method, String name, List<StoredProcedureParameter> params,
174+
VirtualShardKeyStrategy sprocStrategy, List<ShardKeyParameter> shardKeyParameters,
175+
RowMapper<?> resultMapper, boolean useValidation) {
169176
try {
170177
SProcService.WriteTransaction writeTransaction = mapSprocWriteTransactionToServiceWriteTransaction(scA.shardedWriteTransaction(), handlerResult);
171178

172-
StoredProcedure storedProcedure = new StoredProcedure(name, method.getGenericReturnType(), sprocStrategy,
179+
String query = !"".equals(scA.sql()) ? scA.sql() : null;
180+
181+
StoredProcedure storedProcedure = new StoredProcedure(name, query, params, method.getGenericReturnType(), sprocStrategy, shardKeyParameters,
173182
scA.runOnAllShards(), scA.searchShards(), scA.parallel(), resultMapper,
174183
scA.timeoutInMilliSeconds(), new SProcCall.AdvisoryLock(scA.adivsoryLockName(),scA.adivsoryLockId()), useValidation, scA.readOnly(),
175184
writeTransaction);
176-
if (!"".equals(scA.sql())) {
177-
storedProcedure.setQuery(scA.sql());
178-
}
185+
179186
return storedProcedure;
180187
} catch (final InstantiationException | IllegalAccessException e) {
181188
LOG.error("Could not instantiate StoredProcedure. ABORTING.", e);
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package de.zalando.sprocwrapper.proxy;
22

33
/**
4-
* @author jmussler
4+
* @author jmussler
55
*/
6-
public class ShardKeyParameter {
7-
public int javaPos;
6+
class ShardKeyParameter {
87

9-
public ShardKeyParameter(final int j) {
10-
javaPos = j;
8+
private final int pos;
9+
private final Class<?> type;
10+
11+
public ShardKeyParameter(final int pos, final Class<?> type) {
12+
this.pos = pos;
13+
this.type = type;
14+
}
15+
16+
public int getPos() {
17+
return pos;
18+
}
19+
20+
public Class<?> getType() {
21+
return type;
1122
}
1223
}

0 commit comments

Comments
 (0)