c5ab79e78c06a47aff4aaac5fdd5ecc87cf603ef
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / BaseExecuteAction.java
1 package com.quantum.actions;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6 import java.util.List;
7
8 import com.quantum.ImageStore;
9 import com.quantum.Messages;
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.BookmarkCollection;
12 import com.quantum.sql.MultiSQLServer;
13 import com.quantum.sql.SQLResultSetCollection;
14 import com.quantum.sql.SQLResultSetResults;
15 import com.quantum.sql.SQLResults;
16 import com.quantum.sql.SQLUpdateResults;
17 import com.quantum.ui.dialog.ExceptionDisplayDialog;
18 import com.quantum.ui.dialog.SimpleSelectionDialog;
19 import com.quantum.util.connection.ConnectionUtil;
20 import com.quantum.view.LogProxy;
21 import com.quantum.view.tableview.TableView;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IStatusLineManager;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
31
32 /**
33  * @author BC
34  */
35 public abstract class BaseExecuteAction extends Action {
36         
37         class Metrics {
38                 int resultCount = 0;
39                 int resultUpdateCount = 0;
40                 int errorCount = 0;
41                 int resultsDisplayed = 0;
42                 double queryDuration= 0.0;
43                 
44                 public void measure(SQLResults results) {
45                         if (results == null) {
46                                 errorCount++;
47                         } else {
48                                 queryDuration += results.getTime()/1000.0; 
49                                 resultCount++;
50                                 if (results.isResultSet()) {
51                                         resultsDisplayed++;
52                                 } else {
53                                         resultUpdateCount += ((SQLUpdateResults) results).getUpdateCount();
54                                 }
55                         }
56                 }
57                 
58                 public boolean hasErrors() {
59                         return this.errorCount > 0;
60                 }
61                 
62                 Object[] getResults() {
63                         return new Object[] {
64                                 new Integer(this.resultCount),
65                                 new Integer(this.resultUpdateCount),
66                                 new Integer(this.resultsDisplayed),
67                                 new Integer(this.errorCount),
68                                 new Double(this.queryDuration),
69                         };
70                 }
71         }
72     
73     private ConnectionUtil connectionUtil = new ConnectionUtil();
74     
75     protected abstract Shell getShell();
76     
77     protected Connection getConnection(Bookmark bookmark) {
78         return this.connectionUtil.getConnection(bookmark, getShell());
79     }
80
81         String execute1 = Messages.getString(BaseExecuteAction.class, "execute1");
82         String execute2 = Messages.getString(BaseExecuteAction.class, "execute2");
83
84         public void run() {
85                 Bookmark bookmark = getBookmark();
86                 if (bookmark != null) {
87                 execute(bookmark);
88                 }
89         }
90
91         /**
92          * @param bookmark
93          */
94         protected void execute(Bookmark bookmark) {
95                 if (bookmark != null) {
96                 try {
97                                 getStatusLineManager().setErrorMessage(null);
98                                 Connection connection = getConnection(bookmark);
99                                 if (connection != null) {
100                                         execute(bookmark, connection);
101                                 }
102                 } catch (IOException e) {
103                     ExceptionDisplayDialog.openError(getShell(), null, null, e);
104                 } catch (SQLException e) {
105                     ExceptionDisplayDialog.openError(getShell(), null, null, e);
106                 } catch (CoreException e) {
107                     ErrorDialog.openError(getShell(), null, null, e.getStatus());
108                 }
109                 }
110         }
111
112         /**
113          * @param bookmark
114          * @param connection
115          * @throws IOException
116          * @throws CoreException
117          */
118         protected void execute(Bookmark bookmark, Connection connection) 
119                         throws IOException, CoreException, SQLException {
120                 getStatusLineManager().setMessage(execute1);
121                 MultiSQLServer server = MultiSQLServer.getInstance();
122
123                 Metrics metrics = new Metrics();
124                 
125                 List queries = getQueries();
126                 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
127                 progressBar.beginTask("queries", queries.size());
128                 for (int i = 0; i < queries.size(); i++) {
129                         getStatusLineManager().setMessage((i % 2 == 0) ? execute1 : execute2);
130                    
131                         String query = (String) queries.get(i);
132                         System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
133                         if (query != null && query.trim().length() > 0) {
134                                 SQLResults results = getSQLResults(bookmark, connection, server, query);
135                                 metrics.measure(results);
136                                 if (results != null) {
137                                         bookmark.addQuery(query);
138                                         if (results.isResultSet()) {
139                                                 SQLResultSetCollection.getInstance().addSQLResultSet(
140                                                                 (SQLResultSetResults) results);
141                                                 activateTableView();
142                                         }
143                                 }
144                     }
145                         progressBar.worked(i);
146                 }
147                 progressBar.done();
148                 displayFinalStatusMessage(metrics);
149         }
150
151         /**
152          * 
153          */
154         private void activateTableView() {
155                 TableView.getInstance();
156         }
157
158         /**
159          * @return
160          * @throws CoreException
161          * @throws IOException
162          */
163         protected abstract List getQueries() throws IOException, CoreException;
164
165         /**
166          * @return
167          */
168         protected abstract IStatusLineManager getStatusLineManager();
169
170         /**
171          * @param metrics
172          */
173         private void displayFinalStatusMessage(Metrics metrics) {
174                 String message = Messages.getString(
175                                 BaseExecuteAction.class, 
176                                 "done", //$NON-NLS-1$ 
177                                 metrics.getResults()); 
178                 if (metrics.hasErrors()) {
179                         getStatusLineManager().setErrorMessage(
180                                         ImageStore.getImage(ImageStore.STOP), message);
181                 } else {
182                         getStatusLineManager().setMessage(message);
183                 }
184         }
185
186         /**
187          * @param connection
188          * @param server
189          * @param query
190          * @return
191          */
192         private SQLResults getSQLResults(Bookmark bookmark, Connection connection, MultiSQLServer server, String query) {
193                 SQLResults results = null;
194                 try {
195                         results = server.execute(bookmark, connection, query);
196                 } catch (SQLException e) {
197                         LogProxy log = LogProxy.getInstance();
198                         log.addText(LogProxy.ERROR,
199                                 "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
200                         MessageDialog.openConfirm(getShell(), 
201                                         "Database returned error", 
202                                         e.getLocalizedMessage());  //$NON-NLS-1$ 
203                 }
204                 return results;
205         }
206
207         protected Bookmark getBookmark() {
208                 Bookmark bookmark = null;
209                 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
210                                 getShell(), 
211                                 Messages.getString(BaseExecuteAction.class, "selectBookmark"), 
212                                 BookmarkCollection.getInstance().getBookmarks(),
213                                 ImageStore.getImage(ImageStore.BOOKMARK));
214                 if (SimpleSelectionDialog.OK == dialog.open()) {
215                         IStructuredSelection selection = dialog.getSelection();
216                         
217                         bookmark = (Bookmark) selection.getFirstElement();
218                 }
219                 return bookmark;
220         }
221 }