001    package railo.commons.db;
002    
003    import java.sql.Connection;
004    import java.sql.DriverManager;
005    import java.sql.ResultSet;
006    import java.sql.SQLException;
007    import java.sql.Statement;
008    
009    import railo.runtime.db.driver.ConnectionProxy;
010    import railo.runtime.db.driver.state.StateFactory;
011    
012    /**
013     * Utility for db
014     */
015    public final class DBUtil {
016    
017            // TODO impl. this class, not used at the moment
018            /**
019             * returns label matching className
020             * @param className
021             * @return label
022             */
023            public static String getLabelForDriverClass(String className) {
024                    if("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(className))
025                            return "MSSQL DataBase";
026                    // TODO connect WS from railo. ch to get more
027                    return className;
028            }
029    
030            public static void setAutoCommitEL(Connection conn, boolean b) {
031                    /*try {
032                            if(conn!=null){
033                                    if(conn.getAutoCommit()==b) return;
034                            }
035                    } 
036                    catch (Throwable e) {}*/
037                    
038                    
039                    try {
040                            
041                            if(conn!=null)conn.setAutoCommit(b);
042            } 
043            catch (Throwable e) {}
044            }
045    
046            public static void setReadOnlyEL(Connection conn, boolean b) {
047                    try {
048                            if(conn!=null)conn.setReadOnly(b);
049                    } 
050                    catch (Throwable e) {}
051            }
052    
053            public static void commitEL(Connection conn) {
054                    try {
055                            if(conn!=null)conn.commit();
056                    } 
057                    catch (Throwable e) {}
058            }
059    
060            public static void setTransactionIsolationEL(Connection conn,int level) {
061                    try {
062                            if(conn!=null)conn.setTransactionIsolation(level);
063                    } 
064                    catch (Throwable e) {}
065            }
066    
067            public static void closeEL(Statement stat) {
068                    if(stat!=null) {
069                try {
070                    stat.close();
071                } catch (Throwable t) {}
072            }
073            }
074    
075            public static void closeEL(ResultSet rs) {
076                    if(rs!=null) {
077                try {
078                    rs.close();
079                } catch (Throwable t) {}
080            }
081            }
082    
083            public static Connection getConnection(String dsn, String user, String pass) throws SQLException {
084                    try {
085                            //return DriverManager.getConnection(dsn, user, pass);
086                            return new ConnectionProxy(new StateFactory(), DriverManager.getConnection(dsn, user, pass));
087            } 
088            catch (SQLException e) {
089                    if(dsn.indexOf('?')!=-1) {
090                    String connStr=dsn+"&user="+user+"&password="+pass;
091                            //return DriverManager.getConnection(connStr);
092                            return new ConnectionProxy(new StateFactory(), DriverManager.getConnection(connStr));
093                }
094                    throw e;
095            }
096            }
097    
098    }