Java Code Examples for io.debezium.relational.ColumnEditor

Following code examples demonstrate how to use io.debezium.relational.ColumnEditor from 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.ColumnEditor and various code implementation of this class.
Example 1
Project : debezium Source File : TableChanges.java View Source Code on GitHub

        private static Table fromDocument(TableId id, Document document) {
            TableEditor editor = Table.editor()
                .tableId(id)
                .setDefaultCharsetName(document.getString("defaultCharsetName"));

            document.getArray("columns")
                .streamValues()
                .map(Value::asDocument)
                .map(v -> {
                    ColumnEditor columnEditor = Column.editor()
                        .name(v.getString("name"))
                        .jdbcType(v.getInteger("jdbcType"));

                        Integer nativeType = v.getInteger("nativeType");
                        if (nativeType != null) {
                            columnEditor.nativeType(nativeType);
                        }

                       columnEditor.type(v.getString("typeName"), v.getString("typeExpression"))
                           .charsetName(v.getString("charsetName"));

                       Integer length = v.getInteger("length");
                       if (length != null) {
                           columnEditor.length(length);
                       }

                       Integer scale = v.getInteger("scale");
                       if (scale != null) {
                           columnEditor.scale(scale);
                       }

                       columnEditor.position(v.getInteger("position"))
                           .optional(v.getBoolean("optional"))
                           .autoIncremented(v.getBoolean("autoIncremented"))
                           .generated(v.getBoolean("generated"));

                    return columnEditor.create();
                })
                .forEach(editor::addColumn);

            editor.setPrimaryKeyNames(document.getArray("primaryKeyColumnNames")
                .streamValues()
                .map(Value::asString)
                .collect(Collectors.toList()));

            return editor.create();
        }
			
Example 2
Project : debezium Source File : OracleDdlParser.java View Source Code on GitHub

        private void setScale(Precision_partContext precisionPart, ColumnEditor columnEditor) {
            if (precisionPart.numeric().size() > 1) {
                columnEditor.scale(Integer.valueOf(precisionPart.numeric(1).getText()));
            }
            else if (precisionPart.numeric_negative() != null) {
                columnEditor.scale(Integer.valueOf(precisionPart.numeric_negative().getText()));
            }
            else {
                columnEditor.scale(0);
            }
        }
			
Example 3
Project : debezium Source File : AlterTableParserListener.java View Source Code on GitHub

    public void enterAlterByChangeDefault(MySqlParser.AlterByChangeDefaultContext ctx) {
        parserCtx.runIfNotNull(() -> {
            String columnName = parserCtx.parseName(ctx.uid());
            Column column = tableEditor.columnWithName(columnName);
            if (column != null) {
                ColumnEditor columnEditor = column.edit();
                columnEditor.generated(ctx.DROP() != null);
            }
        }, tableEditor);
        super.enterAlterByChangeDefault(ctx);
    }
			
Example 4
Project : debezium Source File : AlterTableParserListener.java View Source Code on GitHub

    public void enterAlterByAddColumn(MySqlParser.AlterByAddColumnContext ctx) {
        parserCtx.runIfNotNull(() -> {
            String columnName = parserCtx.parseName(ctx.uid(0));
            ColumnEditor columnEditor = Column.editor().name(columnName);
            columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditor, parserCtx.dataTypeResolver());
            listeners.add(columnDefinitionListener);
        }, tableEditor);
        super.exitAlterByAddColumn(ctx);
    }
			
Example 5
Project : debezium Source File : CreateTableParserListener.java View Source Code on GitHub

    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);
    }
			
Example 6
Project : debezium Source File : AbstractDdlParser.java View Source Code on GitHub

    protected Column createColumnFromConstant(String columnName, String constantValue) {
        ColumnEditor column = Column.editor().name(columnName);
        try {
            if (constantValue.startsWith("'") || constantValue.startsWith("\"")) {
                column.type("CHAR");
                column.jdbcType(Types.CHAR);
                column.length(constantValue.length() - 2);
            }
            else if (constantValue.equalsIgnoreCase("TRUE") || constantValue.equalsIgnoreCase("FALSE")) {
                column.type("BOOLEAN");
                column.jdbcType(Types.BOOLEAN);
            }
            else {
                setTypeInfoForConstant(constantValue, column);
            }
        }
        catch (Throwable t) {
            logger.debug("Unable to create an artificial column for the constant: " + constantValue);
        }
        return column.create();
    }
			
Example 7
Project : debezium Source File : AbstractDdlParser.java View Source Code on GitHub

    protected void setTypeInfoForConstant(String constantValue, ColumnEditor column) {
        try {
            Integer.parseInt(constantValue);
            column.type("INTEGER");
            column.jdbcType(Types.INTEGER);
        }
        catch (NumberFormatException e) {
        }
        try {
            Long.parseLong(constantValue);
            column.type("BIGINT");
            column.jdbcType(Types.BIGINT);
        }
        catch (NumberFormatException e) {
        }
        try {
            Float.parseFloat(constantValue);
            column.type("FLOAT");
            column.jdbcType(Types.FLOAT);
        }
        catch (NumberFormatException e) {
        }
        try {
            Double.parseDouble(constantValue);
            column.type("DOUBLE");
            column.jdbcType(Types.DOUBLE);
            int precision = 0;
            int scale = 0;
            boolean foundDecimalPoint = false;
            for (int i = 0; i < constantValue.length(); i++) {
                char c = constantValue.charAt(i);
                if (c == '+' || c == '-') {
                    continue;
                }
                else if (c == '.') {
                    foundDecimalPoint = true;
                }
                else if (Character.isDigit(c)) {
                    if (foundDecimalPoint)
                        ++scale;
                    else
                        ++precision;
                }
                else {
                    break;
                }
            }
            column.length(precision);
            column.scale(scale);
        }
        catch (NumberFormatException e) {
        }
        try {
            BigDecimal decimal = new BigDecimal(constantValue);
            column.type("DECIMAL");
            column.jdbcType(Types.DECIMAL);
            column.length(decimal.precision());
            column.scale(decimal.precision());
        }
        catch (NumberFormatException e) {
        }
    }