misc
[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.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IStatusLineManager;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.IViewActionDelegate;
27 import org.eclipse.ui.IViewPart;
28
29 /**
30  * Executes a query from the QueryView
31  *
32  * @author panic
33  */
34 public class ExecuteAction extends BaseSQLAction implements IViewActionDelegate {
35         SQLQueryView view;
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                 setImageDescriptor(QuantumPlugin.getImageDescriptor("play.gif"));
42                 setText(Messages.getString("sqlqueryview.executeQuery"));
43                 setToolTipText(Messages.getString("sqlqueryview.executeQuery"));
44         }
45     
46         public void init(IViewPart view) {
47                 this.view = (SQLQueryView) view;
48         }
49
50         public void run(IAction action) {
51                 run();
52         }
53     
54         public void run() {
55                 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
56                 if (current == null)
57                 {
58                          MessageDialog.openInformation(
59                            view.getSite().getShell(),"Sorry","Please select a bookmark to use as connection.");
60                          return;
61                 }                  
62                 getStatusLineManager().setErrorMessage(null);
63                 Connection con = getConnection();
64                 getStatusLineManager().setMessage(execute1);
65                 MultiSQLServer server = MultiSQLServer.getInstance();
66                 getStatusLineManager().setMessage(Messages.getString("ExecuteAction.Parsing_sql_script3")); //$NON-NLS-1$
67                 Vector queries = new Vector();
68                 String viewQuery = view.getQuery();
69                 // We parse the executable units to send to the JDBC driver
70                 queries = SQLParser.parse(viewQuery);
71                 for (int i = 0; i < queries.size(); i++) {
72                         String query = (String) queries.elementAt(i);
73                         System.out.println(query);
74                 }
75                 int resultCount = 0;
76                 int resultUpdateCount = 0;
77                 int errorCount = 0;
78                 int resultsDisplayed = 0;
79                 double startTime = 0.0; // stores the time when query is started
80                 double queryDuration= 0.0; // stores query's execution time
81                 
82                 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
83                 progressBar.beginTask("queries", queries.size());
84                 for (int i = 0; i < queries.size(); i++) {
85                         if (i % 2 == 0) {
86                                 getStatusLineManager().setMessage(execute1);
87                         } else {
88                                 getStatusLineManager().setMessage(execute2);
89                         }
90                    
91                    String query = (String) queries.elementAt(i);
92                    System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
93                    if (con != null && !query.equals("")) { //$NON-NLS-1$
94         
95                         SQLResults results;
96                         try {
97                                 startTime = System.currentTimeMillis(); // Start the measure of execution time
98                                 results = server.execute(con, query, 1, DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
99                                 queryDuration = (System.currentTimeMillis() - startTime)/1000; // calculate the execution time (in seconds)                              
100                                 current.getBookmark().addQuery(query);
101                         } catch (SQLException e) {
102                                  errorCount++;
103                                 LogProxy log = LogProxy.getInstance();
104                                 log.addText(
105                                         LogProxy.ERROR,
106                                         "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
107                                 MessageDialog.openConfirm(view.getSite().getShell(), "Database returned error", e.getLocalizedMessage());  //$NON-NLS-1$ 
108                                 continue;
109                         }
110                            resultCount++;
111                            if (results.isResultSet()) {
112                                  TableView.getInstance().loadQuery(current.getBookmark(), results);
113                                  resultsDisplayed++;
114                            } else {
115                                  int count = results.getUpdateCount();
116                                  if (count > 0) {
117                                         resultUpdateCount += results.getUpdateCount();
118                                  }
119                            }
120                     }
121 System.out.println(getStatusLineManager().getProgressMonitor() == null ? "not found" : "found!");
122                         progressBar.worked(i);
123 //                  view.setProgress(i + 1, queries.size());
124                 }
125                 progressBar.done();
126                 String message = Messages.getString("ExecuteAction.Done") + "(" + //$NON-NLS-1$ //$NON-NLS-2$
127                                                 resultCount + Messages.getString("ExecuteAction.QueriesExecuted") +  //$NON-NLS-1$
128                                                 resultUpdateCount + Messages.getString("ExecuteAction.RowsUpdated") +  //$NON-NLS-1$
129                                                 resultsDisplayed + Messages.getString("ExecuteAction.ResultsDisplayed") +  //$NON-NLS-1$
130                                                 errorCount + Messages.getString("ExecuteAction.Errors") +  //$NON-NLS-1$
131                                                 queryDuration  + Messages.getString("ExecutAction.TimeExec") + ")"; //$NON-NLS-1$ //$NON-NLS-2$
132                 if (errorCount > 0) {
133                         getStatusLineManager().setErrorMessage(
134                                         QuantumPlugin.getImage("stop.gif"), message);
135                 } else {
136                         getStatusLineManager().setMessage(
137                                         QuantumPlugin.getImage("success.gif"), message);
138                 }
139         }
140
141         /**
142          * @return
143          */
144         private IStatusLineManager getStatusLineManager() {
145                 return this.view.getViewSite().getActionBars().getStatusLineManager();
146         }
147
148         public void selectionChanged(IAction action, ISelection selection) {
149         }
150         protected Bookmark getBookmark() {
151                 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
152                 return current.getBookmark();
153         }
154
155         /* (non-Javadoc)
156          * @see com.quantum.actions.BaseSQLAction#getShell()
157          */
158         protected Shell getShell() {
159                 return this.view.getViewSite().getShell();
160         }
161 }