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

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey. 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);
        }
    }
}