a7a84fe9d3d3ace93b9a540494075f6ff23922d9
[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
5 import com.quantum.adapters.DatabaseAdapter;
6 import com.quantum.model.Bookmark;
7 import com.quantum.model.Column;
8 import com.quantum.model.ConnectionException;
9 import com.quantum.model.Entity;
10 import com.quantum.model.NotConnectedException;
11 import com.quantum.sql.SQLResults;
12 import com.quantum.sql.TableRow;
13 import com.quantum.view.ViewHelper;
14 import com.quantum.view.tableview.TableAdapter;
15
16 import org.eclipse.jface.wizard.WizardPage;
17
18 /**
19  * @author BC Holmes
20  * @author Sirkware
21  */
22 public abstract class BaseSQLPage extends WizardPage implements SQLPage {
23
24     protected TableRow row;
25     protected TableAdapter adapter;
26
27     public BaseSQLPage(String pageName) {
28         super(pageName);
29     }
30     public boolean performFinish() {
31                 try {
32                         Bookmark bookmark = this.adapter.getBookmark();
33                         bookmark.addQuery(getQueryText());
34                         SQLResults sqlResults = ViewHelper.tryGetResults(getShell(),
35                                         bookmark.getConnection(), getQueryText());
36                         return sqlResults.isError() ? false : true;
37                 } catch (ConnectionException e) {
38                         e.printStackTrace();
39                         return false;
40                 }
41         }
42     
43     protected abstract String getQueryText();
44     protected void appendColumn(StringBuffer whereClause, Entity entity, String columnName, DatabaseAdapter adapter, String value) {
45         
46         if (adapter != null && entity != null && getColumn(entity, columnName) != null) {
47             Column column = getColumn(entity, columnName);
48                 whereClause.append(adapter.quote(value, column.getType(), column.getTypeName()));
49         } else {
50                 whereClause.append(value);
51         }
52     }
53     /**
54          * @param entity
55          * @param columnName
56          * @return
57          * @throws NotConnectedException
58          * @throws SQLException
59          */
60         protected Column getColumn(Entity entity, String columnName)  {
61                 try {
62                         return entity == null ? null : entity.getColumn(columnName);
63                 } catch (NotConnectedException e) {
64                         return null;
65                 } catch (SQLException e) {
66                         return null;
67                 }
68         }
69         public void init(TableRow row, TableAdapter adapter) {
70         this.row = row;
71         this.adapter = adapter;
72     }
73 }