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