e31fe2f2d99f71d6196131281e27cc0edaa19d03
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / PHPSelectRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import java.text.MessageFormat;
4
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;
18
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;
23
24 public class PHPSelectRowPage extends WizardPage implements SQLPage {
25         TableRow row;
26         String[] columnNames;
27         Text[] whereValues;
28         // Text[] newValues;
29         Button[] primaryKeys;
30         Button[] setValues;
31         Label query;
32         IPreferenceStore fStore;
33
34         public PHPSelectRowPage(String pageName) {
35                 super(pageName);
36         }
37
38         public void init(TableRow row, TableAdapter adapter) {
39                 this.row = row;
40         }
41
42         public void createControl(Composite parent) {
43                 System.out.println("page create control");
44                 Composite container = new Composite(parent, SWT.NULL);
45                 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
46
47                 GridLayout layout = new GridLayout();
48                 container.setLayout(layout);
49                 int layoutColumns = 4;
50                 layout.numColumns = layoutColumns;
51
52                 if (row == null) {
53                         System.out.println("Row is null");
54                 }
55                 if (row.getColumnNames() == null) {
56                         System.out.println("Columns are null");
57                 }
58                 if (row.getTableData() == null) {
59                         System.out.println("Data is null");
60                 }
61                 columnNames = row.getColumnNames();
62                 String[] data = row.getTableData();
63
64                 for (int i = 0; i < row.getColumnCount(); i++) {
65                         System.out.println("data = " + i + "=" + data[i]);
66                         System.out.println("column = " + i + "=" + columnNames[i]);
67                 }
68
69                 whereValues = new Text[row.getColumnCount()];
70                 //  newValues = new Text[row.getColumnCount()];
71                 primaryKeys = new Button[row.getColumnCount()];
72                 setValues = new Button[row.getColumnCount()];
73                 Label temp = new Label(container, SWT.NULL);
74                 temp.setText("Column Name");
75                 temp = new Label(container, SWT.NULL);
76                 temp.setText("Value");
77                 temp = new Label(container, SWT.NULL);
78                 temp.setText("Where");
79                 //    temp = new Label(container, SWT.NULL);
80                 //    temp.setText("New Value");
81                 temp = new Label(container, SWT.NULL);
82                 temp.setText("Select");
83                 for (int i = 0; i < row.getColumnCount(); i++) {
84                         Label label = new Label(container, SWT.NULL);
85                         label.setText(columnNames[i]);
86                         whereValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
87                         if (data[i] == null || data[i].equals("")) {
88                                 whereValues[i].setText('$' + columnNames[i]);
89                         } else {
90                                 whereValues[i].setText(data[i]);
91                         }
92
93                         whereValues[i].addModifyListener(new ModifyListener() {
94                                 public void modifyText(ModifyEvent e) {
95                                         updateQuery();
96                                 }
97                         });
98
99                         primaryKeys[i] = new Button(container, SWT.CHECK);
100                         //primaryKeys[i].setText("Where Clause");
101                         primaryKeys[i].addSelectionListener(new SelectionListener() {
102                                 public void widgetDefaultSelected(SelectionEvent e) {
103                                 }
104                                 public void widgetSelected(SelectionEvent e) {
105                                         updateQuery();
106                                 }
107                         });
108                         //   newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
109                         //   newValues[i].setText(data[i]);
110                         //   newValues[i].addModifyListener(new ModifyListener() {
111                         //    public void modifyText(ModifyEvent e) {
112                         //       updateQuery();
113                         //    }
114                         //  });
115                         setValues[i] = new Button(container, SWT.CHECK);
116                         //setValues[i].setText("Select Value");
117                         setValues[i].addSelectionListener(new SelectionListener() {
118                                 public void widgetDefaultSelected(SelectionEvent e) {
119                                 }
120                                 public void widgetSelected(SelectionEvent e) {
121                                         updateQuery();
122                                 }
123                         });
124                 }
125                 query = new Label(container, SWT.WRAP);
126                 GridData gridData = new GridData();
127                 gridData.horizontalSpan = layoutColumns;
128                 gridData.horizontalAlignment = GridData.FILL;
129                 gridData.verticalAlignment = GridData.FILL;
130                 gridData.grabExcessHorizontalSpace = true;
131                 gridData.grabExcessVerticalSpace = true;
132                 query.setLayoutData(gridData);
133
134                 setControl(container);
135                 updateQuery();
136
137                 setPageComplete(true);
138         }
139
140         public void updateQuery() {
141                 System.out.println("PHP SELECT");
142                 StringBuffer setClause = new StringBuffer();
143                 StringBuffer whereClause = new StringBuffer();
144                 String temp;
145                 boolean firstClause = false;
146                 for (int i = 0; i < columnNames.length; i++) {
147                         if (primaryKeys[i].getSelection()) {
148                                 if (firstClause) {
149                                         whereClause.append(" AND ");
150                                 }
151                                 firstClause = true;
152                                 whereClause.append(columnNames[i]);
153                                 whereClause.append(" = ");
154                                 temp = whereValues[i].getText();
155                                 //        if (temp.charAt(0) == '$') {
156                                 //          whereClause.append(temp);
157                                 //        } else {
158                                 whereClause.append("'" + temp + "'");
159                                 //        }
160
161                         }
162                         if (setValues[i].getSelection()) {
163                                 setClause.append(columnNames[i]);
164                                 //   setClause.append(" = ");
165                                 //   setClause.append(newValues[i].getText());
166                                 setClause.append(", ");
167                         }
168                 }
169                 //    if (whereClause.length() > 1) {
170                 //      whereClause.deleteCharAt(whereClause.length() - 1);
171                 //      whereClause.deleteCharAt(whereClause.length() - 1);
172                 //    }
173                 if (setClause.length() > 1) {
174                         setClause.deleteCharAt(setClause.length() - 1);
175                         setClause.deleteCharAt(setClause.length() - 1);
176                 }
177
178                 String[] arguments =
179                         { setClause.toString(), row.getTable(), whereClause.toString()};
180                 MessageFormat form =
181                         new MessageFormat(
182                                 fStore.getString("phpeclipse.sql.select.template"));
183
184                 String query = form.format(arguments);
185                 //    String query = "$results = mysql_query(\"SELECT " + setClause.toString();
186                 //    query += " FROM " + row.getTable();
187                 //    query += " WHERE " + whereClause.toString() + "\");";
188                 this.query.setText(query);
189         }
190
191         public boolean performFinish() {
192                 PHPSourceConsole console = PHPSourceConsole.getInstance();
193                 console.clear();
194                 console.print(query.getText());
195                 return true;
196         }
197 }