1 21 22 package org.apache.derby.client.am; 23 24 import java.sql.SQLException ; 25 import java.util.TreeMap ; 26 27 import org.apache.derby.iapi.services.info.JVMInfo; 28 import org.apache.derby.shared.common.i18n.MessageUtil; 29 import org.apache.derby.shared.common.error.ExceptionUtil; 30 import org.apache.derby.shared.common.reference.SQLState; 31 32 33 public class SqlException extends Exception implements Diagnosable { 76 protected static final int DEFAULT_ERRCODE = 99999; 77 protected Sqlca sqlca_ = null; protected String message_ = null; 79 private String batchPositionLabel_; protected String sqlstate_ = null; 81 protected int errorcode_ = DEFAULT_ERRCODE; 82 protected String causeString_ = null; 83 protected SqlException nextException_; 84 protected Throwable throwable_; 85 86 public static String CLIENT_MESSAGE_RESOURCE_NAME = 87 "org.apache.derby.loc.clientmessages"; 88 89 public static final String CAUSED_BY_EXCEPTION_ID = "J106"; 92 public static final String BATCH_POSITION_ID = "J107"; 93 94 protected static SQLExceptionFactory 98 exceptionFactory = new SQLExceptionFactory (); 99 100 106 private static MessageUtil msgutil_; 107 108 120 public static MessageUtil getMessageUtil() { 121 if ( msgutil_ == null ) { 122 msgutil_ = new MessageUtil(CLIENT_MESSAGE_RESOURCE_NAME); 123 } 124 125 return msgutil_; 126 } 127 128 131 protected SQLException wrappedException_; 132 133 138 166 public SqlException(LogWriter logwriter, 167 ClientMessageId msgid, Object [] args, Throwable cause) 168 { 169 this( 170 logwriter, 171 cause, 172 getMessageUtil().getCompleteMessage( 173 msgid.msgid, 174 args), 175 ExceptionUtil.getSQLStateFromIdentifier(msgid.msgid), 176 ExceptionUtil.getSeverityFromIdentifier(msgid.msgid)); 177 } 178 179 public SqlException(LogWriter logWriter, ClientMessageId msgid, Object [] args, 182 SqlCode sqlcode, Throwable t) { 183 this(logWriter, msgid, args, t); 184 this.errorcode_ = sqlcode.getCode(); 185 } 186 187 public SqlException(LogWriter logWriter, ClientMessageId msgid, Object [] args, 188 SqlCode sqlcode) { 189 this(logWriter, msgid, args, sqlcode, (Throwable )null); 190 } 191 192 public SqlException(LogWriter logWriter, ClientMessageId msgid, SqlCode sqlcode) { 193 this(logWriter, msgid, (Object [])null, sqlcode); 194 } 195 196 public SqlException(LogWriter logWriter, ClientMessageId msgid, Object arg1, 197 SqlCode sqlcode) { 198 this(logWriter, msgid, new Object [] {arg1}, sqlcode); 199 } 200 201 public SqlException(LogWriter logWriter, ClientMessageId msgid, Object arg1, 202 Object arg2, SqlCode sqlcode) { 203 this(logWriter, msgid, new Object [] {arg1, arg2}, sqlcode); 204 } 205 206 public SqlException (LogWriter logwriter, 211 ClientMessageId msgid, Throwable cause) { 212 this (logwriter, msgid, (Object [])null, cause); 213 } 214 215 public SqlException(LogWriter logwriter, ClientMessageId msgid, Object [] args) 216 { 217 this(logwriter, msgid, args, (Throwable )null); 218 } 219 220 public SqlException (LogWriter logwriter, ClientMessageId msgid) 221 { 222 this(logwriter, msgid, (Object [])null); 223 } 224 225 public SqlException(LogWriter logwriter, ClientMessageId msgid, Object arg1) 226 { 227 this(logwriter, msgid, new Object [] { arg1 }); 228 } 229 230 public SqlException(LogWriter logwriter, ClientMessageId msgid, 231 Object arg1, Throwable cause) 232 { 233 this(logwriter, msgid, new Object [] { arg1 }, cause); 234 } 235 236 public SqlException(LogWriter logwriter, ClientMessageId msgid, 237 Object arg1, Object arg2, Throwable cause) 238 { 239 this(logwriter, msgid, new Object [] { arg1, arg2 }, cause); 240 } 241 242 public SqlException(LogWriter logwriter, 243 ClientMessageId msgid, Object arg1, Object arg2) 244 { 245 this(logwriter, msgid, new Object [] { arg1, arg2 }); 246 } 247 248 public SqlException(LogWriter logwriter, 249 ClientMessageId msgid, Object arg1, Object arg2, Object arg3) 250 { 251 this(logwriter, msgid, new Object [] { arg1, arg2, arg3 }); 252 } 253 254 public SqlException(LogWriter logWriter, Sqlca sqlca) { 255 this.sqlca_ = sqlca; 256 if ( logWriter != null ) 257 { 258 logWriter.traceDiagnosable(this); 259 } 260 } 261 262 protected SqlException(LogWriter logWriter, String reason, String sqlState, 265 int errorCode) 266 { 267 this(logWriter, (Throwable )null, reason, sqlState, errorCode); 268 } 269 270 protected SqlException(LogWriter logWriter, java.lang.Throwable throwable, 271 String reason, String sqlState, int errorCode ) { 272 message_ = reason; 273 sqlstate_ = sqlState; 274 errorcode_ = errorCode; 275 276 setThrowable(throwable); 277 278 if (logWriter != null) { 279 logWriter.traceDiagnosable(this); 280 } 281 282 } 283 284 288 protected void setThrowable(Throwable throwable) 289 { 290 throwable_ = throwable; 291 292 if ( throwable instanceof SqlException ) 295 { 296 setNextException((SqlException) throwable); 297 } 298 else if ( throwable instanceof SQLException ) 299 { 300 setNextException((SQLException ) throwable ); 301 } 302 else if ( throwable != null ) 303 { 304 if (JVMInfo.JDK_ID < JVMInfo.J2SE_14 ) 308 { 309 causeString_ = " " + 310 getMessageUtil().getTextMessage(CAUSED_BY_EXCEPTION_ID) + " " + 311 throwable.getClass() + ": " + throwable.getMessage(); 312 } 313 else 314 { 315 initCause(throwable); 316 } 317 } 318 319 } 320 321 327 public SqlException(SQLException wrapme) 328 { 329 wrappedException_ = wrapme; 330 } 331 332 333 336 public SQLException getSQLException() 337 { 338 if ( wrappedException_ != null ) 339 { 340 return wrappedException_; 341 } 342 343 SQLException sqle = exceptionFactory.getSQLException(getMessage(), getSQLState(), 346 getErrorCode()); 347 348 if (JVMInfo.JDK_ID >= JVMInfo.J2SE_14 ) 352 { 353 sqle.initCause(this); 354 } 355 356 if ( nextException_ != null ) 358 { 359 sqle.setNextException(nextException_.getSQLException()); 362 } 363 364 return sqle; 365 } 366 367 void setBatchPositionLabel(int index) { 372 batchPositionLabel_ = getMessageUtil().getTextMessage(BATCH_POSITION_ID) + 373 index + ": "; 374 } 375 376 public Sqlca getSqlca() { 377 return sqlca_; 378 } 379 380 public java.lang.Throwable getThrowable() { 381 return throwable_; 382 } 383 384 public String getMessage() { 385 if ( wrappedException_ != null ) 386 { 387 return wrappedException_.getMessage(); 388 } 389 390 if (sqlca_ != null) { 391 message_ = ((Sqlca) sqlca_).getJDBCMessage(); 392 } 393 394 if (batchPositionLabel_ != null) { 395 message_ = batchPositionLabel_ + message_; 396 } 397 398 if ( causeString_ != null ) { 399 message_ += causeString_; 402 } 403 404 return message_; 405 } 406 407 public String getSQLState() { 408 if ( wrappedException_ != null ) 409 { 410 return wrappedException_.getSQLState(); 411 } 412 413 if (sqlca_ == null) { 414 return sqlstate_; 415 } else { 416 return sqlca_.getSqlState(); 417 } 418 } 419 420 public int getErrorCode() { 421 if ( wrappedException_ != null ) 422 { 423 return wrappedException_.getErrorCode(); 424 } 425 426 if (sqlca_ == null) { 427 return errorcode_; 428 } else { 429 return sqlca_.getSqlCode(); 430 } 431 } 432 433 public SqlException getNextException() 434 { 435 if ( wrappedException_ != null ) 436 { 437 return new SqlException(wrappedException_.getNextException()); 438 } 439 else 440 { 441 return nextException_; 442 } 443 } 444 445 public void setNextException(SqlException nextException) 446 { 447 if ( wrappedException_ != null ) 448 { 449 wrappedException_.setNextException(nextException.getSQLException()); 450 } 451 else 452 { 453 nextException_ = nextException; 454 } 455 } 456 457 public void setNextException(SQLException nextException) 458 { 459 if ( wrappedException_ != null ) 460 { 461 wrappedException_.setNextException(nextException); 462 } 463 else 464 { 465 SqlException theEnd = this; 467 while (theEnd.nextException_ != null) { 468 theEnd = theEnd.nextException_; 469 } 470 theEnd.nextException_ = new SqlException(nextException); 471 } 472 } 473 474 public void printTrace(java.io.PrintWriter printWriter, String header) { 475 ExceptionFormatter.printTrace(this, printWriter, header); 476 } 477 478 482 public static SqlException javaException(LogWriter logWriter, Throwable e) { 483 return new SqlException(logWriter, 484 new ClientMessageId (SQLState.JAVA_EXCEPTION), 485 new Object [] {e.getClass().getName(), e.getMessage()}, e); 486 } 487 488 SqlException copyAsUnchainedSQLException(LogWriter logWriter) { 492 if (sqlca_ != null) { 493 return new SqlException(logWriter, sqlca_); } else { 495 return new SqlException(logWriter, getMessage(), getSQLState(), getErrorCode()); } 497 } 498 499 503 public static void setExceptionFactory (SQLExceptionFactory factory) { 504 exceptionFactory = factory; 505 } 506 } 507 508 511 class ColumnTypeConversionException extends SqlException { 512 ColumnTypeConversionException(LogWriter logWriter, String sourceType, 513 String targetType) { 514 super(logWriter, 515 new ClientMessageId(SQLState.LANG_DATA_TYPE_GET_MISMATCH), 516 sourceType, targetType); 517 } 518 } 519 520 523 class LossOfPrecisionConversionException extends SqlException { 524 LossOfPrecisionConversionException(LogWriter logWriter, String instance) { 525 super(logWriter, new ClientMessageId(SQLState.LOSS_OF_PRECISION_EXCEPTION), 526 instance); 527 } 528 } 529 | Popular Tags |