Made the help plugin 3.x compatible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExecuteAgainstAction.java
1 package com.quantum.actions;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.io.IOException;
6 import java.sql.Connection;
7 import java.sql.SQLException;
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import com.quantum.Messages;
13 import com.quantum.model.Bookmark;
14 import com.quantum.model.BookmarkCollection;
15 import com.quantum.model.ConnectionException;
16 import com.quantum.sql.MultiSQLServer;
17 import com.quantum.sql.SQLParser;
18 import com.quantum.sql.SQLResults;
19 import com.quantum.ui.dialog.BookmarkSelectionDialog;
20 import com.quantum.ui.dialog.ExceptionDisplayDialog;
21 import com.quantum.util.io.InputStreamHelper;
22 import com.quantum.view.tableview.DefaultSizes;
23 import com.quantum.view.tableview.TableView;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.dialogs.ErrorDialog;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.IObjectActionDelegate;
34 import org.eclipse.ui.IWorkbenchPart;
35
36 /**
37  * This action can be executed against any .sql file, regardless of 
38  * whether or not the Quantum perspective is open.
39  * 
40  * @author BC
41  */
42 public class ExecuteAgainstAction extends BaseSQLAction
43     implements IObjectActionDelegate, PropertyChangeListener {
44
45     private String selectedBookmark = null;
46     private IFile[] files = null;
47
48     private IWorkbenchPart workbenchPart;
49
50     /**
51      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
52      */
53     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
54         this.workbenchPart = targetPart;
55     }
56
57     protected Shell getShell() {    
58         return this.workbenchPart.getSite().getShell();
59     }
60
61     /**
62      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
63      */
64     public void run(IAction action) {
65         
66         BookmarkSelectionDialog dialog = new BookmarkSelectionDialog(getShell());
67         dialog.addPropertyChangeListener(this);
68         int result = dialog.open();
69
70         if (result == Window.OK) {
71             try {
72                 executeAgainstBookmark();
73             } catch (SQLException e) {
74                 ExceptionDisplayDialog.openError(getShell(), 
75                     null, 
76                     null, e);
77             } catch (IOException e) {
78                 ExceptionDisplayDialog.openError(getShell(), 
79                     Messages.getString("ExecuteAgainstAction.title"), 
80                     Messages.getString("ExecuteAgainstAction.IOException"), e);
81             } catch (ConnectionException e) {
82                 ExceptionDisplayDialog.openError(getShell(), 
83                     null, 
84                     null, e);
85             } catch (CoreException e) {
86                 ErrorDialog.openError(getShell(), null, null, e.getStatus());
87             }
88         }
89     }
90     
91     protected Bookmark getBookmark() {
92         return BookmarkCollection.getInstance().find(this.selectedBookmark);
93     }
94     
95     private void executeAgainstBookmark()
96         throws SQLException, IOException, CoreException, ConnectionException {
97         Bookmark bookmark = getBookmark();
98         if (bookmark != null) {
99             boolean alreadyConnected = bookmark.isConnected();
100             Connection connection = getConnection();
101             try {
102                 for (int i = 0,
103                     length = (this.files == null) ? 0 : this.files.length;
104                     connection != null && i < length;
105                     i++) {
106                     executeAgainstBookmark(bookmark, connection, this.files[i]);
107                 }
108             } finally {
109                 if (!alreadyConnected && connection != null) {
110                     bookmark.disconnect();
111                 }
112             }
113         }
114     }
115
116     private void executeAgainstBookmark(
117         Bookmark bookmark,
118         Connection connection,
119         IFile file)
120         throws SQLException, IOException, CoreException {
121         executeAgainstBookmark(
122             bookmark,
123             connection,
124             InputStreamHelper.readIntoString(file.getContents()));
125     }
126
127     private void executeAgainstBookmark(
128         Bookmark bookmark,
129         Connection connection,
130         String queries)
131         throws SQLException {
132         List queryList = SQLParser.parse(queries);
133         MultiSQLServer server = MultiSQLServer.getInstance();
134         
135         for (Iterator i = queryList.iterator(); i.hasNext();) {
136             String query = (String) i.next();
137             SQLResults results =
138                 server.execute(
139                     connection,
140                     query,
141                     1,
142                     DefaultSizes.PAGE_SIZE,
143                     DefaultSizes.MAX_COLUMN_SIZE);
144             if (results.isResultSet()) {
145                 TableView view = TableView.getInstance();
146                 if (view != null) {
147                     view.loadQuery(bookmark, results);
148                 }
149             }
150         }
151     }
152
153     /**
154      * This method is called with a new selection has been made on one 
155      * of the views.
156      * 
157      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
158      */
159     public void selectionChanged(IAction action, ISelection selection) {
160         if (selection instanceof IStructuredSelection) {
161             IStructuredSelection structuredSelection =
162                 (IStructuredSelection) selection;
163             List list = new ArrayList();
164
165             for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
166                 Object temp = i.next();
167                 if (temp != null && temp instanceof IFile) {
168                     System.out.println(((IFile) temp).getName());
169                     list.add(temp);
170                 }
171             }
172             this.files = (IFile[]) list.toArray(new IFile[list.size()]);
173         }
174     }
175
176     /**
177      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
178      */
179     public void propertyChange(PropertyChangeEvent event) {
180         if ("selection".equals(event.getPropertyName())) {
181             this.selectedBookmark = (String) event.getNewValue();
182         }
183     }
184 }