import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
+import net.sourceforge.phpdt.sql.Messages;
+import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
+import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
import net.sourceforge.phpdt.sql.sql.TableRow;
+import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
+import net.sourceforge.phpdt.sql.view.BookmarkView;
+import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
+import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
public class UpdateRowPage extends WizardPage implements SQLPage {
public void createControl(Composite parent) {
if (DEBUG) {
- System.out.println("page create control");
+ System.out.println("page create control"); //$NON-NLS-1$
}
+
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
+ BookmarkNode bookmark = row.getBookmarkNode();
+ TreeNode node = bookmark.find(row.getTable());
+ ObjectMetaData metadata = null;
+ if (node != null)
+ metadata = node.getMetaData();
+
int layoutColumns = 5;
layout.numColumns = layoutColumns;
if (DEBUG) {
if (row == null) {
- System.out.println("Row is null");
+ System.out.println("Row is null"); //$NON-NLS-1$
}
if (row.getColumnNames() == null) {
- System.out.println("Columns are null");
+ System.out.println("Columns are null"); //$NON-NLS-1$
}
if (row.getTableData() == null) {
- System.out.println("Data is null");
+ System.out.println("Data is null"); //$NON-NLS-1$
}
}
columnNames = row.getColumnNames();
String[] data = row.getTableData();
+
if (DEBUG) {
for (int i = 0; i < row.getColumnCount(); i++) {
- System.out.println("data = " + i + "=" + data[i]);
- System.out.println("column = " + i + "=" + columnNames[i]);
+ System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
+ System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
}
}
+
oldValues = new Text[row.getColumnCount()];
newValues = new Text[row.getColumnCount()];
primaryKeys = new Button[row.getColumnCount()];
setValues = new Button[row.getColumnCount()];
Label temp = new Label(container, SWT.NULL);
- temp.setText("Column Name");
+ temp.setText(Messages.getString("UpdateRowPage.ColumnName")); //$NON-NLS-1$
temp = new Label(container, SWT.NULL);
- temp.setText("Old Value");
+ temp.setText(Messages.getString("UpdateRowPage.OldValue")); //$NON-NLS-1$
temp = new Label(container, SWT.NULL);
- temp.setText("");
+ temp.setText(""); //$NON-NLS-1$
temp = new Label(container, SWT.NULL);
- temp.setText("New Value");
+ temp.setText(Messages.getString("UpdateRowPage.NewValue")); //$NON-NLS-1$
temp = new Label(container, SWT.NULL);
- temp.setText("");
+ temp.setText(Messages.getString("UpdateRowPage._13")); //$NON-NLS-1$
for (int i = 0; i < row.getColumnCount(); i++) {
Label label = new Label(container, SWT.NULL);
label.setText(columnNames[i]);
}
});
primaryKeys[i] = new Button(container, SWT.CHECK);
- primaryKeys[i].setText("Primary Key");
+ primaryKeys[i].setText("Where"); //$NON-NLS-1$
+ if (metadata != null && metadata.getPrimaryKeyOrder(columnNames[i]) > 0)
+ primaryKeys[i].setSelection(true);
primaryKeys[i].addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
}
});
setValues[i] = new Button(container, SWT.CHECK);
- setValues[i].setText("Set Value");
+ setValues[i].setText(Messages.getString("UpdateRowPage.SetValue")); //$NON-NLS-1$
setValues[i].addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
}
public void updateQuery() {
if (DEBUG) {
- System.out.println("Updating query");
+ System.out.println("Updating query"); //$NON-NLS-1$
}
+
StringBuffer setClause = new StringBuffer();
StringBuffer whereClause = new StringBuffer();
+ BookmarkNode bookmark = row.getBookmarkNode();
+ TreeNode node = bookmark.find(row.getTable());
+ ObjectMetaData metadata = null;
+ if (node != null)
+ metadata = node.getMetaData();
+ DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
+
+ int numValuesSet = 0;
+ int numValuesWhere = 0;
for (int i = 0; i < columnNames.length; i++) {
if (primaryKeys[i].getSelection()) {
+ String value = oldValues[i].getText();
+ if (numValuesWhere > 0)
+ whereClause.append(" AND "); //$NON-NLS-1$
+ whereClause.append("("); //$NON-NLS-1$
whereClause.append(columnNames[i]);
- whereClause.append(" = ");
- whereClause.append(oldValues[i].getText());
- whereClause.append(", ");
+ whereClause.append(" = "); //$NON-NLS-1$
+ if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
+ whereClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
+ else
+ whereClause.append(value);
+ whereClause.append(")"); //$NON-NLS-1$
+ numValuesWhere++;
}
if (setValues[i].getSelection()) {
+ String value = newValues[i].getText();
+ if (numValuesSet > 0)
+ setClause.append(", "); //$NON-NLS-1$
setClause.append(columnNames[i]);
- setClause.append(" = ");
- setClause.append(newValues[i].getText());
- setClause.append(", ");
+ setClause.append(" = "); //$NON-NLS-1$
+ if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
+ setClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
+ else
+ setClause.append(value);
+ numValuesSet++;
+
}
}
- if (whereClause.length() > 1) {
- whereClause.deleteCharAt(whereClause.length() - 1);
- whereClause.deleteCharAt(whereClause.length() - 1);
- }
- if (setClause.length() > 1) {
- setClause.deleteCharAt(setClause.length() - 1);
- setClause.deleteCharAt(setClause.length() - 1);
- }
- String query = "UPDATE " + row.getTable();
- query += " SET " + setClause.toString();
- query += " WHERE " + whereClause.toString();
+ String query = "UPDATE " + row.getTable(); //$NON-NLS-1$
+ query += " SET " + setClause.toString(); //$NON-NLS-1$
+ query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
this.query.setText(query);
}
public boolean performFinish() {
MultiSQLServer server = MultiSQLServer.getInstance();
- server.execute(query.getText());
+ BookmarkView bookmarkView = BookmarkView.getInstance();
+ BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
+ server.execute(bookmark.getConnection(), query.getText());
return true;
}
}
\ No newline at end of file