| 1 |
/* |
| 2 |
* Copyright (C) 2013 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.mocks; |
| 18 |
|
| 19 |
import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; |
| 20 |
|
| 21 |
import java.sql.Connection; |
| 22 |
import java.sql.ResultSet; |
| 23 |
import java.sql.SQLException; |
| 24 |
import java.sql.SQLFeatureNotSupportedException; |
| 25 |
import java.sql.SQLWarning; |
| 26 |
import java.sql.Statement; |
| 27 |
|
| 28 |
/** |
| 29 |
* |
| 30 |
* @author Brett Wooldridge |
| 31 |
*/ |
| 32 |
public class StubStatement implements Statement |
| 33 |
{ |
| 34 |
public static volatile boolean oldDriver; |
| 35 |
|
| 36 |
private static volatile long simulatedQueryTime; |
| 37 |
private boolean closed; |
| 38 |
private Connection connection; |
| 39 |
|
| 40 |
public StubStatement(Connection connection) { |
| 41 |
this.connection = connection; |
| 42 |
} |
| 43 |
|
| 44 |
public static void setSimulatedQueryTime(long time) { |
| 45 |
simulatedQueryTime = time; |
| 46 |
} |
| 47 |
|
| 48 |
/** {@inheritDoc} */ |
| 49 |
@SuppressWarnings("unchecked") |
| 50 |
@Override |
| 51 |
public <T> T unwrap(Class<T> iface) throws SQLException |
| 52 |
{ |
| 53 |
checkClosed(); |
| 54 |
return (T) this; |
| 55 |
} |
| 56 |
|
| 57 |
/** {@inheritDoc} */ |
| 58 |
@Override |
| 59 |
public boolean isWrapperFor(Class<?> iface) throws SQLException |
| 60 |
{ |
| 61 |
checkClosed(); |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
/** {@inheritDoc} */ |
| 66 |
@Override |
| 67 |
public ResultSet executeQuery(String sql) throws SQLException |
| 68 |
{ |
| 69 |
checkClosed(); |
| 70 |
StubResultSet resultSet = new StubResultSet(); |
| 71 |
return resultSet; |
| 72 |
} |
| 73 |
|
| 74 |
/** {@inheritDoc} */ |
| 75 |
@Override |
| 76 |
public int executeUpdate(String sql) throws SQLException |
| 77 |
{ |
| 78 |
checkClosed(); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
/** {@inheritDoc} */ |
| 83 |
@Override |
| 84 |
public void close() throws SQLException |
| 85 |
{ |
| 86 |
closed = true; |
| 87 |
} |
| 88 |
|
| 89 |
/** {@inheritDoc} */ |
| 90 |
@Override |
| 91 |
public int getMaxFieldSize() throws SQLException |
| 92 |
{ |
| 93 |
checkClosed(); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
/** {@inheritDoc} */ |
| 98 |
@Override |
| 99 |
public void setMaxFieldSize(int max) throws SQLException |
| 100 |
{ |
| 101 |
checkClosed(); |
| 102 |
} |
| 103 |
|
| 104 |
/** {@inheritDoc} */ |
| 105 |
@Override |
| 106 |
public int getMaxRows() throws SQLException |
| 107 |
{ |
| 108 |
checkClosed(); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
/** {@inheritDoc} */ |
| 113 |
@Override |
| 114 |
public void setMaxRows(int max) throws SQLException |
| 115 |
{ |
| 116 |
checkClosed(); |
| 117 |
} |
| 118 |
|
| 119 |
/** {@inheritDoc} */ |
| 120 |
@Override |
| 121 |
public void setEscapeProcessing(boolean enable) throws SQLException |
| 122 |
{ |
| 123 |
checkClosed(); |
| 124 |
} |
| 125 |
|
| 126 |
/** {@inheritDoc} */ |
| 127 |
@Override |
| 128 |
public int getQueryTimeout() throws SQLException |
| 129 |
{ |
| 130 |
checkClosed(); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
/** {@inheritDoc} */ |
| 135 |
@Override |
| 136 |
public void setQueryTimeout(int seconds) throws SQLException |
| 137 |
{ |
| 138 |
if (oldDriver) { |
| 139 |
throw new SQLFeatureNotSupportedException(); |
| 140 |
} |
| 141 |
|
| 142 |
checkClosed(); |
| 143 |
} |
| 144 |
|
| 145 |
/** {@inheritDoc} */ |
| 146 |
@Override |
| 147 |
public void cancel() throws SQLException |
| 148 |
{ |
| 149 |
checkClosed(); |
| 150 |
} |
| 151 |
|
| 152 |
/** {@inheritDoc} */ |
| 153 |
@Override |
| 154 |
public SQLWarning getWarnings() throws SQLException |
| 155 |
{ |
| 156 |
checkClosed(); |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
/** {@inheritDoc} */ |
| 161 |
@Override |
| 162 |
public void clearWarnings() throws SQLException |
| 163 |
{ |
| 164 |
checkClosed(); |
| 165 |
} |
| 166 |
|
| 167 |
/** {@inheritDoc} */ |
| 168 |
@Override |
| 169 |
public void setCursorName(String name) throws SQLException |
| 170 |
{ |
| 171 |
checkClosed(); |
| 172 |
} |
| 173 |
|
| 174 |
/** {@inheritDoc} */ |
| 175 |
@Override |
| 176 |
public boolean execute(String sql) throws SQLException |
| 177 |
{ |
| 178 |
checkClosed(); |
| 179 |
if (simulatedQueryTime > 0) { |
| 180 |
quietlySleep(simulatedQueryTime); |
| 181 |
} |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
/** {@inheritDoc} */ |
| 186 |
@Override |
| 187 |
public ResultSet getResultSet() throws SQLException |
| 188 |
{ |
| 189 |
checkClosed(); |
| 190 |
return new StubResultSet(); |
| 191 |
} |
| 192 |
|
| 193 |
/** {@inheritDoc} */ |
| 194 |
@Override |
| 195 |
public int getUpdateCount() throws SQLException |
| 196 |
{ |
| 197 |
checkClosed(); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
/** {@inheritDoc} */ |
| 202 |
@Override |
| 203 |
public boolean getMoreResults() throws SQLException |
| 204 |
{ |
| 205 |
checkClosed(); |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
/** {@inheritDoc} */ |
| 210 |
@Override |
| 211 |
public void setFetchDirection(int direction) throws SQLException |
| 212 |
{ |
| 213 |
checkClosed(); |
| 214 |
} |
| 215 |
|
| 216 |
/** {@inheritDoc} */ |
| 217 |
@Override |
| 218 |
public int getFetchDirection() throws SQLException |
| 219 |
{ |
| 220 |
checkClosed(); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
/** {@inheritDoc} */ |
| 225 |
@Override |
| 226 |
public void setFetchSize(int rows) throws SQLException |
| 227 |
{ |
| 228 |
checkClosed(); |
| 229 |
} |
| 230 |
|
| 231 |
/** {@inheritDoc} */ |
| 232 |
@Override |
| 233 |
public int getFetchSize() throws SQLException |
| 234 |
{ |
| 235 |
checkClosed(); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
/** {@inheritDoc} */ |
| 240 |
@Override |
| 241 |
public int getResultSetConcurrency() throws SQLException |
| 242 |
{ |
| 243 |
checkClosed(); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
/** {@inheritDoc} */ |
| 248 |
@Override |
| 249 |
public int getResultSetType() throws SQLException |
| 250 |
{ |
| 251 |
checkClosed(); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
/** {@inheritDoc} */ |
| 256 |
@Override |
| 257 |
public void addBatch(String sql) throws SQLException |
| 258 |
{ |
| 259 |
checkClosed(); |
| 260 |
} |
| 261 |
|
| 262 |
/** {@inheritDoc} */ |
| 263 |
@Override |
| 264 |
public void clearBatch() throws SQLException |
| 265 |
{ |
| 266 |
checkClosed(); |
| 267 |
} |
| 268 |
|
| 269 |
/** {@inheritDoc} */ |
| 270 |
@Override |
| 271 |
public int[] executeBatch() throws SQLException |
| 272 |
{ |
| 273 |
checkClosed(); |
| 274 |
return null; |
| 275 |
} |
| 276 |
|
| 277 |
/** {@inheritDoc} */ |
| 278 |
@Override |
| 279 |
public Connection getConnection() throws SQLException |
| 280 |
{ |
| 281 |
checkClosed(); |
| 282 |
return connection; |
| 283 |
} |
| 284 |
|
| 285 |
/** {@inheritDoc} */ |
| 286 |
@Override |
| 287 |
public boolean getMoreResults(int current) throws SQLException |
| 288 |
{ |
| 289 |
checkClosed(); |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
/** {@inheritDoc} */ |
| 294 |
@Override |
| 295 |
public ResultSet getGeneratedKeys() throws SQLException |
| 296 |
{ |
| 297 |
checkClosed(); |
| 298 |
return new StubResultSet(); |
| 299 |
} |
| 300 |
|
| 301 |
/** {@inheritDoc} */ |
| 302 |
@Override |
| 303 |
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException |
| 304 |
{ |
| 305 |
checkClosed(); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
/** {@inheritDoc} */ |
| 310 |
@Override |
| 311 |
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException |
| 312 |
{ |
| 313 |
checkClosed(); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
/** {@inheritDoc} */ |
| 318 |
@Override |
| 319 |
public int executeUpdate(String sql, String[] columnNames) throws SQLException |
| 320 |
{ |
| 321 |
checkClosed(); |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
/** {@inheritDoc} */ |
| 326 |
@Override |
| 327 |
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException |
| 328 |
{ |
| 329 |
checkClosed(); |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
/** {@inheritDoc} */ |
| 334 |
@Override |
| 335 |
public boolean execute(String sql, int[] columnIndexes) throws SQLException |
| 336 |
{ |
| 337 |
checkClosed(); |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
/** {@inheritDoc} */ |
| 342 |
@Override |
| 343 |
public boolean execute(String sql, String[] columnNames) throws SQLException |
| 344 |
{ |
| 345 |
checkClosed(); |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
/** {@inheritDoc} */ |
| 350 |
@Override |
| 351 |
public int getResultSetHoldability() throws SQLException |
| 352 |
{ |
| 353 |
checkClosed(); |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
/** {@inheritDoc} */ |
| 358 |
@Override |
| 359 |
public boolean isClosed() throws SQLException |
| 360 |
{ |
| 361 |
return closed; |
| 362 |
} |
| 363 |
|
| 364 |
/** {@inheritDoc} */ |
| 365 |
@Override |
| 366 |
public void setPoolable(boolean poolable) throws SQLException |
| 367 |
{ |
| 368 |
checkClosed(); |
| 369 |
} |
| 370 |
|
| 371 |
/** {@inheritDoc} */ |
| 372 |
@Override |
| 373 |
public boolean isPoolable() throws SQLException |
| 374 |
{ |
| 375 |
checkClosed(); |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
/** {@inheritDoc} */ |
| 380 |
public void closeOnCompletion() throws SQLException |
| 381 |
{ |
| 382 |
checkClosed(); |
| 383 |
} |
| 384 |
|
| 385 |
/** {@inheritDoc} */ |
| 386 |
public boolean isCloseOnCompletion() throws SQLException |
| 387 |
{ |
| 388 |
checkClosed(); |
| 389 |
return false; |
| 390 |
} |
| 391 |
|
| 392 |
private void checkClosed() throws SQLException |
| 393 |
{ |
| 394 |
if (closed) { |
| 395 |
throw new SQLException("Statement is closed"); |
| 396 |
} |
| 397 |
} |
| 398 |
} |