/
AbstractStatementAdapter.java
206 lines (172 loc) · 7.31 KB
/
AbstractStatementAdapter.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.driver.jdbc.adapter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationStatement;
import org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteTemplate;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Collection;
/**
* Adapter for {@code Statement}.
*/
@RequiredArgsConstructor
public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperationStatement {
private final Class<? extends Statement> targetClass;
private boolean closed;
private boolean poolable;
private int fetchSize;
private int fecthDirection;
private final ForceExecuteTemplate<Statement> forceExecuteTemplate = new ForceExecuteTemplate<>();
@SuppressWarnings("unchecked")
@Override
public final void close() throws SQLException {
closed = true;
try {
forceExecuteTemplate.execute((Collection) getRoutedStatements(), Statement::close);
} finally {
getRoutedStatements().clear();
}
}
@Override
public final boolean isClosed() {
return closed;
}
@Override
public final boolean isPoolable() {
return poolable;
}
@SuppressWarnings("unchecked")
@Override
public final void setPoolable(final boolean poolable) throws SQLException {
this.poolable = poolable;
recordMethodInvocation(targetClass, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setPoolable(poolable));
}
@Override
public final int getFetchSize() {
return fetchSize;
}
@SuppressWarnings("unchecked")
@Override
public final void setFetchSize(final int rows) throws SQLException {
fetchSize = rows;
recordMethodInvocation(targetClass, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setFetchSize(rows));
}
@Override
public int getFetchDirection() throws SQLException {
return fecthDirection;
}
@Override
public void setFetchDirection(final int direction) throws SQLException {
this.fecthDirection = direction;
recordMethodInvocation(targetClass, "setFetchDirection", new Class[] {int.class}, new Object[] {direction});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setFetchDirection(direction));
}
@SuppressWarnings("unchecked")
@Override
public final void setEscapeProcessing(final boolean enable) throws SQLException {
recordMethodInvocation(targetClass, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setEscapeProcessing(enable));
}
@SuppressWarnings("unchecked")
@Override
public final void cancel() throws SQLException {
forceExecuteTemplate.execute((Collection) getRoutedStatements(), Statement::cancel);
}
@Override
public final int getUpdateCount() throws SQLException {
if (isAccumulate()) {
return accumulate();
} else {
Collection<? extends Statement> statements = getRoutedStatements();
if (statements.isEmpty()) {
return -1;
}
return getRoutedStatements().iterator().next().getUpdateCount();
}
}
private int accumulate() throws SQLException {
long result = 0;
boolean hasResult = false;
for (Statement each : getRoutedStatements()) {
int updateCount = each.getUpdateCount();
if (updateCount > -1) {
hasResult = true;
}
result += updateCount;
}
if (result > Integer.MAX_VALUE) {
result = Integer.MAX_VALUE;
}
return hasResult ? Long.valueOf(result).intValue() : -1;
}
@Override
public final SQLWarning getWarnings() {
return null;
}
@Override
public final void clearWarnings() {
}
@Override
public final boolean getMoreResults() throws SQLException {
boolean result = false;
for (Statement each : getRoutedStatements()) {
result = each.getMoreResults();
}
return result;
}
@Override
public final boolean getMoreResults(final int current) {
return false;
}
@Override
public final int getMaxFieldSize() throws SQLException {
return getRoutedStatements().isEmpty() ? 0 : getRoutedStatements().iterator().next().getMaxFieldSize();
}
@SuppressWarnings("unchecked")
@Override
public final void setMaxFieldSize(final int max) throws SQLException {
recordMethodInvocation(targetClass, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setMaxFieldSize(max));
}
// TODO Confirm MaxRows for multiple databases is need special handle. eg: 10 statements maybe MaxRows / 10
@Override
public final int getMaxRows() throws SQLException {
return getRoutedStatements().isEmpty() ? -1 : getRoutedStatements().iterator().next().getMaxRows();
}
@SuppressWarnings("unchecked")
@Override
public final void setMaxRows(final int max) throws SQLException {
recordMethodInvocation(targetClass, "setMaxRows", new Class[] {int.class}, new Object[] {max});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setMaxRows(max));
}
@Override
public final int getQueryTimeout() throws SQLException {
return getRoutedStatements().isEmpty() ? 0 : getRoutedStatements().iterator().next().getQueryTimeout();
}
@SuppressWarnings("unchecked")
@Override
public final void setQueryTimeout(final int seconds) throws SQLException {
recordMethodInvocation(targetClass, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), statement -> statement.setQueryTimeout(seconds));
}
protected abstract boolean isAccumulate();
protected abstract Collection<? extends Statement> getRoutedStatements();
}