1 package com.quantum.actions;
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.Vector;
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;
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;
31 * Executes a query from the QueryView
33 public class ExecuteAction extends BaseSQLAction implements IViewActionDelegate {
36 String execute1 = Messages.getString("ExecuteAction.Executing_Query3"); //$NON-NLS-1$
37 String execute2 = Messages.getString("ExecuteAction.Executing_Query2"); //$NON-NLS-1$
39 public ExecuteAction() {
40 setActionDefinitionId("com.quantum.actions.ExecuteAction");
43 public void init(IViewPart view) {
44 this.view = (SQLQueryView) view;
47 public void run(IAction action) {
52 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
55 MessageDialog.openInformation(
56 view.getSite().getShell(),"Sorry","Please select a bookmark to use as connection.");
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);
72 int resultUpdateCount = 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
78 for (int i = 0; i < queries.size(); i++) {
80 view.setStatus(execute1);
82 view.setStatus(execute2);
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$
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) {
97 LogProxy log = LogProxy.getInstance();
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$
105 if (results.isResultSet()) {
106 TableView.getInstance().loadQuery(current.getBookmark(), results);
109 int count = results.getUpdateCount();
111 resultUpdateCount += results.getUpdateCount();
116 view.setProgress(i + 1, queries.size());
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$
126 view.setProgress(0, 1);
129 public void selectionChanged(IAction action, ISelection selection) {
131 protected Bookmark getBookmark() {
132 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
133 return current.getBookmark();
137 * @see com.quantum.actions.BaseSQLAction#getShell()
139 protected Shell getShell() {
140 return this.view.getViewSite().getShell();