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) {
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();
53 int layoutColumns = 5;
54 layout.numColumns = layoutColumns;
57 System.out.println("Row is null"); //$NON-NLS-1$
59 if (row.getColumnNames() == null) {
60 System.out.println("Columns are null"); //$NON-NLS-1$
62 if (row.getTableData() == null) {
63 System.out.println("Data is null"); //$NON-NLS-1$
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$
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) {
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) {
101 public void widgetSelected(SelectionEvent e) {
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) {
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) {
117 public void widgetSelected(SelectionEvent e) {
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);
131 setControl(container);
134 setPageComplete(true);
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());
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])));
158 whereClause.append(value);
159 whereClause.append(")"); //$NON-NLS-1$
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])));
170 setClause.append(value);
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);
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());