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