package com.quantum.wizards; import com.quantum.Messages; import com.quantum.adapters.AdapterFactory; import com.quantum.adapters.DatabaseAdapter; import com.quantum.model.Bookmark; import com.quantum.model.Entity; 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; public class UpdateRowPage extends BaseSQLPage implements SQLPage { String[] columnNames; Text[] oldValues; Text[] newValues; Button[] primaryKeys; Button[] setValues; Label query; public UpdateRowPage(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); Entity entity = row.getEntity(); int layoutColumns = 5; layout.numColumns = layoutColumns; if (row == null) { System.out.println("Row is null"); //$NON-NLS-1$ } if (row.getColumnNames() == null) { System.out.println("Columns are null"); //$NON-NLS-1$ } if (row.getTableData() == null) { System.out.println("Data is null"); //$NON-NLS-1$ } columnNames = row.getColumnNames(); String[] data = row.getTableData(); for (int i = 0; i < row.getColumnCount(); i++) { System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$ System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$ } oldValues = new Text[row.getColumnCount()]; newValues = new Text[row.getColumnCount()]; primaryKeys = new Button[row.getColumnCount()]; setValues = new Button[row.getColumnCount()]; Label temp = new Label(container, SWT.NULL); temp.setText(Messages.getString("UpdateRowPage.ColumnName")); //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(Messages.getString("UpdateRowPage.OldValue")); //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(""); //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(Messages.getString("UpdateRowPage.NewValue")); //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(Messages.getString("UpdateRowPage._13")); //$NON-NLS-1$ for (int i = 0; i < row.getColumnCount(); i++) { Label label = new Label(container, SWT.NULL); label.setText(columnNames[i]); oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE); oldValues[i].setText(data[i]); oldValues[i].addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { updateQuery(); } }); primaryKeys[i] = new Button(container, SWT.CHECK); primaryKeys[i].setText("Where"); //$NON-NLS-1$ if (entity != null && entity.getColumn(columnNames[i]).isPrimaryKey()) primaryKeys[i].setSelection(true); primaryKeys[i].addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { updateQuery(); } }); newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE); newValues[i].setText(data[i]); newValues[i].addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { updateQuery(); } }); setValues[i] = new Button(container, SWT.CHECK); setValues[i].setText(Messages.getString("UpdateRowPage.SetValue")); //$NON-NLS-1$ setValues[i].addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { updateQuery(); } }); } query = new Label(container, SWT.WRAP | SWT.V_SCROLL); GridData gridData = new GridData(); gridData.horizontalSpan = layoutColumns; 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("Updating query"); //$NON-NLS-1$ StringBuffer setClause = new StringBuffer(); StringBuffer whereClause = new StringBuffer(); Bookmark bookmark = row.getBookmark(); Entity entity = row.getEntity(); DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType()); int numValuesSet = 0; int numValuesWhere = 0; for (int i = 0; i < columnNames.length; i++) { if (primaryKeys[i].getSelection()) { String value = oldValues[i].getText(); if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$ whereClause.append("("); //$NON-NLS-1$ whereClause.append(columnNames[i]); whereClause.append(" = "); //$NON-NLS-1$ appendColumn(whereClause, entity, columnNames[i], adapter, value); whereClause.append(")"); //$NON-NLS-1$ numValuesWhere++; } if (setValues[i].getSelection()) { String value = newValues[i].getText(); if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$ setClause.append(columnNames[i]); setClause.append(" = "); //$NON-NLS-1$ appendColumn(setClause, entity, columnNames[i], adapter, value); numValuesSet++; } } String query = "UPDATE " + row.getTable(); //$NON-NLS-1$ query += " SET " + setClause.toString(); //$NON-NLS-1$ query += " WHERE " + whereClause.toString(); //$NON-NLS-1$ this.query.setText(query); } /* (non-Javadoc) * @see com.quantum.wizards.BaseSQLPage#getQueryText() */ protected String getQueryText() { return query.getText(); } }