Initial implementation
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / sql / MultiSQLServer.java
index 843d57f..97c1cc1 100644 (file)
@@ -1,13 +1,9 @@
 package com.quantum.sql;
 
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.InputStream;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.Driver;
@@ -15,13 +11,13 @@ import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.Hashtable;
 import java.util.Properties;
 import java.util.Vector;
 
 import com.quantum.model.Bookmark;
 import com.quantum.model.ConnectionException;
 import com.quantum.model.Entity;
+import com.quantum.model.JDBCDriver;
 import com.quantum.model.PasswordFinder;
 import com.quantum.sql.metadata.MetaDataJDBCInterface;
 import com.quantum.sql.metadata.ObjectMetaData;
@@ -39,11 +35,9 @@ public class MultiSQLServer implements ConnectionEstablisher {
     public static final String USERNAME = "user"; //$NON-NLS-1$
     public static final String PASSWORD = "password"; //$NON-NLS-1$
     private static MultiSQLServer instance = null;
-    private Hashtable classLoaderCache = new Hashtable();
     boolean running = true;
 
-    public MultiSQLServer() {
-        //start();
+    private MultiSQLServer() {
     }
     public synchronized static MultiSQLServer getInstance() {
         if (instance == null) {
@@ -93,22 +87,6 @@ public class MultiSQLServer implements ConnectionEstablisher {
         }
     }
 
-    public Vector getSchemas(Connection con) {
-        ResultSet set;
-        Vector schemaList = new Vector();
-        try {
-            DatabaseMetaData meta = con.getMetaData();
-            set = meta.getSchemas();
-            while (set.next()) {
-                schemaList.add(set.getString("TABLE_SCHEM")); //$NON-NLS-1$
-            }
-            set.close();
-        } catch (SQLException e) {
-            LogProxy log = LogProxy.getInstance();
-            log.addText(LogProxy.ERROR, e);
-        }
-        return schemaList;
-    }
     /**
      * Makes a connection to a JDBC driver based on the data from a bookmark
      * @param bookmark - 
@@ -128,59 +106,48 @@ public class MultiSQLServer implements ConnectionEstablisher {
                 bookmark.setPassword(password);
             }
         }
-               Connection con;
+        
         if (password != null) {
-            con = connect(bookmark, password);
+            Connection connection = connect(bookmark, password);
+            if (connection != null) {
+                       // Set the autoCommit state of the bookmark to the default on new connections
+                               bookmark.setAutoCommit(bookmark.getDefaultAutoCommit());
+                               // Set the autoCommit state of the JDBC connection to the bookmark autoCommit statec
+                       setAutoCommit(connection, bookmark.isAutoCommit());
+            }
+               return connection;
         } else {
             return null;
         }
-        // Set the autoCommit state of the bookmark to the default on new connections
-               bookmark.setAutoCommit(bookmark.getDefaultAutoCommit());
-               // Set the autoCommit state of the JDBC connection to the bookmark autoCommit statec
-               setAutoCommit(con, bookmark.isAutoCommit());
-               return con;
         
     }
     private Connection connect(Bookmark bookmark, String password)
         throws ConnectionException {
         LogProxy log = LogProxy.getInstance();
         log.addText(LogProxy.QUERY, "Connecting to: " + bookmark.getName()); //$NON-NLS-1$
-        URL urls[] = new URL[1];
         try {
-            String driverFile = bookmark.getDriverFile();
-            URLClassLoader loader =
-                (URLClassLoader) classLoaderCache.get(driverFile);
-            if (loader == null) {
-                urls[0] = new File(driverFile).toURL();
-                loader = new URLClassLoader(urls);
-                classLoaderCache.put(driverFile, loader);
-                System.out.println("Creating new classloader"); //$NON-NLS-1$
-            } else {
-                System.out.println("Using classloader in cache"); //$NON-NLS-1$
+               JDBCDriver jdbcDriver = bookmark.getJDBCDriver();
+            Driver driver = jdbcDriver.getDriver();
+            Connection connection = null;
+            if (driver != null) {
+                   Properties props = new Properties();
+                   props.put(USERNAME, bookmark.getUsername());
+                   props.put(PASSWORD, password);
+                   connection =
+                       driver.connect(bookmark.getConnect(), props);
+                   if (connection == null) {
+                       throw new ConnectionException("Error: Driver returned a null connection: " + bookmark.toString()); //$NON-NLS-1$
+                   }
+                   
+                   DatabaseMetaData metaData = connection.getMetaData();
+                   jdbcDriver.setName(metaData.getDriverName());
+                   jdbcDriver.setVersion(metaData.getDriverVersion());
+                   log.addText(LogProxy.RESULTS, "Connected to: " + bookmark.getName()); //$NON-NLS-1$
+                   System.out.println("Connected"); //$NON-NLS-1$
             }
-            Class driverClass = loader.loadClass(bookmark.getDriver());
-            Driver driver = (Driver) driverClass.newInstance();
-            Properties props = new Properties();
-            props.put(USERNAME, bookmark.getUsername());
-            props.put(PASSWORD, password);
-            Connection connection =
-                driver.connect(bookmark.getConnect(), props);
-            if (connection == null) {
-                throw new ConnectionException("Error: Driver returned a null connection: " + bookmark.toString()); //$NON-NLS-1$
-            }
-            log.addText(LogProxy.RESULTS, "Connected to: " + bookmark.getName()); //$NON-NLS-1$
-            System.out.println("Connected"); //$NON-NLS-1$
             return connection;
         } catch (SQLException e) {
             throw new ConnectionException(e);
-        } catch (MalformedURLException e) {
-            throw new ConnectionException(e);
-        } catch (ClassNotFoundException e) {
-            throw new ConnectionException(e);
-        } catch (InstantiationException e) {
-            throw new ConnectionException(e);
-        } catch (IllegalAccessException e) {
-            throw new ConnectionException(e);
         }
     }
        public SQLResults execute(Connection con, String s) throws SQLException {
@@ -205,6 +172,7 @@ public class MultiSQLServer implements ConnectionEstablisher {
 
                SQLResults results = new SQLResults();
 
+               long startTime = System.currentTimeMillis();
                System.out.println("Executing"); //$NON-NLS-1$
                LogProxy log = LogProxy.getInstance();
                log.addText(LogProxy.QUERY, "Executing Request [" + s + "]"); //$NON-NLS-1$ //$NON-NLS-2$
@@ -348,6 +316,7 @@ public class MultiSQLServer implements ConnectionEstablisher {
                }
                log.addText(LogProxy.RESULTS, "Success: result set displayed"); //$NON-NLS-1$
                stmt.close();
+               results.setTime(System.currentTimeMillis() - startTime);
                System.out.println("Executed"); //$NON-NLS-1$
                System.out.println();
                return results;