1 package net.sourceforge.phpdt.sql.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 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;
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");
66 columnNames = row.getColumnNames();
67 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]);
74 whereValues = new Text[row.getColumnCount()];
75 // newValues = new Text[row.getColumnCount()];
76 primaryKeys = new Button[row.getColumnCount()];
77 setValues = new Button[row.getColumnCount()];
78 Label temp = new Label(container, SWT.NULL);
79 temp.setText("Column Name");
80 temp = new Label(container, SWT.NULL);
81 temp.setText("Value");
82 temp = new Label(container, SWT.NULL);
83 temp.setText("Where");
84 // temp = new Label(container, SWT.NULL);
85 // temp.setText("New Value");
86 temp = new Label(container, SWT.NULL);
87 temp.setText("Select");
88 for (int i = 0; i < row.getColumnCount(); i++) {
89 Label label = new Label(container, SWT.NULL);
90 label.setText(columnNames[i]);
91 whereValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
92 if (data[i] == null || data[i].equals("")) {
93 whereValues[i].setText('$' + columnNames[i]);
95 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());