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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/*
* 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
*
* https://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.tomcat.dbcp.dbcp2;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.sql.CommonDataSource;
/**
* Defines bridge methods to JDBC 4.1 (Java 7 or above) methods to allow call sites to operate safely (without
* {@link AbstractMethodError}) when using a JDBC driver written for JDBC 4.0 (Java 6 or above).
* <p>
* There should be no need to this kind of code for JDBC 4.2 in Java 8 due to JDBC's use of default methods.
* </p>
* <p>
* This should probably be moved or at least copied in some form to Apache Commons DbUtils.
* </p>
*
* @since 2.6.0
*/
public class Jdbc41Bridge {
/**
* Delegates to {@link Connection#abort(Executor)} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#abort(Executor)}, then call {@link Connection#close()}.
* </p>
*
* @param connection
* the receiver
* @param executor
* See {@link Connection#abort(Executor)}.
* @throws SQLException
* See {@link Connection#abort(Executor)}.
* @see Connection#abort(Executor)
*/
public static void abort(final Connection connection, final Executor executor) throws SQLException {
try {
connection.abort(executor);
} catch (final AbstractMethodError e) {
connection.close();
}
}
/**
* Delegates to {@link Statement#closeOnCompletion()} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Statement#closeOnCompletion()}, then just check that the connection
* is closed to then throw an SQLException.
* </p>
*
* @param statement
* See {@link Statement#closeOnCompletion()}
* @throws SQLException
* See {@link Statement#closeOnCompletion()}
* @see Statement#closeOnCompletion()
*/
public static void closeOnCompletion(final Statement statement) throws SQLException {
try {
statement.closeOnCompletion();
} catch (final AbstractMethodError e) {
if (statement.isClosed()) {
throw new SQLException("Statement closed");
}
}
}
/**
* Delegates to {@link DatabaseMetaData#generatedKeyAlwaysReturned()} without throwing a
* {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link DatabaseMetaData#generatedKeyAlwaysReturned()}, then return false.
* </p>
*
* @param databaseMetaData
* See {@link DatabaseMetaData#generatedKeyAlwaysReturned()}
* @return See {@link DatabaseMetaData#generatedKeyAlwaysReturned()}
* @throws SQLException
* See {@link DatabaseMetaData#generatedKeyAlwaysReturned()}
* @see DatabaseMetaData#generatedKeyAlwaysReturned()
*/
public static boolean generatedKeyAlwaysReturned(final DatabaseMetaData databaseMetaData) throws SQLException {
try {
return databaseMetaData.generatedKeyAlwaysReturned();
} catch (final AbstractMethodError e) {
// do nothing
return false;
}
}
/**
* Delegates to {@link Connection#getNetworkTimeout()} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#getNetworkTimeout()}, then return 0.
* </p>
*
* @param connection
* the receiver
* @return See {@link Connection#getNetworkTimeout()}
* @throws SQLException
* See {@link Connection#getNetworkTimeout()}
* @see Connection#getNetworkTimeout()
*/
public static int getNetworkTimeout(final Connection connection) throws SQLException {
try {
return connection.getNetworkTimeout();
} catch (final AbstractMethodError e) {
return 0;
}
}
/**
* Delegates to {@link ResultSet#getObject(int, Class)} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link ResultSet#getObject(int, Class)}, then return 0.
* </p>
*
* @param <T>
* See {@link ResultSet#getObject(int, Class)}
* @param resultSet
* See {@link ResultSet#getObject(int, Class)}
* @param columnIndex
* See {@link ResultSet#getObject(int, Class)}
* @param type
* See {@link ResultSet#getObject(int, Class)}
* @return See {@link ResultSet#getObject(int, Class)}
* @throws SQLException
* See {@link ResultSet#getObject(int, Class)}
* @see ResultSet#getObject(int, Class)
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(final ResultSet resultSet, final int columnIndex, final Class<T> type)
throws SQLException {
try {
return resultSet.getObject(columnIndex, type);
} catch (final AbstractMethodError e) {
if (type == String.class) {
return (T) resultSet.getString(columnIndex);
}
// Numbers
if (type == Integer.class) {
return (T) Integer.valueOf(resultSet.getInt(columnIndex));
}
if (type == Long.class) {
return (T) Long.valueOf(resultSet.getLong(columnIndex));
}
if (type == Double.class) {
return (T) Double.valueOf(resultSet.getDouble(columnIndex));
}
if (type == Float.class) {
return (T) Float.valueOf(resultSet.getFloat(columnIndex));
}
if (type == Short.class) {
return (T) Short.valueOf(resultSet.getShort(columnIndex));
}
if (type == BigDecimal.class) {
return (T) resultSet.getBigDecimal(columnIndex);
}
if (type == Byte.class) {
return (T) Byte.valueOf(resultSet.getByte(columnIndex));
}
// Dates
if (type == Date.class) {
return (T) resultSet.getDate(columnIndex);
}
if (type == Time.class) {
return (T) resultSet.getTime(columnIndex);
}
if (type == Timestamp.class) {
return (T) resultSet.getTimestamp(columnIndex);
}
// Streams
if (type == InputStream.class) {
return (T) resultSet.getBinaryStream(columnIndex);
}
if (type == Reader.class) {
return (T) resultSet.getCharacterStream(columnIndex);
}
// Other
if (type == Object.class) {
return (T) resultSet.getObject(columnIndex);
}
if (type == Boolean.class) {
return (T) Boolean.valueOf(resultSet.getBoolean(columnIndex));
}
if (type == Array.class) {
return (T) resultSet.getArray(columnIndex);
}
if (type == Blob.class) {
return (T) resultSet.getBlob(columnIndex);
}
if (type == Clob.class) {
return (T) resultSet.getClob(columnIndex);
}
if (type == Ref.class) {
return (T) resultSet.getRef(columnIndex);
}
if (type == RowId.class) {
return (T) resultSet.getRowId(columnIndex);
}
if (type == SQLXML.class) {
return (T) resultSet.getSQLXML(columnIndex);
}
if (type == URL.class) {
return (T) resultSet.getURL(columnIndex);
}
throw new SQLFeatureNotSupportedException(
String.format("resultSet=%s, columnIndex=%,d, type=%s", resultSet, Integer.valueOf(columnIndex), type));
}
}
/**
* Delegates to {@link ResultSet#getObject(String, Class)} without throwing an {@link AbstractMethodError}.
*
* @param <T>
* See {@link ResultSet#getObject(String, Class)}
* @param resultSet
* See {@link ResultSet#getObject(String, Class)}
* @param columnLabel
* See {@link ResultSet#getObject(String, Class)}
* @param type
* See {@link ResultSet#getObject(String, Class)}
* @return See {@link ResultSet#getObject(String, Class)}
* @throws SQLException
* See {@link ResultSet#getObject(String, Class)}
* @see ResultSet#getObject(int, Class)
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(final ResultSet resultSet, final String columnLabel, final Class<T> type)
throws SQLException {
try {
return resultSet.getObject(columnLabel, type);
} catch (final AbstractMethodError e) {
// Numbers
if (type == Integer.class) {
return (T) Integer.valueOf(resultSet.getInt(columnLabel));
}
if (type == Long.class) {
return (T) Long.valueOf(resultSet.getLong(columnLabel));
}
if (type == Double.class) {
return (T) Double.valueOf(resultSet.getDouble(columnLabel));
}
if (type == Float.class) {
return (T) Float.valueOf(resultSet.getFloat(columnLabel));
}
if (type == Short.class) {
return (T) Short.valueOf(resultSet.getShort(columnLabel));
}
if (type == BigDecimal.class) {
return (T) resultSet.getBigDecimal(columnLabel);
}
if (type == Byte.class) {
return (T) Byte.valueOf(resultSet.getByte(columnLabel));
}
// Dates
if (type == Date.class) {
return (T) resultSet.getDate(columnLabel);
}
if (type == Time.class) {
return (T) resultSet.getTime(columnLabel);
}
if (type == Timestamp.class) {
return (T) resultSet.getTimestamp(columnLabel);
}
// Streams
if (type == InputStream.class) {
return (T) resultSet.getBinaryStream(columnLabel);
}
if (type == Reader.class) {
return (T) resultSet.getCharacterStream(columnLabel);
}
// Other
if (type == Object.class) {
return (T) resultSet.getObject(columnLabel);
}
if (type == Boolean.class) {
return (T) Boolean.valueOf(resultSet.getBoolean(columnLabel));
}
if (type == Array.class) {
return (T) resultSet.getArray(columnLabel);
}
if (type == Blob.class) {
return (T) resultSet.getBlob(columnLabel);
}
if (type == Clob.class) {
return (T) resultSet.getClob(columnLabel);
}
if (type == Ref.class) {
return (T) resultSet.getRef(columnLabel);
}
if (type == RowId.class) {
return (T) resultSet.getRowId(columnLabel);
}
if (type == SQLXML.class) {
return (T) resultSet.getSQLXML(columnLabel);
}
if (type == URL.class) {
return (T) resultSet.getURL(columnLabel);
}
throw new SQLFeatureNotSupportedException(
String.format("resultSet=%s, columnLabel=%s, type=%s", resultSet, columnLabel, type));
}
}
/**
* Delegates to {@link CommonDataSource#getParentLogger()} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link CommonDataSource#getParentLogger()}, then return null.
* </p>
*
* @param commonDataSource
* See {@link CommonDataSource#getParentLogger()}
* @return See {@link CommonDataSource#getParentLogger()}
* @throws SQLFeatureNotSupportedException
* See {@link CommonDataSource#getParentLogger()}
*/
public static Logger getParentLogger(final CommonDataSource commonDataSource) throws SQLFeatureNotSupportedException {
try {
return commonDataSource.getParentLogger();
} catch (final AbstractMethodError e) {
throw new SQLFeatureNotSupportedException("javax.sql.CommonDataSource#getParentLogger()");
}
}
/**
* Delegates to {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)} without throwing a
* {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)},
* then return null.
* </p>
*
* @param databaseMetaData
* the receiver
* @param catalog
* See {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)}
* @param schemaPattern
* See {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)}
* @param tableNamePattern
* See {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)}
* @param columnNamePattern
* See {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)}
* @return See {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)}
* @throws SQLException
* See {@link DatabaseMetaData#getPseudoColumns(String, String, String, String)}
* @see DatabaseMetaData#getPseudoColumns(String, String, String, String)
*/
public static ResultSet getPseudoColumns(final DatabaseMetaData databaseMetaData, final String catalog,
final String schemaPattern, final String tableNamePattern, final String columnNamePattern)
throws SQLException {
try {
return databaseMetaData.getPseudoColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
} catch (final AbstractMethodError e) {
// do nothing
return null;
}
}
/**
* Delegates to {@link Connection#getSchema()} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#getSchema()}, then return null.
* </p>
*
* @param connection
* the receiver
* @return null for a JDBC 4 driver or a value per {@link Connection#getSchema()}.
* @throws SQLException
* See {@link Connection#getSchema()}.
* @see Connection#getSchema()
*/
public static String getSchema(final Connection connection) throws SQLException {
try {
return connection.getSchema();
} catch (final AbstractMethodError e) {
// do nothing
return null;
}
}
/**
* Delegates to {@link Statement#isCloseOnCompletion()} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Statement#isCloseOnCompletion()}, then just check that the
* connection is closed to then throw an SQLException.
* </p>
*
* @param statement
* See {@link Statement#isCloseOnCompletion()}
* @return See {@link Statement#isCloseOnCompletion()}
* @throws SQLException
* See {@link Statement#isCloseOnCompletion()}
* @see Statement#closeOnCompletion()
*/
public static boolean isCloseOnCompletion(final Statement statement) throws SQLException {
try {
return statement.isCloseOnCompletion();
} catch (final AbstractMethodError e) {
if (statement.isClosed()) {
throw new SQLException("Statement closed");
}
return false;
}
}
/**
* Delegates to {@link Connection#setNetworkTimeout(Executor, int)} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#setNetworkTimeout(Executor, int)}, then do nothing.
* </p>
*
* @param connection
* the receiver
* @param executor
* See {@link Connection#setNetworkTimeout(Executor, int)}
* @param milliseconds
* {@link Connection#setNetworkTimeout(Executor, int)}
* @throws SQLException
* {@link Connection#setNetworkTimeout(Executor, int)}
* @see Connection#setNetworkTimeout(Executor, int)
*/
public static void setNetworkTimeout(final Connection connection, final Executor executor, final int milliseconds)
throws SQLException {
try {
connection.setNetworkTimeout(executor, milliseconds);
} catch (final AbstractMethodError ignored) {
// do nothing
}
}
/**
* Delegates to {@link Connection#setSchema(String)} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#setSchema(String)}, then do nothing.
* </p>
*
* @param connection
* the receiver
* @param schema
* See {@link Connection#setSchema(String)}.
* @throws SQLException
* See {@link Connection#setSchema(String)}.
* @see Connection#setSchema(String)
*/
public static void setSchema(final Connection connection, final String schema) throws SQLException {
try {
connection.setSchema(schema);
} catch (final AbstractMethodError ignored) {
// do nothing
}
}
/**
* Deprecated, this class only contains static methods.
*
* @deprecated Constructor will be private in the next major release.
*/
@Deprecated
public Jdbc41Bridge() {
// empty
}
}
|