implemented 1416503, automatic closing of braces
[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.SQLExceptionDialog;
19 import com.quantum.ui.dialog.SimpleSelectionDialog;
20 import com.quantum.util.connection.ConnectionUtil;
21 import com.quantum.view.LogProxy;
22 import com.quantum.view.tableview.TableView;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.jface.action.IStatusLineManager;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.actions.SelectionListenerAction;
31
32 /**
33  * @author BC
34  */
35 public abstract class BaseExecuteAction extends SelectionListenerAction {
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         protected BaseExecuteAction(String text) {
85                 super(text);
86         }
87         
88         protected BaseExecuteAction() {
89                 super(null);
90         }
91         
92         public void run() {
93                 Bookmark bookmark = getBookmark();
94                 if (bookmark != null) {
95                 execute(bookmark);
96                 }
97         }
98
99         /**
100          * @param bookmark
101          */
102         protected void execute(Bookmark bookmark) {
103                 if (bookmark != null) {
104                 try {
105                                 getStatusLineManager().setErrorMessage(null);
106                                 Connection connection = getConnection(bookmark);
107                                 if (connection != null) {
108                                         execute(bookmark, connection);
109                                 }
110                 } catch (IOException e) {
111                     ExceptionDisplayDialog.openError(getShell(), null, null, e);
112                 } catch (SQLException e) {
113                     SQLExceptionDialog.openException(getShell(), bookmark, e);
114                 } catch (CoreException e) {
115                     ErrorDialog.openError(getShell(), null, null, e.getStatus());
116                 }
117                 }
118         }
119
120         /**
121          * @param bookmark
122          * @param connection
123          * @throws IOException
124          * @throws CoreException
125          */
126         protected void execute(Bookmark bookmark, Connection connection) 
127                         throws IOException, CoreException, SQLException {
128                 getStatusLineManager().setMessage(execute1);
129                 MultiSQLServer server = MultiSQLServer.getInstance();
130
131                 Metrics metrics = new Metrics();
132                 
133                 List queries = getQueries();
134                 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
135                 progressBar.beginTask("queries", queries.size());
136                 for (int i = 0; i < queries.size(); i++) {
137                         getStatusLineManager().setMessage((i % 2 == 0) ? execute1 : execute2);
138                    
139                         String query = (String) queries.get(i);
140                         System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
141                         if (query != null && query.trim().length() > 0) {
142                                 SQLResults results = getSQLResults(bookmark, connection, server, query);
143                                 metrics.measure(results);
144                                 if (results != null) {
145                                         bookmark.addQuery(query);
146                                         if (results.isResultSet()) {
147                                                 SQLResultSetCollection.getInstance().addSQLResultSet(
148                                                                 (SQLResultSetResults) results);
149                                                 activateTableView();
150                                         }
151                                 }
152                     }
153                         progressBar.worked(i);
154                 }
155                 progressBar.done();
156                 displayFinalStatusMessage(metrics);
157         }
158
159         /**
160          * 
161          */
162         private void activateTableView() {
163                 TableView.getInstance();
164         }
165
166         /**
167          * @return
168          * @throws CoreException
169          * @throws IOException
170          */
171         protected abstract List getQueries() throws IOException, CoreException;
172
173         /**
174          * @return
175          */
176         protected abstract IStatusLineManager getStatusLineManager();
177
178         /**
179          * @param metrics
180          */
181         private void displayFinalStatusMessage(Metrics metrics) {
182                 String message = Messages.getString(
183                                 BaseExecuteAction.class, 
184                                 "done", //$NON-NLS-1$ 
185                                 metrics.getResults()); 
186                 if (metrics.hasErrors()) {
187                         getStatusLineManager().setErrorMessage(
188                                         ImageStore.getImage(ImageStore.STOP), message);
189                 } else {
190                         getStatusLineManager().setMessage(message);
191                 }
192         }
193
194         /**
195          * @param connection
196          * @param server
197          * @param query
198          * @return
199          */
200         private SQLResults getSQLResults(Bookmark bookmark, Connection connection, MultiSQLServer server, String query) {
201                 SQLResults results = null;
202                 try {
203                         results = server.execute(bookmark, connection, query);
204                 } catch (SQLException e) {
205                         LogProxy log = LogProxy.getInstance();
206                         log.addText(LogProxy.ERROR,
207                                 "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
208                         SQLExceptionDialog.openException(getShell(), bookmark, e);
209                 }
210                 return results;
211         }
212
213         protected Bookmark getBookmark() {
214                 Bookmark bookmark = null;
215                 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
216                                 getShell(), 
217                                 Messages.getString(BaseExecuteAction.class, "selectBookmark"), 
218                                 BookmarkCollection.getInstance().getBookmarks(),
219                                 ImageStore.getImage(ImageStore.BOOKMARK));
220                 if (SimpleSelectionDialog.OK == dialog.open()) {
221                         IStructuredSelection selection = dialog.getSelection();
222                         
223                         bookmark = (Bookmark) selection.getFirstElement();
224                 }
225                 return bookmark;
226         }
227 }