*** empty log message ***
[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.util.Vector;
4
5 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
6
7 import org.eclipse.jface.action.Action;
8 import org.eclipse.jface.action.IAction;
9 import org.eclipse.jface.viewers.ISelection;
10 import org.eclipse.swt.graphics.Image;
11 import org.eclipse.ui.IViewActionDelegate;
12 import org.eclipse.ui.IViewPart;
13
14 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
15 import net.sourceforge.phpdt.sql.sql.SQLParser;
16 import net.sourceforge.phpdt.sql.sql.SQLResults;
17 import net.sourceforge.phpdt.sql.view.QueryHistoryView;
18 import net.sourceforge.phpdt.sql.view.SQLQueryView;
19 import net.sourceforge.phpdt.sql.view.TableView;
20 import net.sourceforge.phpdt.sql.view.tableview.DefaultSizes;
21
22 public class ExecuteAction extends Action implements IViewActionDelegate {
23         SQLQueryView view;
24         boolean flag = false;
25         String execute1 = "Executing Query...";
26         String execute2 = "Executing Query..";
27         public void init(IViewPart view) {
28                 this.view = (SQLQueryView) view;
29         }
30
31         public void run(IAction action) {
32                 run();
33         }
34
35         public void run() {
36                 view.setStatus(execute1);
37                 MultiSQLServer server = MultiSQLServer.getInstance();
38             view.setStatus("Parsing sql script...");
39             Vector queries = new Vector();
40             String viewQuery = view.getQuery();
41                 queries = SQLParser.parse(viewQuery);
42                 for (int i = 0; i < queries.size(); i++) {
43                         String query = (String) queries.elementAt(i);
44                         System.out.println(query);
45                 }
46                 int resultCount = 0;
47                 int resultUpdateCount = 0;
48                 int errorCount = 0;
49                 int resultsDisplayed = 0;
50                 for (int i = 0; i < queries.size(); i++) {
51                         if (flag) {
52                                 view.setStatus(execute1);
53                         } else {
54                                 view.setStatus(execute2);
55                         }
56                    String query = (String) queries.elementAt(i);
57                    System.out.println(">" + query + "<");
58                    if (!query.equals("")) {
59                            SQLResults results = server.execute(query, 1, DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
60                            resultCount++;
61                            if (results.isResultSet()) {
62                                  TableView.getInstance().loadQuery(results);
63                                  resultsDisplayed++;
64                            } else {
65                                  int count = results.getUpdateCount();
66                                  if (count > 0) {
67                                         resultUpdateCount += results.getUpdateCount();
68                                  }
69                            }
70                            if (results.isError()) {
71                                 errorCount++;
72                            }
73                    }
74                    flag = !flag;
75                    view.setProgress(i + 1, queries.size());
76             }
77             if (errorCount > 0) {
78                 Image error = PHPEclipseSQLPlugin.getImage("stop.gif");
79                     view.setStatus(error, "Done (" + resultCount + " queries executed, " + resultUpdateCount + " rows updated, " + resultsDisplayed + " results displayed, " + errorCount + " errors)");
80             } else {
81                 Image success = PHPEclipseSQLPlugin.getImage("success.gif");
82                     view.setStatus(success, "Done (" + resultCount + " queries executed, " + resultUpdateCount + " rows updated, " + resultsDisplayed + " results displayed, " + errorCount + " errors)");
83             }
84                 view.setProgress(0, 1);
85                 QueryHistoryView history = QueryHistoryView.getInstance();
86                 history.addQuery(viewQuery);
87         }
88
89         public void selectionChanged(IAction action, ISelection selection) {
90         }
91 }