Search in sources :

Example 1 with DBERegistry

use of org.jkiss.dbeaver.model.edit.DBERegistry in project dbeaver by serge-rider.

the class PostgreUtils method getViewDDL.

public static String getViewDDL(DBRProgressMonitor monitor, PostgreViewBase view, String definition) throws DBException {
    // In some cases view definition already has view header (e.g. Redshift + with no schema binding)
    if (definition.toLowerCase(Locale.ENGLISH).startsWith("create ")) {
        return definition;
    }
    StringBuilder sql = new StringBuilder(view instanceof PostgreView ? "CREATE OR REPLACE " : "CREATE ");
    sql.append(view.getViewType()).append(" ").append(view.getFullyQualifiedName(DBPEvaluationContext.DDL));
    final DBERegistry editorsRegistry = view.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    final PostgreViewManager entityEditor = editorsRegistry.getObjectManager(view.getClass(), PostgreViewManager.class);
    if (entityEditor != null) {
        entityEditor.appendViewDeclarationPrefix(monitor, sql, view);
    }
    definition = definition.trim();
    while (definition.endsWith(";")) {
        definition = definition.substring(0, definition.length() - 1);
    }
    sql.append("\nAS ").append(definition);
    if (entityEditor != null) {
        entityEditor.appendViewDeclarationPostfix(monitor, sql, view);
    }
    sql.append(";");
    return sql.toString();
}
Also used : PostgreViewManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreViewManager) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry)

Example 2 with DBERegistry

use of org.jkiss.dbeaver.model.edit.DBERegistry in project dbeaver by serge-rider.

the class DBStructUtils method generateTableDDL.

public static String generateTableDDL(@NotNull DBRProgressMonitor monitor, @NotNull DBSEntity table, Map<String, Object> options, boolean addComments) throws DBException {
    final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    final SQLObjectEditor entityEditor = editorsRegistry.getObjectManager(table.getClass(), SQLObjectEditor.class);
    if (entityEditor instanceof SQLTableManager) {
        DBEPersistAction[] ddlActions = ((SQLTableManager) entityEditor).getTableDDL(monitor, table, options);
        return SQLUtils.generateScript(table.getDataSource(), ddlActions, addComments);
    }
    log.debug("Table editor not found for " + table.getClass().getName());
    return SQLUtils.generateCommentLine(table.getDataSource(), "Can't generate DDL: table editor not found for " + table.getClass().getName());
}
Also used : SQLTableManager(org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableManager) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLObjectEditor(org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)

Example 3 with DBERegistry

use of org.jkiss.dbeaver.model.edit.DBERegistry in project dbeaver by dbeaver.

the class DBStructUtils method generateTableDDL.

public static String generateTableDDL(@NotNull DBRProgressMonitor monitor, @NotNull DBSEntity table, Map<String, Object> options, boolean addComments) throws DBException {
    final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    final SQLObjectEditor entityEditor = editorsRegistry.getObjectManager(table.getClass(), SQLObjectEditor.class);
    if (entityEditor instanceof SQLTableManager) {
        DBEPersistAction[] ddlActions = ((SQLTableManager) entityEditor).getTableDDL(monitor, table, options);
        return SQLUtils.generateScript(table.getDataSource(), ddlActions, addComments);
    }
    log.debug("Table editor not found for " + table.getClass().getName());
    return SQLUtils.generateCommentLine(table.getDataSource(), "Can't generate DDL: table editor not found for " + table.getClass().getName());
}
Also used : SQLTableManager(org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableManager) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLObjectEditor(org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)

Example 4 with DBERegistry

use of org.jkiss.dbeaver.model.edit.DBERegistry in project dbeaver by dbeaver.

the class SQLTableManager method getTableDDL.

public DBEPersistAction[] getTableDDL(DBRProgressMonitor monitor, OBJECT_TYPE table, Map<String, Object> options) throws DBException {
    List<DBEPersistAction> actions = new ArrayList<>();
    final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    SQLObjectEditor<DBSEntityAttribute, OBJECT_TYPE> tcm = getObjectEditor(editorsRegistry, DBSEntityAttribute.class);
    SQLObjectEditor<DBSEntityConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSEntityConstraint.class);
    SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
    SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
    DBCExecutionContext executionContext = DBUtils.getDefaultContext(table, true);
    if (CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_ONLY_FOREIGN_KEYS)) {
        if (fkm != null) {
            // Create only foreign keys
            try {
                for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
                    if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
                        continue;
                    }
                    DBEPersistAction[] cmdActions = fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options).getPersistActions(monitor, executionContext, options);
                    if (cmdActions != null) {
                        Collections.addAll(actions, cmdActions);
                    }
                }
            } catch (DBException e) {
                // Ignore primary keys
                log.debug(e);
            }
        }
        return actions.toArray(new DBEPersistAction[0]);
    }
    if (isIncludeDropInDDL()) {
        actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop table"));
        for (DBEPersistAction delAction : new ObjectDeleteCommand(table, ModelMessages.model_jdbc_delete_object).getPersistActions(monitor, executionContext, options)) {
            String script = delAction.getScript();
            String delimiter = SQLUtils.getScriptLineDelimiter(SQLUtils.getDialectFromObject(table));
            if (!script.endsWith(delimiter)) {
                script += delimiter;
            }
            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), script));
        }
    }
    StructCreateCommand command = makeCreateCommand(table, options);
    if (tcm != null) {
        // Aggregate nested column, constraint and index commands
        for (DBSEntityAttribute column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
            if (DBUtils.isHiddenObject(column) || DBUtils.isInheritedObject(column)) {
                // Do not include hidden (pseudo?) and inherited columns in DDL
                continue;
            }
            command.aggregateCommand(tcm.makeCreateCommand(column, options));
        }
    }
    if (pkm != null) {
        try {
            for (DBSEntityConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
                if (DBUtils.isHiddenObject(constraint) || DBUtils.isInheritedObject(constraint)) {
                    continue;
                }
                command.aggregateCommand(pkm.makeCreateCommand(constraint, options));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (fkm != null && !CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SKIP_FOREIGN_KEYS)) {
        try {
            for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
                if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
                    continue;
                }
                command.aggregateCommand(fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (im != null && table instanceof DBSTable) {
        try {
            for (DBSTableIndex index : CommonUtils.safeCollection(((DBSTable) table).getIndexes(monitor))) {
                if (!isIncludeIndexInDDL(monitor, index)) {
                    continue;
                }
                command.aggregateCommand(im.makeCreateCommand(index, options));
            }
        } catch (DBException e) {
            // Ignore indexes
            log.debug(e);
        }
    }
    addExtraDDLCommands(monitor, table, options, command);
    Collections.addAll(actions, command.getPersistActions(monitor, executionContext, options));
    return actions.toArray(new DBEPersistAction[0]);
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) DBSTable(org.jkiss.dbeaver.model.struct.rdb.DBSTable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBSTableIndex(org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSTableForeignKey(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)

Example 5 with DBERegistry

use of org.jkiss.dbeaver.model.edit.DBERegistry in project dbeaver by serge-rider.

the class JDBCUtils method generateTableDDL.

public static String generateTableDDL(@NotNull DBRProgressMonitor monitor, @NotNull JDBCTable table, boolean addComments) throws DBException {
    final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    final SQLObjectEditor entityEditor = editorsRegistry.getObjectManager(table.getClass(), SQLObjectEditor.class);
    if (entityEditor instanceof SQLTableManager) {
        DBEPersistAction[] ddlActions = ((SQLTableManager) entityEditor).getTableDDL(monitor, table);
        return SQLUtils.generateScript(table.getDataSource(), ddlActions, addComments);
    }
    log.debug("Table editor not found for " + table.getClass().getName());
    return SQLUtils.generateCommentLine(table.getDataSource(), "Can't generate DDL: table editor not found for " + table.getClass().getName());
}
Also used : SQLTableManager(org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableManager) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLObjectEditor(org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)

Aggregations

DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)11 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)8 SQLObjectEditor (org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)6 SQLTableManager (org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableManager)4 DBException (org.jkiss.dbeaver.DBException)3 DBSTableForeignKey (org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)3 DBSTableIndex (org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex)3 PostgreViewManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreViewManager)2 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)2 SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)2 DBSTable (org.jkiss.dbeaver.model.struct.rdb.DBSTable)2 DBSTableConstraint (org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint)1