1 package net.sourceforge.phpdt.sql.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 net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
20 import net.sourceforge.phpdt.sql.sql.TableRow;
21 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
22 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
24 public class PHPUpdateRowPage extends WizardPage implements SQLPage {
32 IPreferenceStore fStore;
34 public PHPUpdateRowPage(String pageName) {
38 public void init(TableRow row, TableAdapter adapter) {
42 public void createControl(Composite parent) {
43 System.out.println("page create control");
44 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
45 Composite container = new Composite(parent, SWT.NULL);
46 GridLayout layout = new GridLayout();
47 container.setLayout(layout);
48 int layoutColumns = 5;
49 layout.numColumns = layoutColumns;
52 System.out.println("Row is null");
54 if (row.getColumnNames() == null) {
55 System.out.println("Columns are null");
57 if (row.getTableData() == null) {
58 System.out.println("Data is null");
61 columnNames = row.getColumnNames();
62 String[] data = row.getTableData();
63 for (int i = 0; i < row.getColumnCount(); i++) {
64 System.out.println("data = " + i + "=" + data[i]);
65 System.out.println("column = " + i + "=" + columnNames[i]);
67 oldValues = new Text[row.getColumnCount()];
68 newValues = new Text[row.getColumnCount()];
69 primaryKeys = new Button[row.getColumnCount()];
70 setValues = new Button[row.getColumnCount()];
71 Label temp = new Label(container, SWT.NULL);
72 temp.setText("Column Name");
73 temp = new Label(container, SWT.NULL);
74 temp.setText("Where Value");
75 temp = new Label(container, SWT.NULL);
76 temp.setText("Where");
77 temp = new Label(container, SWT.NULL);
78 temp.setText("Set Value");
79 temp = new Label(container, SWT.NULL);
81 for (int i = 0; i < row.getColumnCount(); i++) {
82 Label label = new Label(container, SWT.NULL);
83 label.setText(columnNames[i]);
84 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
85 if (data[i] == null || data[i].equals("")) {
86 oldValues[i].setText('$' + columnNames[i]);
88 oldValues[i].setText(data[i]);
90 oldValues[i].addModifyListener(new ModifyListener() {
91 public void modifyText(ModifyEvent e) {
95 primaryKeys[i] = new Button(container, SWT.CHECK);
96 // primaryKeys[i].setText("Where");
97 primaryKeys[i].addSelectionListener(new SelectionListener() {
98 public void widgetDefaultSelected(SelectionEvent e) {
100 public void widgetSelected(SelectionEvent e) {
104 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
106 if (data[i] == null || data[i].equals("")) {
107 newValues[i].setText('$' + columnNames[i]);
109 newValues[i].setText(data[i]);
111 newValues[i].addModifyListener(new ModifyListener() {
112 public void modifyText(ModifyEvent e) {
116 setValues[i] = new Button(container, SWT.CHECK);
117 // setValues[i].setText("Set Value");
118 setValues[i].addSelectionListener(new SelectionListener() {
119 public void widgetDefaultSelected(SelectionEvent e) {
121 public void widgetSelected(SelectionEvent e) {
126 query = new Label(container, SWT.WRAP);
127 GridData gridData = new GridData();
128 gridData.horizontalSpan = layoutColumns;
129 gridData.horizontalAlignment = GridData.FILL;
130 gridData.verticalAlignment = GridData.FILL;
131 gridData.grabExcessHorizontalSpace = true;
132 gridData.grabExcessVerticalSpace = true;
133 query.setLayoutData(gridData);
135 setControl(container);
138 setPageComplete(true);
140 public void updateQuery() {
141 System.out.println("Updating update query");
142 StringBuffer setClause = new StringBuffer();
143 StringBuffer whereClause = new StringBuffer();
144 for (int i = 0; i < columnNames.length; i++) {
145 if (primaryKeys[i].getSelection()) {
146 whereClause.append(columnNames[i]);
147 whereClause.append(" = ");
148 whereClause.append("'" + oldValues[i].getText() + "'");
149 whereClause.append(", ");
151 if (setValues[i].getSelection()) {
152 setClause.append(columnNames[i]);
153 setClause.append(" = ");
154 setClause.append("'" + newValues[i].getText() + "'");
155 setClause.append(", ");
158 if (whereClause.length() > 1) {
159 whereClause.deleteCharAt(whereClause.length() - 1);
160 whereClause.deleteCharAt(whereClause.length() - 1);
162 if (setClause.length() > 1) {
163 setClause.deleteCharAt(setClause.length() - 1);
164 setClause.deleteCharAt(setClause.length() - 1);
167 { setClause.toString(), row.getTable(), whereClause.toString()};
170 fStore.getString("phpeclipse.sql.update.template"));
172 String query = form.format(arguments);
174 // String query = "$results = mysql_query(\"UPDATE " + row.getTable();
175 // query += " SET " + setClause.toString();
176 // query += " WHERE " + whereClause.toString() + "\");";
177 this.query.setText(query);
179 public boolean performFinish() {
180 PHPSourceConsole console = PHPSourceConsole.getInstance();
182 console.print(query.getText());