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]);
98 whereValues[i].addModifyListener(new ModifyListener() {
99 public void modifyText(ModifyEvent e) {
104 primaryKeys[i] = new Button(container, SWT.CHECK);
105 //primaryKeys[i].setText("Where Clause");
106 primaryKeys[i].addSelectionListener(new SelectionListener() {
107 public void widgetDefaultSelected(SelectionEvent e) {
109 public void widgetSelected(SelectionEvent e) {
113 // newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
114 // newValues[i].setText(data[i]);
115 // newValues[i].addModifyListener(new ModifyListener() {
116 // public void modifyText(ModifyEvent e) {
120 setValues[i] = new Button(container, SWT.CHECK);
121 //setValues[i].setText("Select Value");
122 setValues[i].addSelectionListener(new SelectionListener() {
123 public void widgetDefaultSelected(SelectionEvent e) {
125 public void widgetSelected(SelectionEvent e) {
130 query = new Label(container, SWT.WRAP);
131 GridData gridData = new GridData();
132 gridData.horizontalSpan = layoutColumns;
133 gridData.horizontalAlignment = GridData.FILL;
134 gridData.verticalAlignment = GridData.FILL;
135 gridData.grabExcessHorizontalSpace = true;
136 gridData.grabExcessVerticalSpace = true;
137 query.setLayoutData(gridData);
139 setControl(container);
142 setPageComplete(true);
145 public void updateQuery() {
147 System.out.println("PHP SELECT");
149 StringBuffer setClause = new StringBuffer();
150 StringBuffer whereClause = new StringBuffer();
152 boolean firstClause = false;
153 for (int i = 0; i < columnNames.length; i++) {
154 if (primaryKeys[i].getSelection()) {
156 whereClause.append(" AND ");
159 whereClause.append(columnNames[i]);
160 whereClause.append(" = ");
161 temp = whereValues[i].getText();
162 // if (temp.charAt(0) == '$') {
163 // whereClause.append(temp);
165 whereClause.append("'" + temp + "'");
169 if (setValues[i].getSelection()) {
170 setClause.append(columnNames[i]);
171 // setClause.append(" = ");
172 // setClause.append(newValues[i].getText());
173 setClause.append(", ");
176 // if (whereClause.length() > 1) {
177 // whereClause.deleteCharAt(whereClause.length() - 1);
178 // whereClause.deleteCharAt(whereClause.length() - 1);
180 if (setClause.length() > 1) {
181 setClause.deleteCharAt(setClause.length() - 1);
182 setClause.deleteCharAt(setClause.length() - 1);
185 String[] arguments = { setClause.toString(), row.getTable(), whereClause.toString() };
186 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.select.template"));
188 String query = form.format(arguments);
189 // String query = "$results = mysql_query(\"SELECT " + setClause.toString();
190 // query += " FROM " + row.getTable();
191 // query += " WHERE " + whereClause.toString() + "\");";
192 this.query.setText(query);
195 public boolean performFinish() {
196 PHPSourceConsole console = PHPSourceConsole.getInstance();
198 console.print(query.getText());