01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.lib.jdbc;
20:
21: import java.sql.SQLException;
22: import java.sql.Statement;
23:
24: /**
25: * A {@link SQLException} that contains information about
26: * the {@link Statement} SQL that caused the exception.
27: *
28: * @author Marc Prud'hommeaux
29: * @nojavadoc
30: */
31: public class ReportingSQLException extends SQLException {
32:
33: private final transient Statement _stmnt;
34: private final SQLException _sqle;
35:
36: public ReportingSQLException(SQLException sqle, Statement stmnt,
37: String sql) {
38: super (getExceptionMessage(sqle, stmnt, sql));
39: this ._sqle = sqle;
40: this ._stmnt = stmnt;
41: setNextException(sqle);
42: }
43:
44: public ReportingSQLException(SQLException sqle, String sql) {
45: this (sqle, null, sql);
46: }
47:
48: public ReportingSQLException(SQLException sqle, Statement stmnt) {
49: this (sqle, stmnt, null);
50: }
51:
52: /**
53: * Returns the SQL state of the underlying {@link SQLException}.
54: */
55: public String getSQLState() {
56: return _sqle.getSQLState();
57: }
58:
59: /**
60: * Returns the error code of the underlying {@link SQLException}.
61: */
62: public int getErrorCode() {
63: return _sqle.getErrorCode();
64: }
65:
66: /**
67: * Returns the {@link Statement} that caused the exception.
68: */
69: public Statement getStatement() {
70: return _stmnt;
71: }
72:
73: private static String getExceptionMessage(SQLException sqle,
74: Statement stmnt, String sql) {
75: try {
76: if (stmnt != null)
77: return sqle.getMessage() + " {" + stmnt + "} "
78: + "[code=" + sqle.getErrorCode() + ", state="
79: + sqle.getSQLState() + "]";
80: else if (sql != null)
81: return sqle.getMessage() + " {" + sql + "} " + "[code="
82: + sqle.getErrorCode() + ", state="
83: + sqle.getSQLState() + "]";
84: else
85: return sqle.getMessage() + " " + "[code="
86: + sqle.getErrorCode() + ", state="
87: + sqle.getSQLState() + "]";
88: } catch (Throwable t) {
89: return sqle.getMessage();
90: }
91: }
92: }
|