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.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
16 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
19 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
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");
59 columnNames = row.getColumnNames();
60 String[] data = row.getTableData();
62 for (int i = 0; i < row.getColumnCount(); i++) {
63 System.out.println("data = " + i + "=" + data[i]);
64 System.out.println("column = " + i + "=" + columnNames[i]);
67 values = new Text[row.getColumnCount()];
68 Label temp = new Label(container, SWT.NULL);
69 temp.setText("Column Name");
70 temp = new Label(container, SWT.NULL);
71 temp.setText("Value");
72 for (int i = 0; i < row.getColumnCount(); i++) {
73 Label label = new Label(container, SWT.NULL);
74 label.setText(columnNames[i]);
75 values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
76 GridData fullHorizontal = new GridData();
77 fullHorizontal.horizontalAlignment = GridData.FILL;
78 values[i].setLayoutData(fullHorizontal);
80 //values[i].setText(data[i]);
81 values[i].addModifyListener(new ModifyListener() {
82 public void modifyText(ModifyEvent e) {
87 query = new Label(container, SWT.WRAP);
88 GridData gridData = new GridData();
89 gridData.horizontalSpan = layoutColumns;
90 gridData.horizontalAlignment = GridData.FILL;
91 gridData.verticalAlignment = GridData.FILL;
92 gridData.grabExcessHorizontalSpace = true;
93 gridData.grabExcessVerticalSpace = true;
94 query.setLayoutData(gridData);
96 setControl(container);
99 setPageComplete(true);
101 public void updateQuery() {
103 System.out.println("Updating insert query");
105 StringBuffer fieldClause = new StringBuffer();
107 StringBuffer valuesClause = new StringBuffer();
109 boolean first = false;
110 for (int i = 0; i < columnNames.length; i++) {
111 text = values[i].getText();
112 if (!text.equals("")) {
114 valuesClause.append(", ");
115 fieldClause.append(", ");
117 valuesClause.append("'" + values[i].getText() + "'");
118 fieldClause.append(columnNames[i]);
122 // if (valuesClause.length() > 1) {
123 // valuesClause.deleteCharAt(valuesClause.length() - 1);
124 // valuesClause.deleteCharAt(valuesClause.length() - 1);
126 String[] arguments = { row.getTable(), fieldClause.toString(), valuesClause.toString()};
127 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template"));
129 String query = form.format(arguments);
131 // String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
132 // query += fieldClause.toString() + ") ";
133 // query += " VALUES (" + valuesClause.toString();
135 this.query.setText(query);
137 public boolean performFinish() {
138 PHPSourceConsole console = PHPSourceConsole.getInstance();
140 console.print(query.getText());