Java Code Examples for io.debezium.relational.TableEditor

Following code examples demonstrate how to use io.debezium.relational.TableEditorfrom debezium. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use io.debezium.relational.TableEditorand various code implementation of this class.

    public void enterCreateIndex(MySqlParser.CreateIndexContext ctx) {
        if (ctx.UNIQUE() != null) {
            TableId tableId = parser.parseQualifiedTableId(ctx.tableName().fullId());
            TableEditor tableEditor = parser.databaseTables().editTable(tableId);
            if (tableEditor != null) {
                if (!tableEditor.hasPrimaryKey()) {
                    parser.parsePrimaryIndexColumnNames(ctx.indexColumnNames(), tableEditor);
                    parser.signalCreateIndex(parser.parseName(ctx.uid()), null, ctx);
                }
            }
            else {
                throw new ParsingException(null, "Trying to create index on non existing table " + tableId.toString() + "."
                        + "Query: " + getText(ctx));
            }
        }
        super.enterCreateIndex(ctx);
    } 


    public void exitAlterView(MySqlParser.AlterViewContext ctx) {
        parser.runIfNotNull(() -> {
            tableEditor.addColumns(selectColumnsListener.getSelectedColumns());
            if (!tableEditor.hasDefaultCharsetName()) {
                tableEditor.setDefaultCharsetName(parser.currentDatabaseCharset());
            }
            parser.databaseTables().overwriteTable(tableEditor.create());
            listeners.remove(selectColumnsListener);
        }, tableEditor);
        parser.signalAlterView(parser.parseQualifiedTableId(ctx.fullId()), null, ctx);
        super.exitAlterView(ctx);
    } 

    public void enterAlterView(MySqlParser.AlterViewContext ctx) {
        if (!parser.skipViews()) {
            TableId tableId = parser.parseQualifiedTableId(ctx.fullId());

            tableEditor = parser.databaseTables().editTable(tableId);
            if (tableEditor == null) {
                throw new ParsingException(null, "Trying to alter view " + tableId.toString()
                        + ", which does not exist. Query:" + AntlrDdlParser.getText(ctx));
            }
            tableEditor.columnNames().forEach(tableEditor::removeColumn);
            if (ctx.uidList() != null) {
                ctx.uidList().uid().stream().map(parser::parseName).forEach(columnName -> {
                    tableEditor.addColumn(Column.editor().name(columnName).create());
                });
            }
            selectColumnsListener = new ViewSelectedColumnsParserListener(tableEditor, parser);
            listeners.add(selectColumnsListener);
        }
        super.enterAlterView(ctx);
    } 

    public void exitCreateView(MySqlParser.CreateViewContext ctx) {
        parserCtx.runIfNotNull(() -> {
            tableEditor.addColumns(selectColumnsListener.getSelectedColumns());
            if (!tableEditor.hasDefaultCharsetName()) {
                tableEditor.setDefaultCharsetName(parserCtx.currentDatabaseCharset());
            }
            parserCtx.databaseTables().overwriteTable(tableEditor.create());
            listeners.remove(selectColumnsListener);
        }, tableEditor);
        parserCtx.signalCreateView(parserCtx.parseQualifiedTableId(ctx.fullId()), ctx);
        super.exitCreateView(ctx);
    } 

    public void enterCreateView(MySqlParser.CreateViewContext ctx) {
        if (!parserCtx.skipViews()) {
            tableEditor = parserCtx.databaseTables().editOrCreateTable(parserCtx.parseQualifiedTableId(ctx.fullId()));
            if (ctx.uidList() != null) {
                ctx.uidList().uid().stream().map(parserCtx::parseName).forEach(columnName -> {
                    tableEditor.addColumn(Column.editor().name(columnName).create());
                });
            }
            selectColumnsListener = new ViewSelectedColumnsParserListener(tableEditor, parserCtx);
            listeners.add(selectColumnsListener);
        }
        super.enterCreateView(ctx);
    } 

    public void exitColumnCreateTable(MySqlParser.ColumnCreateTableContext ctx) {
        parser.runIfNotNull(() -> {
            if (!tableEditor.hasDefaultCharsetName()) {
                tableEditor.setDefaultCharsetName(parser.currentDatabaseCharset());
            }
            listeners.remove(columnDefinitionListener);
            columnDefinitionListener = null;
            parser.databaseTables().overwriteTable(tableEditor.create());
            parser.signalCreateTable(tableEditor.tableId(), ctx);
        }, tableEditor);
        super.exitColumnCreateTable(ctx);
    } 

    public void enterColumnCreateTable(MySqlParser.ColumnCreateTableContext ctx) {
        TableId tableId = parser.parseQualifiedTableId(ctx.tableName().fullId());
        tableEditor = parser.databaseTables().editOrCreateTable(tableId);
        super.enterColumnCreateTable(ctx);
    } 

    public void enterPrimaryKeyTableConstraint(MySqlParser.PrimaryKeyTableConstraintContext ctx) {
        parser.runIfNotNull(() -> {
            parser.parsePrimaryIndexColumnNames(ctx.indexColumnNames(), tableEditor);
        }, tableEditor);
        super.enterPrimaryKeyTableConstraint(ctx);
    } 

    public void enterTableOptionCharset(MySqlParser.TableOptionCharsetContext ctx) {
        parser.runIfNotNull(() -> {
            String charsetName = parser.withoutQuotes(ctx.charsetName());
            tableEditor.setDefaultCharsetName(charsetName);
        }, tableEditor);
        super.enterTableOptionCharset(ctx);
    } 

    public void enterColumnDeclaration(MySqlParser.ColumnDeclarationContext ctx) {
        parser.runIfNotNull(() -> {
            String columnName = parser.parseName(ctx.uid());
            ColumnEditor columnEditor = Column.editor().name(columnName);
            if (columnDefinitionListener == null) {
                columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditor, parser.dataTypeResolver(), parser.getConverters());
                listeners.add(columnDefinitionListener);
            } else {
                columnDefinitionListener.setColumnEditor(columnEditor);
            }
        }, tableEditor);
        super.enterColumnDeclaration(ctx);
    } 

    public void enterUniqueKeyTableConstraint(MySqlParser.UniqueKeyTableConstraintContext ctx) {
        parser.runIfNotNull(() -> {
            if (!tableEditor.hasPrimaryKey()) {
                parser.parsePrimaryIndexColumnNames(ctx.indexColumnNames(), tableEditor);
            }
        }, tableEditor);
        super.enterUniqueKeyTableConstraint(ctx);
    } 

    @Override
    public void readSchema(Tables tables, String databaseCatalog, String schemaNamePattern, TableFilter tableFilter,
            ColumnNameFilter columnFilter, boolean removeTablesNotFoundInJdbc) throws SQLException {

        super.readSchema(tables, null, schemaNamePattern, null, columnFilter, removeTablesNotFoundInJdbc);

        Set<TableId> tableIds = new HashSet<>(tables.tableIds());

        for (TableId tableId : tableIds) {
            TableId tableIdWithCatalog = new TableId(databaseCatalog, tableId.schema(), tableId.table());

            if (tableFilter.isIncluded(tableIdWithCatalog)) {
                TableEditor editor = tables.editTable(tableId);
                editor.tableId(tableIdWithCatalog);

                List<String> columnNames = new ArrayList<>(editor.columnNames());
                for (String columnName : columnNames) {
                    Column column = editor.columnWithName(columnName);
                    if (column.jdbcType() == Types.TIMESTAMP) {
                        editor.addColumn(
                                column.edit()
                                    .length(column.scale().orElse(Column.UNSET_INT_VALUE))
                                    .scale(null)
                                    .create()
                                );
                    }
                    else if (column.jdbcType() == OracleTypes.NUMBER) {
                        column.scale()
                            .filter(s -> s == ORACLE_UNSET_SCALE)
                            .ifPresent(s -> {
                                editor.addColumn(
                                        column.edit()
                                            .scale(null)
                                            .create()
                                        );
                            });
                    }
                }
                tables.overwriteTable(editor.create());
            }

            tables.removeTable(tableId);
        }
    } 

Advertisement
Javadoc
An editor for Table instances, normally obtained from a Tables instance. @author Randall Hauch
Advertisement