use of org.jkiss.dbeaver.ui.editors.SimpleCommandContext in project dbeaver by dbeaver.
the class NavigatorHandlerObjectBase method getCommandTarget.
protected static CommandTarget getCommandTarget(IWorkbenchWindow workbenchWindow, DBNNode container, Class<?> childType, boolean openEditor) throws DBException {
final Object parentObject = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getValueObject() : null;
DBSObject objectToSeek = null;
if (parentObject instanceof DBSObject) {
final DBEStructEditor parentStructEditor = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(parentObject.getClass(), DBEStructEditor.class);
if (parentStructEditor != null && RuntimeUtils.isTypeSupported(childType, parentStructEditor.getChildTypes())) {
objectToSeek = (DBSObject) parentObject;
}
}
if (objectToSeek != null) {
for (final IEditorReference editorRef : workbenchWindow.getActivePage().getEditorReferences()) {
final IEditorPart editor = editorRef.getEditor(false);
if (editor instanceof IDatabaseEditor) {
final IDatabaseEditorInput editorInput = (IDatabaseEditorInput) editor.getEditorInput();
if (editorInput.getDatabaseObject() == objectToSeek) {
workbenchWindow.getActivePage().activate(editor);
switchEditorFolder(container, editor);
return new CommandTarget((IDatabaseEditor) editor);
}
}
}
if (openEditor && container instanceof DBNDatabaseNode) {
final IDatabaseEditor editor = (IDatabaseEditor) NavigatorHandlerObjectOpen.openEntityEditor((DBNDatabaseNode) container, null, workbenchWindow);
if (editor != null) {
switchEditorFolder(container, editor);
return new CommandTarget(editor);
}
}
}
if (container instanceof DBNDatabaseNode) {
// No editor found - create new command context
DBSObject object = ((DBNDatabaseNode) container).getObject();
if (object != null) {
return new CommandTarget(new SimpleCommandContext(DBUtils.getDefaultContext(object, false), !openEditor));
}
}
return new CommandTarget();
}
use of org.jkiss.dbeaver.ui.editors.SimpleCommandContext in project dbeaver by dbeaver.
the class PostgreFDWConfigWizard method generateScript.
List<DBEPersistAction> generateScript(DBRProgressMonitor monitor) throws DBException {
PostgreDatabase database = getDatabase();
PostgreDataSource curDataSource = database.getDataSource();
List<DBEPersistAction> actions = new ArrayList<>();
PostgreFDWConfigWizard.FDWInfo selectedFDW = getSelectedFDW();
PropertySourceCustom propertySource = getFdwPropertySource();
Map<String, Object> propValues = propertySource.getPropertiesWithDefaults();
String serverId = getFdwServerId();
actions.add(new SQLDatabasePersistActionComment(curDataSource, "CREATE EXTENSION " + selectedFDW.getId()));
{
StringBuilder script = new StringBuilder();
script.append("CREATE SERVER ").append(serverId).append("\n\tFOREIGN DATA WRAPPER ").append(selectedFDW.getId()).append("\n\tOPTIONS(");
boolean firstProp = true;
for (Map.Entry<String, Object> pe : propValues.entrySet()) {
String propName = CommonUtils.toString(pe.getKey());
String propValue = CommonUtils.toString(pe.getValue());
if (CommonUtils.isEmpty(propName) || CommonUtils.isEmpty(propValue)) {
continue;
}
if (!firstProp)
script.append(", ");
script.append(propName).append(" '").append(propValue).append("'");
firstProp = false;
}
script.append(")");
actions.add(new SQLDatabasePersistAction("Create extension", script.toString()));
}
actions.add(new SQLDatabasePersistAction("CREATE USER MAPPING FOR CURRENT_USER SERVER " + serverId));
// Now tables
DBECommandContext commandContext = new SimpleCommandContext(getExecutionContext(), false);
try {
PostgreFDWConfigWizard.FDWInfo fdwInfo = getSelectedFDW();
Map<String, Object> options = new HashMap<>();
options.put(SQLObjectEditor.OPTION_SKIP_CONFIGURATION, true);
PostgreForeignTableManager tableManager = new PostgreForeignTableManager();
PostgreTableColumnManager columnManager = new PostgreTableColumnManager();
for (DBNDatabaseNode tableNode : getSelectedEntities()) {
DBSEntity entity = (DBSEntity) tableNode.getObject();
PostgreTableForeign pgTable = (PostgreTableForeign) tableManager.createNewObject(monitor, commandContext, getSelectedSchema(), null, options);
if (pgTable == null) {
log.error("Internal error while creating new table");
continue;
}
pgTable.setName(entity.getName());
pgTable.setForeignServerName(serverId);
pgTable.setForeignOptions(new String[0]);
for (DBSEntityAttribute attr : CommonUtils.safeCollection(entity.getAttributes(monitor))) {
// Cache data types
PostgreSchema catalogSchema = database.getCatalogSchema(monitor);
if (catalogSchema != null) {
catalogSchema.getDataTypes(monitor);
}
String defTypeName = DBStructUtils.mapTargetDataType(database, attr, true);
String plainTargetTypeName = SQLUtils.stripColumnTypeModifiers(defTypeName);
PostgreDataType dataType = database.getDataType(monitor, plainTargetTypeName);
if (dataType == null) {
log.error("Data type '" + plainTargetTypeName + "' not found. Skip column mapping.");
continue;
}
PostgreTableColumn newColumn = columnManager.createNewObject(monitor, commandContext, pgTable, null, options);
assert newColumn != null;
newColumn.setName(attr.getName());
newColumn.setDataType(dataType);
}
DBEPersistAction[] tableDDL = tableManager.getTableDDL(monitor, pgTable, options);
Collections.addAll(actions, tableDDL);
}
} finally {
commandContext.resetChanges(true);
}
// CREATE SERVER clickhouse_svr FOREIGN DATA WRAPPER clickhousedb_fdw OPTIONS(dbname 'default', driver '/usr/local/lib/odbc/libclickhouseodbc.so', host '46.101.202.143');
return actions;
}
use of org.jkiss.dbeaver.ui.editors.SimpleCommandContext in project dbeaver by serge-rider.
the class PostgreFDWConfigWizard method generateScript.
List<DBEPersistAction> generateScript(DBRProgressMonitor monitor) throws DBException {
PostgreDatabase database = getDatabase();
PostgreDataSource curDataSource = database.getDataSource();
List<DBEPersistAction> actions = new ArrayList<>();
PostgreFDWConfigWizard.FDWInfo selectedFDW = getSelectedFDW();
PropertySourceCustom propertySource = getFdwPropertySource();
Map<String, Object> propValues = propertySource.getPropertiesWithDefaults();
String serverId = getFdwServerId();
actions.add(new SQLDatabasePersistActionComment(curDataSource, "CREATE EXTENSION " + selectedFDW.getId()));
{
StringBuilder script = new StringBuilder();
script.append("CREATE SERVER ").append(serverId).append("\n\tFOREIGN DATA WRAPPER ").append(selectedFDW.getId()).append("\n\tOPTIONS(");
boolean firstProp = true;
for (Map.Entry<String, Object> pe : propValues.entrySet()) {
String propName = CommonUtils.toString(pe.getKey());
String propValue = CommonUtils.toString(pe.getValue());
if (CommonUtils.isEmpty(propName) || CommonUtils.isEmpty(propValue)) {
continue;
}
if (!firstProp)
script.append(", ");
script.append(propName).append(" '").append(propValue).append("'");
firstProp = false;
}
script.append(")");
actions.add(new SQLDatabasePersistAction("Create extension", script.toString()));
}
actions.add(new SQLDatabasePersistAction("CREATE USER MAPPING FOR CURRENT_USER SERVER " + serverId));
// Now tables
DBECommandContext commandContext = new SimpleCommandContext(getExecutionContext(), false);
try {
PostgreFDWConfigWizard.FDWInfo fdwInfo = getSelectedFDW();
Map<String, Object> options = new HashMap<>();
options.put(SQLObjectEditor.OPTION_SKIP_CONFIGURATION, true);
PostgreForeignTableManager tableManager = new PostgreForeignTableManager();
PostgreTableColumnManager columnManager = new PostgreTableColumnManager();
for (DBNDatabaseNode tableNode : getSelectedEntities()) {
DBSEntity entity = (DBSEntity) tableNode.getObject();
PostgreTableForeign pgTable = (PostgreTableForeign) tableManager.createNewObject(monitor, commandContext, getSelectedSchema(), null, options);
if (pgTable == null) {
log.error("Internal error while creating new table");
continue;
}
pgTable.setName(entity.getName());
pgTable.setForeignServerName(serverId);
pgTable.setForeignOptions(new String[0]);
for (DBSEntityAttribute attr : CommonUtils.safeCollection(entity.getAttributes(monitor))) {
// Cache data types
PostgreSchema catalogSchema = database.getCatalogSchema(monitor);
if (catalogSchema != null) {
catalogSchema.getDataTypes(monitor);
}
String defTypeName = DBStructUtils.mapTargetDataType(database, attr, true);
String plainTargetTypeName = SQLUtils.stripColumnTypeModifiers(defTypeName);
PostgreDataType dataType = database.getDataType(monitor, plainTargetTypeName);
if (dataType == null) {
log.error("Data type '" + plainTargetTypeName + "' not found. Skip column mapping.");
continue;
}
PostgreTableColumn newColumn = columnManager.createNewObject(monitor, commandContext, pgTable, null, options);
assert newColumn != null;
newColumn.setName(attr.getName());
newColumn.setDataType(dataType);
}
DBEPersistAction[] tableDDL = tableManager.getTableDDL(monitor, pgTable, options);
Collections.addAll(actions, tableDDL);
}
} finally {
commandContext.resetChanges(true);
}
// CREATE SERVER clickhouse_svr FOREIGN DATA WRAPPER clickhousedb_fdw OPTIONS(dbname 'default', driver '/usr/local/lib/odbc/libclickhouseodbc.so', host '46.101.202.143');
return actions;
}
use of org.jkiss.dbeaver.ui.editors.SimpleCommandContext in project dbeaver by serge-rider.
the class NavigatorHandlerObjectBase method getCommandTarget.
protected static CommandTarget getCommandTarget(IWorkbenchWindow workbenchWindow, DBNNode container, Class<?> childType, boolean openEditor) throws DBException {
final Object parentObject = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getValueObject() : null;
DBSObject objectToSeek = null;
if (parentObject instanceof DBSObject) {
final DBEStructEditor parentStructEditor = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(parentObject.getClass(), DBEStructEditor.class);
if (parentStructEditor != null && RuntimeUtils.isTypeSupported(childType, parentStructEditor.getChildTypes())) {
objectToSeek = (DBSObject) parentObject;
}
}
if (objectToSeek != null) {
for (final IEditorReference editorRef : workbenchWindow.getActivePage().getEditorReferences()) {
final IEditorPart editor = editorRef.getEditor(false);
if (editor instanceof IDatabaseEditor) {
final IDatabaseEditorInput editorInput = (IDatabaseEditorInput) editor.getEditorInput();
if (editorInput.getDatabaseObject() == objectToSeek) {
workbenchWindow.getActivePage().activate(editor);
switchEditorFolder(container, editor);
return new CommandTarget((IDatabaseEditor) editor);
}
}
}
if (openEditor && container instanceof DBNDatabaseNode) {
final IDatabaseEditor editor = (IDatabaseEditor) NavigatorHandlerObjectOpen.openEntityEditor((DBNDatabaseNode) container, null, workbenchWindow);
if (editor != null) {
switchEditorFolder(container, editor);
return new CommandTarget(editor);
}
}
}
if (container instanceof DBNDatabaseNode) {
// No editor found - create new command context
DBSObject object = ((DBNDatabaseNode) container).getObject();
if (object != null) {
return new CommandTarget(new SimpleCommandContext(DBUtils.getDefaultContext(object, false), !openEditor));
}
}
return new CommandTarget();
}
Aggregations