1 21 22 package org.apache.derby.impl.jdbc; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.iapi.services.i18n.MessageService; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 import org.apache.derby.iapi.services.io.StoredFormatIds; 29 import org.apache.derby.iapi.types.TypeId; 30 31 import org.apache.derby.iapi.error.ExceptionSeverity; 32 33 import org.apache.derby.iapi.reference.SQLState; 34 import org.apache.derby.iapi.reference.MessageId; 35 import org.apache.derby.iapi.reference.JDBC30Translation; 36 import org.apache.derby.iapi.reference.JDBC40Translation; 37 38 import java.sql.SQLException ; 39 import java.sql.Types ; 40 import java.io.IOException ; 41 import java.io.PrintStream ; 42 import java.io.PrintWriter ; 43 44 58 public abstract class Util { 68 69 70 private static SQLExceptionFactory exceptionFactory = 71 new SQLExceptionFactory (); 72 73 76 77 79 84 85 private static SQLException newEmbedSQLException(String messageId, 86 Object [] args, SQLException next, int severity, Throwable t) { 87 String message = MessageService.getCompleteMessage 88 (messageId, args); 89 return exceptionFactory.getSQLException ( 90 message, messageId, next, severity, t, args); 91 } 92 93 public static SQLException newEmbedSQLException(String messageId, 94 Object [] args, int severity) { 95 return newEmbedSQLException(messageId, args, (SQLException ) null, severity, (Throwable ) null); 96 } 97 98 private static SQLException newEmbedSQLException(String messageId, 99 Object [] args, int severity, Throwable t) { 100 return newEmbedSQLException(messageId,args, (SQLException ) null, severity, t); 101 } 102 103 private static SQLException newEmbedSQLException( 104 String messageId, int severity) { 105 return newEmbedSQLException(messageId, (Object []) null, (SQLException ) null, severity, (Throwable ) null); 106 } 107 108 110 111 118 public static void ASSERT(EmbedConnection conn, boolean mustBeTrue, String msg) throws SQLException { 119 if (SanityManager.DEBUG) { 120 try { 121 SanityManager.ASSERT(mustBeTrue, msg); 122 } catch (Throwable t) { 123 SQLException se = conn.handleException(t); 124 SanityManager.ASSERT(se instanceof EmbedSQLException); 127 throw (EmbedSQLException)se; 128 } 129 } 130 } 131 132 137 static void THROWASSERT(EmbedConnection conn, String msg) throws SQLException { 138 if (SanityManager.DEBUG) { 139 try { 140 SanityManager.THROWASSERT(msg); 141 } catch (Throwable t) { 142 SQLException se = conn.handleException(t); 143 SanityManager.ASSERT(se instanceof EmbedSQLException); 146 throw (EmbedSQLException)se; 147 } 148 } 149 } 150 151 159 160 161 static SQLException newException(String messageID, Object a1, 162 Object a2, Object a3) { 163 return newEmbedSQLException(messageID, new Object [] {a1, a2, a3}, 164 StandardException.getSeverityFromIdentifier(messageID)); 165 } 166 167 168 public static SQLException generateCsSQLException(String error) { 169 return newEmbedSQLException(error, 170 StandardException.getSeverityFromIdentifier(error)); 171 } 172 173 public static SQLException generateCsSQLException(String error, Object arg1) { 174 return newEmbedSQLException(error, 175 new Object [] {arg1}, 176 StandardException.getSeverityFromIdentifier(error)); 177 } 178 179 public static SQLException generateCsSQLException( 180 String error, Object arg1, Object arg2){ 181 return newEmbedSQLException(error, 182 new Object [] {arg1, arg2}, 183 StandardException.getSeverityFromIdentifier(error)); 184 } 185 186 public static SQLException generateCsSQLException( 187 String error, Object arg1, Object arg2, Object arg3) { 188 189 return newEmbedSQLException(error, 190 new Object [] {arg1, arg2, arg3}, 191 StandardException.getSeverityFromIdentifier(error)); 192 } 193 194 195 static SQLException generateCsSQLException( 196 String error, Object arg1, Throwable t) { 197 return newEmbedSQLException(error, 198 new Object [] {arg1}, 199 StandardException.getSeverityFromIdentifier(error), t); 200 } 201 202 public static SQLException generateCsSQLException(StandardException se) { 203 return exceptionFactory.getSQLException( 204 se.getMessage(), se.getMessageId(), (SQLException ) null, 205 se.getSeverity(), se, se.getArguments()); 206 } 207 208 public static SQLException noCurrentConnection() { 209 return newEmbedSQLException(SQLState.NO_CURRENT_CONNECTION, 210 StandardException.getSeverityFromIdentifier(SQLState.NO_CURRENT_CONNECTION)); 211 } 212 213 public static SQLException javaException(Throwable t) { 214 String name, msg; 215 216 msg = t.getMessage(); 217 if (msg == null) msg = ""; 218 name = t.getClass().getName(); 219 return newEmbedSQLException(SQLState.JAVA_EXCEPTION, 220 new Object [] {name, msg}, ExceptionSeverity.NO_APPLICABLE_SEVERITY, t); 221 } 222 223 224 public static SQLException notImplemented() { 225 226 return notImplemented( MessageService.getTextMessage(MessageId.CONN_NO_DETAILS) ); 227 } 228 229 public static SQLException notImplemented(String feature) { 230 231 return newEmbedSQLException(SQLState.NOT_IMPLEMENTED, 232 new Object [] {feature}, 233 StandardException.getSeverityFromIdentifier(SQLState.NOT_IMPLEMENTED)); 234 } 235 236 static SQLException setStreamFailure(IOException e) { 237 String msg; 238 239 msg = e.getMessage(); 240 if (msg == null) 241 msg = e.getClass().getName(); 242 return newEmbedSQLException(SQLState.SET_STREAM_FAILURE, 243 new Object [] {msg}, 244 StandardException.getSeverityFromIdentifier(SQLState.SET_STREAM_FAILURE)); 245 } 246 247 static SQLException typeMisMatch(int targetSQLType) { 248 return newEmbedSQLException(SQLState.TYPE_MISMATCH, 249 new Object [] {typeName(targetSQLType)}, 250 StandardException.getSeverityFromIdentifier(SQLState.TYPE_MISMATCH)); 251 } 252 253 257 258 public static void setExceptionFactory (SQLExceptionFactory factory) { 259 exceptionFactory = factory; 260 } 261 262 266 public static SQLExceptionFactory getExceptionFactory() { return exceptionFactory; } 267 268 public static String typeName(int jdbcType) { 269 switch (jdbcType) { 270 case Types.ARRAY: return TypeId.ARRAY_NAME; 271 case Types.BIT : return TypeId.BIT_NAME; 272 case JDBC30Translation.SQL_TYPES_BOOLEAN : return TypeId.BOOLEAN_NAME; 273 case JDBC30Translation.DATALINK: return TypeId.DATALINK_NAME; 274 case Types.TINYINT : return TypeId.TINYINT_NAME; 275 case Types.SMALLINT : return TypeId.SMALLINT_NAME; 276 case Types.INTEGER : return TypeId.INTEGER_NAME; 277 case Types.BIGINT : return TypeId.LONGINT_NAME; 278 279 case Types.FLOAT : return TypeId.FLOAT_NAME; 280 case Types.REAL : return TypeId.REAL_NAME; 281 case Types.DOUBLE : return TypeId.DOUBLE_NAME; 282 283 case Types.NUMERIC : return TypeId.NUMERIC_NAME; 284 case Types.DECIMAL : return TypeId.DECIMAL_NAME; 285 286 case Types.CHAR : return TypeId.CHAR_NAME; 287 case JDBC40Translation.NCHAR: 288 return TypeId.NATIONAL_CHAR_NAME; 289 case Types.VARCHAR : return TypeId.VARCHAR_NAME; 290 case JDBC40Translation.NVARCHAR: 291 return TypeId.NATIONAL_VARCHAR_NAME; 292 case Types.LONGVARCHAR : return "LONGVARCHAR"; 293 case JDBC40Translation.LONGNVARCHAR: 294 return TypeId.NATIONAL_LONGVARCHAR_NAME; 295 case Types.CLOB : return TypeId.CLOB_NAME; 296 case JDBC40Translation.NCLOB: return TypeId.NCLOB_NAME; 297 298 case Types.DATE : return TypeId.DATE_NAME; 299 case Types.TIME : return TypeId.TIME_NAME; 300 case Types.TIMESTAMP : return TypeId.TIMESTAMP_NAME; 301 302 case Types.BINARY : return TypeId.BINARY_NAME; 303 case Types.VARBINARY : return TypeId.VARBINARY_NAME; 304 case Types.LONGVARBINARY : return TypeId.LONGVARBINARY_NAME; 305 case Types.BLOB : return TypeId.BLOB_NAME; 306 307 case Types.OTHER : return "OTHER"; 308 case Types.JAVA_OBJECT : return "Types.JAVA_OBJECT"; 309 case Types.REF : return TypeId.REF_NAME; 310 case JDBC40Translation.ROWID: return TypeId.ROWID_NAME; 311 case Types.STRUCT: return TypeId.STRUCT_NAME; 312 case StoredFormatIds.XML_TYPE_ID : return TypeId.XML_NAME; 313 case JDBC40Translation.SQLXML: return TypeId.SQLXML_NAME; 314 default : return String.valueOf(jdbcType); 315 } 316 } 317 } 318 | Popular Tags |