1 package net.sourceforge.phpdt.sql.wizards;
3 import java.text.MessageFormat;
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.jface.wizard.WizardPage;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
19 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
20 import net.sourceforge.phpdt.sql.sql.TableRow;
21 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
22 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
24 public class PHPUpdateRowPage extends WizardPage implements SQLPage {
32 IPreferenceStore fStore;
34 public PHPUpdateRowPage(String pageName) {
38 public void init(TableRow row, TableAdapter adapter) {
42 public void createControl(Composite parent) {
44 System.out.println("page create control");
46 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
47 Composite container = new Composite(parent, SWT.NULL);
48 GridLayout layout = new GridLayout();
49 container.setLayout(layout);
50 int layoutColumns = 5;
51 layout.numColumns = layoutColumns;
55 System.out.println("Row is null");
57 if (row.getColumnNames() == null) {
58 System.out.println("Columns are null");
60 if (row.getTableData() == null) {
61 System.out.println("Data is null");
65 columnNames = row.getColumnNames();
66 String[] data = row.getTableData();
69 for (int i = 0; i < row.getColumnCount(); i++) {
70 System.out.println("data = " + i + "=" + data[i]);
71 System.out.println("column = " + i + "=" + columnNames[i]);
75 oldValues = new Text[row.getColumnCount()];
76 newValues = new Text[row.getColumnCount()];
77 primaryKeys = new Button[row.getColumnCount()];
78 setValues = new Button[row.getColumnCount()];
79 Label temp = new Label(container, SWT.NULL);
80 temp.setText("Column Name");
81 temp = new Label(container, SWT.NULL);
82 temp.setText("Where Value");
83 temp = new Label(container, SWT.NULL);
84 temp.setText("Where");
85 temp = new Label(container, SWT.NULL);
86 temp.setText("Set Value");
87 temp = new Label(container, SWT.NULL);
89 for (int i = 0; i < row.getColumnCount(); i++) {
90 Label label = new Label(container, SWT.NULL);
91 label.setText(columnNames[i]);
92 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
93 if (data[i] == null || data[i].equals("")) {
94 oldValues[i].setText('$' + columnNames[i]);
96 oldValues[i].setText(data[i]);
98 oldValues[i].addModifyListener(new ModifyListener() {
99 public void modifyText(ModifyEvent e) {
103 primaryKeys[i] = new Button(container, SWT.CHECK);
104 // primaryKeys[i].setText("Where");
105 primaryKeys[i].addSelectionListener(new SelectionListener() {
106 public void widgetDefaultSelected(SelectionEvent e) {
108 public void widgetSelected(SelectionEvent e) {
112 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
114 if (data[i] == null || data[i].equals("")) {
115 newValues[i].setText('$' + columnNames[i]);
117 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("Set Value");
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);
149 public void updateQuery() {
151 System.out.println("Updating update query");
154 StringBuffer setClause = new StringBuffer();
155 StringBuffer whereClause = new StringBuffer();
156 for (int i = 0; i < columnNames.length; i++) {
157 if (primaryKeys[i].getSelection()) {
158 whereClause.append(columnNames[i]);
159 whereClause.append(" = ");
160 whereClause.append("'" + oldValues[i].getText() + "'");
161 whereClause.append(", ");
163 if (setValues[i].getSelection()) {
164 setClause.append(columnNames[i]);
165 setClause.append(" = ");
166 setClause.append("'" + newValues[i].getText() + "'");
167 setClause.append(", ");
170 if (whereClause.length() > 1) {
171 whereClause.deleteCharAt(whereClause.length() - 1);
172 whereClause.deleteCharAt(whereClause.length() - 1);
174 if (setClause.length() > 1) {
175 setClause.deleteCharAt(setClause.length() - 1);
176 setClause.deleteCharAt(setClause.length() - 1);
178 String[] arguments = { row.getTable(), setClause.toString(), whereClause.toString()};
179 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.update.template"));
181 String query = form.format(arguments);
183 // String query = "$results = mysql_query(\"UPDATE " + row.getTable();
184 // query += " SET " + setClause.toString();
185 // query += " WHERE " + whereClause.toString() + "\");";
186 this.query.setText(query);
188 public boolean performFinish() {
189 PHPSourceConsole console = PHPSourceConsole.getInstance();
191 console.print(query.getText());