some little bugfixes
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / InsertRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import org.eclipse.jface.wizard.WizardPage;
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.ModifyEvent;
6 import org.eclipse.swt.events.ModifyListener;
7 import org.eclipse.swt.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.Text;
12
13 import net.sourceforge.phpdt.sql.Messages;
14 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
15 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
16 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
19 import net.sourceforge.phpdt.sql.view.BookmarkView;
20 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
21 import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
22 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
23
24 public class InsertRowPage extends WizardPage implements SQLPage {
25         TableRow row;
26         String[] columnNames;
27         Text[] values;
28         Label query;
29         public InsertRowPage(String pageName) {
30                 super(pageName);
31         }
32
33         public void init(TableRow row, TableAdapter adapter) {
34                 this.row = row;
35         }
36
37         public void createControl(Composite parent) {
38                 System.out.println("page create control"); //$NON-NLS-1$
39                 Composite container = new Composite(parent, SWT.NULL);
40                 GridLayout layout = new GridLayout();
41                 container.setLayout(layout);
42                 layout.numColumns = 2;
43
44                 if (row == null) {
45                         System.out.println("Row is null"); //$NON-NLS-1$
46                 }
47                 if (row.getColumnNames() == null) {
48                         System.out.println("Columns are null"); //$NON-NLS-1$
49                 }
50                 if (row.getTableData() == null) {
51                         System.out.println("Data is null"); //$NON-NLS-1$
52                 }
53                 columnNames = row.getColumnNames();
54                 String[] data = row.getTableData();
55                 for (int i = 0; i < row.getColumnCount(); i++) {
56                         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
57                         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
58                 }
59                 values = new Text[row.getColumnCount()];
60                 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.ColumnName")); //$NON-NLS-1$
61                 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.Value")); //$NON-NLS-1$
62                 for (int i = 0; i < row.getColumnCount(); i++) {
63                         Label label = new Label(container, SWT.NULL);
64                         label.setText(columnNames[i]);
65                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
66                         GridData fullHorizontal = new GridData();
67                         fullHorizontal.horizontalAlignment = GridData.FILL;
68                         values[i].setLayoutData(fullHorizontal);
69
70                         //values[i].setText(data[i]);
71                         values[i].addModifyListener(new ModifyListener() {
72                                 public void modifyText(ModifyEvent e) {
73                                         updateQuery();
74                                 }                               
75                         });
76                 }
77                 query = new Label(container, SWT.WRAP);
78                 GridData gridData = new GridData();
79                 gridData.horizontalSpan = layout.numColumns;
80                 gridData.horizontalAlignment = GridData.FILL;
81                 gridData.verticalAlignment = GridData.FILL;
82                 gridData.grabExcessHorizontalSpace = true;
83                 gridData.grabExcessVerticalSpace = true;
84                 query.setLayoutData(gridData);
85
86                 setControl(container);
87         updateQuery();
88        
89                 setPageComplete(true);
90         }
91         public void updateQuery() {
92                 System.out.println("Updating query"); //$NON-NLS-1$
93                 StringBuffer valuesClause = new StringBuffer();
94                 BookmarkNode bookmark = row.getBookmarkNode();
95                 TreeNode node = bookmark.find(row.getTable());
96                 ObjectMetaData metadata = null;
97                 if (node != null) metadata = node.getMetaData(); 
98                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
99                 
100                 int numValues = 0;
101                 for (int i = 0; i < columnNames.length; i++) {
102                         String value = values[i].getText();
103                         if (numValues > 0) valuesClause.append(", "); //$NON-NLS-1$
104                         if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
105                                 valuesClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
106                         else
107                                 valuesClause.append(value);     
108                         numValues++;
109                 }
110                 String query = "INSERT INTO " + row.getTable(); //$NON-NLS-1$
111                 query += " VALUES (" + valuesClause; //$NON-NLS-1$
112                 query += " )"; //$NON-NLS-1$
113                 this.query.setText(query);
114         }
115         public boolean performFinish() {
116            MultiSQLServer server = MultiSQLServer.getInstance();
117            BookmarkView bookmarkView = BookmarkView.getInstance(); 
118            BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
119            server.execute(bookmark.getConnection(), query.getText());
120            return true;
121         }
122 }