added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExecuteAction.java
1 package com.quantum.actions;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.Vector;
6
7 import com.quantum.Messages;
8 import com.quantum.QuantumPlugin;
9 import com.quantum.model.Bookmark;
10 import com.quantum.sql.MultiSQLServer;
11 import com.quantum.sql.SQLParser;
12 import com.quantum.sql.SQLResults;
13 import com.quantum.view.LogProxy;
14 import com.quantum.view.SQLQueryView;
15 import com.quantum.view.bookmark.BookmarkNode;
16 import com.quantum.view.bookmark.BookmarkView;
17 import com.quantum.view.tableview.DefaultSizes;
18 import com.quantum.view.tableview.TableView;
19
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.IViewActionDelegate;
26 import org.eclipse.ui.IViewPart;
27
28 /**
29  * @author panic
30  *
31  *      Executes a query from the QueryView
32  */
33 public class ExecuteAction extends BaseSQLAction implements IViewActionDelegate {
34         SQLQueryView view;
35         boolean flag = false;
36         String execute1 = Messages.getString("ExecuteAction.Executing_Query3"); //$NON-NLS-1$
37         String execute2 = Messages.getString("ExecuteAction.Executing_Query2"); //$NON-NLS-1$
38     
39         public ExecuteAction() {
40                 setActionDefinitionId("com.quantum.actions.ExecuteAction");
41         }
42     
43         public void init(IViewPart view) {
44                 this.view = (SQLQueryView) view;
45         }
46
47         public void run(IAction action) {
48                 run();
49         }
50     
51         public void run() {
52                 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
53                 if (current == null)
54                 {
55                          MessageDialog.openInformation(
56                            view.getSite().getShell(),"Sorry","Please select a bookmark to use as connection.");
57                          return;
58                 }                  
59                 Connection con = getConnection();
60                 view.setStatus(execute1);
61                 MultiSQLServer server = MultiSQLServer.getInstance();
62                 view.setStatus(Messages.getString("ExecuteAction.Parsing_sql_script3")); //$NON-NLS-1$
63                 Vector queries = new Vector();
64                 String viewQuery = view.getQuery();
65                 // We parse the executable units to send to the JDBC driver
66                 queries = SQLParser.parse(viewQuery);
67                 for (int i = 0; i < queries.size(); i++) {
68                         String query = (String) queries.elementAt(i);
69                         System.out.println(query);
70                 }
71                 int resultCount = 0;
72                 int resultUpdateCount = 0;
73                 int errorCount = 0;
74                 int resultsDisplayed = 0;
75                 double startTime = 0.0; // stores the time when query is started
76                 double queryDuration= 0.0; // stores query's execution time
77                 
78                 for (int i = 0; i < queries.size(); i++) {
79                         if (flag) {
80                                 view.setStatus(execute1);
81                         } else {
82                                 view.setStatus(execute2);
83                         }
84                    
85                    String query = (String) queries.elementAt(i);
86                    System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
87                    if (con != null && !query.equals("")) { //$NON-NLS-1$
88         
89                         SQLResults results;
90                         try {
91                                 startTime = System.currentTimeMillis(); // Start the measure of execution time
92                                 results = server.execute(con, query, 1, DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
93                                 queryDuration = (System.currentTimeMillis() - startTime)/1000; // calculate the execution time (in seconds)                              
94                                 current.getBookmark().addQuery(query);
95                         } catch (SQLException e) {
96                                  errorCount++;
97                                 LogProxy log = LogProxy.getInstance();
98                                 log.addText(
99                                         LogProxy.ERROR,
100                                         "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
101                                 MessageDialog.openConfirm(view.getSite().getShell(), "Database returned error", e.getLocalizedMessage());  //$NON-NLS-1$ 
102                                 continue;
103                         }
104                            resultCount++;
105                            if (results.isResultSet()) {
106                                  TableView.getInstance().loadQuery(current.getBookmark(), results);
107                                  resultsDisplayed++;
108                            } else {
109                                  int count = results.getUpdateCount();
110                                  if (count > 0) {
111                                         resultUpdateCount += results.getUpdateCount();
112                                  }
113                            }
114                    }
115                    flag = !flag;
116                    view.setProgress(i + 1, queries.size());
117                 }
118                 Image statImage = QuantumPlugin.getImage((errorCount > 0) ? "stop.gif" : "success.gif"); //$NON-NLS-1$ //$NON-NLS-2$
119                 view.setStatus(statImage, Messages.getString("ExecuteAction.Done") + "(" + //$NON-NLS-1$ //$NON-NLS-2$
120                                                 resultCount + Messages.getString("ExecuteAction.QueriesExecuted") +  //$NON-NLS-1$
121                                                 resultUpdateCount + Messages.getString("ExecuteAction.RowsUpdated") +  //$NON-NLS-1$
122                                                 resultsDisplayed + Messages.getString("ExecuteAction.ResultsDisplayed") +  //$NON-NLS-1$
123                                                 errorCount + Messages.getString("ExecuteAction.Errors") +  //$NON-NLS-1$
124                                                 queryDuration  + Messages.getString("ExecutAction.TimeExec") + ")"); //$NON-NLS-1$ //$NON-NLS-2$
125
126                 view.setProgress(0, 1);
127         }
128
129         public void selectionChanged(IAction action, ISelection selection) {
130         }
131         protected Bookmark getBookmark() {
132                 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
133                 return current.getBookmark();
134         }
135
136         /* (non-Javadoc)
137          * @see com.quantum.actions.BaseSQLAction#getShell()
138          */
139         protected Shell getShell() {
140                 return this.view.getViewSite().getShell();
141         }
142 }