001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.db;
020
021import java.sql.Connection;
022import java.sql.DriverManager;
023import java.sql.ResultSet;
024import java.sql.SQLException;
025import java.sql.Statement;
026
027import lucee.commons.lang.ExceptionUtil;
028import lucee.runtime.db.driver.ConnectionProxy;
029import lucee.runtime.db.driver.state.StateFactory;
030
031/**
032 * Utility for db
033 */
034public final class DBUtil {
035
036        // TODO impl. this class, not used at the moment
037        /**
038         * returns label matching className
039         * @param className
040         * @return label
041         */
042        public static String getLabelForDriverClass(String className) {
043                if("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(className))
044                        return "MSSQL DataBase";
045                // TODO connect WS from lucee. ch to get more
046                return className;
047        }
048
049        public static void setAutoCommitEL(Connection conn, boolean b) {
050                /*try {
051                        if(conn!=null){
052                                if(conn.getAutoCommit()==b) return;
053                        }
054                } 
055                catch (Throwable e) {}*/
056                
057                
058                try {
059                        
060                        if(conn!=null)conn.setAutoCommit(b);
061        } 
062        catch (Throwable t) {
063                ExceptionUtil.rethrowIfNecessary(t);
064        }
065        }
066
067        public static void setReadOnlyEL(Connection conn, boolean b) {
068                try {
069                        if(conn!=null)conn.setReadOnly(b);
070                } 
071                catch (Throwable t) {
072                ExceptionUtil.rethrowIfNecessary(t);
073        }
074        }
075
076        public static void commitEL(Connection conn) {
077                try {
078                        if(conn!=null)conn.commit();
079                } 
080                catch (Throwable t) {
081                ExceptionUtil.rethrowIfNecessary(t);
082        }
083        }
084
085        public static void setTransactionIsolationEL(Connection conn,int level) {
086                try {
087                        if(conn!=null)conn.setTransactionIsolation(level);
088                } 
089                catch (Throwable t) {
090                ExceptionUtil.rethrowIfNecessary(t);
091        }
092        }
093
094        public static void closeEL(Statement stat) {
095                if(stat!=null) {
096            try {
097                stat.close();
098            } catch (Throwable t) {
099                ExceptionUtil.rethrowIfNecessary(t);
100            }
101        }
102        }
103
104        public static void closeEL(ResultSet rs) {
105                if(rs!=null) {
106            try {
107                rs.close();
108            } catch (Throwable t) {
109                ExceptionUtil.rethrowIfNecessary(t);
110            }
111        }
112        }
113
114        public static Connection getConnection(String connStr, String user, String pass) throws SQLException {
115                try {
116                        //return DriverManager.getConnection(dsn, user, pass);
117                        return new ConnectionProxy(new StateFactory(), DriverManager.getConnection(connStr, user, pass));
118        } 
119        catch (SQLException e) {
120                
121                if(connStr.indexOf('?')!=-1) {
122                connStr=connStr+"&user="+user+"&password="+pass;
123                        //return DriverManager.getConnection(connStr);
124                return new ConnectionProxy(new StateFactory(), DriverManager.getConnection(connStr));
125            }
126                throw e;
127        }
128        }
129
130}