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 PHPSelectRowPage extends WizardPage implements SQLPage {
32 IPreferenceStore fStore;
34 public PHPSelectRowPage(String pageName) {
38 public void init(TableRow row, TableAdapter adapter) {
42 public void createControl(Composite parent) {
44 System.out.println("page create control");
46 Composite container = new Composite(parent, SWT.NULL);
47 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
49 GridLayout layout = new GridLayout();
50 container.setLayout(layout);
51 int layoutColumns = 4;
52 layout.numColumns = layoutColumns;
56 System.out.println("Row is null");
58 if (row.getColumnNames() == null) {
59 System.out.println("Columns are null");
61 if (row.getTableData() == null) {
62 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 whereValues = 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("Value");
83 temp = new Label(container, SWT.NULL);
84 temp.setText("Where");
85 // temp = new Label(container, SWT.NULL);
86 // temp.setText("New Value");
87 temp = new Label(container, SWT.NULL);
88 temp.setText("Select");
89 for (int i = 0; i < row.getColumnCount(); i++) {
90 Label label = new Label(container, SWT.NULL);
91 label.setText(columnNames[i]);
92 whereValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
93 if (data[i] == null || data[i].equals("")) {
94 whereValues[i].setText('$' + columnNames[i]);
96 whereValues[i].setText(data[i]);
99 whereValues[i].addModifyListener(new ModifyListener() {
100 public void modifyText(ModifyEvent e) {
105 primaryKeys[i] = new Button(container, SWT.CHECK);
106 //primaryKeys[i].setText("Where Clause");
107 primaryKeys[i].addSelectionListener(new SelectionListener() {
108 public void widgetDefaultSelected(SelectionEvent e) {
110 public void widgetSelected(SelectionEvent e) {
114 // newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
115 // newValues[i].setText(data[i]);
116 // newValues[i].addModifyListener(new ModifyListener() {
117 // public void modifyText(ModifyEvent e) {
121 setValues[i] = new Button(container, SWT.CHECK);
122 //setValues[i].setText("Select Value");
123 setValues[i].addSelectionListener(new SelectionListener() {
124 public void widgetDefaultSelected(SelectionEvent e) {
126 public void widgetSelected(SelectionEvent e) {
131 query = new Label(container, SWT.WRAP);
132 GridData gridData = new GridData();
133 gridData.horizontalSpan = layoutColumns;
134 gridData.horizontalAlignment = GridData.FILL;
135 gridData.verticalAlignment = GridData.FILL;
136 gridData.grabExcessHorizontalSpace = true;
137 gridData.grabExcessVerticalSpace = true;
138 query.setLayoutData(gridData);
140 setControl(container);
143 setPageComplete(true);
146 public void updateQuery() {
148 System.out.println("PHP SELECT");
150 StringBuffer setClause = new StringBuffer();
151 StringBuffer whereClause = new StringBuffer();
153 boolean firstClause = false;
154 for (int i = 0; i < columnNames.length; i++) {
155 if (primaryKeys[i].getSelection()) {
157 whereClause.append(" AND ");
160 whereClause.append(columnNames[i]);
161 whereClause.append(" = ");
162 temp = whereValues[i].getText();
163 // if (temp.charAt(0) == '$') {
164 // whereClause.append(temp);
166 whereClause.append("'" + temp + "'");
170 if (setValues[i].getSelection()) {
171 setClause.append(columnNames[i]);
172 // setClause.append(" = ");
173 // setClause.append(newValues[i].getText());
174 setClause.append(", ");
177 // if (whereClause.length() > 1) {
178 // whereClause.deleteCharAt(whereClause.length() - 1);
179 // whereClause.deleteCharAt(whereClause.length() - 1);
181 if (setClause.length() > 1) {
182 setClause.deleteCharAt(setClause.length() - 1);
183 setClause.deleteCharAt(setClause.length() - 1);
186 String[] arguments = { setClause.toString(), row.getTable(), whereClause.toString() };
187 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.select.template"));
189 String query = form.format(arguments);
190 // String query = "$results = mysql_query(\"SELECT " + setClause.toString();
191 // query += " FROM " + row.getTable();
192 // query += " WHERE " + whereClause.toString() + "\");";
193 this.query.setText(query);
196 public boolean performFinish() {
197 PHPSourceConsole console = PHPSourceConsole.getInstance();
199 console.print(query.getText());