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 DeleteRowPage extends WizardPage implements SQLPage {
String[] columnNames;
Text[] values;
Button[] whereValues;
- Label query;
+ Text query;
public DeleteRowPage(String pageName) {
super(pageName);
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);
- int layoutColumns = 3;
- layout.numColumns = layoutColumns;
+ layout.numColumns = 3;
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$
}
}
+ BookmarkNode bookmark = row.getBookmarkNode();
+ TreeNode node = bookmark.find(row.getTable());
+ ObjectMetaData metadata = null;
+ if (node != null)
+ metadata = node.getMetaData();
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$
}
}
+
values = new Text[row.getColumnCount()];
whereValues = new Button[row.getColumnCount()];
- Label temp = new Label(container, SWT.NULL);
- temp.setText("Column Name");
- temp = new Label(container, SWT.NULL);
- temp.setText("Value");
- temp = new Label(container, SWT.NULL);
- temp.setText("Include in?");
+ new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$
+ new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$
+ new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$
for (int i = 0; i < row.getColumnCount(); i++) {
Label label = new Label(container, SWT.NULL);
label.setText(columnNames[i]);
});
whereValues[i] = new Button(container, SWT.CHECK);
- whereValues[i].setText("Where clause");
+ whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$
+ // we check if it's a primary key to select it in the WHERE clause
+ if (metadata != null && metadata.getPrimaryKeyOrder(columnNames[i]) > 0)
+ whereValues[i].setSelection(true);
+ else
+ whereValues[i].setSelection(false);
whereValues[i].addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
}
});
}
- query = new Label(container, SWT.WRAP);
+ query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
GridData gridData = new GridData();
- gridData.horizontalSpan = layoutColumns;
+ gridData.horizontalSpan = layout.numColumns;
+ gridData.verticalSpan = 3;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
}
public void updateQuery() {
if (DEBUG) {
- System.out.println("Updating query");
+ System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
}
+
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 numSelected = 0;
for (int i = 0; i < columnNames.length; i++) {
if (whereValues[i].getSelection()) {
+ if (numSelected > 0)
+ whereClause.append(" AND "); //$NON-NLS-1$
numSelected++;
+ whereClause.append("("); //$NON-NLS-1$
whereClause.append(columnNames[i]);
- whereClause.append(" = ");
- whereClause.append(values[i].getText());
- whereClause.append(", ");
+ whereClause.append(" = "); //$NON-NLS-1$
+ if (adapter != null && metadata != null)
+ whereClause.append(adapter.quote(values[i].getText(), metadata.getColumnType(columnNames[i])));
+ else
+ whereClause.append(values[i].getText());
+
+ whereClause.append(")"); //$NON-NLS-1$
}
}
- if (whereClause.length() > 1) {
- whereClause.deleteCharAt(whereClause.length() - 1);
- whereClause.deleteCharAt(whereClause.length() - 1);
- }
- String query = "DELETE FROM " + row.getTable();
+ String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
if (numSelected > 0) {
- query += " WHERE " + whereClause.toString();
+ query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
}
if (numSelected > 0) {
- setMessage("");
+ setMessage(""); //$NON-NLS-1$
} else {
- setMessage("Warning: no \"where clause\" columns selected, all rows will be deleted");
+ setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
}
- this.getControl().pack();
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