Search in sources :

Example 1 with Id

use of org.sagacity.sqltoy.config.annotation.Id in project sagacity-sqltoy by chenrenfei.

the class EntityManager method parseFieldMeta.

/**
 * @todo 解析对象属性跟数据库表字段的信息
 * @param sqlToyContext
 * @param entityMeta
 * @param field
 * @param rejectIdFieldList
 * @param allFieldAry
 * @param loadNamedWhereSql
 * @param loadArgWhereSql
 * @throws Exception
 */
private void parseFieldMeta(SqlToyContext sqlToyContext, EntityMeta entityMeta, Field field, List<String> rejectIdFieldList, List<String> allFieldAry, StringBuilder loadNamedWhereSql, StringBuilder loadArgWhereSql) throws Exception {
    Column column = field.getAnnotation(Column.class);
    if (column == null) {
        return;
    }
    // 字段的详细配置信息,字段名称,字段对应数据库表字段,字段默认值,字段类型
    FieldMeta fieldMeta = new FieldMeta(field.getName(), column.name(), (SqlToyConstants.DEFAULT_NULL.equals(column.defaultValue())) ? null : column.defaultValue(), column.type(), column.nullable(), column.keyword(), Long.valueOf(column.length()).intValue(), column.precision(), column.scale());
    // 增加字段
    allFieldAry.add(column.name());
    // 字段是否自增
    fieldMeta.setAutoIncrement(column.autoIncrement());
    // 设置type类型,并转小写便于后续对比的统一
    fieldMeta.setFieldType(field.getType().getTypeName().toLowerCase());
    // 设置是否分区字段
    if (field.getAnnotation(PartitionKey.class) != null) {
        fieldMeta.setPartitionKey(true);
    }
    // 内部包含了构造表字段名称跟vo属性名称的对照
    entityMeta.addFieldMeta(fieldMeta);
    // 判断字段是否为主键
    Id id = field.getAnnotation(Id.class);
    String idColName;
    if (id != null) {
        fieldMeta.setPK(true);
        // 主键生成策略
        entityMeta.setIdStrategy(PKStrategy.getPKStrategy(id.strategy().toLowerCase()));
        entityMeta.setSequence(id.sequence());
        String idGenerator = id.generator();
        if (StringUtil.isNotBlank(idGenerator)) {
            processIdGenerator(sqlToyContext, entityMeta, idGenerator);
            entityMeta.setIdGenerator(idGenerators.get(idGenerator));
        }
        if (loadNamedWhereSql.length() > 1) {
            loadNamedWhereSql.append(" and ");
            loadArgWhereSql.append(" and ");
        } else {
            loadNamedWhereSql.append(" where ");
            loadArgWhereSql.append(" where ");
        }
        idColName = ReservedWordsUtil.convertWord(column.name(), null);
        loadNamedWhereSql.append(idColName).append("=:").append(field.getName());
        loadArgWhereSql.append(idColName).append("=?");
    } else {
        rejectIdFieldList.add(field.getName());
    }
    // 业务主键策略配置解析
    BusinessId bizId = field.getAnnotation(BusinessId.class);
    if (bizId != null && StringUtil.isNotBlank(bizId.generator())) {
        String bizGenerator = bizId.generator();
        entityMeta.setBizIdLength(bizId.length());
        entityMeta.setBizIdSignature(bizId.signature());
        entityMeta.setHasBizIdConfig(true);
        entityMeta.setBizIdSequenceSize(bizId.sequenceSize());
        entityMeta.setBusinessIdField(field.getName());
        // 生成业务主键关联的字段(主键值生成需要其他字段的值进行组合,入交易业务ID组合交易类别码等)
        if (bizId.relatedColumns() != null && bizId.relatedColumns().length > 0) {
            entityMeta.setBizIdRelatedColumns(bizId.relatedColumns());
        }
        processIdGenerator(sqlToyContext, entityMeta, bizGenerator);
        // 如果是业务主键跟ID重叠,则ID以业务主键策略生成
        if (id != null) {
            entityMeta.setIdGenerator(idGenerators.get(bizGenerator));
            fieldMeta.setLength(bizId.length());
            entityMeta.setBizIdEqPK(true);
        } else {
            entityMeta.setBusinessIdGenerator(idGenerators.get(bizGenerator));
        }
    }
}
Also used : Column(org.sagacity.sqltoy.config.annotation.Column) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta) PartitionKey(org.sagacity.sqltoy.config.annotation.PartitionKey) Id(org.sagacity.sqltoy.config.annotation.Id) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId)

Example 2 with Id

use of org.sagacity.sqltoy.config.annotation.Id in project sagacity-sqltoy by chenrenfei.

the class EntityManager method parseFieldMeta.

/**
 * @todo 解析对象属性跟数据库表字段的信息
 * @param entityMeta
 * @param field
 * @param hasAbstractVO
 * @param fieldNameMap
 * @param rejectIdFieldList
 * @param loadNamedWhereSql
 * @param loadArgWhereSql
 * @throws Exception
 */
private void parseFieldMeta(SqlToyContext sqlToyContext, EntityMeta entityMeta, Field field, boolean hasAbstractVO, HashMap fieldNameMap, List<String> rejectIdFieldList, StringBuilder loadNamedWhereSql, StringBuilder loadArgWhereSql) throws Exception {
    Column column = field.getAnnotation(Column.class);
    if (column == null)
        return;
    // 字段的详细配置信息,字段名称,字段对应数据库表字段,字段默认值,字段类型
    FieldMeta fieldMeta = new FieldMeta(field.getName(), column.name(), StringUtil.isNotBlank(column.defaultValue()) ? column.defaultValue() : null, column.type(), column.nullable(), Long.valueOf(column.length()).intValue(), column.precision(), column.scale());
    // 字段是否自增
    fieldMeta.setAutoIncrement(column.autoIncrement());
    entityMeta.addFieldMeta(fieldMeta);
    // 对于数据库字段处理,仅仅适用于abstractVO中定义的属性
    if (!hasAbstractVO || (hasAbstractVO && fieldNameMap.get(field.getName()).equals("1"))) {
        // 判断字段是否为主键
        Id id = field.getAnnotation(Id.class);
        if (id != null) {
            fieldMeta.setPK(true);
            // 主键生成策略
            entityMeta.setIdStrategy(PKStrategy.getPKStrategy(id.strategy().toLowerCase()));
            entityMeta.setSequence(id.sequence());
            String idGenerator = id.generator();
            if (StringUtil.isNotBlank(idGenerator)) {
                processIdGenerator(sqlToyContext, entityMeta, idGenerator);
                entityMeta.setIdGenerator(idGenerators.get(idGenerator));
            }
            if (loadNamedWhereSql.length() > 1) {
                loadNamedWhereSql.append(" and ");
                loadArgWhereSql.append(" and ");
            } else {
                loadNamedWhereSql.append(" where ");
                loadArgWhereSql.append(" where ");
            }
            loadNamedWhereSql.append(column.name()).append("=:").append(field.getName());
            loadArgWhereSql.append(column.name()).append("=?");
        } else {
            rejectIdFieldList.add(field.getName());
        }
        // 业务主键策略配置解析
        BusinessId bizId = field.getAnnotation(BusinessId.class);
        if (bizId != null) {
            String bizGenerator = bizId.generator();
            entityMeta.setBizIdLength(bizId.length());
            entityMeta.setBizIdSignature(bizId.signature());
            // 生成业务主键关联的字段(主键值生成需要其他字段的值进行组合,入交易业务ID组合交易类别码等)
            if (bizId.relatedColumn() != null)
                entityMeta.setBizIdRelatedColumn(bizId.relatedColumn());
            processIdGenerator(sqlToyContext, entityMeta, bizGenerator);
            // 如果是业务主键跟ID重叠,则ID以业务主键策略生成
            if (id != null) {
                entityMeta.setIdGenerator(idGenerators.get(bizGenerator));
            } else {
                entityMeta.setBusinessIdField(field.getName());
                entityMeta.setBusinessIdGenerator(idGenerators.get(bizGenerator));
            }
        }
    }
}
Also used : Column(org.sagacity.sqltoy.config.annotation.Column) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta) Id(org.sagacity.sqltoy.config.annotation.Id) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId)

Example 3 with Id

use of org.sagacity.sqltoy.config.annotation.Id in project sagacity-sqltoy by chenrenfei.

the class EntityManager method parseIdFileds.

/**
 * @todo 解析主键字段
 * @param idList
 * @param idsIndexMap
 * @param realFields
 * @param hasAbstractVO
 * @param fieldNameMap
 */
private void parseIdFileds(List<String> idList, HashMap<String, Integer> idsIndexMap, Field[] realFields, boolean hasAbstractVO, HashMap fieldNameMap) {
    Id id;
    Column column;
    int idIndex = 0;
    // 优先提取id集合,有利于统一主键在子表操作中的顺序
    for (Field field : realFields) {
        column = field.getAnnotation(Column.class);
        if (column != null) {
            // 对于数据库字段处理,仅仅适用于abstractVO中定义的属性
            if (!hasAbstractVO || (hasAbstractVO && fieldNameMap.get(field.getName()).equals("1"))) {
                // 判断字段是否为主键
                id = field.getAnnotation(Id.class);
                if (id != null) {
                    idList.add(field.getName());
                    idsIndexMap.put(field.getName(), idIndex);
                    idIndex++;
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Column(org.sagacity.sqltoy.config.annotation.Column) Id(org.sagacity.sqltoy.config.annotation.Id) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId)

Aggregations

BusinessId (org.sagacity.sqltoy.config.annotation.BusinessId)3 Column (org.sagacity.sqltoy.config.annotation.Column)3 Id (org.sagacity.sqltoy.config.annotation.Id)3 FieldMeta (org.sagacity.sqltoy.config.model.FieldMeta)2 Field (java.lang.reflect.Field)1 PartitionKey (org.sagacity.sqltoy.config.annotation.PartitionKey)1