public void enterDefaultValue()

in eventmesh-connectors/eventmesh-connector-jdbc/src/main/java/org/apache/eventmesh/connector/jdbc/source/dialect/antlr4/mysql/listener/DefaultValueParserListener.java [42:136]


    public void enterDefaultValue(DefaultValueContext ctx) {

        /**
         * defaultValue
         *     : NULL_LITERAL
         *     | CAST '(' expression AS convertedDataType ')'
         *     | unaryOperator? constant
         *     | currentTimestamp (ON UPDATE currentTimestamp)?
         *     | '(' expression ')'
         *     | '(' fullId ')'
         *     ;
         */
        String sign = "";

        // Default value is NULL
        if (ctx.NULL_LITERAL() != null) {
            return;
        }

        if (ctx.CAST() != null && ctx.expression() != null) {
            columnEditor.defaultValueExpression(ctx.getText());
            return;
        }

        if (ctx.unaryOperator() != null) {
            sign = ctx.unaryOperator().getText();
        }

        /**
         * Process expression
         * constant
         *     : stringLiteral | decimalLiteral
         *     | '-' decimalLiteral
         *     | hexadecimalLiteral | booleanLiteral
         *     | REAL_LITERAL | BIT_STRING
         *     | NOT? nullLiteral=(NULL_LITERAL | NULL_SPEC_LITERAL)
         *     ;
         */
        if (ctx.constant() != null) {
            StringLiteralContext stringLiteralContext = ctx.constant().stringLiteral();
            if (stringLiteralContext != null) {
                if (stringLiteralContext.COLLATE() == null) {
                    columnEditor.defaultValueExpression(sign + unquote(stringLiteralContext.getText()));
                } else {
                    columnEditor.collation(sign + unquote(stringLiteralContext.STRING_LITERAL(0).getText()));
                }
            } else if (ctx.constant().decimalLiteral() != null) {
                columnEditor.defaultValueExpression(sign + ctx.constant().decimalLiteral().getText());
            } else if (ctx.constant().BIT_STRING() != null) {
                columnEditor.defaultValueExpression(unquoteBinary(ctx.constant().BIT_STRING().getText()));
            } else if (ctx.constant().booleanLiteral() != null) {
                columnEditor.defaultValueExpression(ctx.constant().booleanLiteral().getText());
            } else if (ctx.constant().REAL_LITERAL() != null) {
                columnEditor.defaultValueExpression(ctx.constant().REAL_LITERAL().getText());
            }
        } else if (CollectionUtils.isNotEmpty(ctx.currentTimestamp())) {

            /**
             * <a href="https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html">timestamp-initialization</a>
             * <a href="https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html">cast-functions</a>
             * defaultValue
             *     : NULL_LITERAL
             *     | CAST '(' expression AS convertedDataType ')'
             *     | unaryOperator? constant
             *     | currentTimestamp (ON UPDATE currentTimestamp)?
             *     | '(' expression ')'
             *     | '(' fullId ')'
             *     ;
             * currentTimestamp
             *     :
             *     (
             *       (CURRENT_TIMESTAMP | LOCALTIME | LOCALTIMESTAMP)
             *       ('(' decimalLiteral? ')')?
             *       | NOW '(' decimalLiteral? ')'
             *     )
             *     ;
             */
            List<CurrentTimestampContext> currentTimestampContexts = ctx.currentTimestamp();
            if (currentTimestampContexts.size() > 1 && (ctx.ON() != null && ctx.UPDATE() != null)) {
                StringBuilder builder = new StringBuilder();
                builder.append(currentTimestampContexts.get(0).getText()).append(" ").append(ctx.ON().getText()).append(" ").append(ctx.UPDATE())
                    .append(" ").append(currentTimestampContexts.get(1).getText());
                columnEditor.defaultValueExpression(builder.toString());
            } else if (currentTimestampContexts.size() == 1) {
                CurrentTimestampContext currentTimestampContext = currentTimestampContexts.get(0);
                columnEditor.defaultValueExpression(currentTimestampContext.getText());
            }
        } else if (ctx.expression() != null) {
            // e.g. CREATE TABLE t2 (b BLOB DEFAULT ('abc'));
            columnEditor.defaultValueExpression(ctx.expression().getText());
        } else if (ctx.fullId() != null) {
            columnEditor.defaultValueExpression(ctx.expression().getText());
        }
        super.enterDefaultValue(ctx);
    }