package com.quantum.php.wizards; import java.text.MessageFormat; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; 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.QuantumPlugin; import com.quantum.adapters.DatabaseAdapter; import com.quantum.model.Bookmark; import com.quantum.model.Column; import com.quantum.model.Entity; import com.quantum.php.PHPMessages; import com.quantum.wizards.BaseSQLPage; import com.quantum.wizards.SQLPage; public class PHPSelectRowPage extends BaseSQLPage implements SQLPage { String[] columnNames; Text[] variableNames; // Text[] newValues; Button[] primaryKeys; Button[] selectValues; Label query; IPreferenceStore fStore; public PHPSelectRowPage(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); fStore = QuantumPlugin.getDefault().getPreferenceStore(); GridLayout layout = new GridLayout(); container.setLayout(layout); Entity entity = this.results.getEntity(); int layoutColumns = 4; layout.numColumns = layoutColumns; columnNames = this.results.getColumnNames(); variableNames = new Text[columnNames.length]; // newValues = new Text[columnNames.length]; primaryKeys = new Button[columnNames.length]; selectValues = new Button[columnNames.length]; Label temp = new Label(container, SWT.NULL); temp.setText(PHPMessages.getString("PHPSelectRowPage.ColumnName")); //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(PHPMessages.getString("PHPSelectRowPage.VariableName")); //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(""); //$NON-NLS-1$ // temp = new Label(container, SWT.NULL); // temp.setText(PHPMessages.getString("PHPSelectRowPage.NewValue")); // //$NON-NLS-1$ temp = new Label(container, SWT.NULL); temp.setText(PHPMessages.getString("PHPSelectRowPage._13")); //$NON-NLS-1$ for (int i = 0; i < columnNames.length; i++) { Label label = new Label(container, SWT.NULL); label.setText(columnNames[i]); variableNames[i] = new Text(container, SWT.BORDER | SWT.SINGLE); GridData gridData = new GridData(); gridData.widthHint = 100; variableNames[i].setLayoutData(gridData); Object data = this.row == null ? null : this.row.get(i + 1); variableNames[i].setText(data == null ? "" : '$' + columnNames[i]);//data.toString()); variableNames[i].addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { updateQuery(); } }); primaryKeys[i] = new Button(container, SWT.CHECK); primaryKeys[i].setText("Where"); //$NON-NLS-1$ Column column = (entity == null) ? null : getColumn(entity, columnNames[i]); if (column != null && column.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 == null ? "" : '$'+data.toString()); // gridData = new GridData(); // gridData.widthHint = 100; // newValues[i].setLayoutData(gridData); // newValues[i].addModifyListener(new ModifyListener() { // public void modifyText(ModifyEvent e) { // updateQuery(); // } // }); selectValues[i] = new Button(container, SWT.CHECK); selectValues[i].setText(PHPMessages .getString("PHPSelectRowPage.SelectValue")); //$NON-NLS-1$ selectValues[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("Selecting query"); //$NON-NLS-1$ StringBuffer setClause = new StringBuffer(); StringBuffer whereClause = new StringBuffer(); Bookmark bookmark = this.results.getBookmark(); Entity entity = this.results.getEntity(); DatabaseAdapter adapter = bookmark.getAdapter(); int numValuesSet = 0; int numValuesWhere = 0; for (int i = 0; i < columnNames.length; i++) { if (primaryKeys[i].getSelection()) { String value = variableNames[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 (selectValues[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 " + this.results.getEntity().getQuotedTableName(); // //$NON-NLS-1$ // query += " SET " + setClause.toString(); //$NON-NLS-1$ // query += " WHERE " + whereClause.toString(); //$NON-NLS-1$ // String[] arguments = { setClause.toString(), this.results.getEntity().getQuotedTableName(), whereClause.toString() }; MessageFormat form = new MessageFormat(fStore .getString("phpeclipse.sql.select.template")); String query = form.format(arguments); // String query = "$results = mysql_query(\"SELECT " + setClause.toString(); // query += " FROM " + row.getTable(); // query += " WHERE " + whereClause.toString() + "\");"; this.query.setText(query); } /* * (non-Javadoc) * * @see com.quantum.wizards.BaseSQLPage#getQueryText() */ protected String getQueryText() { return query.getText(); } /* * (non-Javadoc) * * @see com.quantum.wizards.SQLPage#performFinish() */ public boolean performFinish() { QuantumPlugin.getDefault().getSysClip().setContents( new Object[] { query.getText() }, new Transfer[] { TextTransfer.getInstance() }); return true; } }