1 package net.sourceforge.phpdt.sql.wizards;
3 import java.text.MessageFormat;
5 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
6 import net.sourceforge.phpdt.sql.sql.TableRow;
7 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
8 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
10 import org.eclipse.jface.preference.IPreferenceStore;
11 import org.eclipse.jface.wizard.WizardPage;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.ModifyEvent;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Text;
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();
68 for (int i = 0; i < row.getColumnCount(); i++) {
69 System.out.println("data = " + i + "=" + data[i]);
70 System.out.println("column = " + i + "=" + columnNames[i]);
73 oldValues = new Text[row.getColumnCount()];
74 newValues = new Text[row.getColumnCount()];
75 primaryKeys = new Button[row.getColumnCount()];
76 setValues = new Button[row.getColumnCount()];
77 Label temp = new Label(container, SWT.NULL);
78 temp.setText("Column Name");
79 temp = new Label(container, SWT.NULL);
80 temp.setText("Where Value");
81 temp = new Label(container, SWT.NULL);
82 temp.setText("Where");
83 temp = new Label(container, SWT.NULL);
84 temp.setText("Set Value");
85 temp = new Label(container, SWT.NULL);
87 for (int i = 0; i < row.getColumnCount(); i++) {
88 Label label = new Label(container, SWT.NULL);
89 label.setText(columnNames[i]);
90 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
91 if (data[i] == null || data[i].equals("")) {
92 oldValues[i].setText('$' + columnNames[i]);
94 oldValues[i].setText(data[i]);
96 oldValues[i].addModifyListener(new ModifyListener() {
97 public void modifyText(ModifyEvent e) {
101 primaryKeys[i] = new Button(container, SWT.CHECK);
102 // primaryKeys[i].setText("Where");
103 primaryKeys[i].addSelectionListener(new SelectionListener() {
104 public void widgetDefaultSelected(SelectionEvent e) {
106 public void widgetSelected(SelectionEvent e) {
110 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
112 if (data[i] == null || data[i].equals("")) {
113 newValues[i].setText('$' + columnNames[i]);
115 newValues[i].setText(data[i]);
117 newValues[i].addModifyListener(new ModifyListener() {
118 public void modifyText(ModifyEvent e) {
122 setValues[i] = new Button(container, SWT.CHECK);
123 // setValues[i].setText("Set Value");
124 setValues[i].addSelectionListener(new SelectionListener() {
125 public void widgetDefaultSelected(SelectionEvent e) {
127 public void widgetSelected(SelectionEvent e) {
132 query = new Label(container, SWT.WRAP);
133 GridData gridData = new GridData();
134 gridData.horizontalSpan = layoutColumns;
135 gridData.horizontalAlignment = GridData.FILL;
136 gridData.verticalAlignment = GridData.FILL;
137 gridData.grabExcessHorizontalSpace = true;
138 gridData.grabExcessVerticalSpace = true;
139 query.setLayoutData(gridData);
141 setControl(container);
144 setPageComplete(true);
146 public void updateQuery() {
148 System.out.println("Updating update query");
150 StringBuffer setClause = new StringBuffer();
151 StringBuffer whereClause = new StringBuffer();
152 for (int i = 0; i < columnNames.length; i++) {
153 if (primaryKeys[i].getSelection()) {
154 whereClause.append(columnNames[i]);
155 whereClause.append(" = ");
156 whereClause.append("'"+oldValues[i].getText()+"'");
157 whereClause.append(", ");
159 if (setValues[i].getSelection()) {
160 setClause.append(columnNames[i]);
161 setClause.append(" = ");
162 setClause.append("'"+newValues[i].getText()+"'");
163 setClause.append(", ");
166 if (whereClause.length() > 1) {
167 whereClause.deleteCharAt(whereClause.length() - 1);
168 whereClause.deleteCharAt(whereClause.length() - 1);
170 if (setClause.length() > 1) {
171 setClause.deleteCharAt(setClause.length() - 1);
172 setClause.deleteCharAt(setClause.length() - 1);
174 String[] arguments = { setClause.toString(), row.getTable(), whereClause.toString() };
175 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.update.template"));
177 String query = form.format(arguments);
179 // String query = "$results = mysql_query(\"UPDATE " + row.getTable();
180 // query += " SET " + setClause.toString();
181 // query += " WHERE " + whereClause.toString() + "\");";
182 this.query.setText(query);
184 public boolean performFinish() {
185 PHPSourceConsole console = PHPSourceConsole.getInstance();
187 console.print(query.getText());