*** empty log message ***
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / UpdateRowPage.java
1 package com.quantum.wizards;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.events.ModifyEvent;
5 import org.eclipse.swt.events.ModifyListener;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.events.SelectionListener;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.swt.widgets.Text;
14
15 import com.quantum.Messages;
16 import com.quantum.adapters.DatabaseAdapter;
17 import com.quantum.model.Bookmark;
18 import com.quantum.model.Column;
19 import com.quantum.model.Entity;
20
21 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
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 createControl(Composite parent) {
33                 System.out.println("page create control"); //$NON-NLS-1$
34                 Composite container = new Composite(parent, SWT.V_SCROLL);
35                 GridLayout layout = new GridLayout();
36                 container.setLayout(layout);
37                 Entity entity = this.results.getEntity();
38                 
39                 int layoutColumns = 5;
40                 layout.numColumns = layoutColumns;
41
42                 columnNames = this.results.getColumnNames();
43                 oldValues = new Text[columnNames.length];
44                 newValues = new Text[columnNames.length];
45                 primaryKeys = new Button[columnNames.length];
46                 setValues = new Button[columnNames.length];
47                 Label temp = new Label(container, SWT.NULL);
48                 temp.setText(Messages.getString("UpdateRowPage.ColumnName")); //$NON-NLS-1$
49                 temp = new Label(container, SWT.NULL);
50                 temp.setText(Messages.getString("UpdateRowPage.OldValue")); //$NON-NLS-1$
51                 temp = new Label(container, SWT.NULL);
52                 temp.setText(""); //$NON-NLS-1$
53                 temp = new Label(container, SWT.NULL);
54                 temp.setText(Messages.getString("UpdateRowPage.NewValue")); //$NON-NLS-1$
55                 temp = new Label(container, SWT.NULL);
56                 temp.setText(Messages.getString("UpdateRowPage._13")); //$NON-NLS-1$
57                 for (int i = 0; i < columnNames.length; i++) {
58                         Label label = new Label(container, SWT.NULL);
59                         label.setText(columnNames[i]);
60                         oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
61                         GridData gridData = new GridData();
62                         gridData.widthHint = 100;
63                         oldValues[i].setLayoutData(gridData);
64                         Object data = this.row == null ? null : this.row.get(i+1);
65                         oldValues[i].setText(data == null ? "" : data.toString());
66                         oldValues[i].addModifyListener(new ModifyListener() {
67                                 public void modifyText(ModifyEvent e) {
68                                         updateQuery();
69                                 }                               
70                         });
71                         primaryKeys[i] = new Button(container, SWT.CHECK);
72                         primaryKeys[i].setText("Where"); //$NON-NLS-1$
73                         Column column = (entity == null) ? null : getColumn(entity, columnNames[i]);
74                         if (column != null && column.isPrimaryKey()) 
75                                 primaryKeys[i].setSelection(true);
76                         primaryKeys[i].addSelectionListener(new SelectionListener() {
77                                 public void widgetDefaultSelected(SelectionEvent e) {
78                                 }
79                                 public void widgetSelected(SelectionEvent e) {
80                                         updateQuery();
81                                 }
82                         });
83                         newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
84                         newValues[i].setText(data == null ? "" : data.toString());
85                         gridData = new GridData();
86                         gridData.widthHint = 100;
87                         newValues[i].setLayoutData(gridData);
88                         newValues[i].addModifyListener(new ModifyListener() {
89                                 public void modifyText(ModifyEvent e) {
90                                         updateQuery();
91                                 }                               
92                         });
93                         setValues[i] = new Button(container, SWT.CHECK);
94                         setValues[i].setText(Messages.getString("UpdateRowPage.SetValue")); //$NON-NLS-1$
95                         setValues[i].addSelectionListener(new SelectionListener() {
96                                 public void widgetDefaultSelected(SelectionEvent e) {
97                                 }
98                                 public void widgetSelected(SelectionEvent e) {
99                                         updateQuery();
100                                 }
101                         });
102                 }
103                 query = new Label(container, SWT.WRAP | SWT.V_SCROLL);
104                 GridData gridData = new GridData();
105                 gridData.horizontalSpan = layoutColumns;
106                 gridData.horizontalAlignment = GridData.FILL;
107                 gridData.verticalAlignment = GridData.FILL;
108                 gridData.grabExcessHorizontalSpace = true;
109                 gridData.grabExcessVerticalSpace = true;
110                 query.setLayoutData(gridData);
111
112                 setControl(container);
113         updateQuery();
114        
115                 setPageComplete(true);
116         }
117         public void updateQuery() {
118                 System.out.println("Updating query"); //$NON-NLS-1$
119                 StringBuffer setClause = new StringBuffer();
120                 StringBuffer whereClause = new StringBuffer();
121                 Bookmark bookmark = this.results.getBookmark();
122                 Entity entity = this.results.getEntity();
123                 DatabaseAdapter adapter = bookmark.getAdapter();
124                 
125                 int numValuesSet = 0;
126                 int numValuesWhere = 0;
127                 for (int i = 0; i < columnNames.length; i++) {
128                         if (primaryKeys[i].getSelection()) {
129                                 String value = oldValues[i].getText();
130                                 if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$
131                                 whereClause.append("("); //$NON-NLS-1$
132                                 whereClause.append(columnNames[i]);
133                                 whereClause.append(" = "); //$NON-NLS-1$
134                 appendColumn(whereClause, entity, columnNames[i], adapter, value);
135                                 whereClause.append(")"); //$NON-NLS-1$
136                                 numValuesWhere++;
137                         } 
138                         if (setValues[i].getSelection()) {
139                                 String value = newValues[i].getText();
140                                 if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$
141                                 setClause.append(columnNames[i]);
142                                 setClause.append(" = "); //$NON-NLS-1$
143                 appendColumn(setClause, entity, columnNames[i], adapter, value);
144                                 numValuesSet++;
145                         }
146                 }
147                 String query = "UPDATE " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
148                 query += " SET " + setClause.toString(); //$NON-NLS-1$
149                 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
150                 this.query.setText(query);
151         }
152
153     /* (non-Javadoc)
154      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
155      */
156     protected String getQueryText() {
157         return query.getText();
158     }
159
160 }