1 package net.sourceforge.phpdt.sql.wizards;
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;
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;
24 public class InsertRowPage extends WizardPage implements SQLPage {
29 public InsertRowPage(String pageName) {
33 public void init(TableRow row, TableAdapter adapter) {
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;
45 System.out.println("Row is null"); //$NON-NLS-1$
47 if (row.getColumnNames() == null) {
48 System.out.println("Columns are null"); //$NON-NLS-1$
50 if (row.getTableData() == null) {
51 System.out.println("Data is null"); //$NON-NLS-1$
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$
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);
70 //values[i].setText(data[i]);
71 values[i].addModifyListener(new ModifyListener() {
72 public void modifyText(ModifyEvent e) {
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);
86 setControl(container);
89 setPageComplete(true);
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());
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])));
107 valuesClause.append(value);
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);
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());