提交 177d5bdc authored 作者: Noel Grandin's avatar Noel Grandin

Use interfaces to replace the java version templating, idea from Lukas Eder.

上级 d0b05c9c
......@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>Use interfaces to replace the java version templating, idea from Lukas Eder.
</li>
<li>Issue #295: JdbcResultSet.getObject(int, Class) returns null instead of throwing.
</li>
</ul>
......
......@@ -10,15 +10,12 @@ import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;
import org.h2.engine.Constants;
import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException;
import org.h2.upgrade.DbUpgrade;
/*## Java 1.7 ##
import java.util.logging.Logger;
//*/
/**
* The database driver. An application should not use this class directly. The
* only thing the application needs to do is load the driver. This can be done
......@@ -31,7 +28,7 @@ import java.util.logging.Logger;
* &quot;jdbc:h2:&tilde;/test&quot;, &quot;sa&quot;, &quot;sa&quot;);
* </pre>
*/
public class Driver implements java.sql.Driver {
public class Driver implements java.sql.Driver, JdbcDriverBackwardsCompat {
private static final Driver INSTANCE = new Driver();
private static final String DEFAULT_URL = "jdbc:default:connection";
......@@ -143,12 +140,10 @@ public class Driver implements java.sql.Driver {
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public Logger getParentLogger() {
return null;
}
//*/
/**
* INTERNAL
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2;
import java.util.logging.Logger;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcDriverBackwardsCompat {
Logger getParentLogger();
}
......@@ -25,7 +25,6 @@ import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.h2.api.ErrorCode;
import org.h2.expression.ParameterInterface;
import org.h2.message.DbException;
......@@ -41,7 +40,7 @@ import org.h2.value.ValueNull;
* @author Thomas Mueller
*/
public class JdbcCallableStatement extends JdbcPreparedStatement implements
CallableStatement {
CallableStatement, JdbcCallableStatementBackwardsCompat {
private BitField outParameters;
private int maxOutParameters;
......@@ -300,6 +299,7 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
* @throws SQLException if the column is not found or if this object is
* closed
*/
@Deprecated
@Override
public BigDecimal getBigDecimal(int parameterIndex, int scale)
throws SQLException {
......@@ -1569,12 +1569,10 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
* @param parameterIndex the parameter index (1, 2, ...)
* @param type the class of the returned value
*/
/*## Java 1.7 ##
@Override
public <T> T getObject(int parameterIndex, Class<T> type) {
return null;
public <T> T getObject(int parameterIndex, Class<T> type) throws SQLException {
throw unsupported("getObject");
}
//*/
/**
* [Not supported]
......@@ -1582,12 +1580,10 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
* @param parameterName the parameter name
* @param type the class of the returned value
*/
/*## Java 1.7 ##
@Override
public <T> T getObject(String parameterName, Class<T> type) {
return null;
public <T> T getObject(String parameterName, Class<T> type) throws SQLException {
throw unsupported("getObject");
}
//*/
private ResultSetMetaData getCheckedMetaData() throws SQLException {
ResultSetMetaData meta = getMetaData();
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.sql.SQLException;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcCallableStatementBackwardsCompat {
<T> T getObject(int parameterIndex, Class<T> type) throws SQLException;
<T> T getObject(String parameterName, Class<T> type) throws SQLException;
}
......@@ -26,16 +26,12 @@ import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
/*## Java 1.7 ##
import java.util.concurrent.Executor;
//*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.regex.Pattern;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
......@@ -67,7 +63,7 @@ import org.h2.value.ValueString;
* connection should only be used in one thread at any time.
* </p>
*/
public class JdbcConnection extends TraceObject implements Connection {
public class JdbcConnection extends TraceObject implements Connection, JdbcConnectionBackwardsCompat {
private static final String NUM_SERVERS = "numServers";
private static final String PREFIX_SERVER = "server";
......@@ -1896,34 +1892,28 @@ public class JdbcConnection extends TraceObject implements Connection {
*
* @param schema the schema
*/
/*## Java 1.7 ##
@Override
public void setSchema(String schema) {
// not supported
}
//*/
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public String getSchema() {
return null;
}
//*/
/**
* [Not supported]
*
* @param executor the executor used by this method
*/
/*## Java 1.7 ##
@Override
public void abort(Executor executor) {
// not supported
}
//*/
/**
* [Not supported]
......@@ -1931,22 +1921,18 @@ public class JdbcConnection extends TraceObject implements Connection {
* @param executor the executor used by this method
* @param milliseconds the TCP connection timeout
*/
/*## Java 1.7 ##
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) {
// not supported
}
//*/
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public int getNetworkTimeout() {
return 0;
}
//*/
/**
* Check that the given type map is either null or empty.
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.util.concurrent.Executor;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcConnectionBackwardsCompat {
void setSchema(String schema);
String getSchema();
void abort(Executor executor);
void setNetworkTimeout(Executor executor, int milliseconds);
int getNetworkTimeout();
}
......@@ -26,7 +26,7 @@ import org.h2.util.StringUtils;
* Represents the meta data for a database.
*/
public class JdbcDatabaseMetaData extends TraceObject implements
DatabaseMetaData {
DatabaseMetaData, JdbcDatabaseMetaDataBackwardsCompat {
private final JdbcConnection conn;
......@@ -3133,12 +3133,10 @@ public class JdbcDatabaseMetaData extends TraceObject implements
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public boolean generatedKeyAlwaysReturned() {
return true;
}
//*/
/**
* [Not supported]
......@@ -3151,13 +3149,11 @@ public class JdbcDatabaseMetaData extends TraceObject implements
* @param columnNamePattern null (to get all objects) or a column name
* (uppercase for unquoted names)
*/
/*## Java 1.7 ##
@Override
public ResultSet getPseudoColumns(String catalog, String schemaPattern,
String tableNamePattern, String columnNamePattern) {
return null;
}
//*/
/**
* INTERNAL
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
* EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
* Group
*/
package org.h2.jdbc;
import java.sql.ResultSet;
/**
* Allows us to compile on older platforms, while still implementing the methods
* from the newer JDBC API.
*/
public interface JdbcDatabaseMetaDataBackwardsCompat {
boolean generatedKeyAlwaysReturned();
ResultSet getPseudoColumns(String catalog, String schemaPattern,
String tableNamePattern, String columnNamePattern);
}
......@@ -71,7 +71,7 @@ import org.h2.value.ValueTimestamp;
* changes are visible, but not own inserts and deletes.
* </p>
*/
public class JdbcResultSet extends TraceObject implements ResultSet {
public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultSetBackwardsCompat {
private final boolean closeStatement;
private final boolean scrollable;
private final boolean updatable;
......@@ -3684,12 +3684,10 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
* @param columnIndex the column index (1, 2, ...)
* @param type the class of the returned value
*/
/*## Java 1.7 ##
@Override
public <T> T getObject(int columnIndex, Class<T> type) {
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
throw unsupported("getObject");
}
//*/
/**
* [Not supported]
......@@ -3697,12 +3695,10 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
* @param columnName the column name
* @param type the class of the returned value
*/
/*## Java 1.7 ##
@Override
public <T> T getObject(String columnName, Class<T> type) {
public <T> T getObject(String columnName, Class<T> type) throws SQLException {
throw unsupported("getObject");
}
//*/
/**
* INTERNAL
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.sql.SQLException;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcResultSetBackwardsCompat {
public abstract <T> T getObject(int columnIndex, Class<T> type) throws SQLException;
public abstract <T> T getObject(String columnName, Class<T> type) throws SQLException;
}
......@@ -23,7 +23,7 @@ import org.h2.util.New;
/**
* Represents a statement.
*/
public class JdbcStatement extends TraceObject implements Statement {
public class JdbcStatement extends TraceObject implements Statement, JdbcStatementBackwardsCompat {
protected JdbcConnection conn;
protected SessionInterface session;
......@@ -929,22 +929,18 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public void closeOnCompletion() {
// not supported
}
//*/
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public boolean isCloseOnCompletion() {
return true;
}
//*/
// =============================================================
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcStatementBackwardsCompat {
void closeOnCompletion();
boolean isCloseOnCompletion();
}
......@@ -23,17 +23,14 @@ import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.PooledConnection;
import org.h2.util.New;
import org.h2.message.DbException;
/*## Java 1.7 ##
import java.util.logging.Logger;
//*/
import org.h2.util.New;
/**
* A simple standalone JDBC connection pool.
......@@ -62,7 +59,7 @@ import java.util.logging.Logger;
* (<a href="http://www.source-code.biz">www.source-code.biz</a>)
* @author Thomas Mueller
*/
public class JdbcConnectionPool implements DataSource, ConnectionEventListener {
public class JdbcConnectionPool implements DataSource, ConnectionEventListener, JdbcConnectionPoolBackwardsCompat {
private static final int DEFAULT_TIMEOUT = 30;
private static final int DEFAULT_MAX_CONNECTIONS = 10;
......@@ -333,12 +330,10 @@ public class JdbcConnectionPool implements DataSource, ConnectionEventListener {
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public Logger getParentLogger() {
return null;
}
//*/
}
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbcx;
import java.util.logging.Logger;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcConnectionPoolBackwardsCompat {
Logger getParentLogger();
}
......@@ -12,6 +12,7 @@ import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;
......@@ -25,10 +26,6 @@ import org.h2.jdbc.JdbcConnection;
import org.h2.message.TraceObject;
import org.h2.util.StringUtils;
/*## Java 1.7 ##
import java.util.logging.Logger;
//*/
/**
* A data source for H2 database connections. It is a factory for XAConnection
* and Connection objects. This class is usually registered in a JNDI naming
......@@ -63,7 +60,7 @@ import java.util.logging.Logger;
* well; this may be a security problem in some cases.
*/
public class JdbcDataSource extends TraceObject implements XADataSource,
DataSource, ConnectionPoolDataSource, Serializable, Referenceable {
DataSource, ConnectionPoolDataSource, Serializable, Referenceable, JdbcDataSourceBackwardsCompat {
private static final long serialVersionUID = 1288136338451857771L;
......@@ -425,12 +422,10 @@ public class JdbcDataSource extends TraceObject implements XADataSource,
/**
* [Not supported]
*/
/*## Java 1.7 ##
@Override
public Logger getParentLogger() {
return null;
}
//*/
/**
* INTERNAL
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbcx;
import java.util.logging.Logger;
/**
* Allows us to compile on older platforms, while still implementing the methods from the newer JDBC API.
*/
public interface JdbcDataSourceBackwardsCompat {
Logger getParentLogger();
}
......@@ -28,8 +28,8 @@ import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcResultSetBackwardsCompat;
import org.h2.message.DbException;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
......@@ -54,7 +54,7 @@ import org.h2.value.DataType;
* </pre>
*
*/
public class SimpleResultSet implements ResultSet, ResultSetMetaData {
public class SimpleResultSet implements ResultSet, ResultSetMetaData, JdbcResultSetBackwardsCompat {
private ArrayList<Object[]> rows;
private Object[] currentRow;
......@@ -391,6 +391,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
/**
* @deprecated INTERNAL
*/
@Deprecated
@Override
public BigDecimal getBigDecimal(int columnIndex, int scale)
throws SQLException {
......@@ -400,6 +401,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
/**
* @deprecated INTERNAL
*/
@Deprecated
@Override
public BigDecimal getBigDecimal(String columnLabel, int scale)
throws SQLException {
......@@ -821,12 +823,10 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @param columnIndex the column index (1, 2, ...)
* @param type the class of the returned value
*/
/*## Java 1.7 ##
@Override
public <T> T getObject(int columnIndex, Class<T> type) {
return null;
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
throw getUnsupportedException();
}
//*/
/**
* INTERNAL
......@@ -834,12 +834,10 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @param columnName the column name
* @param type the class of the returned value
*/
/*## Java 1.7 ##
@Override
public <T> T getObject(String columnName, Class<T> type) {
return null;
public <T> T getObject(String columnName, Class<T> type) throws SQLException {
throw getUnsupportedException();
}
//*/
/**
* INTERNAL
......@@ -1045,6 +1043,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
/**
* @deprecated INTERNAL
*/
@Deprecated
@Override
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
throw getUnsupportedException();
......@@ -1053,6 +1052,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
/**
* @deprecated INTERNAL
*/
@Deprecated
@Override
public InputStream getUnicodeStream(String columnLabel) throws SQLException {
throw getUnsupportedException();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论