KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > PDOError


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.quercus.lib.db;
31
32 import com.caucho.quercus.env.ArrayValue;
33 import com.caucho.quercus.env.ArrayValueImpl;
34 import com.caucho.quercus.env.Env;
35 import com.caucho.util.L10N;
36
37 import java.sql.SQLException JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 class PDOError {
42   private final static L10N L = new L10N(PDOError.class);
43   private final static Logger JavaDoc log = Logger.getLogger(PDOError.class.getName());
44
45   private static final int ERRMODE_SILENT = PDO.ERRMODE_SILENT;
46   private static final int ERRMODE_WARNING = PDO.ERRMODE_WARNING;
47   private static final int ERRMODE_EXCEPTION = PDO.ERRMODE_EXCEPTION;
48
49   private static final String JavaDoc ERR_NONE = PDO.ERR_NONE;
50   private static final String JavaDoc ERR_GENERAL = "HY000";
51
52   private final Env _env;
53
54   private int _errmode;
55   private boolean _isError;
56   private String JavaDoc _errorCode = ERR_NONE;
57   private ArrayValue _errorInfo;
58
59   public PDOError(Env env)
60   {
61     _env = env;
62   }
63
64   /**
65    * Clear the error if there is one.
66    */

67   public void clear()
68   {
69     _isError = false;
70     _errorCode = ERR_NONE;
71     _errorInfo = null;
72   }
73
74   private void error(String JavaDoc errorCode, int driverError, String JavaDoc errorMessage)
75   {
76     _isError = true;
77
78     int level = Math.max(_errmode, ERRMODE_SILENT);
79
80     _errorCode = errorCode;
81
82     _errorInfo = new ArrayValueImpl();
83     _errorInfo.put(errorCode);
84     _errorInfo.put(driverError);
85     _errorInfo.put(errorMessage);
86
87     if (level == ERRMODE_WARNING) {
88       _env.warning("SQLSTATE[" + _errorCode + "]: " + errorMessage);
89     }
90     else if (level == ERRMODE_EXCEPTION) {
91       // XXX: throw exception, or return exception object?
92
}
93   }
94   /**
95    * Save an error for subsequent calls to
96    * {@link #errorCode} and {@link #errorInfo},
97    * and depending on the value of {@link #setErrmode}
98    * show nothing, show a warning, or throw an exception.
99    */

100   public void error(Throwable JavaDoc exception)
101   {
102     log.log(Level.FINE, exception.toString(), exception);
103
104     String JavaDoc errorCode;
105     String JavaDoc errorMessage;
106     int driverError;
107
108     if (exception instanceof SQLException JavaDoc) {
109       SQLException JavaDoc sqlException = (SQLException JavaDoc) exception;
110       errorCode = sqlException.getSQLState();
111       errorMessage = sqlException.getMessage();
112       driverError = sqlException.getErrorCode();
113     }
114     else {
115       errorCode = ERR_GENERAL;
116       errorMessage = exception.getMessage();
117       driverError = 0;
118     }
119
120     error(errorCode, driverError, errorMessage);
121   }
122
123   public String JavaDoc errorCode()
124   {
125     return _errorCode;
126   }
127
128   public ArrayValue errorInfo()
129   {
130     if (_errorInfo == null) {
131       _errorInfo = new ArrayValueImpl();
132       _errorInfo.put(ERR_NONE);
133     }
134
135     return _errorInfo;
136   }
137
138   public int getErrmode()
139   {
140     return _errmode;
141   }
142
143   public boolean isError()
144   {
145     return _isError;
146   }
147
148   /**
149    * Show a notice and return a "HY000" general error for subsequent calls to
150    * {@link #errorCode} and {@link #errorInfo}.
151    */

152   public void notice(String JavaDoc message)
153   {
154     _isError = true;
155
156     _errorCode = ERR_GENERAL;
157
158     _errorInfo = new ArrayValueImpl();
159     _errorInfo.put(_errorCode);
160     _errorInfo.put(2050);
161     _errorInfo.put("");
162
163     _env.notice(message);
164   }
165
166   /**
167    * Set's the error mode.
168    *
169    * <dl>
170    * <dt>{@link ERRMODE_SILENT}
171    * <dt>{@link ERRMODE_WARNING}
172    * <dt>{@link ERRMODE_EXCEPTION}
173    * </dl>
174    *
175    * @return true on success, false on error.
176    */

177   public boolean setErrmode(int value)
178   {
179     switch (value) {
180       case ERRMODE_SILENT:
181       case ERRMODE_WARNING:
182       case ERRMODE_EXCEPTION:
183         _errmode = value;
184         return true;
185
186       default:
187         warning(L.l("invalid error mode"));
188         return false;
189     }
190   }
191
192   /**
193    * Show a warning and return a "HY000" general error for subsequent calls to
194    * {@link #errorCode} and {@link #errorInfo}.
195    */

196   public void warning(String JavaDoc message)
197   {
198     _isError = true;
199
200     _errorCode = ERR_GENERAL;
201
202     _errorInfo = new ArrayValueImpl();
203     _errorInfo.put(_errorCode);
204
205     if (_errmode == ERRMODE_EXCEPTION) {
206       // XXX: throw exception, or return exception object?
207
}
208     else {
209       _env.warning("SQLSTATE[" + _errorCode + "]: " + message);
210     }
211   }
212
213   public void unsupportedAttribute(int attribute)
214   {
215     error("IM001", 0, L.l("attribute `{0}' is not supported", attribute));
216   }
217
218   public void unsupportedAttributeValue(Object JavaDoc value)
219   {
220     error("IM001", 0, L.l("attribute value `{0}' is not supported", value));
221   }
222 }
223
Popular Tags