1 package net.sourceforge.phpdt.sql.wizards;
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;
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;
20 public class UpdateRowPage extends WizardPage implements SQLPage {
28 public UpdateRowPage(String pageName) {
32 public void init(TableRow row, TableAdapter adapter) {
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;
45 System.out.println("Row is null");
47 if (row.getColumnNames() == null) {
48 System.out.println("Columns are null");
50 if (row.getTableData() == null) {
51 System.out.println("Data is null");
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]);
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);
69 temp = new Label(container, SWT.NULL);
70 temp.setText("New Value");
71 temp = new Label(container, SWT.NULL);
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) {
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) {
88 public void widgetSelected(SelectionEvent e) {
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) {
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) {
104 public void widgetSelected(SelectionEvent e) {
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);
118 setControl(container);
121 setPageComplete(true);
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(", ");
134 if (setValues[i].getSelection()) {
135 setClause.append(columnNames[i]);
136 setClause.append(" = ");
137 setClause.append(newValues[i].getText());
138 setClause.append(", ");
141 if (whereClause.length() > 1) {
142 whereClause.deleteCharAt(whereClause.length() - 1);
143 whereClause.deleteCharAt(whereClause.length() - 1);
145 if (setClause.length() > 1) {
146 setClause.deleteCharAt(setClause.length() - 1);
147 setClause.deleteCharAt(setClause.length() - 1);
149 String query = "UPDATE " + row.getTable();
150 query += " SET " + setClause.toString();
151 query += " WHERE " + whereClause.toString();
152 this.query.setText(query);
154 public boolean performFinish() {
155 MultiSQLServer server = MultiSQLServer.getInstance();
156 server.execute(query.getText());