-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
AbstractStatementAdapter.java
222 lines (193 loc) · 6.99 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationStatement;
import lombok.RequiredArgsConstructor;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;
/**
* 静态语句对象适配类.
*
* @author gaohongtao
*/
@RequiredArgsConstructor
public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperationStatement {
private final Class<? extends Statement> recordTargetClass;
private boolean closed;
private boolean poolable;
private int fetchSize;
@Override
@SuppressWarnings("unchecked")
public final void close() throws SQLException {
closed = true;
getRoutedStatements().clear();
Collection<SQLException> exceptions = new LinkedList<>();
for (Statement each : getRoutedStatements()) {
try {
each.close();
} catch (final SQLException ex) {
exceptions.add(ex);
}
}
throwSQLExceptionIfNecessary(exceptions);
}
@Override
public final boolean isClosed() throws SQLException {
return closed;
}
@Override
public final boolean isPoolable() throws SQLException {
return poolable;
}
@Override
public final void setPoolable(final boolean poolable) throws SQLException {
this.poolable = poolable;
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
return;
}
for (Statement each : getRoutedStatements()) {
each.setPoolable(poolable);
}
}
@Override
public final int getFetchSize() throws SQLException {
return fetchSize;
}
@Override
public final void setFetchSize(final int rows) throws SQLException {
this.fetchSize = rows;
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
return;
}
for (Statement each : getRoutedStatements()) {
each.setFetchSize(rows);
}
}
@Override
public final void setEscapeProcessing(final boolean enable) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
return;
}
for (Statement each : getRoutedStatements()) {
each.setEscapeProcessing(enable);
}
}
@Override
public final void cancel() throws SQLException {
for (Statement each : getRoutedStatements()) {
each.cancel();
}
}
@Override
public final void setCursorName(final String name) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setCursorName", new Class[] {String.class}, new Object[] {name});
return;
}
for (Statement each : getRoutedStatements()) {
each.setCursorName(name);
}
}
@Override
public final int getUpdateCount() throws SQLException {
long result = 0;
boolean hasResult = false;
for (Statement each : getRoutedStatements()) {
if (each.getUpdateCount() > -1) {
hasResult = true;
}
result += each.getUpdateCount();
}
if (result > Integer.MAX_VALUE) {
result = Integer.MAX_VALUE;
}
return hasResult ? Long.valueOf(result).intValue() : -1;
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}
@Override
public void clearWarnings() throws SQLException {
}
/*
* 只有存储过程会出现多结果集, 因此不支持.
*/
@Override
public final boolean getMoreResults() throws SQLException {
return false;
}
@Override
public final boolean getMoreResults(final int current) throws SQLException {
return false;
}
@Override
public final int getMaxFieldSize() throws SQLException {
return getRoutedStatements().isEmpty() ? 0 : getRoutedStatements().iterator().next().getMaxFieldSize();
}
@Override
public final void setMaxFieldSize(final int max) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
return;
}
for (Statement each : getRoutedStatements()) {
each.setMaxFieldSize(max);
}
}
// TODO 未来需要确认MaxRows是否在多数据库情况下需要特殊处理,以满足校验需要. 如: 10个statement可能需要将MaxRows / 10
@Override
public final int getMaxRows() throws SQLException {
return getRoutedStatements().isEmpty() ? -1 : getRoutedStatements().iterator().next().getMaxRows();
}
@Override
public final void setMaxRows(final int max) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setMaxRows", new Class[] {int.class}, new Object[] {max});
return;
}
for (Statement each : getRoutedStatements()) {
each.setMaxRows(max);
}
}
@Override
public final int getQueryTimeout() throws SQLException {
return getRoutedStatements().isEmpty() ? 0 : getRoutedStatements().iterator().next().getQueryTimeout();
}
@Override
public final void setQueryTimeout(final int seconds) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
return;
}
for (Statement each : getRoutedStatements()) {
each.setQueryTimeout(seconds);
}
}
/**
* 获取路由的静态语句对象集合.
*
* @return 路由的静态语句对象集合
*/
protected abstract Collection<? extends Statement> getRoutedStatements();
}