added PHP code generators for UPDATE, INSERT, DELETE
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / PHPInsertRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import net.sourceforge.phpdt.sql.sql.TableRow;
4 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
5 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
6
7 import org.eclipse.jface.wizard.WizardPage;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.Text;
16
17 public class PHPInsertRowPage extends WizardPage implements SQLPage {
18   TableRow row;
19   String[] columnNames;
20   Text[] values;
21   Label query;
22   public PHPInsertRowPage(String pageName) {
23     super(pageName);
24   }
25
26   public void init(TableRow row, TableAdapter adapter) {
27     this.row = row;
28   }
29
30   public void createControl(Composite parent) {
31     if (DEBUG) {
32       System.out.println("page create control");
33     }
34     Composite container = new Composite(parent, SWT.NULL);
35     GridLayout layout = new GridLayout();
36     container.setLayout(layout);
37     int layoutColumns = 2;
38     layout.numColumns = layoutColumns;
39
40     if (DEBUG) {
41       if (row == null) {
42         System.out.println("Row is null");
43       }
44       if (row.getColumnNames() == null) {
45         System.out.println("Columns are null");
46       }
47       if (row.getTableData() == null) {
48         System.out.println("Data is null");
49       }
50     }
51     columnNames = row.getColumnNames();
52     String[] data = row.getTableData();
53     if (DEBUG) {
54       for (int i = 0; i < row.getColumnCount(); i++) {
55         System.out.println("data = " + i + "=" + data[i]);
56         System.out.println("column = " + i + "=" + columnNames[i]);
57       }
58     }
59     values = new Text[row.getColumnCount()];
60     Label temp = new Label(container, SWT.NULL);
61     temp.setText("Column Name");
62     temp = new Label(container, SWT.NULL);
63     temp.setText("Value");
64     for (int i = 0; i < row.getColumnCount(); i++) {
65       Label label = new Label(container, SWT.NULL);
66       label.setText(columnNames[i]);
67       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
68       GridData fullHorizontal = new GridData();
69       fullHorizontal.horizontalAlignment = GridData.FILL;
70       values[i].setLayoutData(fullHorizontal);
71
72       //values[i].setText(data[i]);
73       values[i].addModifyListener(new ModifyListener() {
74         public void modifyText(ModifyEvent e) {
75           updateQuery();
76         }
77       });
78     }
79     query = new Label(container, SWT.WRAP);
80     GridData gridData = new GridData();
81     gridData.horizontalSpan = layoutColumns;
82     gridData.horizontalAlignment = GridData.FILL;
83     gridData.verticalAlignment = GridData.FILL;
84     gridData.grabExcessHorizontalSpace = true;
85     gridData.grabExcessVerticalSpace = true;
86     query.setLayoutData(gridData);
87
88     setControl(container);
89     updateQuery();
90
91     setPageComplete(true);
92   }
93   public void updateQuery() {
94     if (DEBUG) {
95       System.out.println("Updating insert query");
96     }
97     StringBuffer fieldClause = new StringBuffer();
98
99     StringBuffer valuesClause = new StringBuffer();
100     String text;
101     boolean first = false;
102     for (int i = 0; i < columnNames.length; i++) {
103       text = values[i].getText();
104       if (! text.equals("")) {
105         if (first) {
106           valuesClause.append(", ");
107           fieldClause.append(", ");
108         }
109         valuesClause.append("'"+values[i].getText()+"'");
110         fieldClause.append(columnNames[i]);
111         first = true;
112       }
113     }
114 //    if (valuesClause.length() > 1) {
115 //      valuesClause.deleteCharAt(valuesClause.length() - 1);
116 //      valuesClause.deleteCharAt(valuesClause.length() - 1);
117 //    }
118     String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
119     query += fieldClause.toString() + ") ";
120     query += " VALUES (" + valuesClause.toString();
121     query += ")\");";
122     this.query.setText(query);
123   }
124   public boolean performFinish() {
125     PHPSourceConsole console = PHPSourceConsole.getInstance();
126     console.clear();
127     console.print(query.getText());
128     return true;
129   }
130 }