Search in sources :

Example 1 with PostgreForeignTableManager

use of org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager 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;
}
Also used : SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) PostgreForeignTableManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) PostgreTableColumnManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager) PropertySourceCustom(org.jkiss.dbeaver.runtime.properties.PropertySourceCustom) SimpleCommandContext(org.jkiss.dbeaver.ui.editors.SimpleCommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 2 with PostgreForeignTableManager

use of org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager 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;
}
Also used : SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) PostgreForeignTableManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) PostgreTableColumnManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager) PropertySourceCustom(org.jkiss.dbeaver.runtime.properties.PropertySourceCustom) SimpleCommandContext(org.jkiss.dbeaver.ui.editors.SimpleCommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

PostgreForeignTableManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager)2 PostgreTableColumnManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager)2 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)2 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)2 SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)2 SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)2 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)2 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)2 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)2 PropertySourceCustom (org.jkiss.dbeaver.runtime.properties.PropertySourceCustom)2 SimpleCommandContext (org.jkiss.dbeaver.ui.editors.SimpleCommandContext)2