1 package net.sourceforge.phpdt.sql.wizards;
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;
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;
27 public class DeleteRowPage extends WizardPage implements SQLPage {
34 public DeleteRowPage(String pageName) {
38 public void init(TableRow row, TableAdapter adapter) {
42 public void createControl(Composite parent) {
44 System.out.println("page create control"); //$NON-NLS-1$
46 Composite container = new Composite(parent, SWT.NULL);
47 GridLayout layout = new GridLayout();
48 container.setLayout(layout);
49 layout.numColumns = 3;
53 System.out.println("Row is null"); //$NON-NLS-1$
55 if (row.getColumnNames() == null) {
56 System.out.println("Columns are null"); //$NON-NLS-1$
58 if (row.getTableData() == null) {
59 System.out.println("Data is null"); //$NON-NLS-1$
62 BookmarkNode bookmark = row.getBookmarkNode();
63 TreeNode node = bookmark.find(row.getTable());
64 ObjectMetaData metadata = null;
66 metadata = node.getMetaData();
67 columnNames = row.getColumnNames();
68 String[] data = row.getTableData();
71 for (int i = 0; i < row.getColumnCount(); i++) {
72 System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
73 System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
77 values = new Text[row.getColumnCount()];
78 whereValues = new Button[row.getColumnCount()];
79 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$
80 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$
81 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$
82 for (int i = 0; i < row.getColumnCount(); i++) {
83 Label label = new Label(container, SWT.NULL);
84 label.setText(columnNames[i]);
85 values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
86 GridData fullHorizontal = new GridData();
87 fullHorizontal.horizontalAlignment = GridData.FILL;
88 values[i].setLayoutData(fullHorizontal);
89 values[i].setText(data[i]);
91 values[i].addModifyListener(new ModifyListener() {
92 public void modifyText(ModifyEvent e) {
97 whereValues[i] = new Button(container, SWT.CHECK);
98 whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$
99 // we check if it's a primary key to select it in the WHERE clause
100 if (metadata != null && metadata.getPrimaryKeyOrder(columnNames[i]) > 0)
101 whereValues[i].setSelection(true);
103 whereValues[i].setSelection(false);
104 whereValues[i].addSelectionListener(new SelectionListener() {
105 public void widgetDefaultSelected(SelectionEvent e) {
107 public void widgetSelected(SelectionEvent e) {
112 query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
113 GridData gridData = new GridData();
114 gridData.horizontalSpan = layout.numColumns;
115 gridData.verticalSpan = 3;
116 gridData.horizontalAlignment = GridData.FILL;
117 gridData.verticalAlignment = GridData.FILL;
118 gridData.grabExcessHorizontalSpace = true;
119 gridData.grabExcessVerticalSpace = true;
120 query.setLayoutData(gridData);
122 setControl(container);
125 setPageComplete(true);
127 public void updateQuery() {
129 System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
132 StringBuffer whereClause = new StringBuffer();
133 BookmarkNode bookmark = row.getBookmarkNode();
134 TreeNode node = bookmark.find(row.getTable());
135 ObjectMetaData metadata = null;
137 metadata = node.getMetaData();
138 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
141 for (int i = 0; i < columnNames.length; i++) {
142 if (whereValues[i].getSelection()) {
144 whereClause.append(" AND "); //$NON-NLS-1$
146 whereClause.append("("); //$NON-NLS-1$
147 whereClause.append(columnNames[i]);
148 whereClause.append(" = "); //$NON-NLS-1$
149 if (adapter != null && metadata != null)
150 whereClause.append(adapter.quote(values[i].getText(), metadata.getColumnType(columnNames[i])));
152 whereClause.append(values[i].getText());
154 whereClause.append(")"); //$NON-NLS-1$
157 String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
158 if (numSelected > 0) {
159 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
161 if (numSelected > 0) {
162 setMessage(""); //$NON-NLS-1$
164 setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
166 this.query.setText(query);
169 public boolean performFinish() {
170 MultiSQLServer server = MultiSQLServer.getInstance();
171 BookmarkView bookmarkView = BookmarkView.getInstance();
172 BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
173 server.execute(bookmark.getConnection(), query.getText());