Skip to content

Commit ce15f91

Browse files
committed
Unifying ParameterBinding hierarchy into a single class and reducing its mutability
1 parent 14bbf5c commit ce15f91

5 files changed

Lines changed: 20 additions & 50 deletions

File tree

cayenne/src/main/java/org/apache/cayenne/access/jdbc/BatchAction.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ public class BatchAction extends BaseSQLAction {
5555
protected BatchQuery query;
5656
protected RowDescriptor keyRowDescriptor;
5757

58-
private static void bind(DbAdapter adapter, PreparedStatement statement, ParameterBinding[] bindings)
59-
throws Exception {
60-
61-
for (ParameterBinding b : bindings) {
62-
if (!b.isDisabled()) {
63-
adapter.bindParameter(statement, b);
64-
}
65-
}
66-
}
67-
6858
/**
6959
* @since 4.0
7060
*/
@@ -132,7 +122,9 @@ protected void runAsBatch(Connection con, BatchTranslator translator, OperationO
132122

133123
ParameterBinding[] bindings = translator.updateBindings(row);
134124
logger.logQueryParameters("batch bind", bindings);
135-
bind(adapter, statement, bindings);
125+
for (ParameterBinding b : bindings) {
126+
adapter.bindParameter(statement, b);
127+
}
136128

137129
statement.addBatch();
138130
}
@@ -192,7 +184,9 @@ protected void runAsIndividualQueries(Connection connection, BatchTranslator tra
192184
ParameterBinding[] bindings = translator.updateBindings(row);
193185
logger.logQueryParameters("bind", bindings);
194186

195-
bind(adapter, statement, bindings);
187+
for (ParameterBinding b : bindings) {
188+
adapter.bindParameter(statement, b);
189+
}
196190

197191
int updated = statement.executeUpdate();
198192
if (useOptimisticLock && updated != 1) {

cayenne/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ private static void bind(DbAdapter adapter, PreparedStatement statement, Paramet
4848

4949
for (ParameterBinding b : bindings) {
5050

51-
if (b.isDisabled()) {
52-
continue;
53-
}
54-
5551
// null DbAttributes are a result of inferior qualifier
5652
// processing (qualifier can't map parameters to DbAttributes
5753
// and therefore only supports standard java types now) hence, a

cayenne/src/main/java/org/apache/cayenne/access/translator/ParameterBinding.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
*/
2929
public class ParameterBinding {
3030

31-
private static final int EXCLUDED_POSITION = -1;
32-
3331
private final DbAttribute attribute;
3432
private final int jdbcType;
3533
private final int scale;
@@ -52,7 +50,20 @@ public ParameterBinding(int jdbcType, int scale, DbAttribute attribute) {
5250
this.attribute = attribute;
5351
this.jdbcType = jdbcType;
5452
this.scale = scale;
55-
this.statementPosition = EXCLUDED_POSITION;
53+
54+
this.statementPosition = -1;
55+
}
56+
57+
/**
58+
* Sets the value, statement position and {@link ExtendedType} of the binding.
59+
*
60+
* @since 5.0
61+
*/
62+
public ParameterBinding reset(int statementPosition, Object value, ExtendedType<?> extendedType) {
63+
this.statementPosition = statementPosition;
64+
this.value = value;
65+
this.extendedType = extendedType;
66+
return this;
5667
}
5768

5869
public Object getValue() {
@@ -63,33 +74,10 @@ public int getStatementPosition() {
6374
return statementPosition;
6475
}
6576

66-
public boolean isDisabled() {
67-
return statementPosition == EXCLUDED_POSITION;
68-
}
69-
7077
public ExtendedType getExtendedType() {
7178
return extendedType;
7279
}
7380

74-
/**
75-
* Marks the binding object as excluded for the current iteration.
76-
*/
77-
public void disable() {
78-
this.statementPosition = EXCLUDED_POSITION;
79-
this.value = null;
80-
this.extendedType = null;
81-
}
82-
83-
/**
84-
* Sets the value, statement position and {@link ExtendedType} of the binding.
85-
*/
86-
public ParameterBinding reset(int statementPosition, Object value, ExtendedType<?> extendedType) {
87-
this.statementPosition = statementPosition;
88-
this.value = value;
89-
this.extendedType = extendedType;
90-
return this;
91-
}
92-
9381
public int getJdbcType() {
9482
return jdbcType;
9583
}

cayenne/src/main/java/org/apache/cayenne/log/CompactSlf4jJdbcEventLogger.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ private Map<String, List<String>> collectBindings(ParameterBinding[] bindings) {
112112
String key = null;
113113
String value;
114114
for (ParameterBinding b : bindings) {
115-
if (b.isDisabled()) {
116-
continue;
117-
}
118-
119115
DbAttribute attribute = b.getAttribute();
120116
if (attribute != null) {
121117
key = attribute.getName();

cayenne/src/main/java/org/apache/cayenne/log/Slf4jJdbcEventLogger.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ protected void appendParameters(StringBuilder buffer, String label, ParameterBin
100100
for (int i = 0, j = 1; i < len; i++) {
101101
ParameterBinding b = bindings[i];
102102

103-
if (b.isDisabled()) {
104-
continue;
105-
}
106-
107103
if (hasIncluded) {
108104
buffer.append(", ");
109105
} else {

0 commit comments

Comments
 (0)