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

javadocs

上级 2c4da91e
...@@ -63,18 +63,17 @@ public class Message { ...@@ -63,18 +63,17 @@ public class Message {
} }
/** /**
* Gets the SQL Exception object for a specific SQLState. Supported * Gets the SQL exception object for a specific error code.
* SQL states are:
* *
* @param sqlState the SQL state * @param errorCode the error code
* @param p1 the first parameter of the message * @param p1 the first parameter of the message
* @return the SQLException object * @return the SQLException object
*/ */
public static JdbcSQLException getSQLException(int sqlState, String p1) { public static JdbcSQLException getSQLException(int errorCode, String p1) {
return getSQLException(sqlState, new 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); String message = MESSAGES.getProperty(key);
if (message == null) { if (message == null) {
message = "(Message " +key+ " not found)"; message = "(Message " +key+ " not found)";
...@@ -86,62 +85,121 @@ public class Message { ...@@ -86,62 +85,121 @@ public class Message {
return 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 sqlstate = ErrorCode.getState(errorCode);
String message = translate(sqlstate, param); String message = translate(sqlstate, params);
return new JdbcSQLException(message, null, sqlstate, errorCode, cause, null); 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) { public static SQLException getSyntaxError(String sql, int index) {
sql = StringUtils.addAsterisk(sql, index); sql = StringUtils.addAsterisk(sql, index);
return getSQLException(ErrorCode.SYNTAX_ERROR_1, sql); 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) { public static SQLException getSyntaxError(String sql, int index, String expected) {
sql = StringUtils.addAsterisk(sql, index); sql = StringUtils.addAsterisk(sql, index);
return getSQLException(ErrorCode.SYNTAX_ERROR_2, new String[]{sql, expected}); 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 - * @param errorCode the error code
* the SQL State
* @return the SQLException object * @return the SQLException object
*/ */
public static JdbcSQLException getSQLException(int sqlstate) { public static JdbcSQLException getSQLException(int errorCode) {
return getSQLException(sqlstate, (String) null); return getSQLException(errorCode, (String) null);
} }
/**
* Gets a SQL exception meaning this feature is not supported.
*
* @return the SQLException object
*/
public static JdbcSQLException getUnsupportedException() { public static JdbcSQLException getUnsupportedException() {
return getSQLException(ErrorCode.FEATURE_NOT_SUPPORTED); 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) { public static JdbcSQLException getInvalidValueException(String value, String param) {
return getSQLException(ErrorCode.INVALID_VALUE_2, new String[]{value, 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) { public static Error getInternalError(String s) {
Error e = new Error(s); Error e = new Error(s);
TraceSystem.traceThrowable(e); TraceSystem.traceThrowable(e);
return 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) { public static Error getInternalError(String s, Exception e) {
Error e2 = new Error(s);
//## Java 1.4 begin ## //## Java 1.4 begin ##
Error e2 = new Error(s, e); e2.initCause(e);
//## Java 1.4 end ## //## Java 1.4 end ##
/*## Java 1.3 only begin ##
Error e2 = new Error(s);
## Java 1.3 only end ##*/
TraceSystem.traceThrowable(e2); TraceSystem.traceThrowable(e2);
return 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) { public static SQLException addSQL(SQLException e, String sql) {
if (e instanceof JdbcSQLException) { if (e instanceof JdbcSQLException) {
JdbcSQLException j = (JdbcSQLException) e; JdbcSQLException j = (JdbcSQLException) e;
...@@ -156,6 +214,12 @@ public class Message { ...@@ -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) { public static SQLException convert(Throwable e) {
if (e instanceof InternalException) { if (e instanceof InternalException) {
e = ((InternalException) e).getOriginalCause(); e = ((InternalException) e).getOriginalCause();
...@@ -175,6 +239,13 @@ public class Message { ...@@ -175,6 +239,13 @@ public class Message {
return getSQLException(ErrorCode.GENERAL_ERROR_1, new String[]{e.toString()}, e); 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) { public static SQLException convertIOException(IOException e, String message) {
if (message == null) { if (message == null) {
return getSQLException(ErrorCode.IO_EXCEPTION_1, new String[]{e.toString()}, e); return getSQLException(ErrorCode.IO_EXCEPTION_1, new String[]{e.toString()}, e);
...@@ -183,14 +254,31 @@ public class Message { ...@@ -183,14 +254,31 @@ public class Message {
} }
} }
/**
* Gets an internal error.
*
* @return the error object
*/
public static Error getInternalError() { 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) { public static InternalException convertToInternal(Exception e) {
return new InternalException(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) { public static IOException convertToIOException(Throwable e) {
if (e instanceof JdbcSQLException) { if (e instanceof JdbcSQLException) {
JdbcSQLException e2 = (JdbcSQLException) e; JdbcSQLException e2 = (JdbcSQLException) e;
......
...@@ -14,26 +14,89 @@ import org.h2.util.StringUtils; ...@@ -14,26 +14,89 @@ import org.h2.util.StringUtils;
*/ */
public class Trace { public class Trace {
private TraceWriter traceWriter; /**
private String module; * The trace module name for commands.
private String lineSeparator; */
public static final String LOCK = "lock";
public static final String SETTING = "setting";
public static final String COMMAND = "command"; 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 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 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 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"; public static final String LOG = "log";
/**
* The trace module name for schemas.
*/
public static final String SCHEMA = "schema"; public static final String SCHEMA = "schema";
public static final String DATABASE = "database";
/**
* The trace module name for sessions.
*/
public static final String SESSION = "session"; 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) { Trace(TraceWriter traceWriter, String module) {
this.traceWriter = traceWriter; this.traceWriter = traceWriter;
...@@ -41,36 +104,80 @@ public class Trace { ...@@ -41,36 +104,80 @@ public class Trace {
this.lineSeparator = SysProperties.LINE_SEPARATOR; this.lineSeparator = SysProperties.LINE_SEPARATOR;
} }
/**
* Check if the trace level is equal or higher than INFO.
*
* @return true if it is
*/
public boolean isInfoEnabled() { public boolean isInfoEnabled() {
return traceWriter.isEnabled(TraceSystem.INFO); return traceWriter.isEnabled(TraceSystem.INFO);
} }
/**
* Check if the trace level is equal or higher than DEBUG.
*
* @return true if it is
*/
public boolean isDebugEnabled() { public boolean isDebugEnabled() {
return traceWriter.isEnabled(TraceSystem.DEBUG); 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) { public void error(String s) {
traceWriter.write(TraceSystem.ERROR, module, s, null); 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) { public void error(String s, Throwable t) {
traceWriter.write(TraceSystem.ERROR, module, s, 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) { public void info(String s) {
traceWriter.write(TraceSystem.INFO, module, s, null); 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) { public void debugCode(String java) {
traceWriter.write(TraceSystem.DEBUG, module, lineSeparator + "/**/" + java, null); 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) { public void infoCode(String java) {
traceWriter.write(TraceSystem.INFO, module, lineSeparator + "/**/" + java, null); 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) { 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(lineSeparator);
buff.append("/*SQL"); buff.append("/*SQL");
boolean space = false; boolean space = false;
...@@ -106,10 +213,21 @@ public class Trace { ...@@ -106,10 +213,21 @@ public class Trace {
traceWriter.write(TraceSystem.INFO, module, sql, null); 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) { public void debug(String s) {
traceWriter.write(TraceSystem.DEBUG, module, s, null); 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) { public void debug(String s, Throwable t) {
traceWriter.write(TraceSystem.DEBUG, module, s, t); traceWriter.write(TraceSystem.DEBUG, module, s, t);
} }
......
...@@ -24,11 +24,11 @@ import org.h2.util.StringUtils; ...@@ -24,11 +24,11 @@ import org.h2.util.StringUtils;
* The base class for objects that can print trace information about themselves. * The base class for objects that can print trace information about themselves.
*/ */
public class TraceObject { 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, PREPARED_STATEMENT = 3, RESULT_SET = 4, RESULT_SET_META_DATA = 5,
SAVEPOINT = 6, SQL_EXCEPTION = 7, STATEMENT = 8, BLOB = 9, CLOB = 10, SAVEPOINT = 6, SQL_EXCEPTION = 7, STATEMENT = 8, BLOB = 9, CLOB = 10,
PARAMETER_META_DATA = 11; 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 static final int LAST = ARRAY + 1;
private Trace trace; private Trace trace;
...@@ -37,7 +37,8 @@ public class TraceObject { ...@@ -37,7 +37,8 @@ public class TraceObject {
"call", "conn", "dbMeta", "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob", "pMeta", "call", "conn", "dbMeta", "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob", "pMeta",
"ds", "xads", "xid", "ar" "ds", "xads", "xid", "ar"
}; };
private int type, id; private int type;
private int id;
protected void setTrace(Trace trace, int type, int id) { protected void setTrace(Trace trace, int type, int id) {
this.trace = trace; this.trace = trace;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论