shortened display text of variable proposal in content assist
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / adapters / AdapterFactory.java
1 package com.quantum.adapters;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 /**
11  * Basically this Factory is a Singleton that is used to return the proper adapter
12  * @author root
13  */
14 public class AdapterFactory {
15         
16         static class ComparatorImpl implements Comparator {
17
18                 public int compare(Object arg0, Object arg1) {
19                         DatabaseAdapter adapter0 = (DatabaseAdapter) arg0;
20                         DatabaseAdapter adapter1 = (DatabaseAdapter) arg1;
21                         
22                         if (adapter0 == null && adapter0 != null) {
23                                 return -1;
24                         } else if (adapter0 != null && adapter1 == null) {
25                                 return 1;
26                         } else if (adapter0 == null && adapter1 == null) {
27                                 return 0;
28                         } else {
29                                 return adapter0.getDisplayName().compareTo(adapter1.getDisplayName());
30                         }
31                 }
32         }
33         
34         static class DriverInfo {
35                 private final String type;
36                 private final String urlPattern;
37
38                 DriverInfo(String type) {
39                         this(type, null);
40                 }
41                 DriverInfo(String type, String urlPattern) {
42                         this.type = type;
43                         this.urlPattern = urlPattern;
44                         
45                 }
46                 public String getType() {
47                         return this.type;
48                 }
49                 public String getURLPattern() {
50                         return this.urlPattern;
51                 }
52         }
53         
54         public static final String GENERIC = "GENERIC"; //$NON-NLS-1$
55         public static final String HSQLDB = "HSQLDB"; //$NON-NLS-1$
56     public static final String ORACLE = "ORACLE"; //$NON-NLS-1$
57     public static final String POSTGRES = "POSTGRES"; //$NON-NLS-1$
58     public static final String MYSQL = "MYSQL"; //$NON-NLS-1$
59     public static final String DB2 = "DB2"; //$NON-NLS-1$
60     public static final String DB2AS400 = "DB2AS400"; //$NON-NLS-1$
61         public static final String ADABASD = "ADABASD"; //$NON-NLS-1$
62     public static final String INFORMIX = "INFORMIX"; //$NON-NLS-1$
63         public static final String REDBRICK = "REDBRICK"; //$NON-NLS-1$
64         public static final String POINTBASE = "POINTBASE"; //$NON-NLS-1$
65     public static final String SYBASE = "SYBASE"; //$NON-NLS-1$
66     public static final String JDBC_ODBC_BRIDGE = "JDBC_ODBC_BRIDGE"; //$NON-NLS-1$
67     public static final String MS_SQL_SERVER = "MS_SQL_SERVER"; //$NON-NLS-1$
68     public static final String DERBY = "DERBY"; //$NON-NLS-1$
69
70         private static final Map DRIVER_MAP = Collections.synchronizedMap(new HashMap());
71         
72         static {
73                 DRIVER_MAP.put("com.ddtek.jdbc.informix.InformixDriver", new DriverInfo(INFORMIX));
74                 DRIVER_MAP.put("com.ddtek.jdbc.db2.DB2Driver", new DriverInfo(DB2, "jdbc:datadirect:db2://${hostname}:${port};DatabaseName=${dbname}"));
75                 DRIVER_MAP.put("com.ddtek.jdbc.oracle.OracleDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
76                 DRIVER_MAP.put("com.ddtek.jdbc.sqlserver.SQLServerDriver", new DriverInfo(GENERIC));
77                 DRIVER_MAP.put("com.ddtek.jdbc.sybase.SybaseDriver", new DriverInfo(SYBASE));
78                 DRIVER_MAP.put("com.ibm.as400.access.AS400JDBCDriver", new DriverInfo(DB2AS400));
79                 DRIVER_MAP.put("COM.ibm.db2.jdbc.app.DB2Driver", new DriverInfo(DB2, "jdbc:db2:{dbname}"));
80                 DRIVER_MAP.put("COM.ibm.db2.jdbc.net.DB2Driver", new DriverInfo(DB2, "jdbc:db2://{hostname}:{port}/{dbname}"));
81                 DRIVER_MAP.put("com.inet.ora.OraDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
82                 DRIVER_MAP.put("com.inet.drda.DRDADriver", new DriverInfo(DB2, "jdbc:inetdb2:{hostname}:{port}?database={dbname}"));
83                 DRIVER_MAP.put("com.inet.syb.SybDriver", new DriverInfo(SYBASE));
84                 DRIVER_MAP.put("com.inet.tds.TdsDriver", new DriverInfo(MS_SQL_SERVER));
85                 DRIVER_MAP.put("com.informix.jdbc.IfxDriver", new DriverInfo(INFORMIX, "jdbc:informix-sqli://{hostname}:{port}/{dbname}:INFORMIXSERVER={informixserver}"));
86                 DRIVER_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", new DriverInfo(MS_SQL_SERVER));
87                 DRIVER_MAP.put("com.mysql.jdbc.Driver", new DriverInfo(MYSQL, "jdbc:mysql://{hostname}:{port}/{dbname}"));
88                 DRIVER_MAP.put("com.pointbase.jdbc.jdbcUniversalDriver", new DriverInfo(POINTBASE, "jdbc:pointbase:server://{hostname}:{port}/{dbname}"));
89                 DRIVER_MAP.put("com.sybase.jdbc.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
90                 DRIVER_MAP.put("com.sybase.jdbc2.jdbc.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
91                 DRIVER_MAP.put("com.sybase.jdbcx.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
92                 DRIVER_MAP.put("net.sourceforge.jtds.jdbc.Driver", new DriverInfo(MS_SQL_SERVER));
93                 DRIVER_MAP.put("oracle.jdbc.driver.OracleDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
94                 DRIVER_MAP.put("oracle.jdbc.OracleDriver", new DriverInfo(ORACLE ,"jdbc:oracle:thin:@{hostname}:{port}:{dbname}"));
95                 DRIVER_MAP.put("org.gjt.mm.mysql.Driver", new DriverInfo(MYSQL, "jdbc:mysql://{hostname}:{port}/{dbname}"));
96                 DRIVER_MAP.put("org.hsqldb.jdbcDriver", new DriverInfo(HSQLDB));
97                 DRIVER_MAP.put("org.postgresql.Driver", new DriverInfo(POSTGRES, "jdbc:postgresql://{hostname}:{port}/{dbname}"));
98                 DRIVER_MAP.put("sun.jdbc.odbc.JdbcOdbcDriver", new DriverInfo(JDBC_ODBC_BRIDGE, "jdbc:odbc:{datasource}"));
99                 DRIVER_MAP.put("weblogic.jdbc.informix.InformixDriver", new DriverInfo(INFORMIX));
100                 DRIVER_MAP.put("weblogic.jdbc.sqlserver.SybaseDriver", new DriverInfo(SYBASE));
101                 DRIVER_MAP.put("org.apache.derby.jdbc.driver20", new DriverInfo(DERBY, "jdbc:derby:{dbname}"));
102                 DRIVER_MAP.put("org.apache.derby.jdbc.driver30", new DriverInfo(DERBY, "jdbc:derby:{dbname}"));
103                 DRIVER_MAP.put("org.apache.derby.jdbc.EmbeddedDriver", new DriverInfo(DERBY, "jdbc:derby:{dbname}"));
104                 
105         }
106         
107     private static AdapterFactory instance;
108     
109     private Map adapters = Collections.synchronizedMap(new HashMap());
110     
111     private AdapterFactory() {
112         addAdapter(new GenericAdapter(GENERIC));
113         addAdapter(new GenericAdapter(HSQLDB));
114         addAdapter(new OracleAdapter());
115         addAdapter(new DB2Adapter());
116         addAdapter(new DB2AS400Adapter());
117         addAdapter(new PostgresAdapter());
118         addAdapter(new MySQLAdapter());
119         addAdapter(new AdabasDAdapter());
120         addAdapter(new InformixAdapter());
121         addAdapter(new RedBrickAdapter());
122         addAdapter(new GenericAdapter(SYBASE));
123         addAdapter(new GenericAdapter(POINTBASE));
124         addAdapter(new GenericAdapter(JDBC_ODBC_BRIDGE));
125         addAdapter(new MSSQLServerAdapter());
126         addAdapter(new GenericAdapter(DERBY));
127     }
128     private void addAdapter(DatabaseAdapter adapter) {
129         this.adapters.put(adapter.getType(), adapter);
130     }
131     
132     public static synchronized AdapterFactory getInstance() {
133         if (instance == null) {
134                 instance = new AdapterFactory();
135         }
136         return instance;
137     }
138
139     public DatabaseAdapter getAdapter(String type){
140         DatabaseAdapter result = (DatabaseAdapter) this.adapters.get(type);
141         if (result == null) {
142                 result = (DatabaseAdapter) this.adapters.get(GENERIC);
143         }
144         return result == null ? null : result;
145     }
146     
147     public DatabaseAdapter[] getDriverList() {
148         List list = new ArrayList(this.adapters.values());
149         Collections.sort(list, new ComparatorImpl());
150         return (DatabaseAdapter[]) list.toArray(new DatabaseAdapter[list.size()]);
151     }
152     
153     /**
154      * 
155      * @param driverClassName
156      * @return the adapter type, or null if the adapter type is not known
157      */
158     public String getAdapterType(String driverClassName) {
159         DriverInfo driverInfo = (DriverInfo) DRIVER_MAP.get(driverClassName);
160                 return driverInfo == null ? null : driverInfo.getType();
161     }
162         /**
163          * @param type
164          * @return
165          */
166         public String getURLPattern(String driverClassName) {
167         DriverInfo driverInfo = (DriverInfo) DRIVER_MAP.get(driverClassName);
168                 return driverInfo == null ? null : driverInfo.getURLPattern();
169         }
170 }