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.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;
27 public class UpdateRowPage extends WizardPage implements SQLPage {
35 public UpdateRowPage(String pageName) {
39 public void init(TableRow row, TableAdapter adapter) {
43 public void createControl(Composite parent) {
45 System.out.println("page create control"); //$NON-NLS-1$
48 Composite container = new Composite(parent, SWT.NULL);
49 GridLayout layout = new GridLayout();
50 container.setLayout(layout);
51 BookmarkNode bookmark = row.getBookmarkNode();
52 TreeNode node = bookmark.find(row.getTable());
53 ObjectMetaData metadata = null;
55 metadata = node.getMetaData();
57 int layoutColumns = 5;
58 layout.numColumns = layoutColumns;
62 System.out.println("Row is null"); //$NON-NLS-1$
64 if (row.getColumnNames() == null) {
65 System.out.println("Columns are null"); //$NON-NLS-1$
67 if (row.getTableData() == null) {
68 System.out.println("Data is null"); //$NON-NLS-1$
72 columnNames = row.getColumnNames();
73 String[] data = row.getTableData();
76 for (int i = 0; i < row.getColumnCount(); i++) {
77 System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
78 System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
82 oldValues = new Text[row.getColumnCount()];
83 newValues = new Text[row.getColumnCount()];
84 primaryKeys = new Button[row.getColumnCount()];
85 setValues = new Button[row.getColumnCount()];
86 Label temp = new Label(container, SWT.NULL);
87 temp.setText(Messages.getString("UpdateRowPage.ColumnName")); //$NON-NLS-1$
88 temp = new Label(container, SWT.NULL);
89 temp.setText(Messages.getString("UpdateRowPage.OldValue")); //$NON-NLS-1$
90 temp = new Label(container, SWT.NULL);
91 temp.setText(""); //$NON-NLS-1$
92 temp = new Label(container, SWT.NULL);
93 temp.setText(Messages.getString("UpdateRowPage.NewValue")); //$NON-NLS-1$
94 temp = new Label(container, SWT.NULL);
95 temp.setText(Messages.getString("UpdateRowPage._13")); //$NON-NLS-1$
96 for (int i = 0; i < row.getColumnCount(); i++) {
97 Label label = new Label(container, SWT.NULL);
98 label.setText(columnNames[i]);
99 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
100 oldValues[i].setText(data[i]);
101 oldValues[i].addModifyListener(new ModifyListener() {
102 public void modifyText(ModifyEvent e) {
106 primaryKeys[i] = new Button(container, SWT.CHECK);
107 primaryKeys[i].setText("Where"); //$NON-NLS-1$
108 if (metadata != null && metadata.getPrimaryKeyOrder(columnNames[i]) > 0)
109 primaryKeys[i].setSelection(true);
110 primaryKeys[i].addSelectionListener(new SelectionListener() {
111 public void widgetDefaultSelected(SelectionEvent e) {
113 public void widgetSelected(SelectionEvent e) {
117 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
118 newValues[i].setText(data[i]);
119 newValues[i].addModifyListener(new ModifyListener() {
120 public void modifyText(ModifyEvent e) {
124 setValues[i] = new Button(container, SWT.CHECK);
125 setValues[i].setText(Messages.getString("UpdateRowPage.SetValue")); //$NON-NLS-1$
126 setValues[i].addSelectionListener(new SelectionListener() {
127 public void widgetDefaultSelected(SelectionEvent e) {
129 public void widgetSelected(SelectionEvent e) {
134 query = new Label(container, SWT.WRAP);
135 GridData gridData = new GridData();
136 gridData.horizontalSpan = layoutColumns;
137 gridData.horizontalAlignment = GridData.FILL;
138 gridData.verticalAlignment = GridData.FILL;
139 gridData.grabExcessHorizontalSpace = true;
140 gridData.grabExcessVerticalSpace = true;
141 query.setLayoutData(gridData);
143 setControl(container);
146 setPageComplete(true);
148 public void updateQuery() {
150 System.out.println("Updating query"); //$NON-NLS-1$
153 StringBuffer setClause = new StringBuffer();
154 StringBuffer whereClause = new StringBuffer();
155 BookmarkNode bookmark = row.getBookmarkNode();
156 TreeNode node = bookmark.find(row.getTable());
157 ObjectMetaData metadata = null;
159 metadata = node.getMetaData();
160 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
162 int numValuesSet = 0;
163 int numValuesWhere = 0;
164 for (int i = 0; i < columnNames.length; i++) {
165 if (primaryKeys[i].getSelection()) {
166 String value = oldValues[i].getText();
167 if (numValuesWhere > 0)
168 whereClause.append(" AND "); //$NON-NLS-1$
169 whereClause.append("("); //$NON-NLS-1$
170 whereClause.append(columnNames[i]);
171 whereClause.append(" = "); //$NON-NLS-1$
172 if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
173 whereClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
175 whereClause.append(value);
176 whereClause.append(")"); //$NON-NLS-1$
179 if (setValues[i].getSelection()) {
180 String value = newValues[i].getText();
181 if (numValuesSet > 0)
182 setClause.append(", "); //$NON-NLS-1$
183 setClause.append(columnNames[i]);
184 setClause.append(" = "); //$NON-NLS-1$
185 if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
186 setClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
188 setClause.append(value);
193 String query = "UPDATE " + row.getTable(); //$NON-NLS-1$
194 query += " SET " + setClause.toString(); //$NON-NLS-1$
195 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
196 this.query.setText(query);
198 public boolean performFinish() {
199 MultiSQLServer server = MultiSQLServer.getInstance();
200 BookmarkView bookmarkView = BookmarkView.getInstance();
201 BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
202 server.execute(bookmark.getConnection(), query.getText());