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) {
39 System.out.println("page create control"); //$NON-NLS-1$
42 Composite container = new Composite(parent, SWT.NULL);
43 GridLayout layout = new GridLayout();
44 container.setLayout(layout);
45 layout.numColumns = 2;
49 System.out.println("Row is null"); //$NON-NLS-1$
51 if (row.getColumnNames() == null) {
52 System.out.println("Columns are null"); //$NON-NLS-1$
54 if (row.getTableData() == null) {
55 System.out.println("Data is null"); //$NON-NLS-1$
59 columnNames = row.getColumnNames();
60 String[] data = row.getTableData();
63 for (int i = 0; i < row.getColumnCount(); i++) {
64 System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
65 System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
69 values = new Text[row.getColumnCount()];
70 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.ColumnName")); //$NON-NLS-1$
71 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.Value")); //$NON-NLS-1$
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 = layout.numColumns;
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 query"); //$NON-NLS-1$
106 StringBuffer valuesClause = new StringBuffer();
107 BookmarkNode bookmark = row.getBookmarkNode();
108 TreeNode node = bookmark.find(row.getTable());
109 ObjectMetaData metadata = null;
111 metadata = node.getMetaData();
112 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
115 for (int i = 0; i < columnNames.length; i++) {
116 String value = values[i].getText();
118 valuesClause.append(", "); //$NON-NLS-1$
119 if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
120 valuesClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
122 valuesClause.append(value);
125 String query = "INSERT INTO " + row.getTable(); //$NON-NLS-1$
126 query += " VALUES (" + valuesClause; //$NON-NLS-1$
127 query += " )"; //$NON-NLS-1$
128 this.query.setText(query);
130 public boolean performFinish() {
131 MultiSQLServer server = MultiSQLServer.getInstance();
132 BookmarkView bookmarkView = BookmarkView.getInstance();
133 BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
134 server.execute(bookmark.getConnection(), query.getText());