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.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
21 public class PHPInsertRowPage extends WizardPage implements SQLPage {
26 private IPreferenceStore fStore;
28 public PHPInsertRowPage(String pageName) {
32 public void init(TableRow row, TableAdapter adapter) {
36 public void createControl(Composite parent) {
38 System.out.println("page create control");
40 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
41 Composite container = new Composite(parent, SWT.NULL);
42 GridLayout layout = new GridLayout();
43 container.setLayout(layout);
44 int layoutColumns = 2;
45 layout.numColumns = layoutColumns;
49 System.out.println("Row is null");
51 if (row.getColumnNames() == null) {
52 System.out.println("Columns are null");
54 if (row.getTableData() == null) {
55 System.out.println("Data is null");
58 columnNames = row.getColumnNames();
59 String[] data = row.getTableData();
61 for (int i = 0; i < row.getColumnCount(); i++) {
62 System.out.println("data = " + i + "=" + data[i]);
63 System.out.println("column = " + i + "=" + columnNames[i]);
66 values = new Text[row.getColumnCount()];
67 Label temp = new Label(container, SWT.NULL);
68 temp.setText("Column Name");
69 temp = new Label(container, SWT.NULL);
70 temp.setText("Value");
71 for (int i = 0; i < row.getColumnCount(); i++) {
72 Label label = new Label(container, SWT.NULL);
73 label.setText(columnNames[i]);
74 values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
75 GridData fullHorizontal = new GridData();
76 fullHorizontal.horizontalAlignment = GridData.FILL;
77 values[i].setLayoutData(fullHorizontal);
79 //values[i].setText(data[i]);
80 values[i].addModifyListener(new ModifyListener() {
81 public void modifyText(ModifyEvent e) {
86 query = new Label(container, SWT.WRAP);
87 GridData gridData = new GridData();
88 gridData.horizontalSpan = layoutColumns;
89 gridData.horizontalAlignment = GridData.FILL;
90 gridData.verticalAlignment = GridData.FILL;
91 gridData.grabExcessHorizontalSpace = true;
92 gridData.grabExcessVerticalSpace = true;
93 query.setLayoutData(gridData);
95 setControl(container);
98 setPageComplete(true);
100 public void updateQuery() {
102 System.out.println("Updating insert query");
104 StringBuffer fieldClause = new StringBuffer();
106 StringBuffer valuesClause = new StringBuffer();
108 boolean first = false;
109 for (int i = 0; i < columnNames.length; i++) {
110 text = values[i].getText();
111 if (! text.equals("")) {
113 valuesClause.append(", ");
114 fieldClause.append(", ");
116 valuesClause.append("'"+values[i].getText()+"'");
117 fieldClause.append(columnNames[i]);
121 // if (valuesClause.length() > 1) {
122 // valuesClause.deleteCharAt(valuesClause.length() - 1);
123 // valuesClause.deleteCharAt(valuesClause.length() - 1);
125 String[] arguments = { row.getTable(), fieldClause.toString(), valuesClause.toString() };
126 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template"));
128 String query = form.format(arguments);
130 // String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
131 // query += fieldClause.toString() + ") ";
132 // query += " VALUES (" + valuesClause.toString();
134 this.query.setText(query);
136 public boolean performFinish() {
137 PHPSourceConsole console = PHPSourceConsole.getInstance();
139 console.print(query.getText());