1 package com.quantum.wizards;
3 import java.text.MessageFormat;
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.jface.wizard.WizardPage;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
19 import com.quantum.QuantumPlugin;
20 import com.quantum.sql.TableRow;
21 import com.quantum.view.PHPSourceConsole;
22 import com.quantum.view.tableview.TableAdapter;
24 public class PHPUpdateRowPage extends WizardPage implements SQLPage {
32 IPreferenceStore fStore;
33 private final static boolean DEBUG = false;
35 public PHPUpdateRowPage(String pageName) {
39 public void init(TableRow row, TableAdapter adapter) {
43 public void createControl(Composite parent) {
45 System.out.println("page create control");
47 fStore = QuantumPlugin.getDefault().getPreferenceStore();
48 Composite container = new Composite(parent, SWT.NULL);
49 GridLayout layout = new GridLayout();
50 container.setLayout(layout);
51 int layoutColumns = 5;
52 layout.numColumns = layoutColumns;
56 System.out.println("Row is null");
58 if (row.getColumnNames() == null) {
59 System.out.println("Columns are null");
61 if (row.getTableData() == null) {
62 System.out.println("Data is null");
66 columnNames = row.getColumnNames();
67 String[] data = row.getTableData();
70 for (int i = 0; i < row.getColumnCount(); i++) {
71 System.out.println("data = " + i + "=" + data[i]);
72 System.out.println("column = " + i + "=" + columnNames[i]);
76 oldValues = new Text[row.getColumnCount()];
77 newValues = new Text[row.getColumnCount()];
78 primaryKeys = new Button[row.getColumnCount()];
79 setValues = new Button[row.getColumnCount()];
80 Label temp = new Label(container, SWT.NULL);
81 temp.setText("Column Name");
82 temp = new Label(container, SWT.NULL);
83 temp.setText("Where Value");
84 temp = new Label(container, SWT.NULL);
85 temp.setText("Where");
86 temp = new Label(container, SWT.NULL);
87 temp.setText("Set Value");
88 temp = new Label(container, SWT.NULL);
90 for (int i = 0; i < row.getColumnCount(); i++) {
91 Label label = new Label(container, SWT.NULL);
92 label.setText(columnNames[i]);
93 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
94 if (data[i] == null || data[i].equals("")) {
95 oldValues[i].setText('$' + columnNames[i]);
97 oldValues[i].setText(data[i]);
99 oldValues[i].addModifyListener(new ModifyListener() {
100 public void modifyText(ModifyEvent e) {
104 primaryKeys[i] = new Button(container, SWT.CHECK);
105 // primaryKeys[i].setText("Where");
106 primaryKeys[i].addSelectionListener(new SelectionListener() {
107 public void widgetDefaultSelected(SelectionEvent e) {
109 public void widgetSelected(SelectionEvent e) {
113 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
115 if (data[i] == null || data[i].equals("")) {
116 newValues[i].setText('$' + columnNames[i]);
118 newValues[i].setText(data[i]);
120 newValues[i].addModifyListener(new ModifyListener() {
121 public void modifyText(ModifyEvent e) {
125 setValues[i] = new Button(container, SWT.CHECK);
126 // setValues[i].setText("Set Value");
127 setValues[i].addSelectionListener(new SelectionListener() {
128 public void widgetDefaultSelected(SelectionEvent e) {
130 public void widgetSelected(SelectionEvent e) {
135 query = new Label(container, SWT.WRAP);
136 GridData gridData = new GridData();
137 gridData.horizontalSpan = layoutColumns;
138 gridData.horizontalAlignment = GridData.FILL;
139 gridData.verticalAlignment = GridData.FILL;
140 gridData.grabExcessHorizontalSpace = true;
141 gridData.grabExcessVerticalSpace = true;
142 query.setLayoutData(gridData);
144 setControl(container);
147 setPageComplete(true);
150 public void updateQuery() {
152 System.out.println("Updating update query");
155 StringBuffer setClause = new StringBuffer();
156 StringBuffer whereClause = new StringBuffer();
157 for (int i = 0; i < columnNames.length; i++) {
158 if (primaryKeys[i].getSelection()) {
159 whereClause.append(columnNames[i]);
160 whereClause.append(" = ");
161 whereClause.append("'" + oldValues[i].getText() + "'");
162 whereClause.append(", ");
164 if (setValues[i].getSelection()) {
165 setClause.append(columnNames[i]);
166 setClause.append(" = ");
167 setClause.append("'" + newValues[i].getText() + "'");
168 setClause.append(", ");
171 if (whereClause.length() > 1) {
172 whereClause.deleteCharAt(whereClause.length() - 1);
173 whereClause.deleteCharAt(whereClause.length() - 1);
175 if (setClause.length() > 1) {
176 setClause.deleteCharAt(setClause.length() - 1);
177 setClause.deleteCharAt(setClause.length() - 1);
179 String[] arguments = { row.getTable(), setClause.toString(), whereClause.toString()};
180 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.update.template"));
182 String query = form.format(arguments);
184 // String query = "$results = mysql_query(\"UPDATE " + row.getTable();
185 // query += " SET " + setClause.toString();
186 // query += " WHERE " + whereClause.toString() + "\");";
187 this.query.setText(query);
189 public boolean performFinish() {
190 PHPSourceConsole console = PHPSourceConsole.getInstance();
192 console.print(query.getText());