1 package com.quantum.wizards;
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;
15 import com.quantum.Messages;
16 import com.quantum.adapters.DatabaseAdapter;
17 import com.quantum.model.Bookmark;
18 import com.quantum.model.Entity;
20 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
27 public UpdateRowPage(String pageName) {
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();
38 int layoutColumns = 5;
39 layout.numColumns = layoutColumns;
42 System.out.println("Row is null"); //$NON-NLS-1$
44 if (row.getColumnNames() == null) {
45 System.out.println("Columns are null"); //$NON-NLS-1$
47 if (row.getTableData() == null) {
48 System.out.println("Data is null"); //$NON-NLS-1$
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$
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) {
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) {
87 public void widgetSelected(SelectionEvent e) {
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) {
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) {
103 public void widgetSelected(SelectionEvent e) {
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);
117 setControl(container);
120 setPageComplete(true);
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();
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$
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);
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);
159 * @see com.quantum.wizards.BaseSQLPage#getQueryText()
161 protected String getQueryText() {
162 return query.getText();