Java Code Examples for com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: youran   Source File: ReverseEngineeringService.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 从单个建表语句中创建实体
 *
 * @param project
 * @param sqlStatement
 */
private void saveEntityFromSqlStatement(MetaProjectPO project, SQLCreateTableStatement sqlStatement) {
    SQLCreateTableStatement createTableStatement = sqlStatement;
    // 解析表名
    String tableName = this.parseTableName(createTableStatement);
    if (StringUtils.isBlank(tableName)) {
        return;
    }
    String comment = cleanQuote(SafeUtil.getString(createTableStatement.getComment()));
    MetaEntityPO entity = createEntity(project, tableName, comment);
    // 获取单独声明的主键
    String pkAlone = this.getPkAlone(createTableStatement);
    List<SQLTableElement> tableElementList = createTableStatement.getTableElementList();
    int orderNo = 0;
    // 缓存遍历到的字段
    Map<String, MetaFieldPO> fieldMap = new HashMap<>(32);
    for (SQLTableElement element : tableElementList) {
        // 创建字段
        if (element instanceof SQLColumnDefinition) {
            orderNo += 10;
            SQLColumnDefinition sqlColumnDefinition = (SQLColumnDefinition) element;
            String fieldName = cleanQuote(sqlColumnDefinition.getNameAsString());
            boolean pk = this.isPkField(sqlColumnDefinition, pkAlone);
            String fieldType = StringUtils.lowerCase(sqlColumnDefinition.getDataType().getName());
            int fieldLength = 0;
            int fieldScale = 0;
            List<SQLExpr> arguments = sqlColumnDefinition.getDataType().getArguments();
            if (CollectionUtils.isNotEmpty(arguments)) {
                fieldLength = SafeUtil.getInteger(arguments.get(0));
                if (arguments.size() >= 2) {
                    fieldScale = SafeUtil.getInteger(arguments.get(1));
                }
            }
            boolean autoIncrement = sqlColumnDefinition.isAutoIncrement();
            boolean notNull = sqlColumnDefinition.containsNotNullConstaint();
            if (pk) {
                notNull = true;
            }
            String defaultValue = sqlColumnDefinition.getDefaultExpr() == null ? "NULL" : sqlColumnDefinition.getDefaultExpr().toString();
            String desc = sqlColumnDefinition.getComment() == null ? "" : cleanQuote(sqlColumnDefinition.getComment().toString());

            MetaFieldPO field = this.createField(entity, fieldName, fieldType,
                fieldLength, fieldScale, pk,
                autoIncrement, notNull, orderNo,
                defaultValue, desc);
            fieldMap.put(fieldName, field);
            continue;
        }
        if (element instanceof MySqlPrimaryKey) {
            continue;
        }
        // 创建索引
        if (element instanceof MySqlKey) {
            boolean unique = (element instanceof MySqlUnique);
            MySqlKey sqlKey = (MySqlKey) element;
            String indexName = cleanQuote(sqlKey.getName().toString());
            List<MetaFieldPO> fields = new ArrayList<>();
            for (SQLSelectOrderByItem item : sqlKey.getColumns()) {
                String columnName = cleanQuote(item.getExpr().toString());
                fields.add(fieldMap.get(columnName));
            }
            this.createIndex(entity, indexName, unique, fields);
        }
    }
}
 
Example 2
Source Project: dble   Source File: DruidAlterTableParser.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc, boolean isExplain)
        throws SQLException {
    SQLAlterTableStatement alterTable = (SQLAlterTableStatement) stmt;
    String schemaName = schema == null ? null : schema.getName();
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, alterTable.getTableSource());
    boolean support = false;
    String msg = "The DDL is not supported, sql:";
    for (SQLAlterTableItem alterItem : alterTable.getItems()) {
        if (alterItem instanceof SQLAlterTableAddColumn ||
                alterItem instanceof SQLAlterTableDropKey ||
                alterItem instanceof SQLAlterTableDropPrimaryKey) {
            support = true;
        } else if (alterItem instanceof SQLAlterTableAddIndex ||
                alterItem instanceof SQLAlterTableDropIndex) {
            support = true;
            rrs.setOnline(true);
        } else if (alterItem instanceof SQLAlterTableAddConstraint) {
            SQLConstraint constraint = ((SQLAlterTableAddConstraint) alterItem).getConstraint();
            if (constraint instanceof MySqlPrimaryKey) {
                support = true;
            }
        } else if (alterItem instanceof MySqlAlterTableChangeColumn ||
                alterItem instanceof MySqlAlterTableModifyColumn ||
                alterItem instanceof SQLAlterTableDropColumnItem) {
            List<SQLName> columnList = new ArrayList<>();
            if (alterItem instanceof MySqlAlterTableChangeColumn) {
                columnList.add(((MySqlAlterTableChangeColumn) alterItem).getColumnName());
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                columnList.add(((MySqlAlterTableModifyColumn) alterItem).getNewColumnDefinition().getName());
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                columnList = ((SQLAlterTableDropColumnItem) alterItem).getColumns();
            }
            support = !this.columnInfluenceCheck(columnList, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
            if (!support) {
                msg = "The columns may be sharding keys or ER keys, are not allowed to alter sql:";
            }
        }
    }
    if (!support) {
        msg = msg + stmt;
        throw new SQLNonTransientException(msg);
    }
    String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
    rrs.setStatement(statement);
    String noShardingNode = RouterUtil.isNoShardingDDL(schemaInfo.getSchemaConfig(), schemaInfo.getTable());
    if (noShardingNode != null) {
        RouterUtil.routeToSingleDDLNode(schemaInfo, rrs, noShardingNode);
        return schemaInfo.getSchemaConfig();
    }
    RouterUtil.routeToDDLNode(schemaInfo, rrs);
    return schemaInfo.getSchemaConfig();
}
 
Example 3
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MySqlPrimaryKey parsePrimaryKey() {
    accept(Token.PRIMARY);
    accept(Token.KEY);

    MySqlPrimaryKey primaryKey = new MySqlPrimaryKey();

    if (lexer.identifierEquals(FnvHash.Constants.USING)) {
        lexer.nextToken();
        primaryKey.setIndexType(lexer.stringVal());
        lexer.nextToken();
    }

    if (lexer.token() != Token.LPAREN) {
        SQLName name = this.name();
        primaryKey.setName(name);
    }

    accept(Token.LPAREN);
    for (;;) {
        SQLExpr expr;
        if (lexer.token() == Token.LITERAL_ALIAS) {
            expr = this.name();
        } else {
            expr = this.expr();
        }
        primaryKey.addColumn(expr);
        if (!(lexer.token() == (Token.COMMA))) {
            break;
        } else {
            lexer.nextToken();
        }
    }
    accept(Token.RPAREN);

    if (lexer.identifierEquals(FnvHash.Constants.USING)) {
        lexer.nextToken();
        primaryKey.setIndexType(lexer.stringVal());
        lexer.nextToken();
    }

    return primaryKey;
}