Added some examples for context menu on Outline
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / actions / ExecuteAction.java
1 package net.sourceforge.phpdt.sql.actions;
2
3 import java.sql.Connection;
4 import java.util.Vector;
5
6 import org.eclipse.jface.action.Action;
7 import org.eclipse.jface.action.IAction;
8 import org.eclipse.jface.viewers.ISelection;
9 import org.eclipse.swt.graphics.Image;
10 import org.eclipse.ui.IViewActionDelegate;
11 import org.eclipse.ui.IViewPart;
12
13 import net.sourceforge.phpdt.sql.Messages;
14 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
15 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
16 import net.sourceforge.phpdt.sql.sql.SQLParser;
17 import net.sourceforge.phpdt.sql.sql.SQLResults;
18 import net.sourceforge.phpdt.sql.view.BookmarkView;
19 import net.sourceforge.phpdt.sql.view.QueryHistoryView;
20 import net.sourceforge.phpdt.sql.view.SQLQueryView;
21 import net.sourceforge.phpdt.sql.view.TableView;
22 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
23 import net.sourceforge.phpdt.sql.view.tableview.DefaultSizes;
24
25 /**
26  * @author panic
27  *
28  *      Executes a query from the QueryView
29  */
30 public class ExecuteAction extends Action implements IViewActionDelegate {
31         SQLQueryView view;
32         boolean flag = false;
33         String execute1 = Messages.getString("ExecuteAction.Executing_Query3"); //$NON-NLS-1$
34         String execute2 = Messages.getString("ExecuteAction.Executing_Query2"); //$NON-NLS-1$
35         public void init(IViewPart view) {
36                 this.view = (SQLQueryView) view;
37         }
38
39         public void run(IAction action) {
40                 run();
41         }
42
43         public void run() {
44                 view.setStatus(execute1);
45                 MultiSQLServer server = MultiSQLServer.getInstance();
46             view.setStatus(Messages.getString("ExecuteAction.Parsing_sql_script3")); //$NON-NLS-1$
47             Vector queries = new Vector();
48             String viewQuery = view.getQuery();
49                 queries = SQLParser.parse(viewQuery);
50                 for (int i = 0; i < queries.size(); i++) {
51                         String query = (String) queries.elementAt(i);
52                         System.out.println(query);
53                 }
54                 int resultCount = 0;
55                 int resultUpdateCount = 0;
56                 int errorCount = 0;
57                 int resultsDisplayed = 0;
58                 for (int i = 0; i < queries.size(); i++) {
59                         if (flag) {
60                                 view.setStatus(execute1);
61                         } else {
62                                 view.setStatus(execute2);
63                         }
64                    BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
65                    Connection con = current.getConnection();
66                    
67                    String query = (String) queries.elementAt(i);
68                    System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
69                    if (con != null && !query.equals("")) { //$NON-NLS-1$
70                            SQLResults results = server.execute(con, query, 1, DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
71                            resultCount++;
72                            if (results.isResultSet()) {
73                                  PHPEclipseSQLPlugin phpesqlPluginInstance = PHPEclipseSQLPlugin.getDefault();
74                                  phpesqlPluginInstance.showView("net.sourceforge.phpdt.sql.view.tableview"); //$NON-NLS-1$
75                                  TableView.getInstance().loadQuery(current, results);
76                                  resultsDisplayed++;
77                            } else {
78                                  int count = results.getUpdateCount();
79                                  if (count > 0) {
80                                         resultUpdateCount += results.getUpdateCount();
81                                  }
82                            }
83                            if (results.isError()) {
84                                 errorCount++;
85                            }
86                    }
87                    flag = !flag;
88                    view.setProgress(i + 1, queries.size());
89             }
90                 Image statImage = PHPEclipseSQLPlugin.getImage((errorCount > 0) ? "stop.gif" : "success.gif"); //$NON-NLS-1$ //$NON-NLS-2$
91                 view.setStatus(statImage, Messages.getString("ExecuteAction.Done") + "(" + //$NON-NLS-1$ //$NON-NLS-2$
92                                                 resultCount + Messages.getString("ExecuteAction.QueriesExecuted") +  //$NON-NLS-1$
93                                                 resultUpdateCount + Messages.getString("ExecuteAction.RowsUpdated") +  //$NON-NLS-1$
94                                                 resultsDisplayed + Messages.getString("ExecuteAction.ResultsDisplayed") +  //$NON-NLS-1$
95                                                 errorCount + Messages.getString("ExecuteAction.Errors") + ")");  //$NON-NLS-1$ //$NON-NLS-2$
96                 view.setProgress(0, 1);
97                 QueryHistoryView history = QueryHistoryView.getInstance();
98                 if (history != null) history.addQuery(viewQuery);
99         }
100
101         public void selectionChanged(IAction action, ISelection selection) {
102         }
103 }