d04961512435a0c6d864d61bc8ec61c8be2f9560
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / UpdateRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import org.eclipse.jface.wizard.WizardPage;
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.ModifyEvent;
6 import org.eclipse.swt.events.ModifyListener;
7 import org.eclipse.swt.events.SelectionEvent;
8 import org.eclipse.swt.events.SelectionListener;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
15
16 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
19
20 public class UpdateRowPage extends WizardPage implements SQLPage {
21         TableRow row;
22         String[] columnNames;
23         Text[] oldValues;
24         Text[] newValues;
25         Button[] primaryKeys;
26         Button[] setValues;
27         Label query;
28         public UpdateRowPage(String pageName) {
29                 super(pageName);
30         }
31
32         public void init(TableRow row, TableAdapter adapter) {
33                 this.row = row;
34         }
35         
36         public void createControl(Composite parent) {
37                 System.out.println("page create control");
38                 Composite container = new Composite(parent, SWT.NULL);
39                 GridLayout layout = new GridLayout();
40                 container.setLayout(layout);
41                 int layoutColumns = 5;
42                 layout.numColumns = layoutColumns;
43
44                 if (row == null) {
45                         System.out.println("Row is null");
46                 }
47                 if (row.getColumnNames() == null) {
48                         System.out.println("Columns are null");
49                 }
50                 if (row.getTableData() == null) {
51                         System.out.println("Data is null");
52                 }
53                 columnNames = row.getColumnNames();
54                 String[] data = row.getTableData();
55                 for (int i = 0; i < row.getColumnCount(); i++) {
56                         System.out.println("data = " + i + "=" + data[i]);
57                         System.out.println("column = " + i + "=" + columnNames[i]);
58                 }
59                 oldValues = new Text[row.getColumnCount()];
60                 newValues = new Text[row.getColumnCount()];
61                 primaryKeys = new Button[row.getColumnCount()];
62                 setValues = new Button[row.getColumnCount()];
63                 Label temp = new Label(container, SWT.NULL);
64                 temp.setText("Column Name");
65                 temp = new Label(container, SWT.NULL);
66                 temp.setText("Old Value");
67                 temp = new Label(container, SWT.NULL);
68                 temp.setText("");
69                 temp = new Label(container, SWT.NULL);
70                 temp.setText("New Value");
71                 temp = new Label(container, SWT.NULL);
72                 temp.setText("");
73                 for (int i = 0; i < row.getColumnCount(); i++) {
74                         Label label = new Label(container, SWT.NULL);
75                         label.setText(columnNames[i]);
76                         oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
77                         oldValues[i].setText(data[i]);
78                         oldValues[i].addModifyListener(new ModifyListener() {
79                                 public void modifyText(ModifyEvent e) {
80                                         updateQuery();
81                                 }                               
82                         });
83                         primaryKeys[i] = new Button(container, SWT.CHECK);
84                         primaryKeys[i].setText("Primary Key");
85                         primaryKeys[i].addSelectionListener(new SelectionListener() {
86                                 public void widgetDefaultSelected(SelectionEvent e) {
87                                 }
88                                 public void widgetSelected(SelectionEvent e) {
89                                         updateQuery();
90                                 }
91                         });
92                         newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
93                         newValues[i].setText(data[i]);
94                         newValues[i].addModifyListener(new ModifyListener() {
95                                 public void modifyText(ModifyEvent e) {
96                                         updateQuery();
97                                 }                               
98                         });
99                         setValues[i] = new Button(container, SWT.CHECK);
100                         setValues[i].setText("Set Value");
101                         setValues[i].addSelectionListener(new SelectionListener() {
102                                 public void widgetDefaultSelected(SelectionEvent e) {
103                                 }
104                                 public void widgetSelected(SelectionEvent e) {
105                                         updateQuery();
106                                 }
107                         });
108                 }
109                 query = new Label(container, SWT.WRAP);
110                 GridData gridData = new GridData();
111                 gridData.horizontalSpan = layoutColumns;
112                 gridData.horizontalAlignment = GridData.FILL;
113                 gridData.verticalAlignment = GridData.FILL;
114                 gridData.grabExcessHorizontalSpace = true;
115                 gridData.grabExcessVerticalSpace = true;
116                 query.setLayoutData(gridData);
117
118                 setControl(container);
119         updateQuery();
120        
121                 setPageComplete(true);
122         }
123         public void updateQuery() {
124                 System.out.println("Updating query");
125                 StringBuffer setClause = new StringBuffer();
126                 StringBuffer whereClause = new StringBuffer();
127                 for (int i = 0; i < columnNames.length; i++) {
128                         if (primaryKeys[i].getSelection()) {
129                                 whereClause.append(columnNames[i]);
130                                 whereClause.append(" = ");
131                                 whereClause.append(oldValues[i].getText());
132                                 whereClause.append(", ");
133                         } 
134                         if (setValues[i].getSelection()) {
135                                 setClause.append(columnNames[i]);
136                                 setClause.append(" = ");
137                                 setClause.append(newValues[i].getText());
138                                 setClause.append(", ");
139                         }
140                 }
141                 if (whereClause.length() > 1) {
142                         whereClause.deleteCharAt(whereClause.length() - 1);
143                         whereClause.deleteCharAt(whereClause.length() - 1);
144                 }
145                 if (setClause.length() > 1) {
146                         setClause.deleteCharAt(setClause.length() - 1);
147                         setClause.deleteCharAt(setClause.length() - 1);
148                 }
149                 String query = "UPDATE " + row.getTable();
150                 query += " SET " + setClause.toString();
151                 query += " WHERE " + whereClause.toString();
152                 this.query.setText(query);
153         }
154         public boolean performFinish() {
155            MultiSQLServer server = MultiSQLServer.getInstance();
156            server.execute(query.getText());
157            return true;
158         }
159 }