Separate XDebugResponse from ResponseListner.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / Bookmark.java
index d2dff2b..6396940 100644 (file)
@@ -5,6 +5,7 @@ import java.beans.PropertyChangeSupport;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Hashtable;
@@ -28,16 +29,20 @@ import org.eclipse.jface.preference.IPreferenceStore;
  * 
  * @author root
  */
-public class Bookmark {
+public class Bookmark implements Displayable {
+       
+       public static final int SCHEMA_RULE_USE_ALL = 1;
+       public static final int SCHEMA_RULE_USE_DEFAULT = 2;
+       public static final int SCHEMA_RULE_USE_SELECTED = 3;
     
     private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
        private String name = ""; //$NON-NLS-1$
        private String username = ""; //$NON-NLS-1$
     private String password = ""; //$NON-NLS-1$
-    private String connect = ""; //$NON-NLS-1$
-    private String driver = ""; //$NON-NLS-1$
-    private String type = ""; //$NON-NLS-1$
-    private String driverFile = ""; //$NON-NLS-1$
+    private String connectionUrl = ""; //$NON-NLS-1$
+    private JDBCDriver driver;
+    
+    private int schemaRule = SCHEMA_RULE_USE_ALL;
     
     /**
      * A quick list is a list of favourite tables that a person might want to view
@@ -52,6 +57,7 @@ public class Bookmark {
     private boolean promptForPassword = false;
     private boolean autoCommit = true;
     private String autoCommitPreference = IQuantumConstants.autoCommitTrue;
+       private Database database;
        
        public Bookmark() {
         this(MultiSQLServer.getInstance());
@@ -67,12 +73,11 @@ public class Bookmark {
                setUsername(data.getUsername());
                setPassword(data.getPassword());
                setConnect(data.getConnect());
-               setDriver(data.getDriver());
-               setType(data.getType());
-               setDriverFile(data.getDriverFile());
+               setJDBCDriver(data.getJDBCDriver());
         setPromptForPassword(data.getPromptForPassword());
         setAutoCommit(data.isAutoCommit());
         setAutoCommitPreference(data.getAutoCommitPreference());
+        setSchemaRule(data.getSchemaRule());
         
         this.schemas.addAll(data.schemas);
         this.quickList = new Hashtable(data.quickList);
@@ -83,23 +88,7 @@ public class Bookmark {
         * @return String
         */
        public String getConnect() {
-               return connect;
-       }
-
-       /**
-        * Returns the driver.
-        * @return String
-        */
-       public String getDriver() {
-               return driver;
-       }
-
-       /**
-        * Returns the driverFile.
-        * @return String
-        */
-       public String getDriverFile() {
-               return driverFile;
+               return this.connectionUrl;
        }
 
        /**
@@ -107,7 +96,11 @@ public class Bookmark {
         * @return String
         */
        public String getPassword() {
-               return password;
+               if (this.promptForPassword) {
+                       return null;
+               } else {
+                       return this.password;
+               }
        }
 
        /**
@@ -120,35 +113,13 @@ public class Bookmark {
 
        /**
         * Sets the connect.
-        * @param connect The connect to set
-        */
-       public void setConnect(String connect) {
-               if (connect == null) {
-                       connect = ""; //$NON-NLS-1$
-               }
-               this.connect = connect;
-       }
-
-       /**
-        * Sets the driver.
-        * @param driver The driver to set
-        */
-       public void setDriver(String driver) {
-               if (driver == null) {
-                       driver = ""; //$NON-NLS-1$
-               }
-               this.driver = driver;
-       }
-
-       /**
-        * Sets the driverFile.
-        * @param driverFile The driverFile to set
+        * @param connectionUrl The connect to set
         */
-       public void setDriverFile(String driverFile) {
-               if (driverFile == null) {
-                       driverFile = ""; //$NON-NLS-1$
+       public void setConnect(String connectionUrl) {
+               if (connectionUrl == null) {
+                       connectionUrl = ""; //$NON-NLS-1$
                }
-               this.driverFile = driverFile;
+               this.connectionUrl = connectionUrl;
        }
 
        /**
@@ -202,10 +173,9 @@ public class Bookmark {
                if (name.equals("") && //$NON-NLS-1$
                username.equals("") && //$NON-NLS-1$
                password.equals("") && //$NON-NLS-1$
-               connect.equals("") && //$NON-NLS-1$
+               connectionUrl.equals("") && //$NON-NLS-1$
                driver.equals("") && //$NON-NLS-1$
-               type.equals("") && //$NON-NLS-1$
-               driverFile.equals("")) { //$NON-NLS-1$
+               driver == null) {
                        return true;
                }
                return false;
@@ -222,28 +192,14 @@ public class Bookmark {
                buffer.append("password=****"); //$NON-NLS-1$
                buffer.append(", "); //$NON-NLS-1$
                buffer.append("connect="); //$NON-NLS-1$
-               buffer.append(connect);
+               buffer.append(connectionUrl);
                buffer.append(", "); //$NON-NLS-1$
                buffer.append("driver="); //$NON-NLS-1$
                buffer.append(driver);
-               buffer.append(", "); //$NON-NLS-1$
-               buffer.append("type="); //$NON-NLS-1$
-               buffer.append(type);
-               buffer.append(", "); //$NON-NLS-1$
-               buffer.append("driverFile="); //$NON-NLS-1$
-               buffer.append(driverFile);
                buffer.append("]"); //$NON-NLS-1$
                return buffer.toString();
        }
        
-       public String getType() {
-               return type;
-       }
-
-       public void setType(String type) {
-               this.type = type;
-       }
-
     public Connection connect(PasswordFinder passwordFinder) throws ConnectionException {
         boolean isConnected = isConnected();
         if (this.connection == null) {
@@ -284,7 +240,7 @@ public class Bookmark {
        this.connection = connection;
     }
 
-    public void disconnect() throws ConnectionException {
+    public void disconnect() throws SQLException {
         boolean isConnected = isConnected();
         try {
             if (this.connection != null) {
@@ -292,6 +248,7 @@ public class Bookmark {
             }
         } finally {
             this.connection = null;
+            this.database = null;
             if (isConnected() != isConnected) {
                 this.propertyChangeSupport.firePropertyChange(
                     "connected", isConnected, isConnected());
@@ -312,7 +269,7 @@ public class Bookmark {
         }
     }
 
-    public void setSchemas(Schema[] schemas) {
+    public void setSchemaSelections(Schema[] schemas) {
         this.schemas.clear();
         for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
             i < length;
@@ -327,17 +284,53 @@ public class Bookmark {
     /**
      * @return a list of all the schemas that have been set up.
      */
-    public Schema[] getSchemas() {
-        Set set = new HashSet();
-        set.addAll(this.schemas);
-        if (set.isEmpty()) {
-            set.add(new Schema(getAdapter().getDefaultSchema(this.username), 
-                this.username, true));
-        }
-        List list = new ArrayList(set);
+    public Schema[] getSchemaSelections() {
+        List list = new ArrayList(this.schemas);
         Collections.sort(list);
         return (Schema[]) list.toArray(new Schema[list.size()]);
     }
+    
+    public Schema[] getSchemas() throws NotConnectedException, SQLException {
+       Schema[] schemas = null;
+       if (useUsernameAsSchema()) {
+               // do nothing
+       } else if (useAllSchemas()) {
+               schemas = getDatabase().getSchemas();
+       } else {
+               schemas = verifySchemas(getSchemaSelections());
+       }
+       return (schemas == null || schemas.length == 0) 
+                               ? new Schema[] { getDefaultSchema() } 
+                       : schemas;
+    }
+
+       /**
+        * @param schemaSelections
+        * @return
+        * @throws SQLException
+        * @throws NotConnectedException
+        */
+       private Schema[] verifySchemas(Schema[] schemaSelections) 
+                       throws NotConnectedException, SQLException {
+               Schema[] schemasFromDatabase = getDatabase().getSchemas();
+               List list = Arrays.asList(schemasFromDatabase);
+               for (int i = 0, length = schemaSelections == null ? 0 : schemaSelections.length; 
+                               i < length; i++) {
+                       schemaSelections[i].setExists(list.contains(schemaSelections[i]));
+               }
+               return schemaSelections;
+       }
+
+       /**
+        * @return
+        * @throws NotConnectedException
+        * @throws SQLException
+        */
+       private Schema getDefaultSchema() throws NotConnectedException, SQLException {
+               String username = getDatabase().getUsername();
+               String actual = getAdapter().getDefaultSchema(username);
+               return new Schema(actual, username, true);
+       }
 
        /**
         * @see java.lang.Object#equals(java.lang.Object)
@@ -363,20 +356,20 @@ public class Bookmark {
         this.propertyChangeSupport.removePropertyChangeListener(listener);
     }
 
-    public void addQuickListEntry(String type, String schemaName, String name) {
-        Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type);
-        this.quickList.put(entity.getCondQualifiedName(), entity);
+    public void addQuickListEntry(String type, String schemaName, String name, boolean isSynonym) {
+        Entity entity = EntityFactory.getInstance().create(this, schemaName, name, type, isSynonym);
+        this.quickList.put(entity.getQualifiedName(), entity);
         this.propertyChangeSupport.firePropertyChange("quickList", null, null);
         this.changed = true;
     }
     
     public void addQuickListEntry(Entity entity) {
-        addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName());
+        addQuickListEntry(entity.getType(), entity.getSchema(), entity.getName(), entity.isSynonym());
     }
     
     public void removeQuickListEntry(Entity entity) {
-        if (entity != null  && this.quickList.containsKey(entity.getCondQualifiedName())) {
-            this.quickList.remove(entity.getCondQualifiedName());
+        if (entity != null  && this.quickList.containsKey(entity.getQualifiedName())) {
+            this.quickList.remove(entity.getQualifiedName());
             this.propertyChangeSupport.firePropertyChange("quickList", null, null);
             this.changed = true;
         }
@@ -407,11 +400,16 @@ public class Bookmark {
         if (!isConnected()) {
             throw new NotConnectedException();
         }
-        return new Database(this);
+        if (this.database == null) {
+               this.database = new Database(this);
+        }
+        return this.database;
     }
     
     public DatabaseAdapter getAdapter() {
-        return AdapterFactory.getInstance().getAdapter(getType());
+        return this.driver == null 
+                               ? null 
+                               : AdapterFactory.getInstance().getAdapter(this.driver.getType());
     }
 
     public Entity[] getEntitiesForSchema(Schema schema, String type) throws SQLException {
@@ -440,7 +438,7 @@ public class Bookmark {
     }
     
     public boolean isInQuickList(Entity entity) {
-        return this.quickList.containsKey(entity.getCondQualifiedName());
+        return this.quickList.containsKey(entity.getQualifiedName());
     }
     
     /**
@@ -521,5 +519,45 @@ public class Bookmark {
                
                return true;    
        }
+       
+       public void setJDBCDriver(JDBCDriver jdbcDriver) {
+               this.driver = BookmarkCollection.getInstance().findDriver(jdbcDriver);
+        this.changed = true;
+       }
+       
+       public JDBCDriver getJDBCDriver() {
+               return this.driver;
+       }
+       public boolean useAllSchemas() {
+               return this.schemaRule == SCHEMA_RULE_USE_ALL;
+       }
+       public boolean useUsernameAsSchema() {
+               return this.schemaRule == SCHEMA_RULE_USE_DEFAULT;
+       }
+       public boolean useSelectedSchemas() {
+               return this.schemaRule == SCHEMA_RULE_USE_SELECTED;
+       }
+       public int getSchemaRule() {
+               return this.schemaRule;
+       }
+       public void setSchemaRule(int schemaRule) {
+        if (this.schemaRule != schemaRule) {
+            this.schemaRule = schemaRule;
+            this.propertyChangeSupport.firePropertyChange("schemas", null, null);
+        }
+       }
+
+       public String getDisplayName() {
+               return this.name;
+       }
 
+       /**
+        * @param query
+        */
+       public void removeQuery(String query) {
+               if (this.queries.remove(query)) {
+               this.propertyChangeSupport.firePropertyChange("queries", null, null);
+               this.changed = true;
+               }
+       }
 }