Java Code Examples for com.gitee.hengboy.builder.core.database.model.Column

The following examples show how to use com.gitee.hengboy.builder.core.database.model.Column. 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: code-builder   Source File: AbstractDataBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取表内的主键列表
 *
 * @param tableName 表名
 * @return 主键列表
 */
public List<Column> getPrimaryKeys(String tableName) {
    try {
        // 获取表内的主键列表
        ResultSet resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName);
        List<Column> primaryKeys = new ArrayList<Column>();
        while (resultSet.next()) {
            // 获取主键的列名
            String columnName = resultSet.getString(TableMetaDataEnum.COLUMN_NAME.getValue());
            // 获取主键列的详细信息
            ResultSet columnResultSet = connection.getMetaData().getColumns(null, null, tableName, columnName);
            if (columnResultSet.next()) {
                // 添加主键信息
                primaryKeys.add(getColumn(columnResultSet, true));
            }
        }
        return primaryKeys;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    throw new CodeBuilderException(ErrorEnum.NOT_GET_PRIMARY_KEYS, tableName);
}
 
Example 2
Source Project: beihu-boot   Source File: VariableExpression.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * init variable map
 *
 * @param wrapper            class builder wrapper
 * @param codegenSettingJson codegen setting json
 */
public static CodegenSetting initVariable(ClassBuilderWrapper wrapper, String codegenSettingJson) {
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.NOW, DateFormatTools.formatTime(System.currentTimeMillis(), DateFormatTools.YYYY_MM_DD_HH_MM_SS));
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.DESC, wrapper.getTable().getRemark());
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_NAME, wrapper.getTableCamelName());
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_POSITION, wrapper.getPackageName() + EnhanceCodegenConstant.POINT + wrapper.getTableCamelName());
    List<Column> pks = wrapper.getTable().getPrimaryKeys();
    if (!ObjectUtils.isEmpty(pks) && pks.size() > 0) {
        Column pk = pks.get(0);
        CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_ID_TYPE, pk.getJavaType());
    }
    // parse CodegenSetting
    CodegenSetting codegenSetting = JSON.parseObject(codegenSettingJson, CodegenSetting.class);
    List<Template> templates = codegenSetting.getTemplates();
    if (!ObjectUtils.isEmpty(templates)) {
        templates.stream().forEach(template -> {
            // entity name
            // template java suffix
            String templateName = String.format("%s%s", wrapper.getTableCamelName(), template.getJavaSuffix());
            // entity package name
            // .
            // template package
            // .
            // template name
            String templatePosition = String.format("%s%s%s%s%s", wrapper.getPackageName(), EnhanceCodegenConstant.POINT, template.getPackageName(), EnhanceCodegenConstant.POINT, templateName);

            CodegenTemplateVariable.VARIABLES.put(String.format(TEMPLATE_PACKAGE_NAME, template.getPackageName()), templateName);
            CodegenTemplateVariable.VARIABLES.put(String.format(TEMPLATE_PACKAGE_POSITION, template.getPackageName()), templatePosition);
        });
    }

    // replace variable
    codegenSettingJson = convertVariable(codegenSettingJson);
    // parse CodegenSetting
    return JSON.parseObject(codegenSettingJson, CodegenSetting.class);
}
 
Example 3
Source Project: api-boot   Source File: VariableExpression.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * init variable map
 *
 * @param wrapper            class builder wrapper
 * @param codegenSettingJson codegen setting json
 */
public static CodegenSetting initVariable(ClassBuilderWrapper wrapper, String codegenSettingJson) {
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.NOW, DateFormatTools.formatTime(System.currentTimeMillis(), DateFormatTools.YYYY_MM_DD_HH_MM_SS));
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.DESC, wrapper.getTable().getRemark());
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_NAME, wrapper.getTableCamelName());
    CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_POSITION, wrapper.getPackageName() + EnhanceCodegenConstant.POINT + wrapper.getTableCamelName());
    List<Column> pks = wrapper.getTable().getPrimaryKeys();
    if (!ObjectUtils.isEmpty(pks) && pks.size() > 0) {
        Column pk = pks.get(0);
        CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_ID_TYPE, pk.getJavaType());
    }
    // parse CodegenSetting
    CodegenSetting codegenSetting = JSON.parseObject(codegenSettingJson, CodegenSetting.class);
    List<Template> templates = codegenSetting.getTemplates();
    if (!ObjectUtils.isEmpty(templates)) {
        templates.stream().forEach(template -> {
            // entity name
            // template java suffix
            String templateName = String.format("%s%s", wrapper.getTableCamelName(), template.getJavaSuffix());
            // entity package name
            // .
            // template package
            // .
            // template name
            String templatePosition = String.format("%s%s%s%s%s", wrapper.getPackageName(), EnhanceCodegenConstant.POINT, template.getPackageName(), EnhanceCodegenConstant.POINT, templateName);

            CodegenTemplateVariable.VARIABLES.put(String.format(TEMPLATE_PACKAGE_NAME, template.getPackageName()), templateName);
            CodegenTemplateVariable.VARIABLES.put(String.format(TEMPLATE_PACKAGE_POSITION, template.getPackageName()), templatePosition);
        });
    }

    // replace variable
    codegenSettingJson = convertVariable(codegenSettingJson);
    // parse CodegenSetting
    return JSON.parseObject(codegenSettingJson, CodegenSetting.class);
}
 
Example 4
Source Project: code-builder   Source File: AbstractDataBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取表内的所有列
 *
 * @param tableName 表名
 * @return 数据列列表
 */
public List<Column> getColumns(String tableName) {
    List<Column> columns = new ArrayList<Column>();
    try {
        // 获取列信息
        ResultSet resultSet = getConnection().getMetaData().getColumns(null, null, tableName, "%");
        while (resultSet.next()) {
            columns.add(getColumn(resultSet, false));
        }
        return columns;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    throw new CodeBuilderException(ErrorEnum.NOT_GET_COLUMN, tableName);
}
 
Example 5
Source Project: beihu-boot   Source File: DynamicEntityClassBuilder.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * generator dynamic entity
 *
 * @return
 */
@Override
public String getClassContent() {
    try {
        StringWriter stringWriter = new StringWriter();
        CodeWriter writer = new JavaWriter(stringWriter);
        // current table
        Table table = getWrapper().getTable();

        // package
        writer.packageDecl(getWrapper().getPackageName());

        writer.imports(ColumnExpression.class, TableExpression.class);

        // formatter super class name
        String superClassName = String.format(TABLE_EXPRESSION, getEntityName(getWrapper().getTableCamelName()));

        // java doc
        writer.javadoc(table.getRemark(), AUTHOR);
        // begin class
        writer.beginClass(new SimpleType(getWrapper().getTableCamelName(), EnhanceCodegenConstant.EMPTY_STRING, getWrapper().getTableCamelName()),
                new SimpleType(superClassName, EnhanceCodegenConstant.EMPTY_STRING, superClassName));

        // constructor
        writer.beginConstructor(new Parameter(CONSTRUCTOR_PARAMETER_NAME, Types.STRING));
        writer.line(String.format(SUPER_CONSTRUCTOR, CONSTRUCTOR_PARAMETER_NAME));
        writer.end();

        // dsl method
        writer.beginStaticMethod(new SimpleType(getWrapper().getTableCamelName(), EnhanceCodegenConstant.EMPTY_STRING, getWrapper().getTableCamelName()), DYNAMIC_METHOD_NAME);
        writer.line(String.format(DYNAMIC_INSTANCE, getWrapper().getTableCamelName(), table.getTableName()));
        writer.end();

        StringBuffer columns = new StringBuffer();

        // column expression
        for (int i = 0; i < table.getColumns().size(); i++) {
            Column column = table.getColumns().get(i);
            // column java doc
            writer.javadoc(column.getRemark());
            writer.line(String.format(COLUMN_EXPRESSION, formatterJavaPropertyName(column.getColumnName()), column.getColumnName()));
            columns.append(formatterJavaPropertyName(column.getColumnName()));
            columns.append(i == table.getColumns().size() - 1 ? EnhanceCodegenConstant.EMPTY_STRING : ", ");
        }

        // getColumns method
        writer.annotation(Override.class);
        writer.beginPublicMethod(new SimpleType(COLUMN_EXPRESSION_ARRAY, EnhanceCodegenConstant.EMPTY_STRING, COLUMN_EXPRESSION_ARRAY), GET_COLUMN_METHOD_NAME);
        writer.line(String.format(COLUMN_EXPRESSION_ARRAY_INSTANCE, columns.toString()));
        writer.end();

        // end class
        writer.end();

        return stringWriter.toString();
    } catch (Exception e) {
        return null;
    }
}
 
Example 6
Source Project: api-boot   Source File: DynamicEntityClassBuilder.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * generator dynamic entity
 *
 * @return
 */
@Override
public String getClassContent() {
    try {
        StringWriter stringWriter = new StringWriter();
        CodeWriter writer = new JavaWriter(stringWriter);
        // current table
        Table table = getWrapper().getTable();

        // package
        writer.packageDecl(getWrapper().getPackageName());

        writer.imports(ColumnExpression.class, TableExpression.class);

        // formatter super class name
        String superClassName = String.format(TABLE_EXPRESSION, getEntityName(getWrapper().getTableCamelName()));

        // java doc
        writer.javadoc(table.getRemark(), AUTHOR);
        // begin class
        writer.beginClass(new SimpleType(getWrapper().getTableCamelName(), EnhanceCodegenConstant.EMPTY_STRING, getWrapper().getTableCamelName()),
                new SimpleType(superClassName, EnhanceCodegenConstant.EMPTY_STRING, superClassName));

        // constructor
        writer.beginConstructor(new Parameter(CONSTRUCTOR_PARAMETER_NAME, Types.STRING));
        writer.line(String.format(SUPER_CONSTRUCTOR, CONSTRUCTOR_PARAMETER_NAME));
        writer.end();

        // dsl method
        writer.beginStaticMethod(new SimpleType(getWrapper().getTableCamelName(), EnhanceCodegenConstant.EMPTY_STRING, getWrapper().getTableCamelName()), DYNAMIC_METHOD_NAME);
        writer.line(String.format(DYNAMIC_INSTANCE, getWrapper().getTableCamelName(), table.getTableName()));
        writer.end();

        StringBuffer columns = new StringBuffer();

        // column expression
        for (int i = 0; i < table.getColumns().size(); i++) {
            Column column = table.getColumns().get(i);
            // column java doc
            writer.javadoc(column.getRemark());
            writer.line(String.format(COLUMN_EXPRESSION, formatterJavaPropertyName(column.getColumnName()), column.getColumnName()));
            columns.append(formatterJavaPropertyName(column.getColumnName()));
            columns.append(i == table.getColumns().size() - 1 ? EnhanceCodegenConstant.EMPTY_STRING : ", ");
        }

        // getColumns method
        writer.annotation(Override.class);
        writer.beginPublicMethod(new SimpleType(COLUMN_EXPRESSION_ARRAY, EnhanceCodegenConstant.EMPTY_STRING, COLUMN_EXPRESSION_ARRAY), GET_COLUMN_METHOD_NAME);
        writer.line(String.format(COLUMN_EXPRESSION_ARRAY_INSTANCE, columns.toString()));
        writer.end();

        // end class
        writer.end();

        return stringWriter.toString();
    } catch (Exception e) {
        return null;
    }
}
 
Example 7
Source Project: code-builder   Source File: AbstractDataBase.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 根据结果集获取列的详细信息
 * 根据不同的columnPattern获取到的列元数据
 *
 * @param resultSet       结果集
 * @param isPrimaryColumn true:主键列,false:普通列
 * @return 列基本信息对象
 */
private Column getColumn(ResultSet resultSet, boolean isPrimaryColumn) {
    try {
        // 数据库字段类型
        int jdbcType = resultSet.getInt(TableMetaDataEnum.DATA_TYPE.getValue());
        // 列名
        String columnName = resultSet.getString(TableMetaDataEnum.COLUMN_NAME.getValue());
        // 表名
        String tableName = resultSet.getString(TableMetaDataEnum.TABLE_NAME.getValue());

        return Column.builder()
                // 列名
                .columnName(columnName)
                // 列长度
                .size(resultSet.getInt(TableMetaDataEnum.COLUMN_SIZE.getValue()))
                // 是否为空
                .nullable(resultSet.getBoolean(TableMetaDataEnum.NULLABLE.getValue()))
                // 默认值
                .defaultValue(resultSet.getString(TableMetaDataEnum.COLUMN_DEF.getValue()))
                // 数据库列类型
                .jdbcType(jdbcType)
                // 是否自增
                .autoincrement(hasColumn(resultSet, TableMetaDataEnum.IS_AUTOINCREMENT.getValue()) ? resultSet.getBoolean(TableMetaDataEnum.IS_AUTOINCREMENT.getValue()) : false)
                // 列备注信息
                .remark(resultSet.getString(TableMetaDataEnum.REMARKS.getValue()))
                // 精度
                .decimalDigits(resultSet.getInt(TableMetaDataEnum.DECIMAL_DIGITS.getValue()))
                // jdbc类型名称
                .jdbcTypeName(JdbcTypeResolver.getJdbcTypeName(jdbcType))
                // 格式化后的java field
                .javaProperty(StringUtil.getCamelCaseString(columnName, false))
                // 对应的java数据类型
                .javaType(JavaTypeResolver.getJavaType(jdbcType, false))
                // java数据类型
                .fullJavaType(JavaTypeResolver.getJavaType(jdbcType, true))
                // 是否为主键列
                // 如果是主键获取列信息,直接返回true
                .primaryKey(!isPrimaryColumn ? isPrimaryKey(tableName, columnName) : isPrimaryColumn)
                // 是否为外键列
                // 如果是主键获取列信息,直接返回false
                .foreignKey(!isPrimaryColumn ? isForeignKey(tableName, columnName) : false)
                .build();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 8
Source Project: code-builder   Source File: DataBase.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 查询表内的全部列表
 *
 * @param tableName 表名
 * @return 数据列列表
 */
List<Column> getColumns(String tableName);
 
Example 9
Source Project: code-builder   Source File: DataBase.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 查询表内
 *
 * @param tableName 表名
 * @return 主键列表
 */
List<Column> getPrimaryKeys(String tableName);