提交 635405a9 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add more subclases of SQLException and use it for some error codes

上级 8813bbf4
......@@ -533,9 +533,8 @@ See also <a href="build.html#providing_patches">Providing Patches</a>.
</li><li>Long running transactions: log session id when detected.
</li><li>Optimization: "select id from test" should use the index on id even without "order by".
</li><li>Sybase SQL Anywhere compatibility: SELECT TOP ... START AT ...
</li><li>Use Java 6 SQLException subclasses.
</li><li>Use Java 6 SQLException subclasses for more kinds of errors.
</li><li>Issue 390: RUNSCRIPT FROM '...' CONTINUE_ON_ERROR
</li><li>Use Java 6 exceptions: SQLDataException, SQLSyntaxErrorException, SQLTimeoutException,..
</li></ul>
<h2>Not Planned</h2>
......
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLDataException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLDataException extends SQLDataException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLDataException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLDataException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLFeatureNotSupportedException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLFeatureNotSupportedException extends SQLFeatureNotSupportedException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLFeatureNotSupportedException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLFeatureNotSupportedException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLIntegrityConstraintViolationException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLIntegrityConstraintViolationException extends SQLIntegrityConstraintViolationException
implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLIntegrityConstraintViolationException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLIntegrityConstraintViolationException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLInvalidAuthorizationSpecException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLInvalidAuthorizationSpecException extends SQLInvalidAuthorizationSpecException
implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLInvalidAuthorizationSpecException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLInvalidAuthorizationSpecException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLNonTransientConnectionException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLNonTransientConnectionException extends SQLNonTransientConnectionException
implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLNonTransientConnectionException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLNonTransientConnectionException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLSyntaxErrorException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLSyntaxErrorException extends SQLSyntaxErrorException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLSyntaxErrorException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLSyntaxErrorException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 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.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLTransactionRollbackException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLTransactionRollbackException extends SQLTransactionRollbackException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLTransactionRollbackException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLTransactionRollbackException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
......@@ -20,7 +20,14 @@ import java.util.Properties;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.jdbc.JdbcException;
import org.h2.jdbc.JdbcSQLDataException;
import org.h2.jdbc.JdbcSQLException;
import org.h2.jdbc.JdbcSQLFeatureNotSupportedException;
import org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException;
import org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException;
import org.h2.jdbc.JdbcSQLNonTransientConnectionException;
import org.h2.jdbc.JdbcSQLSyntaxErrorException;
import org.h2.jdbc.JdbcSQLTransactionRollbackException;
import org.h2.util.SortedProperties;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
......@@ -414,7 +421,36 @@ public class DbException extends RuntimeException {
*/
public static SQLException getJdbcSQLException(String message, String sql, String state, int errorCode,
Throwable cause, String stackTrace) {
return new JdbcSQLException(message, filterSQL(sql), state, errorCode, cause, stackTrace);
sql = filterSQL(sql);
// Use SQLState class value to detect type
switch (errorCode / 1_000) {
case 8:
return new JdbcSQLNonTransientConnectionException(message, sql, state, errorCode, cause, stackTrace);
case 22:
return new JdbcSQLDataException(message, sql, state, errorCode, cause, stackTrace);
case 23:
return new JdbcSQLIntegrityConstraintViolationException(message, sql, state, errorCode, cause, stackTrace);
case 28:
return new JdbcSQLInvalidAuthorizationSpecException(message, sql, state, errorCode, cause, stackTrace);
case 40:
return new JdbcSQLTransactionRollbackException(message, sql, state, errorCode, cause, stackTrace);
case 42:
return new JdbcSQLSyntaxErrorException(message, sql, state, errorCode, cause, stackTrace);
}
// Check error code
switch (errorCode){
case ErrorCode.FEATURE_NOT_SUPPORTED_1:
return new JdbcSQLFeatureNotSupportedException(message, sql, state, errorCode, cause, stackTrace);
case ErrorCode.HEX_STRING_ODD_1:
case ErrorCode.HEX_STRING_WRONG_1:
case ErrorCode.INVALID_VALUE_2:
case ErrorCode.PARSE_ERROR_1:
case ErrorCode.INVALID_TO_DATE_FORMAT:
case ErrorCode.STRING_FORMAT_ERROR_1:
return new JdbcSQLDataException(message, sql, state, errorCode, cause, stackTrace);
}
// Default
return new JdbcSQLException(message, sql, state, errorCode, cause, stackTrace);
}
private static String filterSQL(String sql) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论