added the method codeAnalysis() with an empty default implementation
[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.Messages;
17 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
18 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
19 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
20 import net.sourceforge.phpdt.sql.sql.TableRow;
21 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
22 import net.sourceforge.phpdt.sql.view.BookmarkView;
23 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
24 import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
25 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
26
27 public class UpdateRowPage extends WizardPage implements SQLPage {
28         TableRow row;
29         String[] columnNames;
30         Text[] oldValues;
31         Text[] newValues;
32         Button[] primaryKeys;
33         Button[] setValues;
34         Label query;
35         public UpdateRowPage(String pageName) {
36                 super(pageName);
37         }
38
39         public void init(TableRow row, TableAdapter adapter) {
40                 this.row = row;
41         }
42         
43         public void createControl(Composite parent) {
44                 System.out.println("page create control"); //$NON-NLS-1$
45                 Composite container = new Composite(parent, SWT.NULL);
46                 GridLayout layout = new GridLayout();
47                 container.setLayout(layout);
48                 BookmarkNode bookmark = row.getBookmarkNode();
49                 TreeNode node = bookmark.find(row.getTable());
50                 ObjectMetaData metadata = null;
51                 if (node != null) metadata = node.getMetaData(); 
52                 
53                 int layoutColumns = 5;
54                 layout.numColumns = layoutColumns;
55
56                 if (row == null) {
57                         System.out.println("Row is null"); //$NON-NLS-1$
58                 }
59                 if (row.getColumnNames() == null) {
60                         System.out.println("Columns are null"); //$NON-NLS-1$
61                 }
62                 if (row.getTableData() == null) {
63                         System.out.println("Data is null"); //$NON-NLS-1$
64                 }
65                 columnNames = row.getColumnNames();
66                 String[] data = row.getTableData();
67                 for (int i = 0; i < row.getColumnCount(); i++) {
68                         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
69                         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
70                 }
71                 oldValues = new Text[row.getColumnCount()];
72                 newValues = new Text[row.getColumnCount()];
73                 primaryKeys = new Button[row.getColumnCount()];
74                 setValues = new Button[row.getColumnCount()];
75                 Label temp = new Label(container, SWT.NULL);
76                 temp.setText(Messages.getString("UpdateRowPage.ColumnName")); //$NON-NLS-1$
77                 temp = new Label(container, SWT.NULL);
78                 temp.setText(Messages.getString("UpdateRowPage.OldValue")); //$NON-NLS-1$
79                 temp = new Label(container, SWT.NULL);
80                 temp.setText(""); //$NON-NLS-1$
81                 temp = new Label(container, SWT.NULL);
82                 temp.setText(Messages.getString("UpdateRowPage.NewValue")); //$NON-NLS-1$
83                 temp = new Label(container, SWT.NULL);
84                 temp.setText(Messages.getString("UpdateRowPage._13")); //$NON-NLS-1$
85                 for (int i = 0; i < row.getColumnCount(); i++) {
86                         Label label = new Label(container, SWT.NULL);
87                         label.setText(columnNames[i]);
88                         oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
89                         oldValues[i].setText(data[i]);
90                         oldValues[i].addModifyListener(new ModifyListener() {
91                                 public void modifyText(ModifyEvent e) {
92                                         updateQuery();
93                                 }                               
94                         });
95                         primaryKeys[i] = new Button(container, SWT.CHECK);
96                         primaryKeys[i].setText("Where"); //$NON-NLS-1$
97                         if (metadata != null && metadata.getPrimaryKeyOrder(columnNames[i]) > 0) primaryKeys[i].setSelection(true);
98                         primaryKeys[i].addSelectionListener(new SelectionListener() {
99                                 public void widgetDefaultSelected(SelectionEvent e) {
100                                 }
101                                 public void widgetSelected(SelectionEvent e) {
102                                         updateQuery();
103                                 }
104                         });
105                         newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
106                         newValues[i].setText(data[i]);
107                         newValues[i].addModifyListener(new ModifyListener() {
108                                 public void modifyText(ModifyEvent e) {
109                                         updateQuery();
110                                 }                               
111                         });
112                         setValues[i] = new Button(container, SWT.CHECK);
113                         setValues[i].setText(Messages.getString("UpdateRowPage.SetValue")); //$NON-NLS-1$
114                         setValues[i].addSelectionListener(new SelectionListener() {
115                                 public void widgetDefaultSelected(SelectionEvent e) {
116                                 }
117                                 public void widgetSelected(SelectionEvent e) {
118                                         updateQuery();
119                                 }
120                         });
121                 }
122                 query = new Label(container, SWT.WRAP);
123                 GridData gridData = new GridData();
124                 gridData.horizontalSpan = layoutColumns;
125                 gridData.horizontalAlignment = GridData.FILL;
126                 gridData.verticalAlignment = GridData.FILL;
127                 gridData.grabExcessHorizontalSpace = true;
128                 gridData.grabExcessVerticalSpace = true;
129                 query.setLayoutData(gridData);
130
131                 setControl(container);
132         updateQuery();
133        
134                 setPageComplete(true);
135         }
136         public void updateQuery() {
137                 System.out.println("Updating query"); //$NON-NLS-1$
138                 StringBuffer setClause = new StringBuffer();
139                 StringBuffer whereClause = new StringBuffer();
140                 BookmarkNode bookmark = row.getBookmarkNode();
141                 TreeNode node = bookmark.find(row.getTable());
142                 ObjectMetaData metadata = null;
143                 if (node != null) metadata = node.getMetaData(); 
144                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
145                 
146                 int numValuesSet = 0;
147                 int numValuesWhere = 0;
148                 for (int i = 0; i < columnNames.length; i++) {
149                         if (primaryKeys[i].getSelection()) {
150                                 String value = oldValues[i].getText();
151                                 if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$
152                                 whereClause.append("("); //$NON-NLS-1$
153                                 whereClause.append(columnNames[i]);
154                                 whereClause.append(" = "); //$NON-NLS-1$
155                                 if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
156                                         whereClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
157                                 else
158                                         whereClause.append(value);
159                                 whereClause.append(")"); //$NON-NLS-1$
160                                 numValuesWhere++;
161                         } 
162                         if (setValues[i].getSelection()) {
163                                 String value = newValues[i].getText();
164                                 if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$
165                                 setClause.append(columnNames[i]);
166                                 setClause.append(" = "); //$NON-NLS-1$
167                                 if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
168                                         setClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
169                                 else
170                                         setClause.append(value);
171                                 numValuesSet++;
172                                 
173                         }
174                 }
175                         String query = "UPDATE " + row.getTable(); //$NON-NLS-1$
176                 query += " SET " + setClause.toString(); //$NON-NLS-1$
177                 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
178                 this.query.setText(query);
179         }
180         public boolean performFinish() {
181            MultiSQLServer server = MultiSQLServer.getInstance();
182            BookmarkView bookmarkView = BookmarkView.getInstance(); 
183            BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
184            server.execute(bookmark.getConnection(), query.getText());
185            return true;
186         }
187 }