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