1 package net.sourceforge.phpdt.sql.wizards;
3 import net.sourceforge.phpdt.sql.sql.TableRow;
4 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
5 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
7 import org.eclipse.jface.wizard.WizardPage;
8 import org.eclipse.swt.SWT;
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 public class PHPUpdateRowPage extends WizardPage implements SQLPage {
28 public PHPUpdateRowPage(String pageName) {
32 public void init(TableRow row, TableAdapter adapter) {
36 public void createControl(Composite parent) {
38 System.out.println("page create control");
40 Composite container = new Composite(parent, SWT.NULL);
41 GridLayout layout = new GridLayout();
42 container.setLayout(layout);
43 int layoutColumns = 5;
44 layout.numColumns = layoutColumns;
48 System.out.println("Row is null");
50 if (row.getColumnNames() == null) {
51 System.out.println("Columns are null");
53 if (row.getTableData() == null) {
54 System.out.println("Data is null");
58 columnNames = row.getColumnNames();
59 String[] data = row.getTableData();
61 for (int i = 0; i < row.getColumnCount(); i++) {
62 System.out.println("data = " + i + "=" + data[i]);
63 System.out.println("column = " + i + "=" + columnNames[i]);
66 oldValues = new Text[row.getColumnCount()];
67 newValues = new Text[row.getColumnCount()];
68 primaryKeys = new Button[row.getColumnCount()];
69 setValues = new Button[row.getColumnCount()];
70 Label temp = new Label(container, SWT.NULL);
71 temp.setText("Column Name");
72 temp = new Label(container, SWT.NULL);
73 temp.setText("Where Value");
74 temp = new Label(container, SWT.NULL);
75 temp.setText("Where");
76 temp = new Label(container, SWT.NULL);
77 temp.setText("Set Value");
78 temp = new Label(container, SWT.NULL);
80 for (int i = 0; i < row.getColumnCount(); i++) {
81 Label label = new Label(container, SWT.NULL);
82 label.setText(columnNames[i]);
83 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
84 if (data[i] == null || data[i].equals("")) {
85 oldValues[i].setText('$' + columnNames[i]);
87 oldValues[i].setText(data[i]);
89 oldValues[i].addModifyListener(new ModifyListener() {
90 public void modifyText(ModifyEvent e) {
94 primaryKeys[i] = new Button(container, SWT.CHECK);
95 // primaryKeys[i].setText("Where");
96 primaryKeys[i].addSelectionListener(new SelectionListener() {
97 public void widgetDefaultSelected(SelectionEvent e) {
99 public void widgetSelected(SelectionEvent e) {
103 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
105 if (data[i] == null || data[i].equals("")) {
106 newValues[i].setText('$' + columnNames[i]);
108 newValues[i].setText(data[i]);
110 newValues[i].addModifyListener(new ModifyListener() {
111 public void modifyText(ModifyEvent e) {
115 setValues[i] = new Button(container, SWT.CHECK);
116 // setValues[i].setText("Set Value");
117 setValues[i].addSelectionListener(new SelectionListener() {
118 public void widgetDefaultSelected(SelectionEvent e) {
120 public void widgetSelected(SelectionEvent e) {
125 query = new Label(container, SWT.WRAP);
126 GridData gridData = new GridData();
127 gridData.horizontalSpan = layoutColumns;
128 gridData.horizontalAlignment = GridData.FILL;
129 gridData.verticalAlignment = GridData.FILL;
130 gridData.grabExcessHorizontalSpace = true;
131 gridData.grabExcessVerticalSpace = true;
132 query.setLayoutData(gridData);
134 setControl(container);
137 setPageComplete(true);
139 public void updateQuery() {
141 System.out.println("Updating update query");
143 StringBuffer setClause = new StringBuffer();
144 StringBuffer whereClause = new StringBuffer();
145 for (int i = 0; i < columnNames.length; i++) {
146 if (primaryKeys[i].getSelection()) {
147 whereClause.append(columnNames[i]);
148 whereClause.append(" = ");
149 whereClause.append("'"+oldValues[i].getText()+"'");
150 whereClause.append(", ");
152 if (setValues[i].getSelection()) {
153 setClause.append(columnNames[i]);
154 setClause.append(" = ");
155 setClause.append("'"+newValues[i].getText()+"'");
156 setClause.append(", ");
159 if (whereClause.length() > 1) {
160 whereClause.deleteCharAt(whereClause.length() - 1);
161 whereClause.deleteCharAt(whereClause.length() - 1);
163 if (setClause.length() > 1) {
164 setClause.deleteCharAt(setClause.length() - 1);
165 setClause.deleteCharAt(setClause.length() - 1);
167 String query = "$results = mysql_query(\"UPDATE " + row.getTable();
168 query += " SET " + setClause.toString();
169 query += " WHERE " + whereClause.toString() + "\");";
170 this.query.setText(query);
172 public boolean performFinish() {
173 PHPSourceConsole console = PHPSourceConsole.getInstance();
175 console.print(query.getText());