提交 47fa5cab authored 作者: Noel Grandin's avatar Noel Grandin

skip intermediate DbException object when creating SQLException

上级 542100a4
......@@ -16,7 +16,6 @@ import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcSQLException;
import org.h2.util.SortedProperties;
......@@ -342,6 +341,42 @@ public class DbException extends RuntimeException {
return get(ErrorCode.IO_EXCEPTION_2, e, e.toString(), message);
}
/**
* Gets the SQL exception object for a specific error code.
*
* @param errorCode the error code
* @return the SQLException object
*/
public static SQLException getSQLException(int errorCode)
{
return getJdbcSQLException(errorCode, null, new String[0]);
}
/**
* Gets the SQL exception object for a specific error code.
*
* @param errorCode the error code
* @param p1 the first parameter of the message
* @return the SQLException object
*/
public static SQLException getSQLException(int errorCode, String p1)
{
return getJdbcSQLException(errorCode, null, new String[] { p1 });
}
/**
* Create the SQL exception object for a specific error code.
*
* @param errorCode the error code
* @param cause the cause of the exception
* @param params the list of parameters of the message
* @return the exception
*/
public static SQLException getSQLException(int errorCode, Throwable cause, String... params)
{
return getJdbcSQLException(errorCode, cause, params);
}
/**
* Gets the SQL exception object for a specific error code.
*
......
......@@ -9,7 +9,6 @@ import java.nio.channels.FileChannel;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.message.DbException;
......@@ -44,18 +43,18 @@ public class FileLister {
lock.lock(FileLockMethod.FILE);
lock.unlock();
} catch (DbException e) {
throw DbException.get(
throw DbException.getSQLException(
ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1,
message).getSQLException();
message);
}
} else if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
try (FileChannel f = FilePath.get(fileName).open("r")) {
java.nio.channels.FileLock lock = f.tryLock(0, Long.MAX_VALUE, true);
lock.release();
} catch (Exception e) {
throw DbException.get(
throw DbException.getSQLException(
ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, e,
message).getSQLException();
message);
}
}
}
......
......@@ -15,7 +15,6 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import org.h2.api.ErrorCode;
import org.h2.engine.Database;
import org.h2.engine.SysProperties;
......@@ -227,9 +226,8 @@ public class LobStorageBackend implements LobStorageInterface {
prep.setLong(1, block);
ResultSet rs = prep.executeQuery();
if (!rs.next()) {
throw DbException.get(ErrorCode.IO_EXCEPTION_1,
"Missing lob entry, block: " + block)
.getSQLException();
throw DbException.getSQLException(ErrorCode.IO_EXCEPTION_1,
"Missing lob entry, block: " + block);
}
int compressed = rs.getInt(1);
byte[] buffer = rs.getBytes(2);
......@@ -654,8 +652,8 @@ public class LobStorageBackend implements LobStorageInterface {
prep.setLong(1, lobId);
ResultSet rs = prep.executeQuery();
if (!rs.next()) {
throw DbException.get(ErrorCode.IO_EXCEPTION_1,
"Missing lob entry: " + lobId).getSQLException();
throw DbException.getSQLException(ErrorCode.IO_EXCEPTION_1,
"Missing lob entry: " + lobId);
}
byteCount = rs.getLong(1);
reuse(sql, prep);
......@@ -669,8 +667,8 @@ public class LobStorageBackend implements LobStorageInterface {
rs.next();
int lobMapCount = rs.getInt(1);
if (lobMapCount == 0) {
throw DbException.get(ErrorCode.IO_EXCEPTION_1,
"Missing lob entry: " + lobId).getSQLException();
throw DbException.getSQLException(ErrorCode.IO_EXCEPTION_1,
"Missing lob entry: " + lobId);
}
reuse(sql, prep);
......
......@@ -23,7 +23,6 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
......@@ -587,7 +586,7 @@ public class Csv implements SimpleRowSource {
}
private static SQLException convertException(String message, Exception e) {
return DbException.get(ErrorCode.IO_EXCEPTION_1, e, message).getSQLException();
return DbException.getSQLException(ErrorCode.IO_EXCEPTION_1, e, message);
}
/**
......
......@@ -242,7 +242,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
@Override
public void beforeFirst() throws SQLException {
if (autoClose) {
throw DbException.get(ErrorCode.RESULT_SET_NOT_SCROLLABLE).getSQLException();
throw DbException.getSQLException(ErrorCode.RESULT_SET_NOT_SCROLLABLE);
}
rowId = -1;
if (source != null) {
......@@ -278,8 +278,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
}
}
}
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, columnLabel)
.getSQLException();
throw DbException.getSQLException(ErrorCode.COLUMN_NOT_FOUND_1, columnLabel);
}
/**
......@@ -2265,13 +2264,12 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
* INTERNAL
*/
static SQLException getUnsupportedException() {
return DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1).
getSQLException();
return DbException.getSQLException(ErrorCode.FEATURE_NOT_SUPPORTED_1);
}
private void checkClosed() throws SQLException {
if (columns == null) {
throw DbException.get(ErrorCode.OBJECT_CLOSED).getSQLException();
throw DbException.getSQLException(ErrorCode.OBJECT_CLOSED);
}
}
......@@ -2284,8 +2282,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
private Object get(int columnIndex) throws SQLException {
if (currentRow == null) {
throw DbException.get(ErrorCode.NO_DATA_AVAILABLE).
getSQLException();
throw DbException.getSQLException(ErrorCode.NO_DATA_AVAILABLE);
}
checkColumnIndex(columnIndex);
columnIndex--;
......
......@@ -10,7 +10,6 @@ import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.Properties;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.store.FileLister;
......@@ -65,8 +64,8 @@ public abstract class Tool {
*/
protected SQLException throwUnsupportedOption(String option)
throws SQLException {
throw DbException.get(
ErrorCode.FEATURE_NOT_SUPPORTED_1, option).getSQLException();
throw DbException.getSQLException(
ErrorCode.FEATURE_NOT_SUPPORTED_1, option);
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论