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.dnd.TextTransfer;
9 import org.eclipse.swt.dnd.Transfer;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
21 import com.quantum.QuantumPlugin;
22 import com.quantum.sql.TableRow;
23 //import com.quantum.view.PHPSourceConsole;
24 import com.quantum.view.tableview.TableAdapter;
26 public class PHPSelectRowPage extends WizardPage implements SQLPage {
34 IPreferenceStore fStore;
35 private final static boolean DEBUG = false;
37 public PHPSelectRowPage(String pageName) {
41 public void init(TableRow row, TableAdapter adapter) {
45 public void createControl(Composite parent) {
47 System.out.println("page create control");
49 Composite container = new Composite(parent, SWT.NULL);
50 fStore = QuantumPlugin.getDefault().getPreferenceStore();
52 GridLayout layout = new GridLayout();
53 container.setLayout(layout);
54 int layoutColumns = 4;
55 layout.numColumns = layoutColumns;
59 System.out.println("Row is null");
61 if (row.getColumnNames() == null) {
62 System.out.println("Columns are null");
64 if (row.getTableData() == null) {
65 System.out.println("Data is null");
69 columnNames = row.getColumnNames();
70 String[] data = row.getTableData();
72 for (int i = 0; i < row.getColumnCount(); i++) {
73 System.out.println("data = " + i + "=" + data[i]);
74 System.out.println("column = " + i + "=" + columnNames[i]);
77 whereValues = new Text[row.getColumnCount()];
78 // newValues = new Text[row.getColumnCount()];
79 primaryKeys = new Button[row.getColumnCount()];
80 setValues = new Button[row.getColumnCount()];
81 Label temp = new Label(container, SWT.NULL);
82 temp.setText("Column Name");
83 temp = new Label(container, SWT.NULL);
84 temp.setText("Value");
85 temp = new Label(container, SWT.NULL);
86 temp.setText("Where");
87 // temp = new Label(container, SWT.NULL);
88 // temp.setText("New Value");
89 temp = new Label(container, SWT.NULL);
90 temp.setText("Select");
91 for (int i = 0; i < row.getColumnCount(); i++) {
92 Label label = new Label(container, SWT.NULL);
93 label.setText(columnNames[i]);
94 whereValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
95 if (data[i] == null || data[i].equals("")) {
96 whereValues[i].setText('$' + columnNames[i]);
98 whereValues[i].setText(data[i]);
101 whereValues[i].addModifyListener(new ModifyListener() {
102 public void modifyText(ModifyEvent e) {
107 primaryKeys[i] = new Button(container, SWT.CHECK);
108 //primaryKeys[i].setText("Where Clause");
109 primaryKeys[i].addSelectionListener(new SelectionListener() {
110 public void widgetDefaultSelected(SelectionEvent e) {
112 public void widgetSelected(SelectionEvent e) {
116 // newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
117 // newValues[i].setText(data[i]);
118 // newValues[i].addModifyListener(new ModifyListener() {
119 // public void modifyText(ModifyEvent e) {
123 setValues[i] = new Button(container, SWT.CHECK);
124 //setValues[i].setText("Select Value");
125 setValues[i].addSelectionListener(new SelectionListener() {
126 public void widgetDefaultSelected(SelectionEvent e) {
128 public void widgetSelected(SelectionEvent e) {
133 query = new Label(container, SWT.WRAP);
134 GridData gridData = new GridData();
135 gridData.horizontalSpan = layoutColumns;
136 gridData.horizontalAlignment = GridData.FILL;
137 gridData.verticalAlignment = GridData.FILL;
138 gridData.grabExcessHorizontalSpace = true;
139 gridData.grabExcessVerticalSpace = true;
140 query.setLayoutData(gridData);
142 setControl(container);
145 setPageComplete(true);
148 public void updateQuery() {
150 System.out.println("PHP SELECT");
152 StringBuffer setClause = new StringBuffer();
153 StringBuffer whereClause = new StringBuffer();
155 boolean firstClause = false;
156 for (int i = 0; i < columnNames.length; i++) {
157 if (primaryKeys[i].getSelection()) {
159 whereClause.append(" AND ");
162 whereClause.append(columnNames[i]);
163 whereClause.append(" = ");
164 temp = whereValues[i].getText();
165 // if (temp.charAt(0) == '$') {
166 // whereClause.append(temp);
168 whereClause.append("'" + temp + "'");
172 if (setValues[i].getSelection()) {
173 setClause.append(columnNames[i]);
174 // setClause.append(" = ");
175 // setClause.append(newValues[i].getText());
176 setClause.append(", ");
179 // if (whereClause.length() > 1) {
180 // whereClause.deleteCharAt(whereClause.length() - 1);
181 // whereClause.deleteCharAt(whereClause.length() - 1);
183 if (setClause.length() > 1) {
184 setClause.deleteCharAt(setClause.length() - 1);
185 setClause.deleteCharAt(setClause.length() - 1);
188 String[] arguments = { setClause.toString(), row.getTable(), whereClause.toString()};
189 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.select.template"));
191 String query = form.format(arguments);
192 // String query = "$results = mysql_query(\"SELECT " + setClause.toString();
193 // query += " FROM " + row.getTable();
194 // query += " WHERE " + whereClause.toString() + "\");";
195 this.query.setText(query);
198 public boolean performFinish() {
199 // PHPSourceConsole console = PHPSourceConsole.getInstance();
201 // console.print(query.getText());
202 QuantumPlugin.getDefault().getSysClip().setContents(
203 new Object[] { query.getText() },
204 new Transfer[] { TextTransfer.getInstance()});