001 package railo.runtime.exp; 002 003 import java.sql.DatabaseMetaData; 004 import java.sql.SQLException; 005 006 import railo.commons.lang.StringUtil; 007 import railo.runtime.config.Config; 008 import railo.runtime.db.DataSource; 009 import railo.runtime.db.DatasourceConnection; 010 import railo.runtime.db.SQL; 011 import railo.runtime.op.Caster; 012 013 014 /** 015 * Database Exception Object 016 */ 017 018 019 020 public final class DatabaseException extends PageExceptionImpl { 021 022 private SQL sql; 023 private String sqlstate=""; 024 private int errorcode=-1; 025 private DataSource datasource; 026 027 /** 028 * Constructor of the class 029 * @param message error message 030 * @param detail detailed error message 031 * @param sqle 032 * @param sql 033 * @param dc 034 */ 035 public DatabaseException(String message, String detail, SQLException sqle, SQL sql,DatasourceConnection dc) { 036 super(message,"database"); 037 String sqleMessage=sqle!=null?sqle.getMessage():""; 038 this.sql=sql; 039 if(dc!=null)datasource=dc.getDatasource(); 040 if(detail!=null){ 041 if(!StringUtil.isEmpty(sqleMessage)) 042 setDetail(detail+"\n"+sqleMessage); 043 else 044 setDetail(detail); 045 } 046 else { 047 if(!StringUtil.isEmpty(sqleMessage)) 048 setDetail(sqleMessage); 049 } 050 if(sqle!=null) { 051 sqlstate=sqle.getSQLState(); 052 errorcode=sqle.getErrorCode(); 053 this.setStackTrace(sqle.getStackTrace()); 054 } 055 if(sql!=null) { 056 setAdditional("SQL",sql.toString()); 057 } 058 if(dc!=null) { 059 try { 060 DatabaseMetaData md = dc.getConnection().getMetaData(); 061 md.getDatabaseProductName(); 062 setAdditional("DatabaseName",md.getDatabaseProductName()); 063 setAdditional("DatabaseVersion",md.getDatabaseProductVersion()); 064 setAdditional("DriverName",md.getDriverName()); 065 setAdditional("DriverVersion",md.getDriverVersion()); 066 //setAdditional("url",md.getURL()); 067 068 setAdditional("Datasource",dc.getDatasource().getName()); 069 070 071 } 072 catch (SQLException e) {} 073 074 } 075 } 076 077 /** 078 * Constructor of the class 079 * @param message 080 * @param sqle 081 * @param sql 082 */ 083 public DatabaseException(String message, SQLException sqle, SQL sql,DatasourceConnection dc) { 084 this(message,null,sqle,sql,dc); 085 } 086 087 /** 088 * Constructor of the class 089 * @param sqle 090 * @param sql 091 */ 092 public DatabaseException(SQLException sqle, SQL sql,DatasourceConnection dc) { 093 this(sqle!=null?sqle.getMessage():null,null,sqle,sql,dc); 094 } 095 096 /** 097 * Constructor of the class 098 * @param sqle 099 */ 100 public DatabaseException(SQLException sqle,DatasourceConnection dc) { 101 this(sqle!=null?sqle.getMessage():null,null,sqle,null,dc); 102 } 103 104 public CatchBlock getCatchBlock(Config config) { 105 String strSQL=sql==null?"":sql.toString(); 106 if(StringUtil.isEmpty(strSQL))strSQL=Caster.toString(getAdditional().get("SQL", ""),""); 107 108 String datasourceName=datasource==null?"":datasource.getName(); 109 if(StringUtil.isEmpty(datasourceName))datasourceName=Caster.toString(getAdditional().get("DataSource", ""),""); 110 111 CatchBlock sct = super.getCatchBlock(config); 112 sct.setEL("NativeErrorCode",new Double(errorcode)); 113 sct.setEL("DataSource",datasourceName); 114 sct.setEL("SQLState",sqlstate); 115 sct.setEL("Sql",strSQL); 116 sct.setEL("queryError",strSQL); 117 sct.setEL("where",""); 118 return sct; 119 } 120 }