suppress "variable not initialized" error for break statements. In switch-case statem...
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / adapters / AdapterFactory.java
index f26b278..bd05994 100644 (file)
@@ -2,16 +2,57 @@ package com.quantum.adapters;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
-
-import com.quantum.Messages;
+import java.util.Map;
 
 /**
- * @author root
  * Basically this Factory is a Singleton that is used to return the proper adapter
+ * @author root
  */
 public class AdapterFactory {
+       
+       static class ComparatorImpl implements Comparator {
+
+               public int compare(Object arg0, Object arg1) {
+                       DatabaseAdapter adapter0 = (DatabaseAdapter) arg0;
+                       DatabaseAdapter adapter1 = (DatabaseAdapter) arg1;
+                       
+                       if (adapter0 == null && adapter0 != null) {
+                               return -1;
+                       } else if (adapter0 != null && adapter1 == null) {
+                               return 1;
+                       } else if (adapter0 == null && adapter1 == null) {
+                               return 0;
+                       } else {
+                               return adapter0.getDisplayName().compareTo(adapter1.getDisplayName());
+                       }
+               }
+       }
+       
+       static class DriverInfo {
+               private final String type;
+               private final String urlPattern;
+
+               DriverInfo(String type) {
+                       this(type, null);
+               }
+               DriverInfo(String type, String urlPattern) {
+                       this.type = type;
+                       this.urlPattern = urlPattern;
+                       
+               }
+               public String getType() {
+                       return this.type;
+               }
+               public String getURLPattern() {
+                       return this.urlPattern;
+               }
+       }
+       
        public static final String GENERIC = "GENERIC"; //$NON-NLS-1$
+       public static final String HSQLDB = "HSQLDB"; //$NON-NLS-1$
     public static final String ORACLE = "ORACLE"; //$NON-NLS-1$
     public static final String POSTGRES = "POSTGRES"; //$NON-NLS-1$
     public static final String MYSQL = "MYSQL"; //$NON-NLS-1$
@@ -20,15 +61,74 @@ public class AdapterFactory {
        public static final String ADABASD = "ADABASD"; //$NON-NLS-1$
     public static final String INFORMIX = "INFORMIX"; //$NON-NLS-1$
        public static final String REDBRICK = "REDBRICK"; //$NON-NLS-1$
+       public static final String POINTBASE = "POINTBASE"; //$NON-NLS-1$
     public static final String SYBASE = "SYBASE"; //$NON-NLS-1$
+    public static final String JDBC_ODBC_BRIDGE = "JDBC_ODBC_BRIDGE"; //$NON-NLS-1$
+    public static final String MS_SQL_SERVER = "MS_SQL_SERVER"; //$NON-NLS-1$
+    public static final String DERBY = "DERBY"; //$NON-NLS-1$
 
+       private static final Map DRIVER_MAP = Collections.synchronizedMap(new HashMap());
+       
+       static {
+               DRIVER_MAP.put("com.ddtek.jdbc.informix.InformixDriver", new DriverInfo(INFORMIX));
+               DRIVER_MAP.put("com.ddtek.jdbc.db2.DB2Driver", new DriverInfo(DB2, "jdbc:datadirect:db2://${hostname}:${port};DatabaseName=${dbname}"));
+               DRIVER_MAP.put("com.ddtek.jdbc.oracle.OracleDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
+               DRIVER_MAP.put("com.ddtek.jdbc.sqlserver.SQLServerDriver", new DriverInfo(GENERIC));
+               DRIVER_MAP.put("com.ddtek.jdbc.sybase.SybaseDriver", new DriverInfo(SYBASE));
+               DRIVER_MAP.put("com.ibm.as400.access.AS400JDBCDriver", new DriverInfo(DB2AS400));
+               DRIVER_MAP.put("COM.ibm.db2.jdbc.app.DB2Driver", new DriverInfo(DB2, "jdbc:db2:{dbname}"));
+               DRIVER_MAP.put("COM.ibm.db2.jdbc.net.DB2Driver", new DriverInfo(DB2, "jdbc:db2://{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("com.inet.ora.OraDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
+               DRIVER_MAP.put("com.inet.drda.DRDADriver", new DriverInfo(DB2, "jdbc:inetdb2:{hostname}:{port}?database={dbname}"));
+               DRIVER_MAP.put("com.inet.syb.SybDriver", new DriverInfo(SYBASE));
+               DRIVER_MAP.put("com.inet.tds.TdsDriver", new DriverInfo(MS_SQL_SERVER));
+               DRIVER_MAP.put("com.informix.jdbc.IfxDriver", new DriverInfo(INFORMIX, "jdbc:informix-sqli://{hostname}:{port}/{dbname}:INFORMIXSERVER={informixserver}"));
+               DRIVER_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", new DriverInfo(MS_SQL_SERVER));
+               DRIVER_MAP.put("com.mysql.jdbc.Driver", new DriverInfo(MYSQL, "jdbc:mysql://{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("com.pointbase.jdbc.jdbcUniversalDriver", new DriverInfo(POINTBASE, "jdbc:pointbase:server://{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("com.sybase.jdbc.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("com.sybase.jdbc2.jdbc.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("com.sybase.jdbcx.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("net.sourceforge.jtds.jdbc.Driver", new DriverInfo(MS_SQL_SERVER));
+               DRIVER_MAP.put("oracle.jdbc.driver.OracleDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
+               DRIVER_MAP.put("oracle.jdbc.OracleDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
+               DRIVER_MAP.put("org.gjt.mm.mysql.Driver", new DriverInfo(MYSQL, "jdbc:mysql://{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("org.hsqldb.jdbcDriver", new DriverInfo(HSQLDB));
+               DRIVER_MAP.put("org.postgresql.Driver", new DriverInfo(POSTGRES, "jdbc:postgresql://{hostname}:{port}/{dbname}"));
+               DRIVER_MAP.put("sun.jdbc.odbc.JdbcOdbcDriver", new DriverInfo(JDBC_ODBC_BRIDGE, "jdbc:odbc:{datasource}"));
+               DRIVER_MAP.put("weblogic.jdbc.informix.InformixDriver", new DriverInfo(INFORMIX));
+               DRIVER_MAP.put("weblogic.jdbc.sqlserver.SybaseDriver", new DriverInfo(SYBASE));
+               DRIVER_MAP.put("org.apache.derby.jdbc.driver20", new DriverInfo(DERBY, "jdbc:derby:{dbname}"));
+               DRIVER_MAP.put("org.apache.derby.jdbc.driver30", new DriverInfo(DERBY, "jdbc:derby:{dbname}"));
+               DRIVER_MAP.put("org.apache.derby.jdbc.EmbeddedDriver", new DriverInfo(DERBY, "jdbc:derby:{dbname}"));
+               
+       }
+       
     private static AdapterFactory instance;
     
-    private ArrayList drivers;
+    private Map adapters = Collections.synchronizedMap(new HashMap());
     
     private AdapterFactory() {
-       loadDrivers();
+       addAdapter(new GenericAdapter(GENERIC));
+       addAdapter(new GenericAdapter(HSQLDB));
+       addAdapter(new OracleAdapter());
+       addAdapter(new DB2Adapter());
+       addAdapter(new DB2AS400Adapter());
+       addAdapter(new PostgresAdapter());
+       addAdapter(new MySQLAdapter());
+       addAdapter(new AdabasDAdapter());
+       addAdapter(new InformixAdapter());
+       addAdapter(new RedBrickAdapter());
+       addAdapter(new GenericAdapter(SYBASE));
+       addAdapter(new GenericAdapter(POINTBASE));
+       addAdapter(new GenericAdapter(JDBC_ODBC_BRIDGE));
+       addAdapter(new MSSQLServerAdapter());
+       addAdapter(new GenericAdapter(DERBY));
+    }
+    private void addAdapter(DatabaseAdapter adapter) {
+       this.adapters.put(adapter.getType(), adapter);
     }
+    
     public static synchronized AdapterFactory getInstance() {
        if (instance == null) {
                instance = new AdapterFactory();
@@ -36,58 +136,35 @@ public class AdapterFactory {
        return instance;
     }
 
-    /**
-     * Master list of supported drivers
-     */
-    private void loadDrivers() {
-       drivers = new ArrayList();
-
-               DriverInfo generic = new DriverInfo(GENERIC, Messages.getString("adapters.generic"), new GenericAdapter()); //$NON-NLS-1$
-               DriverInfo oracle = new DriverInfo(ORACLE, Messages.getString("adapters.oracle"), new OracleAdapter()); //$NON-NLS-1$
-               DriverInfo db2 = new DriverInfo(DB2,  Messages.getString("adapters.db2"), new DB2Adapter()); //$NON-NLS-1$
-               DriverInfo db2as400 = new DriverInfo(DB2AS400,  Messages.getString("adapters.db2as400"), new DB2AS400Adapter()); //$NON-NLS-1$
-               DriverInfo postgres = new DriverInfo(POSTGRES,  Messages.getString("adapters.postgres"), new PostgresAdapter()); //$NON-NLS-1$
-               DriverInfo mysql = new DriverInfo(MYSQL,  Messages.getString("adapters.mysql"), new GenericAdapter()); //$NON-NLS-1$
-               DriverInfo adabasd = new DriverInfo(ADABASD, Messages.getString("adapters.adabasd"), new AdabasDAdapter()); //$NON-NLS-1$
-        DriverInfo informix = new DriverInfo(INFORMIX, Messages.getString("adapters.informix"), new GenericAdapter()); //$NON-NLS-1$
-               DriverInfo redbrick = new DriverInfo(REDBRICK, Messages.getString("adapters.redbrick"), new RedBrickAdapter()); //$NON-NLS-1$
-        DriverInfo sybase = new DriverInfo(SYBASE, Messages.getString("adapters.sybase"), new GenericAdapter()); //$NON-NLS-1$
-
-       drivers.add(generic);
-       drivers.add(oracle);
-       drivers.add(db2);
-       drivers.add(db2as400);
-       drivers.add(postgres);
-       drivers.add(mysql);
-               drivers.add(adabasd);
-        drivers.add(informix);
-               drivers.add(redbrick);
-        drivers.add(sybase);
+    public DatabaseAdapter getAdapter(String type){
+       DatabaseAdapter result = (DatabaseAdapter) this.adapters.get(type);
+       if (result == null) {
+               result = (DatabaseAdapter) this.adapters.get(GENERIC);
+       }
+       return result == null ? null : result;
     }
     
-    public synchronized DatabaseAdapter getAdapter(String type){
-       if (drivers == null) {
-               loadDrivers();
-       }
-       for (int i = 0; i < drivers.size(); i++) {
-               DriverInfo info = (DriverInfo) drivers.get(i);
-               if (type.equals(info.getDriverType())) {
-                       return info.getAdapter();
-               }
-       }
-       // If its not a recognized driver, we return the generic one
-               for (int i = 0; i < drivers.size(); i++) {
-                       DriverInfo info = (DriverInfo) drivers.get(i);
-                       if (type.equals(GENERIC)) {
-                               return info.getAdapter();
-                       }
-               }
-               return null;
+    public DatabaseAdapter[] getDriverList() {
+        List list = new ArrayList(this.adapters.values());
+        Collections.sort(list, new ComparatorImpl());
+       return (DatabaseAdapter[]) list.toArray(new DatabaseAdapter[list.size()]);
     }
     
-    public synchronized DriverInfo[] getDriverList() {
-        List list = new ArrayList(this.drivers);
-        Collections.sort(list);
-       return (DriverInfo[]) list.toArray(new DriverInfo[list.size()]);
+    /**
+     * 
+     * @param driverClassName
+     * @return the adapter type, or null if the adapter type is not known
+     */
+    public String getAdapterType(String driverClassName) {
+       DriverInfo driverInfo = (DriverInfo) DRIVER_MAP.get(driverClassName);
+               return driverInfo == null ? null : driverInfo.getType();
     }
+       /**
+        * @param type
+        * @return
+        */
+       public String getURLPattern(String driverClassName) {
+       DriverInfo driverInfo = (DriverInfo) DRIVER_MAP.get(driverClassName);
+               return driverInfo == null ? null : driverInfo.getURLPattern();
+       }
 }
\ No newline at end of file