Search in sources :

Example 1 with SQLNotNullConstraint

use of com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint in project druid by alibaba.

the class SQLExprParser method parseColumnRest.

public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
    if (lexer.token() == Token.DEFAULT) {
        lexer.nextToken();
        column.setDefaultExpr(bitOr());
        return parseColumnRest(column);
    }
    if (lexer.token() == Token.NOT) {
        lexer.nextToken();
        accept(Token.NULL);
        column.addConstraint(new SQLNotNullConstraint());
        return parseColumnRest(column);
    }
    if (lexer.token() == Token.NULL) {
        lexer.nextToken();
        column.getConstraints().add(new SQLNullConstraint());
        return parseColumnRest(column);
    }
    if (lexer.token == Token.PRIMARY) {
        lexer.nextToken();
        accept(Token.KEY);
        column.addConstraint(new SQLColumnPrimaryKey());
        return parseColumnRest(column);
    }
    if (lexer.token == Token.UNIQUE) {
        lexer.nextToken();
        if (lexer.token() == Token.KEY) {
            lexer.nextToken();
        }
        column.addConstraint(new SQLColumnUniqueKey());
        return parseColumnRest(column);
    }
    if (lexer.token == Token.CONSTRAINT) {
        lexer.nextToken();
        SQLName name = this.name();
        if (lexer.token() == Token.PRIMARY) {
            lexer.nextToken();
            accept(Token.KEY);
            SQLColumnPrimaryKey pk = new SQLColumnPrimaryKey();
            pk.setName(name);
            column.addConstraint(pk);
            return parseColumnRest(column);
        }
        if (lexer.token() == Token.UNIQUE) {
            lexer.nextToken();
            SQLColumnUniqueKey uk = new SQLColumnUniqueKey();
            uk.setName(name);
            column.addConstraint(uk);
            return parseColumnRest(column);
        }
        if (lexer.token() == Token.REFERENCES) {
            lexer.nextToken();
            SQLColumnReference ref = new SQLColumnReference();
            ref.setName(name);
            ref.setTable(this.name());
            accept(Token.LPAREN);
            this.names(ref.getColumns(), ref);
            accept(Token.RPAREN);
            column.addConstraint(ref);
            return parseColumnRest(column);
        }
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            accept(Token.NULL);
            SQLNotNullConstraint notNull = new SQLNotNullConstraint();
            notNull.setName(name);
            column.addConstraint(notNull);
            return parseColumnRest(column);
        }
        if (lexer.token == Token.CHECK) {
            SQLColumnCheck check = parseColumnCheck();
            check.setName(name);
            check.setParent(column);
            column.addConstraint(check);
            return parseColumnRest(column);
        }
        if (lexer.token == Token.DEFAULT) {
            lexer.nextToken();
            SQLExpr expr = this.expr();
            column.setDefaultExpr(expr);
            return parseColumnRest(column);
        }
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
    if (lexer.token == Token.CHECK) {
        SQLColumnCheck check = parseColumnCheck();
        column.addConstraint(check);
        return parseColumnRest(column);
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        column.setComment(primary());
        return parseColumnRest(column);
    }
    return column;
}
Also used : SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLColumnPrimaryKey(com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey) SQLColumnReference(com.alibaba.druid.sql.ast.statement.SQLColumnReference) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLColumnUniqueKey(com.alibaba.druid.sql.ast.statement.SQLColumnUniqueKey) SQLNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNullConstraint)

Example 2 with SQLNotNullConstraint

use of com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint in project canal by alibaba.

the class MemoryTableMeta method processTableElement.

private void processTableElement(SQLTableElement element, TableMeta tableMeta) {
    if (element instanceof SQLColumnDefinition) {
        FieldMeta fieldMeta = new FieldMeta();
        SQLColumnDefinition column = (SQLColumnDefinition) element;
        String name = getSqlName(column.getName());
        // String charset = getSqlName(column.getCharsetExpr());
        SQLDataType dataType = column.getDataType();
        String dataTypStr = dataType.getName();
        if (StringUtils.equalsIgnoreCase(dataTypStr, "float")) {
            if (dataType.getArguments().size() == 1) {
                int num = Integer.valueOf(dataType.getArguments().get(0).toString());
                if (num > 24) {
                    dataTypStr = "double";
                }
            }
        }
        if (dataType.getArguments().size() > 0) {
            dataTypStr += "(";
            for (int i = 0; i < column.getDataType().getArguments().size(); i++) {
                if (i != 0) {
                    dataTypStr += ",";
                }
                SQLExpr arg = column.getDataType().getArguments().get(i);
                dataTypStr += arg.toString();
            }
            dataTypStr += ")";
        }
        if (dataType instanceof SQLDataTypeImpl) {
            SQLDataTypeImpl dataTypeImpl = (SQLDataTypeImpl) dataType;
            if (dataTypeImpl.isUnsigned()) {
                dataTypStr += " unsigned";
            }
            if (dataTypeImpl.isZerofill()) {
                // 如果设置了zerofill,自动给列添加unsigned属性
                if (!dataTypeImpl.isUnsigned()) {
                    dataTypStr += " unsigned";
                }
                dataTypStr += " zerofill";
            }
        }
        if (column.getDefaultExpr() == null || column.getDefaultExpr() instanceof SQLNullExpr) {
            fieldMeta.setDefaultValue(null);
        } else {
            fieldMeta.setDefaultValue(DruidDdlParser.unescapeQuotaName(getSqlName(column.getDefaultExpr())));
        }
        fieldMeta.setColumnName(name);
        fieldMeta.setColumnType(dataTypStr);
        fieldMeta.setNullable(true);
        List<SQLColumnConstraint> constraints = column.getConstraints();
        for (SQLColumnConstraint constraint : constraints) {
            if (constraint instanceof SQLNotNullConstraint) {
                fieldMeta.setNullable(false);
            } else if (constraint instanceof SQLNullConstraint) {
                fieldMeta.setNullable(true);
            } else if (constraint instanceof SQLColumnPrimaryKey) {
                fieldMeta.setKey(true);
                fieldMeta.setNullable(false);
            } else if (constraint instanceof SQLColumnUniqueKey) {
                fieldMeta.setUnique(true);
            }
        }
        tableMeta.addFieldMeta(fieldMeta);
    } else if (element instanceof MySqlPrimaryKey) {
        MySqlPrimaryKey column = (MySqlPrimaryKey) element;
        List<SQLSelectOrderByItem> pks = column.getColumns();
        for (SQLSelectOrderByItem pk : pks) {
            String name = getSqlName(pk.getExpr());
            FieldMeta field = tableMeta.getFieldMetaByName(name);
            field.setKey(true);
            field.setNullable(false);
        }
    } else if (element instanceof MySqlUnique) {
        MySqlUnique column = (MySqlUnique) element;
        List<SQLSelectOrderByItem> uks = column.getColumns();
        for (SQLSelectOrderByItem uk : uks) {
            String name = getSqlName(uk.getExpr());
            FieldMeta field = tableMeta.getFieldMetaByName(name);
            field.setUnique(true);
        }
    }
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) FieldMeta(com.alibaba.otter.canal.parse.inbound.TableMeta.FieldMeta) SQLNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNullConstraint) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLColumnConstraint(com.alibaba.druid.sql.ast.statement.SQLColumnConstraint) SQLNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNullConstraint) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLColumnPrimaryKey(com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) SQLColumnUniqueKey(com.alibaba.druid.sql.ast.statement.SQLColumnUniqueKey) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) SQLColumnConstraint(com.alibaba.druid.sql.ast.statement.SQLColumnConstraint) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) List(java.util.List)

Example 3 with SQLNotNullConstraint

use of com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint in project druid by alibaba.

the class SQLASTVisitorAdapterTest method test_adapter.

public void test_adapter() throws Exception {
    SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
    new SQLBinaryOpExpr().accept(adapter);
    new SQLInListExpr().accept(adapter);
    new SQLSelectQueryBlock().accept(adapter);
    new SQLDropTableStatement().accept(adapter);
    new SQLCreateTableStatement().accept(adapter);
    new SQLDeleteStatement().accept(adapter);
    new SQLCurrentOfCursorExpr().accept(adapter);
    new SQLInsertStatement().accept(adapter);
    new SQLUpdateStatement().accept(adapter);
    new SQLNotNullConstraint().accept(adapter);
    new SQLMethodInvokeExpr().accept(adapter);
    new SQLCallStatement().accept(adapter);
    new SQLSomeExpr().accept(adapter);
    new SQLAnyExpr().accept(adapter);
    new SQLAllExpr().accept(adapter);
    new SQLDefaultExpr().accept(adapter);
    new SQLCommentStatement().accept(adapter);
    new SQLDropViewStatement().accept(adapter);
    new SQLSavePointStatement().accept(adapter);
    new SQLReleaseSavePointStatement().accept(adapter);
    new SQLCreateDatabaseStatement().accept(adapter);
    new SQLAlterTableDropIndex().accept(adapter);
    new SQLOver().accept(adapter);
    new SQLWithSubqueryClause().accept(adapter);
    new SQLAlterTableAlterColumn().accept(adapter);
    new SQLAlterTableStatement().accept(adapter);
    new SQLAlterTableDisableConstraint().accept(adapter);
    new SQLAlterTableEnableConstraint().accept(adapter);
    new SQLColumnCheck().accept(adapter);
    new SQLExprHint().accept(adapter);
    new SQLAlterTableDropConstraint().accept(adapter);
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLAllExpr(com.alibaba.druid.sql.ast.expr.SQLAllExpr) SQLReleaseSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLDropViewStatement(com.alibaba.druid.sql.ast.statement.SQLDropViewStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAlterTableDisableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint) SQLSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLSavePointStatement) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLSomeExpr(com.alibaba.druid.sql.ast.expr.SQLSomeExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLExprHint(com.alibaba.druid.sql.ast.statement.SQLExprHint) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLCommentStatement(com.alibaba.druid.sql.ast.statement.SQLCommentStatement) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLAnyExpr(com.alibaba.druid.sql.ast.expr.SQLAnyExpr) SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLAlterTableEnableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint)

Aggregations

SQLNotNullConstraint (com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint)3 SQLColumnCheck (com.alibaba.druid.sql.ast.statement.SQLColumnCheck)2 SQLColumnPrimaryKey (com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey)2 SQLColumnUniqueKey (com.alibaba.druid.sql.ast.statement.SQLColumnUniqueKey)2 SQLNullConstraint (com.alibaba.druid.sql.ast.statement.SQLNullConstraint)2 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)1 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1 SQLAllExpr (com.alibaba.druid.sql.ast.expr.SQLAllExpr)1 SQLAnyExpr (com.alibaba.druid.sql.ast.expr.SQLAnyExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)1 SQLSomeExpr (com.alibaba.druid.sql.ast.expr.SQLSomeExpr)1 SQLAlterTableAlterColumn (com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn)1 SQLAlterTableDisableConstraint (com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint)1