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.runtime.db; 020 021import java.sql.PreparedStatement; 022import java.sql.SQLException; 023import java.sql.Statement; 024 025import lucee.commons.lang.ExceptionUtil; 026import lucee.commons.lang.StringUtil; 027 028 029public class DataSourceUtil { 030 031 032 public static boolean isHSQLDB(DatasourceConnection dc) { 033 try { 034 if(dc.getConnection().getMetaData().getDatabaseProductName().indexOf("HSQL")!=-1) return true; 035 } 036 catch (SQLException e) { 037 String className=dc.getDatasource().getClazz().getName(); 038 if(className.equals("org.hsqldb.jdbcDriver")) 039 return true; 040 } 041 return false; 042 } 043 044 045 public static boolean isOracle(DatasourceConnection dc) { 046 try { 047 if(dc.getConnection().getMetaData().getDatabaseProductName().indexOf("Oracle")!=-1) return true; 048 } 049 catch (SQLException e) { 050 String className=dc.getDatasource().getClazz().getName(); 051 if(className.indexOf("OracleDriver")!=-1) 052 return true; 053 } 054 return false; 055 } 056 057 public static boolean isMySQL(DatasourceConnection dc) { 058 try { 059 if(dc.getConnection().getMetaData().getDatabaseProductName().indexOf("MySQL")!=-1) return true; 060 } 061 catch (SQLException e) { 062 String className=dc.getDatasource().getClazz().getName(); 063 if(className.equals("org.gjt.mm.mysql.Driver")) 064 return true; 065 } 066 return false; 067 } 068 069 070 071 public static boolean isMSSQL(DatasourceConnection dc) { 072 try { 073 if(dc.getConnection().getMetaData().getDatabaseProductName().indexOf("Microsoft")!=-1) return true; 074 } 075 catch (SQLException e) { 076 String className=dc.getDatasource().getClazz().getName(); 077 if(className.equals("com.microsoft.jdbc.sqlserver.SQLServerDriver") || 078 className.equals("net.sourceforge.jtds.jdbc.Driver")) 079 return true; 080 } 081 return false; 082 } 083 084 085 public static boolean isMSSQLDriver(DatasourceConnection dc) { 086 try { 087 if(dc.getConnection().getMetaData().getDriverName().indexOf("Microsoft SQL Server JDBC Driver")!=-1) 088 return true; 089 } 090 catch (SQLException e) {} 091 092 String className=dc.getDatasource().getClazz().getName(); 093 return className.equals("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 094 } 095 096 public static boolean isValid(DatasourceConnection dc, int timeout) throws Throwable { 097 return dc.getConnection().isValid(timeout); 098 } 099 100 101 public static boolean isClosed(PreparedStatement ps, boolean defaultValue) { 102 try { 103 return ps.isClosed(); 104 } 105 catch (Throwable t) { 106 ExceptionUtil.rethrowIfNecessary(t); 107 return defaultValue; 108 } 109 } 110 public static String getDatabaseName(DatasourceConnection dc) throws SQLException { 111 String dbName=null; 112 try { 113 dbName = dc.getDatasource().getDatabase(); 114 } catch(Throwable t) { 115 ExceptionUtil.rethrowIfNecessary(t); 116 } 117 if (StringUtil.isEmpty(dbName)) 118 dbName = dc.getConnection().getCatalog(); // works on most JDBC drivers (except Oracle ) 119 return dbName; 120 } 121 122 123 public static void setQueryTimeoutSilent(Statement stat, int seconds) { 124 // some jdbc driver multiply the value by 1000 to get milli second what can end in a negative value, so we have to make sure the given timeout can be multiply by 1000 125 int max=Integer.MAX_VALUE/1000; 126 if(max<seconds) seconds=max; 127 try { 128 if(seconds>0)stat.setQueryTimeout(seconds); 129 } 130 catch (SQLException e) {} 131 } 132 133}