released 1.0.9a version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / DeleteRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import org.eclipse.jface.wizard.WizardPage;
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.ModifyEvent;
6 import org.eclipse.swt.events.ModifyListener;
7 import org.eclipse.swt.events.SelectionEvent;
8 import org.eclipse.swt.events.SelectionListener;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
15
16 import net.sourceforge.phpdt.sql.Messages;
17 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
18 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
19 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
20 import net.sourceforge.phpdt.sql.sql.TableRow;
21 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
22 import net.sourceforge.phpdt.sql.view.BookmarkView;
23 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
24 import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
25 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
26
27 public class DeleteRowPage extends WizardPage implements SQLPage {
28         TableRow row;
29         String[] columnNames;
30         Text[] values;
31         Button[] whereValues;
32         Text query;
33
34         public DeleteRowPage(String pageName) {
35                 super(pageName);
36         }
37         
38         public void init(TableRow row, TableAdapter adapter) {
39                 this.row = row;
40         }
41
42         public void createControl(Composite parent) {
43                 System.out.println("page create control"); //$NON-NLS-1$
44                 Composite container = new Composite(parent, SWT.NULL);
45                 GridLayout layout = new GridLayout();
46                 container.setLayout(layout);
47                 layout.numColumns = 3;
48
49                 if (row == null) {
50                         System.out.println("Row is null"); //$NON-NLS-1$
51                 }
52                 if (row.getColumnNames() == null) {
53                         System.out.println("Columns are null"); //$NON-NLS-1$
54                 }
55                 if (row.getTableData() == null) {
56                         System.out.println("Data is null"); //$NON-NLS-1$
57                 }
58                 BookmarkNode bookmark = row.getBookmarkNode();
59                 TreeNode node = bookmark.find(row.getTable());
60                 ObjectMetaData metadata = null;
61                 if (node != null) metadata = node.getMetaData(); 
62                 columnNames = row.getColumnNames();
63                 String[] data = row.getTableData();
64                 for (int i = 0; i < row.getColumnCount(); i++) {
65                         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
66                         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
67                 }
68                 values = new Text[row.getColumnCount()];
69                 whereValues = new Button[row.getColumnCount()];
70                 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$
71                 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$
72                 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$
73                 for (int i = 0; i < row.getColumnCount(); i++) {
74                         Label label = new Label(container, SWT.NULL);
75                         label.setText(columnNames[i]);
76                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
77                         GridData fullHorizontal = new GridData();
78                         fullHorizontal.horizontalAlignment = GridData.FILL;
79                         values[i].setLayoutData(fullHorizontal);
80                         values[i].setText(data[i]);
81
82                         values[i].addModifyListener(new ModifyListener() {
83                                 public void modifyText(ModifyEvent e) {
84                                         updateQuery();
85                                 }                               
86                         });
87                         
88                         whereValues[i] = new Button(container, SWT.CHECK);
89                         whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$
90                         // we check if it's a primary key to select it in the WHERE clause
91                         if (metadata != null && metadata.getPrimaryKeyOrder(columnNames[i]) > 0)
92                                 whereValues[i].setSelection(true);
93                         else
94                                 whereValues[i].setSelection(false);
95                         whereValues[i].addSelectionListener(new SelectionListener() {
96                                 public void widgetDefaultSelected(SelectionEvent e) {
97                                 }
98                                 public void widgetSelected(SelectionEvent e) {
99                                         updateQuery();
100                                 }
101                         });
102                 }
103                 query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
104                 GridData gridData = new GridData();
105                 gridData.horizontalSpan = layout.numColumns;
106                 gridData.verticalSpan = 3;
107                 gridData.horizontalAlignment = GridData.FILL;
108                 gridData.verticalAlignment = GridData.FILL;
109                 gridData.grabExcessHorizontalSpace = true;
110                 gridData.grabExcessVerticalSpace = true;
111                 query.setLayoutData(gridData);
112
113                 setControl(container);
114         updateQuery();
115        
116                 setPageComplete(true);
117         }
118         public void updateQuery() {
119                 System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
120                 StringBuffer whereClause = new StringBuffer();
121                 BookmarkNode bookmark = row.getBookmarkNode();
122                 TreeNode node = bookmark.find(row.getTable());
123                 ObjectMetaData metadata = null;
124                 if (node != null) metadata = node.getMetaData(); 
125                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
126                 
127                 int numSelected = 0;
128                 for (int i = 0; i < columnNames.length; i++) {
129                         if (whereValues[i].getSelection()) {
130                                 if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
131                                 numSelected++;
132                                 whereClause.append("("); //$NON-NLS-1$
133                                 whereClause.append(columnNames[i]);
134                                 whereClause.append(" = "); //$NON-NLS-1$
135                                 if (adapter != null && metadata != null)
136                                         whereClause.append(adapter.quote(values[i].getText(), metadata.getColumnType(columnNames[i])));
137                                 else
138                                         whereClause.append(values[i].getText());
139                                                                 
140                                 whereClause.append(")"); //$NON-NLS-1$
141                         }
142                 }
143                 String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
144                 if (numSelected > 0) {
145                         query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
146                 }
147                 if (numSelected > 0) {
148                         setMessage(""); //$NON-NLS-1$
149                 } else {
150                         setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
151                 }
152                 this.query.setText(query);
153         }
154         
155         
156         public boolean performFinish() {
157            MultiSQLServer server = MultiSQLServer.getInstance();
158            BookmarkView bookmarkView = BookmarkView.getInstance(); 
159            BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
160            server.execute(bookmark.getConnection(), query.getText());
161            return true;
162         }
163 }