KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > SQLState


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SQLState.java,v 1.1 2003/10/18 19:54:01 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.util.Arrays JavaDoc;
14
15
16 /**
17  * A SQLSTATE diagnostic code.
18  * <p>
19  * SQLSTATE is a 5-character string that some JDBC drivers return in their
20  * SQLExceptions to indicate status in a vendor-independent way.
21  * SQLSTATE can contain only digits and capital letters.
22  * The first two characters of SQLSTATE indicate a class; the following three
23  * characters indicate a subclass.
24  * Class codes are unique, but subclass codes are not; the meaning of a subclass
25  * code depends on the class code that accompanies it.
26  * <p>
27  * The initial character of the class and subclass indicates the source document
28  * that defines that return condition:
29  * <ul>
30  * <li>
31  * Class codes starting with 0-4 or A-H mean that the result condition is
32  * defined in the International Standard (or, for class 'HZ', in ISO RDA).
33  * In this case, subclass codes for conditions specified in the same standard
34  * also start with 0-4 or A-H.
35  * The meaning of all other subclass codes is implementation-defined.
36  * </li>
37  * <li>
38  * Class codes starting with 5-9 or I-Z denote implementation-specific
39  * conditions.
40  * In this case, subclass codes can start with any character.
41  * The meaning of all subclass codes is implementation-defined, except that
42  * implementations may not define '000' but return '000' when there is no
43  * subclass information.
44  * </li>
45  *
46  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
47  * @version $Revision: 1.1 $
48  *
49  * @see DatabaseAdapter
50  */

51
52 public class SQLState
53 {
54     private static final int LENGTH = 5;
55
56     private final String JavaDoc value;
57
58
59     /**
60      * Constructs a SQLState object from the specified SQLSTATE string.
61      *
62      * @param s
63      * A 5-character SQLSTATE string.
64      *
65      * @exception IllegalArgumentException
66      * if <var>s</var> is not a valid 5-character SQLSTATE string.
67      */

68
69     public SQLState(String JavaDoc s) throws IllegalArgumentException JavaDoc
70     {
71         if (s.length() != LENGTH)
72             throw new IllegalArgumentException JavaDoc(s);
73
74         s = s.toUpperCase();
75
76         for (int i = 0; i < LENGTH; ++i)
77         {
78             if (!isValidChar(s.charAt(i)))
79                 throw new IllegalArgumentException JavaDoc(s);
80         }
81
82         value = s;
83     }
84
85
86     private static boolean isValidChar(char c)
87     {
88         return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
89     }
90
91
92     private static boolean isStandardChar(char c)
93     {
94         return (c >= '0' && c <= '4') || (c >= 'A' && c <= 'H');
95     }
96
97
98     /**
99      * Returns the class code for this SQLSTATE.
100      * This is the first two characters.
101      */

102
103     public final String JavaDoc classCode()
104     {
105         return value.substring(0, 2);
106     }
107
108
109     /**
110      * Returns the subclass code for this SQLSTATE.
111      * This is the last three characters.
112      */

113
114     public final String JavaDoc subclassCode()
115     {
116         return value.substring(2, 5);
117     }
118
119
120     /**
121      * Indicates whether or not this SQLSTATE value is defined by the
122      * international SQL standard.
123      *
124      * @return
125      * <code>true</code> if this is a standard SQLSTATE value,
126      * <code>false</code> otherwise.
127      */

128
129     public final boolean isStandard()
130     {
131         return isStandardChar(value.charAt(0)) && isStandardChar(value.charAt(2));
132     }
133
134
135     /**
136      * Indicates whether or not this SQLSTATE represents an error whose cause
137      * may be transient, meaning the operation or transaction may succeed if
138      * retried.
139      * <p>
140      * The implementation in this class errs on the side of declaring things
141      * retryable, meaning it returns false only for recognized values that are
142      * known not to be worth retrying.
143      * Subclasses may override this method in vendor-specific ways.
144      *
145      * @return
146      * <code>true</code> if the error may be transient,
147      * <code>false</code> if a retry is known to be pointless.
148      */

149
150     public boolean isWorthRetrying()
151     {
152         if (Arrays.binarySearch(ClassCode.NOT_RETRYABLE, classCode()) >= 0)
153             return false;
154         else
155             return true;
156     }
157
158
159     /**
160      * Contains constants for known SQLSTATE class codes.
161      * This list is a merging of all codes defined in the SQL/92 and X/Open
162      * standards.
163      */

164
165     public static class ClassCode
166     {
167         /** Private constructor prevents instantiation. */
168         private ClassCode() {}
169
170         public static final String JavaDoc
171             SUCCESSFUL_COMPLETION = "00",
172             SUCCESS_WITH_WARNING = "01",
173             NO_DATA = "02",
174             DYNAMIC_SQL_ERROR = "07",
175             CONNECTION_EXCEPTION = "08",
176             FEATURE_NOT_SUPPORTED = "0A",
177             CARDINALITY_VIOLATION = "21",
178             DATA_EXCEPTION = "22",
179             INTEGRITY_CONSTRAINT_VIOLATION = "23",
180             INVALID_CURSOR_STATE = "24",
181             INVALID_TRANSACTION_STATE = "25",
182             INVALID_SQL_STATEMENT_NAME = "26",
183             TRIGGERED_DATA_CHANGE_VIOLATION = "27",
184             INVALID_AUTHORIZATION_SPECIFICATION = "28",
185             SYNTAX_ERROR_OR_ACCESS_VIOLATION_IN_DIRECT_SQL_STATEMENT = "2A",
186             DEPENDENT_PRIVILEGE_DESCRIPTORS_STILL_EXIST = "2B",
187             INVALID_CHARACTER_SET_NAME = "2C",
188             INVALID_TRANSACTION_TERMINATION = "2D",
189             INVALID_CONNECTION_NAME = "2E",
190             INVALID_SQL_DESCRIPTOR_NAME = "33",
191             INVALID_CURSOR_NAME = "34",
192             INVALID_EXCEPTION_NUMBER = "35",
193             SYNTAX_ERROR_OR_ACCESS_VIOLATION_IN_DYNAMIC_SQL_STATEMENT = "37",
194             AMBIGUOUS_CURSOR_NAME = "3C",
195             INVALID_CATALOG_NAME = "3D",
196             INVALID_SCHEMA_NAME = "3F",
197             TRANSACTION_ROLLBACK = "40",
198             SYNTAX_ERROR_OR_ACCESS_VIOLATION = "42",
199             WITH_CHECK_OPTION_VIOLATION = "44",
200             REMOTE_DATABASE_ACCESS_ERROR = "HZ";
201
202         /**
203          * An array representing all those class codes that are known
204          * <em>not</em> to be worth retrying.
205          * The array is sorted for efficient searching.
206          */

207         public static final String JavaDoc[] NOT_RETRYABLE =
208         {
209             /* Take care to keep this list sorted by value. */
210             SUCCESSFUL_COMPLETION,
211             SUCCESS_WITH_WARNING,
212             NO_DATA,
213             DYNAMIC_SQL_ERROR,
214             //CONNECTION_EXCEPTION,
215
FEATURE_NOT_SUPPORTED,
216             CARDINALITY_VIOLATION,
217             DATA_EXCEPTION,
218             INTEGRITY_CONSTRAINT_VIOLATION,
219             //INVALID_CURSOR_STATE,
220
//INVALID_TRANSACTION_STATE,
221
INVALID_SQL_STATEMENT_NAME,
222             TRIGGERED_DATA_CHANGE_VIOLATION,
223             INVALID_AUTHORIZATION_SPECIFICATION,
224             SYNTAX_ERROR_OR_ACCESS_VIOLATION_IN_DIRECT_SQL_STATEMENT,
225             DEPENDENT_PRIVILEGE_DESCRIPTORS_STILL_EXIST,
226             INVALID_CHARACTER_SET_NAME,
227             //INVALID_TRANSACTION_TERMINATION,
228
INVALID_CONNECTION_NAME,
229             INVALID_SQL_DESCRIPTOR_NAME,
230             INVALID_CURSOR_NAME,
231             INVALID_EXCEPTION_NUMBER,
232             SYNTAX_ERROR_OR_ACCESS_VIOLATION_IN_DYNAMIC_SQL_STATEMENT,
233             AMBIGUOUS_CURSOR_NAME,
234             INVALID_CATALOG_NAME,
235             INVALID_SCHEMA_NAME,
236             //TRANSACTION_ROLLBACK,
237
SYNTAX_ERROR_OR_ACCESS_VIOLATION,
238             WITH_CHECK_OPTION_VIOLATION,
239             //REMOTE_DATABASE_ACCESS_ERROR
240
};
241     }
242 }
243
244 /*
245  * For reference, all the SQLSTATE codes defined in the SQL/92 standard.
246  *
247  * 00000 Successful completion
248  * 01000 Warning
249  * 01001 Cursor operation conflict
250  * 01002 Disconnect error
251  * 01003 Null value eliminated in set function
252  * 01004 String data
253  * 01005 Insufficient item descriptor areas
254  * 01006 Privilege not revoked
255  * 01007 Privilege not granted
256  * 01008 Implicit zero-bit padding
257  * 01009 Search condition too long for information schema
258  * 0100A Query expression too long for information schema
259  * 02000 No data
260  * 07000 Dynamic SQL error
261  * 07001 Using clause does not match dynamic parameter specifications
262  * 07002 Using clause does not match target specifications
263  * 07003 Cursor specification cannot be executed
264  * 07004 Using clause required for dynamic parameters
265  * 07005 Prepared statement not a cursor specification
266  * 07006 Restricted data type attribute violation
267  * 07007 Using clause required for result fields
268  * 07008 Invalid descriptor count
269  * 07009 Invalid descriptor index
270  * 08000 Connection exception
271  * 08001 SQL-client unable to establish SQL-connection
272  * 08002 Connection name in use
273  * 08003 Connection does not exist
274  * 08004 SQL-server rejected establishment of SQL-connection
275  * 08006 Connection failure
276  * 08007 Transaction resolution unknown
277  * 0A000 Feature not supported
278  * 0A001 Multiple server transactions
279  * 21000 Cardinality violation
280  * 22000 Data exception
281  * 22001 String data
282  * 22002 Null value
283  * 22003 Numeric value out of range
284  * 22005 Error in assignment
285  * 22007 Invalid datetime format
286  * 22008 Datetime field overflow
287  * 22009 Invalid time zone displacement value
288  * 22011 Substring error
289  * 22012 Division by zero
290  * 22015 Interval field overflow
291  * 22018 Invalid character value for cast
292  * 22019 Invalid escape character
293  * 22021 Character not in repertoire
294  * 22022 Indicator overflow
295  * 22023 Invalid parameter value
296  * 22024 Unterminated C string
297  * 22025 Invalid escape sequence
298  * 22026 String data
299  * 22027 Trim error
300  * 23000 Integrity constraint violation
301  * 24000 Invalid cursor state
302  * 25000 Invalid transaction state
303  * 26000 Invalid SQL statement name
304  * 27000 Triggered data change violation
305  * 28000 Invalid authorization specification
306  * 2A000 Syntax error or access rule violation in direct SQL statement
307  * 2B000 Dependent privilege descriptors still exist
308  * 2C000 Invalid character set name
309  * 2D000 Invalid transaction termination
310  * 2E000 Invalid connection name
311  * 33000 Invalid SQL descriptor name
312  * 34000 Invalid cursor name
313  * 35000 Invalid condition number
314  * 37000 Syntax error or access rule violation in dynamic SQL statement
315  * 3C000 Ambiguous cursor name
316  * 3D000 Invalid catalog name
317  * 3F000 Invalid schema name
318  * 40000 Transaction rollback
319  * 40001 Serialization failure
320  * 40002 Integrity constraint violation
321  * 40003 Statement completion unknown
322  * 42000 Syntax error or access rule violation
323  * 44000 With check option violation
324  * HZ000 Remote Database Access
325  *
326  *
327  * For reference, all the SQLSTATE codes defined in the X/Open SQL standard.
328  *
329  * 00000 Success
330  * 01000 Success with warning
331  * 01002 Disconnect error
332  * 01003 Null value eliminated in set function
333  * 01004 String data, right truncation
334  * 01005 Insufficient item descriptor areas
335  * 01006 Privilege not revoked
336  * 01007 Privilege not granted
337  * 0100B Default value too long for system view
338  * 02000 No data
339  * 07000 Dynamic SQL error
340  * 07001 using-clause does not match dynamic parameters
341  * 07002 using-clause does not match target specifications
342  * 07003 Cursor specification cannot be executed
343  * 07004 using-clause is required for dynamic parameters
344  * 07005 Prepared statement is not a cursor specification
345  * 07006 Restricted data type attribute violation
346  * 07008 Invalid descriptor count
347  * 07009 Invalid descriptor index
348  * 08000 Connection exception
349  * 08001 Client unable to establish connection
350  * 08002 Connection name in use
351  * 08003 Connection does not exist
352  * 08004 Server rejected the connection
353  * 08006 Connection failure
354  * 08007 Transaction resolution unknown
355  * 0A000 Feature not supported
356  * 0A001 Multiple-server transaction
357  * 21000 Cardinality violation
358  * 21S01 Insert value list does not match column list
359  * 21S02 Degree of derived table does not match column list
360  * 22000 Data exception
361  * 22001 String data, right truncation
362  * 22002 Null value, no indicator parameter
363  * 22003 Numeric value out of range
364  * 22005 Error in assignment
365  * 22006 Invalid interval format
366  * 22007 Invalid date/time format
367  * 22008 Date/time field overflow
368  * 22011 Substring error
369  * 22012 Division by zero
370  * 22015 Interval field overflow
371  * 22018 Invalid character value for CAST
372  * 22019 Invalid escape character
373  * 22021 Translation result not in target repertoire
374  * 22024 Unterminated string
375  * 22025 Invalid escape sequence
376  * 22027 Trim error
377  * 23000 Integrity constraint violation
378  * 24000 Invalid cursor state
379  * 25000 Invalid transaction state
380  * 26000 Invalid SQL statement identifier
381  * 28000 Invalid authorisation specification
382  * 2C000 Invalid character set name
383  * 2D000 Invalid transaction termination
384  * 2E000 Invalid connection name
385  * 33000 Invalid SQL descriptor name
386  * 35000 Invalid exception number
387  * 3D000 Invalid catalog name
388  * 3F000 Invalid schema name
389  * 40000 Transaction rollback
390  * 40003 Statement completion unknown
391  * 42000 Syntax error or access violation
392  * 42S01 Base table or viewed table already exists
393  * 42S02 Base table not found
394  * 42S11 Index already exists
395  * 42S12 Index not found
396  * 42S21 Column already exists
397  * 42S22 Column not found
398  * 42S31 Schema already exists
399  * 42S32 Schema not found
400  * 42S42 Catalog not found
401  * 42S51 Character set already exists
402  * 42S52 Character set not found
403  * 42S61 Collation already exists
404  * 42S62 Collation not found
405  * 42S72 Conversion not found
406  * 42S81 Translation already exists
407  * 42S82 Translation not found
408  * 44000 WITH CHECK OPTION violation
409  * HZ000 RDA error
410  * HZ010 AccessControlViolation
411  * HZ020 BadRepetitionCount
412  * HZ030 CommandHandleUnknown
413  * HZ040 ControlAuthenticationFailure
414  * HZ050 DataResourceHandleNotSpecified
415  * HZ060 DataResourceHandleUnknown
416  * HZ070 DataResourceNameNotSpecified
417  * HZ080 DataResourceNotAvailable
418  * HZ081 (transient error)
419  * HZ090 DataResourceAlreadyOpen
420  * HZ100 DataResourceUnknown
421  * HZ110 DialogueIDUnknown
422  * HZ120 DuplicateCommandHandle
423  * HZ130 DuplicateDataResourceHandle
424  * HZ140 DuplicateDialogueID
425  * HZ150 DuplicateOperationID
426  * HZ160 InvalidSequence
427  * HZ161 (dialogue already active)
428  * HZ162 (dialogue initialising)
429  * HZ163 (dialogue not active)
430  * HZ164 (dialogue terminating)
431  * HZ165 (transaction not open)
432  * HZ166 (transaction open)
433  * HZ167 (transaction terminating)
434  * HZ170 NoDataResourceAvailable
435  * HZ180 OperationAborted
436  * HZ181 (transient error)
437  * HZ190 OperationCancelled
438  * HZ200 ServiceNotNegotiated
439  * HZ210 TransactionRolledBack
440  * HZ220 UserAuthenticationFailure
441  * HZ230 ControlServicesNotAllowed
442  * HZ300 HostIdentifierError
443  * HZ310 InvalidSQLConformanceLevel
444  * HZ320 RDATransactionNotOpen
445  * HZ325 RDATransactionOpen
446  * HZ330 SQLAccessControlViolation
447  * HZ340 SQLDatabaseResourceAlreadyOpenError
448  * HZ350 SQLDBLArgumentCountMismatch
449  * HZ360 SQLDBLArgumentTypeMismatch
450  * HZ365 SQLNoCharSet
451  * HZ370 SQLDBLTransactionStatementNotAllowed
452  * HZ380 SQLUsageModeViolation
453  * HZ410 Abort failure, service provider
454  * HZ411 Abort failure, service user
455  * HZ420 Association failure, permanent
456  * HZ421 Association failure, transient
457  * HZ430 Release failure
458  * HZ450 Begin dialogue rejected, provider
459  * HZ451 Begin dialogue rejected, user
460  * HZ460 Heuristic hazard
461  * HZ461 Heuristic mix
462  * HZ470 PAbort rollback false
463  * HZ471 PAbort rollback true
464  * HZ480 Rollback
465  * HZ490 UAbort rollback false
466  * HZ491 UAbort rollback true
467  * HZ4A0 UError
468  */

469
Popular Tags