new Preferences tree
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / BaseSQLPage.java
1 package com.quantum.wizards;
2
3 import java.sql.SQLException;
4 import java.util.Arrays;
5
6 import com.quantum.adapters.DatabaseAdapter;
7 import com.quantum.model.Bookmark;
8 import com.quantum.model.Column;
9 import com.quantum.model.Entity;
10 import com.quantum.model.NotConnectedException;
11 import com.quantum.sql.MultiSQLServer;
12 import com.quantum.sql.SQLResultSetResults;
13 import com.quantum.sql.SQLResults;
14 import com.quantum.ui.dialog.SQLExceptionDialog;
15 import com.quantum.util.connection.ConnectionUtil;
16
17 import org.eclipse.jface.wizard.WizardPage;
18 import org.eclipse.swt.widgets.Table;
19 import org.eclipse.swt.widgets.TableColumn;
20
21 /**
22  * @author BC Holmes
23  * @author Sirkware
24  */
25 public abstract class BaseSQLPage extends WizardPage implements SQLPage {
26
27     protected SQLResultSetResults.Row row;
28         protected SQLResultSetResults results;
29         private ConnectionUtil connectionUtil = new ConnectionUtil();
30
31     public BaseSQLPage(String pageName) {
32         super(pageName);
33     }
34     public boolean performFinish() {
35                 Bookmark bookmark = this.results.getBookmark();
36                 try {
37                         bookmark.addQuery(getQueryText());
38                         SQLResults sqlResults = MultiSQLServer.getInstance().execute(bookmark, 
39                                         this.connectionUtil.getConnection(bookmark, getShell()), getQueryText());
40                         return sqlResults == null ? false : true;
41                 } catch (SQLException e) {
42                         SQLExceptionDialog.openException(getShell(), bookmark, e);
43                         return false;
44                 }
45         }
46     
47     protected abstract String getQueryText();
48     protected void appendColumn(StringBuffer whereClause, Entity entity, String columnName, DatabaseAdapter adapter, String value) {
49         
50         if (adapter != null && entity != null && getColumn(entity, columnName) != null) {
51             Column column = getColumn(entity, columnName);
52                 whereClause.append(adapter.quote(value, column.getType(), column.getTypeName()));
53         } else {
54                 whereClause.append(value);
55         }
56     }
57     /**
58          * @param entity
59          * @param columnName
60          * @return
61          * @throws NotConnectedException
62          * @throws SQLException
63          */
64         protected Column getColumn(Entity entity, String columnName)  {
65                 try {
66                         return entity == null ? null : entity.getColumn(columnName);
67                 } catch (NotConnectedException e) {
68                         return null;
69                 } catch (SQLException e) {
70                         return null;
71                 }
72         }
73         public void init(SQLResultSetResults results, SQLResultSetResults.Row row) {
74         this.results = results;
75                 this.row = row;
76     }
77         
78         /**
79          * @param table
80          * @param colName
81          * @param style
82          * @param pos
83          * @param width
84          */
85         protected void createTableColumn(Table table, String colName, int style, int pos, int width) {
86         TableColumn column = new TableColumn(table, style, pos);
87                 column.setText(colName);                
88                 column.setWidth(width);
89     }
90         
91         /**
92          * @param columnNames
93          * @return
94          */
95         protected java.util.List getColumnNamesAsList(String[] columnNames) {
96                 return Arrays.asList(columnNames);      
97         }
98 }