initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExecuteAction.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/actions/ExecuteAction.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/actions/ExecuteAction.java
new file mode 100644 (file)
index 0000000..f8c4418
--- /dev/null
@@ -0,0 +1,142 @@
+package com.quantum.actions;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Vector;
+
+import com.quantum.Messages;
+import com.quantum.QuantumPlugin;
+import com.quantum.model.Bookmark;
+import com.quantum.sql.MultiSQLServer;
+import com.quantum.sql.SQLParser;
+import com.quantum.sql.SQLResults;
+import com.quantum.view.LogProxy;
+import com.quantum.view.SQLQueryView;
+import com.quantum.view.bookmark.BookmarkNode;
+import com.quantum.view.bookmark.BookmarkView;
+import com.quantum.view.tableview.DefaultSizes;
+import com.quantum.view.tableview.TableView;
+
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IViewActionDelegate;
+import org.eclipse.ui.IViewPart;
+
+/**
+ * @author panic
+ *
+ *     Executes a query from the QueryView
+ */
+public class ExecuteAction extends BaseSQLAction implements IViewActionDelegate {
+       SQLQueryView view;
+       boolean flag = false;
+       String execute1 = Messages.getString("ExecuteAction.Executing_Query3"); //$NON-NLS-1$
+       String execute2 = Messages.getString("ExecuteAction.Executing_Query2"); //$NON-NLS-1$
+    
+       public ExecuteAction() {
+               setActionDefinitionId("com.quantum.actions.ExecuteAction");
+       }
+    
+       public void init(IViewPart view) {
+               this.view = (SQLQueryView) view;
+       }
+
+       public void run(IAction action) {
+               run();
+       }
+    
+       public void run() {
+               BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
+               if (current == null)
+               {
+                        MessageDialog.openInformation(
+                          view.getSite().getShell(),"Sorry","Please select a bookmark to use as connection.");
+                        return;
+               }                  
+               Connection con = getConnection();
+               view.setStatus(execute1);
+               MultiSQLServer server = MultiSQLServer.getInstance();
+               view.setStatus(Messages.getString("ExecuteAction.Parsing_sql_script3")); //$NON-NLS-1$
+               Vector queries = new Vector();
+               String viewQuery = view.getQuery();
+               // We parse the executable units to send to the JDBC driver
+               queries = SQLParser.parse(viewQuery);
+               for (int i = 0; i < queries.size(); i++) {
+                       String query = (String) queries.elementAt(i);
+                       System.out.println(query);
+               }
+               int resultCount = 0;
+               int resultUpdateCount = 0;
+               int errorCount = 0;
+               int resultsDisplayed = 0;
+               double startTime = 0.0; // stores the time when query is started
+               double queryDuration= 0.0; // stores query's execution time
+               
+               for (int i = 0; i < queries.size(); i++) {
+                       if (flag) {
+                               view.setStatus(execute1);
+                       } else {
+                               view.setStatus(execute2);
+                       }
+                  
+                  String query = (String) queries.elementAt(i);
+                  System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
+                  if (con != null && !query.equals("")) { //$NON-NLS-1$
+       
+                       SQLResults results;
+                       try {
+                               startTime = System.currentTimeMillis(); // Start the measure of execution time
+                               results = server.execute(con, query, 1, DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
+                               queryDuration = (System.currentTimeMillis() - startTime)/1000; // calculate the execution time (in seconds)                              
+                               current.getBookmark().addQuery(query);
+                       } catch (SQLException e) {
+                                errorCount++;
+                               LogProxy log = LogProxy.getInstance();
+                               log.addText(
+                                       LogProxy.ERROR,
+                                       "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
+                               MessageDialog.openConfirm(view.getSite().getShell(), "Database returned error", e.getLocalizedMessage());  //$NON-NLS-1$ 
+                               continue;
+                       }
+                          resultCount++;
+                          if (results.isResultSet()) {
+                                TableView.getInstance().loadQuery(current.getBookmark(), results);
+                                resultsDisplayed++;
+                          } else {
+                                int count = results.getUpdateCount();
+                                if (count > 0) {
+                                       resultUpdateCount += results.getUpdateCount();
+                                }
+                          }
+                  }
+                  flag = !flag;
+                  view.setProgress(i + 1, queries.size());
+               }
+               Image statImage = QuantumPlugin.getImage((errorCount > 0) ? "stop.gif" : "success.gif"); //$NON-NLS-1$ //$NON-NLS-2$
+               view.setStatus(statImage, Messages.getString("ExecuteAction.Done") + "(" + //$NON-NLS-1$ //$NON-NLS-2$
+                                               resultCount + Messages.getString("ExecuteAction.QueriesExecuted") +  //$NON-NLS-1$
+                                               resultUpdateCount + Messages.getString("ExecuteAction.RowsUpdated") +  //$NON-NLS-1$
+                                               resultsDisplayed + Messages.getString("ExecuteAction.ResultsDisplayed") +  //$NON-NLS-1$
+                                               errorCount + Messages.getString("ExecuteAction.Errors") +  //$NON-NLS-1$
+                                               queryDuration  + Messages.getString("ExecutAction.TimeExec") + ")"); //$NON-NLS-1$ //$NON-NLS-2$
+
+               view.setProgress(0, 1);
+       }
+
+       public void selectionChanged(IAction action, ISelection selection) {
+       }
+       protected Bookmark getBookmark() {
+               BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
+               return current.getBookmark();
+       }
+
+       /* (non-Javadoc)
+        * @see com.quantum.actions.BaseSQLAction#getShell()
+        */
+       protected Shell getShell() {
+               return this.view.getViewSite().getShell();
+       }
+}