1 package com.quantum.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 com.quantum.QuantumPlugin;
20 import com.quantum.sql.TableRow;
21 import com.quantum.view.PHPSourceConsole;
22 import com.quantum.view.tableview.TableAdapter;
24 public class PHPSelectRowPage extends WizardPage implements SQLPage {
32 IPreferenceStore fStore;
33 private final static boolean DEBUG = false;
35 public PHPSelectRowPage(String pageName) {
39 public void init(TableRow row, TableAdapter adapter) {
43 public void createControl(Composite parent) {
45 System.out.println("page create control");
47 Composite container = new Composite(parent, SWT.NULL);
48 fStore = QuantumPlugin.getDefault().getPreferenceStore();
50 GridLayout layout = new GridLayout();
51 container.setLayout(layout);
52 int layoutColumns = 4;
53 layout.numColumns = layoutColumns;
57 System.out.println("Row is null");
59 if (row.getColumnNames() == null) {
60 System.out.println("Columns are null");
62 if (row.getTableData() == null) {
63 System.out.println("Data is null");
67 columnNames = row.getColumnNames();
68 String[] data = row.getTableData();
70 for (int i = 0; i < row.getColumnCount(); i++) {
71 System.out.println("data = " + i + "=" + data[i]);
72 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());