001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.jdbc;
007:
008: import java.io.PrintStream;
009: import java.io.PrintWriter;
010: import java.sql.SQLException;
011:
012: import org.h2.engine.Constants;
013:
014: /**
015: * Represents a database exception.
016: */
017: public class JdbcSQLException extends SQLException {
018:
019: private static final long serialVersionUID = -8200821788226954151L;
020: private final String originalMessage;
021: private final Throwable cause;
022: private final String trace;
023: private String message;
024: private String sql;
025:
026: /**
027: * Creates a SQLException a message, sqlstate and cause.
028: *
029: * @param message the reason
030: * @param state the SQL state
031: * @param cause the exception that was the reason for this exception
032: */
033: public JdbcSQLException(String message, String sql, String state,
034: int errorCode, Throwable cause, String trace) {
035: super (message, state, errorCode);
036: this .originalMessage = message;
037: this .sql = sql;
038: this .cause = cause;
039: this .trace = trace;
040: buildMessage();
041: //#ifdef JDK14
042: initCause(cause);
043: //#endif
044: }
045:
046: /**
047: * Get the detail error message.
048: *
049: * @return the message
050: */
051: public String getMessage() {
052: return message;
053: }
054:
055: /**
056: * INTERNAL
057: */
058: public String getOriginalMessage() {
059: return originalMessage;
060: }
061:
062: /**
063: * Prints the stack trace to the standard error stream.
064: */
065: public void printStackTrace() {
066: // The default implementation already does that,
067: // but we do it again to avoid problems.
068: // If it is not implemented, somebody might implement it
069: // later on which would be a problem if done in the wrong way.
070: printStackTrace(System.err);
071: }
072:
073: /**
074: * Prints the stack trace to the specified print writer.
075: *
076: * @param s the print writer
077: */
078: public void printStackTrace(PrintWriter s) {
079: if (s != null) {
080: super .printStackTrace(s);
081: //#ifdef JDK13
082: /*
083: if (cause != null) {
084: cause.printStackTrace(s);
085: }
086: */
087: //#endif
088: // getNextException().printStackTrace(s) would be very very slow
089: // if many exceptions are joined
090: SQLException next = getNextException();
091: for (int i = 0; i < 100 && next != null; i++) {
092: s.println(next.toString());
093: next = next.getNextException();
094: }
095: if (next != null) {
096: s.println("(truncated)");
097: }
098: }
099: }
100:
101: /**
102: * Prints the stack trace to the specified print stream.
103: *
104: * @param s the print stream
105: */
106: public void printStackTrace(PrintStream s) {
107: if (s != null) {
108: super .printStackTrace(s);
109: //#ifdef JDK13
110: /*
111: if (cause != null) {
112: cause.printStackTrace(s);
113: }
114: */
115: //#endif
116: // getNextException().printStackTrace(s) would be very very slow
117: // if many exceptions are joined
118: SQLException next = getNextException();
119: for (int i = 0; i < 100 && next != null; i++) {
120: s.println(next.toString());
121: next = next.getNextException();
122: }
123: if (next != null) {
124: s.println("(truncated)");
125: }
126: }
127: }
128:
129: /**
130: * INTERNAL
131: */
132: public Throwable getOriginalCause() {
133: return cause;
134: }
135:
136: /**
137: * Returns the SQL statement.
138: *
139: * @return the SQL statement
140: */
141: public String getSQL() {
142: return sql;
143: }
144:
145: /**
146: * INTERNAL
147: */
148: public void setSQL(String sql) {
149: this .sql = sql;
150: buildMessage();
151: }
152:
153: private void buildMessage() {
154: StringBuffer buff = new StringBuffer(
155: originalMessage == null ? "- " : originalMessage);
156: if (sql != null) {
157: buff.append("; SQL statement:\n");
158: buff.append(sql);
159: }
160: buff.append(" [");
161: buff.append(getErrorCode());
162: buff.append('-');
163: buff.append(Constants.BUILD_ID);
164: buff.append(']');
165: message = buff.toString();
166: }
167:
168: /**
169: * Returns the class name, the message, and in the server mode, the stack
170: * trace of the server
171: *
172: * @return the string representation
173: */
174: public String toString() {
175: if (trace == null) {
176: return super.toString();
177: } else {
178: return trace;
179: }
180: }
181:
182: }
|