| 1 19 package org.lucane.server.database; 20 21 import org.lucane.common.Logging; 22 import org.lucane.server.ServerConfig; 23 import org.lucane.server.database.xml.*; 24 25 import java.lang.reflect.*; 26 import java.sql.*; 27 28 import org.apache.commons.dbcp.BasicDataSource; 29 import javax.sql.DataSource ; 30 31 35 public abstract class DatabaseAbstractionLayer 36 { 37 41 public static DatabaseAbstractionLayer createLayer(ServerConfig config) 42 throws Exception  43 { 44 Class.forName(config.getDbDriver()); 45 46 BasicDataSource ds = new BasicDataSource(); 47 ds.setDriverClassName(config.getDbDriver()); 48 ds.setUsername(config.getDbLogin()); 49 ds.setPassword(config.getDbPassword()); 50 ds.setUrl(config.getDbUrl()); 51 52 ds.setPoolPreparedStatements(false); 53 ds.setInitialSize(config.getDbPoolInitialSize()); 54 ds.setMaxActive(config.getDbPoolMaxActive()); 55 ds.setMaxIdle(config.getDbPoolMaxIdle()); 56 ds.setMinIdle(config.getDbPoolMinIdle()); 57 ds.setMaxWait(config.getDbPoolMaxWait()); 58 59 Logging.getLogger().info("Pool initialized (" + 60 "initial size=" + config.getDbPoolInitialSize() + 61 ", max active=" + config.getDbPoolMaxActive() + 62 ", max idle=" + config.getDbPoolMaxIdle() + 63 ", min idle=" + config.getDbPoolMinIdle() + 64 ", max wait=" + config.getDbPoolMaxWait() + 65 ")"); 66 67 68 Class klass = Class.forName(config.getDbLayer()); 70 Class [] types = {DataSource.class}; 71 Object [] values = {ds}; 72 Constructor constr = klass.getConstructor(types); 73 return (DatabaseAbstractionLayer)constr.newInstance(values); 74 } 75 76 81 public abstract Connection getConnection() throws SQLException; 82 83 89 public boolean hasTable(String tableName) 90 throws SQLException 91 { 92 boolean has = false; 93 String [] types = {"TABLE"}; 94 Connection c = this.getConnection(); 95 ResultSet rs = c.getMetaData().getTables(null, null, null, types); 96 97 while(!has && rs.next()) 98 has = rs.getString(3).equalsIgnoreCase(tableName); 99 100 rs.close(); 101 c.close(); 102 103 return has; 104 } 105 106 112 public String escape(String query) 113 { 114 return query.replaceAll("'", "\\'"); 115 } 116 117 121 public TableCreator getTableCreator() 122 { 123 TableCreator creator = new DefaultTableCreator(); 124 creator.setDbLayer(this); 125 return creator; 126 } 127 128 134 public abstract String resolveType(String type); 135 } 136 | Popular Tags |