forked from tarantool/tarantool-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLStatement.java
More file actions
420 lines (352 loc) · 11.4 KB
/
Copy pathSQLStatement.java
File metadata and controls
420 lines (352 loc) · 11.4 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package org.tarantool.jdbc;
import org.tarantool.util.JdbcConstants;
import org.tarantool.util.SQLStates;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLNonTransientException;
import java.sql.SQLTimeoutException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Tarantool {@link Statement} implementation.
* <p>
* Supports {@link ResultSet#TYPE_FORWARD_ONLY} and {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
* types of cursors.
* Supports only {@link ResultSet#HOLD_CURSORS_OVER_COMMIT} holdability type.
*/
public class SQLStatement implements TarantoolStatement {
protected final SQLConnection connection;
/**
* Current result set / update count associated to this statement.
*/
protected SQLResultSet resultSet;
protected int updateCount;
private boolean isCloseOnCompletion;
private final int resultSetType;
private final int resultSetConcurrency;
private final int resultSetHoldability;
private int maxRows;
/**
* Query timeout in millis.
*/
private long timeout;
private final AtomicBoolean isClosed = new AtomicBoolean(false);
protected SQLStatement(SQLConnection sqlConnection) throws SQLException {
this.connection = sqlConnection;
this.resultSetType = ResultSet.TYPE_FORWARD_ONLY;
this.resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
this.resultSetHoldability = sqlConnection.getHoldability();
}
protected SQLStatement(SQLConnection sqlConnection,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
this.connection = sqlConnection;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
checkNotClosed();
if (!executeInternal(sql)) {
throw new SQLException("No results were returned", SQLStates.NO_DATA.getSqlState());
}
return resultSet;
}
@Override
public int executeUpdate(String sql) throws SQLException {
checkNotClosed();
if (executeInternal(sql)) {
throw new SQLException(
"Result was returned but nothing was expected",
SQLStates.TOO_MANY_RESULTS.getSqlState()
);
}
return updateCount;
}
@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
checkNotClosed();
JdbcConstants.checkGeneratedKeysConstant(autoGeneratedKeys);
if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) {
throw new SQLFeatureNotSupportedException();
}
return executeUpdate(sql);
}
@Override
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void close() throws SQLException {
if (isClosed.compareAndSet(false, true)) {
cancel();
discardLastResults();
}
}
@Override
public int getMaxFieldSize() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void setMaxFieldSize(int max) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int getMaxRows() throws SQLException {
checkNotClosed();
return maxRows;
}
@Override
public void setMaxRows(int maxRows) throws SQLException {
checkNotClosed();
if (maxRows < 0) {
throw new SQLNonTransientException("Max rows parameter can't be a negative value");
}
this.maxRows = maxRows;
}
@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int getQueryTimeout() throws SQLException {
return (int) TimeUnit.MILLISECONDS.toSeconds(timeout);
}
@Override
public void setQueryTimeout(int seconds) throws SQLException {
if (seconds < 0) {
throw new SQLNonTransientException(
"Query timeout must be positive or zero",
SQLStates.INVALID_PARAMETER_VALUE.getSqlState()
);
}
timeout = TimeUnit.SECONDS.toMillis(seconds);
}
@Override
public void cancel() throws SQLException {
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public void setCursorName(String name) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean execute(String sql) throws SQLException {
checkNotClosed();
return executeInternal(sql);
}
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
checkNotClosed();
JdbcConstants.checkGeneratedKeysConstant(autoGeneratedKeys);
if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) {
throw new SQLFeatureNotSupportedException();
}
return execute(sql);
}
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean execute(String sql, String[] columnNames) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public ResultSet getResultSet() throws SQLException {
checkNotClosed();
return resultSet;
}
@Override
public int getUpdateCount() throws SQLException {
checkNotClosed();
return updateCount;
}
@Override
public boolean getMoreResults() throws SQLException {
checkNotClosed();
return false;
}
@Override
public boolean getMoreResults(int current) throws SQLException {
checkNotClosed();
return false;
}
@Override
public void setFetchDirection(int direction) throws SQLException {
checkNotClosed();
if (direction != ResultSet.FETCH_FORWARD) {
throw new SQLFeatureNotSupportedException();
}
}
@Override
public int getFetchDirection() throws SQLException {
checkNotClosed();
return ResultSet.FETCH_FORWARD;
}
@Override
public void setFetchSize(int rows) throws SQLException {
checkNotClosed();
// no-op
}
@Override
public int getFetchSize() throws SQLException {
return 0;
}
@Override
public int getResultSetConcurrency() throws SQLException {
checkNotClosed();
return resultSetConcurrency;
}
@Override
public int getResultSetType() throws SQLException {
checkNotClosed();
return resultSetType;
}
@Override
public void addBatch(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void clearBatch() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int[] executeBatch() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Connection getConnection() throws SQLException {
return connection;
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
checkNotClosed();
return new SQLResultSet(SQLResultHolder.ofEmptyQuery(), this);
}
@Override
public int getResultSetHoldability() throws SQLException {
checkNotClosed();
return resultSetHoldability;
}
@Override
public boolean isClosed() throws SQLException {
return isClosed.get() || connection.isClosed();
}
@Override
public void setPoolable(boolean poolable) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isPoolable() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
/**
* {@inheritDoc}
* <p>
* <strong>Impl Note:</strong> this method doesn't affect
* execution methods which close the last result set implicitly.
* It is applied only when {@link ResultSet#close()} is invoked
* explicitly by the app.
*
* @throws SQLException if this method is called on a closed
* {@code Statement}
*/
@Override
public void closeOnCompletion() throws SQLException {
checkNotClosed();
isCloseOnCompletion = true;
}
@Override
public boolean isCloseOnCompletion() throws SQLException {
checkNotClosed();
return isCloseOnCompletion;
}
@Override
public void checkCompletion() throws SQLException {
if (isCloseOnCompletion &&
resultSet != null &&
resultSet.isClosed()) {
close();
}
}
@Override
public <T> T unwrap(Class<T> type) throws SQLException {
if (isWrapperFor(type)) {
return type.cast(this);
}
throw new SQLNonTransientException("SQLStatement does not wrap " + type.getName());
}
@Override
public boolean isWrapperFor(Class<?> type) throws SQLException {
return type.isAssignableFrom(this.getClass());
}
/**
* Clears the results of the most recent execution.
*/
protected void discardLastResults() throws SQLException {
final SQLResultSet lastResultSet = resultSet;
clearWarnings();
updateCount = -1;
resultSet = null;
if (lastResultSet != null) {
try {
lastResultSet.close();
} catch (Exception ignored) {
// No-op.
}
}
}
/**
* Performs query execution.
*
* @param sql query
* @param params optional params
*
* @return {@code true}, if the result is a ResultSet object;
*/
protected boolean executeInternal(String sql, Object... params) throws SQLException {
discardLastResults();
SQLResultHolder holder;
try {
holder = connection.execute(timeout, sql, params);
} catch (StatementTimeoutException e) {
cancel();
throw new SQLTimeoutException();
}
if (holder.isQueryResult()) {
resultSet = new SQLResultSet(holder, this);
}
updateCount = holder.getUpdateCount();
return holder.isQueryResult();
}
@Override
public ResultSet executeMetadata(SQLResultHolder data) throws SQLException {
checkNotClosed();
return createResultSet(data);
}
protected SQLResultSet createResultSet(SQLResultHolder holder) throws SQLException {
return new SQLResultSet(holder, this);
}
protected void checkNotClosed() throws SQLException {
if (isClosed()) {
throw new SQLNonTransientException("Statement is closed.");
}
}
}