提交 1244fb71 authored 作者: Thomas Mueller's avatar Thomas Mueller

javadocs

上级 2c4da91e
......@@ -63,18 +63,17 @@ public class Message {
}
/**
* Gets the SQL Exception object for a specific SQLState. Supported
* SQL states are:
* Gets the SQL exception object for a specific error code.
*
* @param sqlState the SQL state
* @param errorCode the error code
* @param p1 the first parameter of the message
* @return the SQLException object
*/
public static JdbcSQLException getSQLException(int sqlState, String p1) {
return getSQLException(sqlState, new String[] { p1 });
public static JdbcSQLException getSQLException(int errorCode, String p1) {
return getSQLException(errorCode, new String[] { p1 });
}
public static String translate(String key, String[] param) {
private static String translate(String key, String[] param) {
String message = MESSAGES.getProperty(key);
if (message == null) {
message = "(Message " +key+ " not found)";
......@@ -86,62 +85,121 @@ public class Message {
return message;
}
public static JdbcSQLException getSQLException(int errorCode, String[] param, Throwable cause) {
/**
* Gets the SQL exception object for a specific error code.
*
* @param errorCode the error code
* @param params the list of parameters of the message
* @param cause the cause of the exception
* @return the SQLException object
*/
public static JdbcSQLException getSQLException(int errorCode, String[] params, Throwable cause) {
String sqlstate = ErrorCode.getState(errorCode);
String message = translate(sqlstate, param);
String message = translate(sqlstate, params);
return new JdbcSQLException(message, null, sqlstate, errorCode, cause, null);
}
public static JdbcSQLException getSQLException(int errorCode, String[] param) {
return getSQLException(errorCode, param, null);
/**
* Gets the SQL exception object for a specific error code.
*
* @param errorCode the error code
* @param params the list of parameters of the message
* @return the SQLException object
*/
public static JdbcSQLException getSQLException(int errorCode, String[] params) {
return getSQLException(errorCode, params, null);
}
/**
* Constructs a syntax error SQL exception.
*
* @param sql the SQL statement
* @param index the position of the error in the SQL statement
* @return the SQLException object
*/
public static SQLException getSyntaxError(String sql, int index) {
sql = StringUtils.addAsterisk(sql, index);
return getSQLException(ErrorCode.SYNTAX_ERROR_1, sql);
}
/**
* Constructs a syntax error SQL exception.
*
* @param sql the SQL statement
* @param index the position of the error in the SQL statement
* @param expected the expected keyword at the given position
* @return the SQLException object
*/
public static SQLException getSyntaxError(String sql, int index, String expected) {
sql = StringUtils.addAsterisk(sql, index);
return getSQLException(ErrorCode.SYNTAX_ERROR_2, new String[]{sql, expected});
}
/**
* Gets the SQL Exception object for a specific SQLState.
* Gets the SQL exception object for a specific error code.
*
* @param sqlstate -
* the SQL State
* @param errorCode the error code
* @return the SQLException object
*/
public static JdbcSQLException getSQLException(int sqlstate) {
return getSQLException(sqlstate, (String) null);
public static JdbcSQLException getSQLException(int errorCode) {
return getSQLException(errorCode, (String) null);
}
/**
* Gets a SQL exception meaning this feature is not supported.
*
* @return the SQLException object
*/
public static JdbcSQLException getUnsupportedException() {
return getSQLException(ErrorCode.FEATURE_NOT_SUPPORTED);
}
/**
* Gets a SQL exception meaning this value is invalid.
*
* @param value the value passed
* @param param the name of the parameter
* @return the SQLException object
*/
public static JdbcSQLException getInvalidValueException(String value, String param) {
return getSQLException(ErrorCode.INVALID_VALUE_2, new String[]{value, param});
}
/**
* Gets an internal error.
*
* @param s the message
* @return the error object
*/
public static Error getInternalError(String s) {
Error e = new Error(s);
TraceSystem.traceThrowable(e);
return e;
}
/**
* Gets an internal error.
*
* @param s the message
* @param e the root cause
* @return the error object
*/
public static Error getInternalError(String s, Exception e) {
Error e2 = new Error(s);
//## Java 1.4 begin ##
Error e2 = new Error(s, e);
e2.initCause(e);
//## Java 1.4 end ##
/*## Java 1.3 only begin ##
Error e2 = new Error(s);
## Java 1.3 only end ##*/
TraceSystem.traceThrowable(e2);
return e2;
}
/**
* Attach a SQL statement to the exception if this is not already done.
*
* @param e the original SQL exception
* @param sql the SQL statement
* @return the error object
*/
public static SQLException addSQL(SQLException e, String sql) {
if (e instanceof JdbcSQLException) {
JdbcSQLException j = (JdbcSQLException) e;
......@@ -156,6 +214,12 @@ public class Message {
}
}
/**
* Convert an exception to a SQL exception using the default mapping.
*
* @param e the root cause
* @return the SQL exception object
*/
public static SQLException convert(Throwable e) {
if (e instanceof InternalException) {
e = ((InternalException) e).getOriginalCause();
......@@ -175,6 +239,13 @@ public class Message {
return getSQLException(ErrorCode.GENERAL_ERROR_1, new String[]{e.toString()}, e);
}
/**
* Convert an IO exception to a SQL exception.
*
* @param e the root cause
* @param message the message
* @return the SQL exception object
*/
public static SQLException convertIOException(IOException e, String message) {
if (message == null) {
return getSQLException(ErrorCode.IO_EXCEPTION_1, new String[]{e.toString()}, e);
......@@ -183,14 +254,31 @@ public class Message {
}
}
/**
* Gets an internal error.
*
* @return the error object
*/
public static Error getInternalError() {
return getInternalError("unexpected code path");
return getInternalError("Unexpected code path");
}
/**
* Convert an exception to an internal runtime exception.
*
* @param e the root cause
* @return the error object
*/
public static InternalException convertToInternal(Exception e) {
return new InternalException(e);
}
/**
* Convert an exception to an IO exception.
*
* @param e the root cause
* @return the IO exception
*/
public static IOException convertToIOException(Throwable e) {
if (e instanceof JdbcSQLException) {
JdbcSQLException e2 = (JdbcSQLException) e;
......
......@@ -14,26 +14,89 @@ import org.h2.util.StringUtils;
*/
public class Trace {
private TraceWriter traceWriter;
private String module;
private String lineSeparator;
public static final String LOCK = "lock";
public static final String SETTING = "setting";
/**
* The trace module name for commands.
*/
public static final String COMMAND = "command";
public static final String INDEX = "index";
public static final String SEQUENCE = "sequence";
/**
* The trace module name for constraints.
*/
public static final String CONSTRAINT = "constraint";
public static final String USER = "user";
public static final String TRIGGER = "trigger";
/**
* The trace module name for databases.
*/
public static final String DATABASE = "database";
/**
* The trace module name for functions.
*/
public static final String FUNCTION = "function";
public static final String JDBC = "jdbc";
/**
* The trace module name for file locks.
*/
public static final String FILE_LOCK = "fileLock";
public static final String TABLE = "table";
/**
* The trace module name for indexes.
*/
public static final String INDEX = "index";
/**
* The trace module name for the JDBC API.
*/
public static final String JDBC = "jdbc";
/**
* The trace module name for locks.
*/
public static final String LOCK = "lock";
/**
* The trace module name for the transaction log.
*/
public static final String LOG = "log";
/**
* The trace module name for schemas.
*/
public static final String SCHEMA = "schema";
public static final String DATABASE = "database";
/**
* The trace module name for sessions.
*/
public static final String SESSION = "session";
/**
* The trace module name for sequences.
*/
public static final String SEQUENCE = "sequence";
/**
* The trace module name for settings.
*/
public static final String SETTING = "setting";
/**
* The trace module name for tables.
*/
public static final String TABLE = "table";
/**
* The trace module name for triggers.
*/
public static final String TRIGGER = "trigger";
/**
* The trace module name for users.
*/
public static final String USER = "user";
private TraceWriter traceWriter;
private String module;
private String lineSeparator;
Trace(TraceWriter traceWriter, String module) {
this.traceWriter = traceWriter;
......@@ -41,36 +104,80 @@ public class Trace {
this.lineSeparator = SysProperties.LINE_SEPARATOR;
}
/**
* Check if the trace level is equal or higher than INFO.
*
* @return true if it is
*/
public boolean isInfoEnabled() {
return traceWriter.isEnabled(TraceSystem.INFO);
}
/**
* Check if the trace level is equal or higher than DEBUG.
*
* @return true if it is
*/
public boolean isDebugEnabled() {
return traceWriter.isEnabled(TraceSystem.DEBUG);
}
/**
* Write a message with trace level ERROR to the trace system.
*
* @param s the message
*/
public void error(String s) {
traceWriter.write(TraceSystem.ERROR, module, s, null);
}
/**
* Write a message with trace level ERROR to the trace system.
*
* @param s the message
* @param t the exception
*/
public void error(String s, Throwable t) {
traceWriter.write(TraceSystem.ERROR, module, s, t);
}
/**
* Write a message with trace level INFO to the trace system.
*
* @param s the message
*/
public void info(String s) {
traceWriter.write(TraceSystem.INFO, module, s, null);
}
/**
* Write Java source code with trace level DEBUG to the trace system.
*
* @param java the source cod
*/
public void debugCode(String java) {
traceWriter.write(TraceSystem.DEBUG, module, lineSeparator + "/**/" + java, null);
}
/**
* Write Java source code with trace level INFO to the trace system.
*
* @param java the source cod
*/
public void infoCode(String java) {
traceWriter.write(TraceSystem.INFO, module, lineSeparator + "/**/" + java, null);
}
/**
* Write a SQL statement with trace level INFO to the trace system.
*
* @param sql the SQL statement
* @param params the parameters used, in the for {1:...}
* @param count the update count
* @param time the time it took to run the statement in ms
*/
public void infoSQL(String sql, String params, int count, long time) {
StringBuffer buff = new StringBuffer(sql.length() + 20);
StringBuffer buff = new StringBuffer(sql.length() + params.length() + 20);
buff.append(lineSeparator);
buff.append("/*SQL");
boolean space = false;
......@@ -106,10 +213,21 @@ public class Trace {
traceWriter.write(TraceSystem.INFO, module, sql, null);
}
/**
* Write a message with trace level DEBUG to the trace system.
*
* @param s the message
*/
public void debug(String s) {
traceWriter.write(TraceSystem.DEBUG, module, s, null);
}
/**
* Write a message with trace level DEBUG to the trace system.
*
* @param s the message
* @param t the exception
*/
public void debug(String s, Throwable t) {
traceWriter.write(TraceSystem.DEBUG, module, s, t);
}
......
......@@ -24,11 +24,11 @@ import org.h2.util.StringUtils;
* The base class for objects that can print trace information about themselves.
*/
public class TraceObject {
public static final int CALLABLE_STATEMENT = 0, CONNECTION = 1, DATABASE_META_DATA = 2,
protected static final int CALLABLE_STATEMENT = 0, CONNECTION = 1, DATABASE_META_DATA = 2,
PREPARED_STATEMENT = 3, RESULT_SET = 4, RESULT_SET_META_DATA = 5,
SAVEPOINT = 6, SQL_EXCEPTION = 7, STATEMENT = 8, BLOB = 9, CLOB = 10,
PARAMETER_META_DATA = 11;
public static final int DATA_SOURCE = 12, XA_DATA_SOURCE = 13, XID = 14, ARRAY = 15;
protected static final int DATA_SOURCE = 12, XA_DATA_SOURCE = 13, XID = 14, ARRAY = 15;
private static final int LAST = ARRAY + 1;
private Trace trace;
......@@ -37,7 +37,8 @@ public class TraceObject {
"call", "conn", "dbMeta", "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob", "pMeta",
"ds", "xads", "xid", "ar"
};
private int type, id;
private int type;
private int id;
protected void setTrace(Trace trace, int type, int id) {
this.trace = trace;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论