提交 3b2fc250 编写于 作者: J jurgen

Generate SQL for tables and RS

Former-commit-id: e3d00af6
上级 a99563c0
......@@ -4310,12 +4310,22 @@ public abstract class LightGrid extends Canvas {
*
* @return an array representing the cell selection
*/
public Collection<GridPos> getCellSelection()
public Collection<GridPos> getSelection()
{
checkWidget();
if (isDisposed()) {
return Collections.emptyList();
}
return Collections.unmodifiableCollection(selectedCells);
}
/**
* Returns selected rows indexes
* @return
*/
public Collection<Integer> getRowSelection()
{
return Collections.unmodifiableCollection(selectedRows.keySet());
}
private void getCells(GridColumn col, List<GridPos> cells)
{
......
......@@ -21,6 +21,8 @@ package org.jkiss.dbeaver.ui.controls.resultset;
import org.eclipse.jface.viewers.IStructuredSelection;
import java.util.Collection;
/**
* Result set selection
*/
......@@ -28,4 +30,6 @@ public interface ResultSetSelection extends IStructuredSelection {
ResultSetViewer getResultSetViewer();
Collection<Object[]> getSelectedRows();
}
......@@ -329,6 +329,24 @@ public class ResultSetViewer extends Viewer implements IDataSourceProvider, ISpr
{
return ResultSetViewer.this;
}
@Override
public Collection<Object[]> getSelectedRows()
{
if (mode == ResultSetMode.RECORD) {
if (curRowNum < 0 || curRowNum >= curRows.size()) {
return Collections.emptyList();
}
return Collections.singletonList(curRows.get(curRowNum));
} else {
Collection<Integer> rowSelection = spreadsheet.getRowSelection();
List<Object[]> data = new ArrayList<Object[]>(rowSelection.size());
for (Integer row : rowSelection) {
data.add(curRows.get(row));
}
return data;
}
}
};
}
......
......@@ -224,16 +224,11 @@ public class Spreadsheet extends LightGrid implements Listener {
super.redraw();
}
public Collection<GridPos> getSelection()
{
if (isDisposed()) {
return Collections.emptyList();
}
return super.getCellSelection();
}
public int getCurrentRow()
{
if (super.isDisposed()) {
return -1;
}
return super.getFocusItem();
}
......
......@@ -50,6 +50,7 @@ import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.ui.DBIcon;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.controls.resultset.ResultSetSelection;
import org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer;
import org.jkiss.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
......@@ -74,6 +75,33 @@ public class GenerateSQLContributor extends CompoundContributionItem {
List<IContributionItem> menu = new ArrayList<IContributionItem>();
if (structuredSelection instanceof ResultSetSelection) {
// Results
ResultSetSelection rss = (ResultSetSelection) structuredSelection;
ResultSetViewer rsv = rss.getResultSetViewer();
DBSTable table = (DBSTable)rsv.getDataContainer();
menu.add(makeAction("SELECT by Unique Key", new TableAnalysisRunner(table) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql) throws DBException
{
Collection<? extends DBSEntityAttribute> keyAttributes = getKeyAttributes(monitor);
sql.append("SELECT ");
boolean hasAttr = false;
for (DBSEntityAttribute attr : getValueAttributes(monitor, keyAttributes)) {
if (hasAttr) sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr));
hasAttr = true;
}
sql.append("\nFROM ").append(DBUtils.getObjectFullName(table));
sql.append("\nWHERE ");
hasAttr = false;
for (DBSEntityAttribute attr : keyAttributes) {
if (hasAttr) sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(attr)).append("=").append("''");
hasAttr = true;
}
sql.append(";\n");
}
}));
} else {
final DBSTable table =
(DBSTable) ((DBNDatabaseNode)RuntimeUtils.getObjectAdapter(structuredSelection.getFirstElement(), DBNNode.class)).getObject();
......@@ -84,7 +112,7 @@ public class GenerateSQLContributor extends CompoundContributionItem {
{
sql.append("SELECT ");
boolean hasAttr = false;
for (DBSEntityAttribute attr : CommonUtils.safeCollection(table.getAttributes(monitor))) {
for (DBSEntityAttribute attr : getAllAttributes(monitor)) {
if (hasAttr) sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr));
hasAttr = true;
......
......@@ -55,7 +55,7 @@ public class SQLUtilsPropertyTester extends PropertyTester
if (property.equals(PROP_CAN_GENERATE)) {
if (structuredSelection instanceof ResultSetSelection) {
// Results
return true;
return (((ResultSetSelection) structuredSelection).getResultSetViewer()).getDataContainer() instanceof DBSTable;
} else {
// Table
if (structuredSelection.size() != 1) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册