1 package com.quantum.php.wizards;
3 import java.text.MessageFormat;
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.dnd.TextTransfer;
8 import org.eclipse.swt.dnd.Transfer;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Text;
20 import com.quantum.QuantumPlugin;
21 import com.quantum.adapters.DatabaseAdapter;
22 import com.quantum.model.Bookmark;
23 import com.quantum.model.Column;
24 import com.quantum.model.Entity;
25 import com.quantum.php.PHPMessages;
26 import com.quantum.wizards.BaseSQLPage;
27 import com.quantum.wizards.SQLPage;
29 public class PHPUpdateRowPage extends BaseSQLPage implements SQLPage {
36 IPreferenceStore fStore;
37 public PHPUpdateRowPage(String pageName) {
41 public void createControl(Composite parent) {
42 System.out.println("page create control"); //$NON-NLS-1$
43 Composite container = new Composite(parent, SWT.V_SCROLL);
44 fStore = QuantumPlugin.getDefault().getPreferenceStore();
45 GridLayout layout = new GridLayout();
46 container.setLayout(layout);
47 Entity entity = this.results.getEntity();
49 int layoutColumns = 5;
50 layout.numColumns = layoutColumns;
52 columnNames = this.results.getColumnNames();
53 oldValues = new Text[columnNames.length];
54 newValues = new Text[columnNames.length];
55 primaryKeys = new Button[columnNames.length];
56 setValues = new Button[columnNames.length];
57 Label temp = new Label(container, SWT.NULL);
58 temp.setText(PHPMessages.getString("PHPUpdateRowPage.ColumnName")); //$NON-NLS-1$
59 temp = new Label(container, SWT.NULL);
60 temp.setText(PHPMessages.getString("PHPUpdateRowPage.OldValue")); //$NON-NLS-1$
61 temp = new Label(container, SWT.NULL);
62 temp.setText(""); //$NON-NLS-1$
63 temp = new Label(container, SWT.NULL);
64 temp.setText(PHPMessages.getString("PHPUpdateRowPage.NewValue")); //$NON-NLS-1$
65 temp = new Label(container, SWT.NULL);
66 temp.setText(PHPMessages.getString("PHPUpdateRowPage._13")); //$NON-NLS-1$
67 for (int i = 0; i < columnNames.length; i++) {
68 Label label = new Label(container, SWT.NULL);
69 label.setText(columnNames[i]);
70 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
71 GridData gridData = new GridData();
72 gridData.widthHint = 100;
73 oldValues[i].setLayoutData(gridData);
74 Object data = this.row == null ? null : this.row.get(i+1);
75 oldValues[i].setText(data == null ? "" : '$'+columnNames[i]);
76 oldValues[i].addModifyListener(new ModifyListener() {
77 public void modifyText(ModifyEvent e) {
81 primaryKeys[i] = new Button(container, SWT.CHECK);
82 primaryKeys[i].setText("Where"); //$NON-NLS-1$
83 Column column = (entity == null) ? null : getColumn(entity, columnNames[i]);
84 if (column != null && column.isPrimaryKey())
85 primaryKeys[i].setSelection(true);
86 primaryKeys[i].addSelectionListener(new SelectionListener() {
87 public void widgetDefaultSelected(SelectionEvent e) {
89 public void widgetSelected(SelectionEvent e) {
93 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
94 newValues[i].setText(data == null ? "" : '$'+columnNames[i]);
95 gridData = new GridData();
96 gridData.widthHint = 100;
97 newValues[i].setLayoutData(gridData);
98 newValues[i].addModifyListener(new ModifyListener() {
99 public void modifyText(ModifyEvent e) {
103 setValues[i] = new Button(container, SWT.CHECK);
104 setValues[i].setText(PHPMessages.getString("PHPUpdateRowPage.SetValue")); //$NON-NLS-1$
105 setValues[i].addSelectionListener(new SelectionListener() {
106 public void widgetDefaultSelected(SelectionEvent e) {
108 public void widgetSelected(SelectionEvent e) {
113 query = new Label(container, SWT.WRAP | SWT.V_SCROLL);
114 GridData gridData = new GridData();
115 gridData.horizontalSpan = layoutColumns;
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() {
128 System.out.println("Updating query"); //$NON-NLS-1$
129 StringBuffer setClause = new StringBuffer();
130 StringBuffer whereClause = new StringBuffer();
131 Bookmark bookmark = this.results.getBookmark();
132 Entity entity = this.results.getEntity();
133 DatabaseAdapter adapter = bookmark.getAdapter();
135 int numValuesSet = 0;
136 int numValuesWhere = 0;
137 for (int i = 0; i < columnNames.length; i++) {
138 if (primaryKeys[i].getSelection()) {
139 String value = oldValues[i].getText();
140 if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$
141 whereClause.append("("); //$NON-NLS-1$
142 whereClause.append(columnNames[i]);
143 whereClause.append(" = "); //$NON-NLS-1$
144 appendColumn(whereClause, entity, columnNames[i], adapter, value);
145 whereClause.append(")"); //$NON-NLS-1$
148 if (setValues[i].getSelection()) {
149 String value = newValues[i].getText();
150 if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$
151 setClause.append(columnNames[i]);
152 setClause.append(" = "); //$NON-NLS-1$
153 appendColumn(setClause, entity, columnNames[i], adapter, value);
157 // String query = "UPDATE " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
158 // query += " SET " + setClause.toString(); //$NON-NLS-1$
159 // query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
161 String[] arguments = { this.results.getEntity().getQuotedTableName(), setClause.toString(), whereClause.toString()};
162 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.update.template"));
164 String query = form.format(arguments);
165 this.query.setText(query);
170 * @see com.quantum.wizards.BaseSQLPage#getQueryText()
172 protected String getQueryText() {
173 return query.getText();
177 * @see com.quantum.wizards.SQLPage#performFinish()
179 public boolean performFinish() {
180 QuantumPlugin.getDefault().getSysClip().setContents(
181 new Object[] { query.getText() },
182 new Transfer[] { TextTransfer.getInstance()});