fixed NPE
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / BaseSQLPage.java
index 459a949..435f0cc 100644 (file)
@@ -1,49 +1,98 @@
 package com.quantum.wizards;
 
+import java.sql.SQLException;
+import java.util.Arrays;
+
 import com.quantum.adapters.DatabaseAdapter;
 import com.quantum.model.Bookmark;
 import com.quantum.model.Column;
-import com.quantum.model.ConnectionException;
 import com.quantum.model.Entity;
-import com.quantum.sql.TableRow;
-import com.quantum.view.ViewHelper;
-import com.quantum.view.tableview.TableAdapter;
+import com.quantum.model.NotConnectedException;
+import com.quantum.sql.MultiSQLServer;
+import com.quantum.sql.SQLResultSetResults;
+import com.quantum.sql.SQLResults;
+import com.quantum.ui.dialog.SQLExceptionDialog;
+import com.quantum.util.connection.ConnectionUtil;
 
 import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
 
 /**
- * @author BC
+ * @author BC Holmes
+ * @author Sirkware
  */
 public abstract class BaseSQLPage extends WizardPage implements SQLPage {
 
-    protected TableRow row;
-    protected TableAdapter adapter;
+    protected SQLResultSetResults.Row row;
+       protected SQLResultSetResults results;
+       private ConnectionUtil connectionUtil = new ConnectionUtil();
 
     public BaseSQLPage(String pageName) {
         super(pageName);
     }
     public boolean performFinish() {
-        try {
-            Bookmark bookmark = this.adapter.getBookmark();
-            bookmark.addQuery(getQueryText());
-            ViewHelper.tryGetResults(getShell(), bookmark.getConnection(), getQueryText());
-        } catch (ConnectionException e) {
-            e.printStackTrace();
-        }
-        return true;
-    }
+               Bookmark bookmark = this.results.getBookmark();
+               try {
+                       bookmark.addQuery(getQueryText());
+                       SQLResults sqlResults = MultiSQLServer.getInstance().execute(bookmark, 
+                                       this.connectionUtil.getConnection(bookmark, getShell()), getQueryText());
+                       return sqlResults == null ? false : true;
+               } catch (SQLException e) {
+                       SQLExceptionDialog.openException(getShell(), bookmark, e);
+                       return false;
+               }
+       }
+    
     protected abstract String getQueryText();
     protected void appendColumn(StringBuffer whereClause, Entity entity, String columnName, DatabaseAdapter adapter, String value) {
-            
-        if (adapter != null && entity != null && entity.getColumn(columnName) != null) {
-            Column column = entity.getColumn(columnName);
+        
+        if (adapter != null && entity != null && getColumn(entity, columnName) != null) {
+            Column column = getColumn(entity, columnName);
                whereClause.append(adapter.quote(value, column.getType(), column.getTypeName()));
         } else {
                whereClause.append(value);
         }
     }
-    public void init(TableRow row, TableAdapter adapter) {
-       this.row = row;
-        this.adapter = adapter;
+    /**
+        * @param entity
+        * @param columnName
+        * @return
+        * @throws NotConnectedException
+        * @throws SQLException
+        */
+       protected Column getColumn(Entity entity, String columnName)  {
+               try {
+                       return entity == null ? null : entity.getColumn(columnName);
+               } catch (NotConnectedException e) {
+                       return null;
+               } catch (SQLException e) {
+                       return null;
+               }
+       }
+       public void init(SQLResultSetResults results, SQLResultSetResults.Row row) {
+       this.results = results;
+               this.row = row;
+    }
+       
+       /**
+        * @param table
+        * @param colName
+        * @param style
+        * @param pos
+        * @param width
+        */
+       protected void createTableColumn(Table table, String colName, int style, int pos, int width) {
+        TableColumn column = new TableColumn(table, style, pos);
+               column.setText(colName);                
+               column.setWidth(width);
     }
+       
+       /**
+        * @param columnNames
+        * @return
+        */
+       protected java.util.List getColumnNamesAsList(String[] columnNames) {
+               return Arrays.asList(columnNames);      
+       }
 }