avoid OutOfBoundsException and break of format action, when marker positions are...
[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.Entity;
9 import com.quantum.model.NotConnectedException;
10 import com.quantum.sql.MultiSQLServer;
11 import com.quantum.sql.SQLResultSetResults;
12 import com.quantum.sql.SQLResults;
13 import com.quantum.ui.dialog.SQLExceptionDialog;
14 import com.quantum.util.connection.ConnectionUtil;
15
16 import org.eclipse.jface.wizard.WizardPage;
17
18 /**
19  * @author BC Holmes
20  */
21 public abstract class BaseSQLPage extends WizardPage implements SQLPage {
22
23     protected SQLResultSetResults.Row row;
24         protected SQLResultSetResults results;
25         private ConnectionUtil connectionUtil = new ConnectionUtil();
26
27     public BaseSQLPage(String pageName) {
28         super(pageName);
29     }
30     public boolean performFinish() {
31                 Bookmark bookmark = this.results.getBookmark();
32                 try {
33                         bookmark.addQuery(getQueryText());
34                         SQLResults sqlResults = MultiSQLServer.getInstance().execute(bookmark, 
35                                         this.connectionUtil.getConnection(bookmark, getShell()), getQueryText());
36                         return sqlResults == null ? false : true;
37                 } catch (SQLException e) {
38                         SQLExceptionDialog.openException(getShell(), bookmark, e);
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(SQLResultSetResults results, SQLResultSetResults.Row row) {
70         this.results = results;
71                 this.row = row;
72     }
73 }