package com.quantum.wizards; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import com.quantum.Messages; import com.quantum.adapters.DatabaseAdapter; import com.quantum.model.Bookmark; import com.quantum.model.Column; import com.quantum.model.Entity; public class DeleteRowPage extends BaseSQLPage implements SQLPage { String[] columnNames; Text[] values; Button[] whereValues; Text query; public DeleteRowPage(String pageName) { super(pageName); } public void createControl(Composite parent) { System.out.println("page create control"); //$NON-NLS-1$ Composite container = new Composite(parent, SWT.V_SCROLL); GridLayout layout = new GridLayout(); container.setLayout(layout); layout.numColumns = 3; Entity entity = this.results.getEntity(); columnNames = this.results.getColumnNames(); values = new Text[columnNames.length]; whereValues = new Button[columnNames.length]; new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$ new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$ new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$ for (int i = 0; i < columnNames.length; i++) { Label label = new Label(container, SWT.NULL); label.setText(columnNames[i]); values[i] = new Text(container, SWT.BORDER | SWT.SINGLE); Object object = this.row == null ? null : this.row.get(i+1); values[i].setText(object == null ? "" : object.toString()); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 150; values[i].setLayoutData(gridData); values[i].addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent event) { updateQuery(); } }); whereValues[i] = new Button(container, SWT.CHECK); whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$ // we check if it's a primary key to select it in the WHERE clause Column column = getColumn(entity, columnNames[i]); whereValues[i].setSelection(column == null ? false : column.isPrimaryKey()); whereValues[i].addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { updateQuery(); } }); } query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP); GridData gridData = new GridData(); gridData.horizontalSpan = layout.numColumns; gridData.verticalSpan = 3; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; query.setLayoutData(gridData); setControl(container); updateQuery(); setPageComplete(true); } public void updateQuery() { System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$ StringBuffer whereClause = new StringBuffer(); Bookmark bookmark = this.results.getBookmark(); Entity entity = this.results.getEntity(); DatabaseAdapter adapter = bookmark.getAdapter(); int numSelected = 0; for (int i = 0; i < columnNames.length; i++) { if (whereValues[i].getSelection()) { if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$ numSelected++; whereClause.append("("); //$NON-NLS-1$ whereClause.append(columnNames[i]); whereClause.append(" = "); //$NON-NLS-1$ appendColumn(whereClause, entity, columnNames[i], adapter, values[i].getText()); whereClause.append(")"); //$NON-NLS-1$ } } String query = "DELETE FROM " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$ if (numSelected > 0) { query += " WHERE " + whereClause.toString(); //$NON-NLS-1$ } if (numSelected > 0) { setMessage(""); //$NON-NLS-1$ } else { setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$ } this.query.setText(query); } protected String getQueryText() { return query.getText(); } }