Search in sources :

Example 1 with ForeignAssociationMapper

use of indi.mybatis.flying.models.ForeignAssociationMapper in project mybatis.flying by limeng32.

the class SqlBuilder method dealMapperAnnotationIterationForCount.

private static void dealMapperAnnotationIterationForCount(boolean objectIsClass, Object object, StringBuilder[] sqlBuilders, TableName originTableName, Mapperable originFieldMapper, String fieldPerfix, AtomicInteger index, TableName lastTableName, String indexStr) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
    Class<?> objectType = objectIsClass ? (Class<?>) object : object.getClass();
    Map<String, Object> dtoFieldMap = objectIsClass ? Collections.emptyMap() : PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(objectType));
    QueryMapper queryMapper = buildQueryMapper(object.getClass(), getTableMappedClass(objectType));
    TableName tableName = new TableName(tableMapper, index.getAndIncrement(), lastTableName.getMap());
    StringBuilder fromSql = sqlBuilders[0];
    StringBuilder whereSql = sqlBuilders[1];
    /*
		 * If originFieldMapper is null, it is considered to be the first traversal.In
		 * the first iteration, handle fromSql.
		 * 
		 */
    if (originFieldMapper == null) {
        fromSql.append(tableName.sqlSelect());
        if (indexStr != null) {
            fromSql.append(" ").append(indexStr);
        }
    }
    /*
		 * If the originFieldMapper and originTableName are not null, it can be
		 * considered a non-first traversal. In the non-first traversal, process
		 * fieldPerfix and fromSql.
		 * 
		 */
    String temp = null;
    if (originFieldMapper != null && originTableName != null) {
        /* Processing fieldPerfix */
        temp = originFieldMapper.getFieldName();
        if (fieldPerfix != null) {
            temp = new StringBuilder(fieldPerfix).append(DOT).append(temp).toString();
        }
        /* Processing fromSql */
        fromSql.append(originFieldMapper.getAssociationType().value()).append(tableName.sqlSelect()).append(BLANK_ON_BLANK).append(originTableName.sqlWhere()).append(originFieldMapper.getDbFieldNameForJoin()).append(BLANK_EQUAL_BLANK).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
        ForeignAssociationMapper[] fams = originFieldMapper.getForeignAssociationMappers();
        if (fams != null && fams.length > 0) {
            for (ForeignAssociationMapper fam : fams) {
                fromSql.append(BLANK_AND_BLANK).append(originTableName.sqlWhere()).append(fam.getDbFieldName()).append(fam.getCondition().value()).append(tableName.sqlWhere()).append(fam.getDbAssociationFieldName());
            }
        }
    }
    /* Handle the conditions in the fieldMapper */
    for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
        if (fieldMapper.isHasDelegate() && dtoFieldMap.get(fieldMapper.getDelegate().getFieldName()) != null) {
            dealConditionEqual(whereSql, fieldMapper.getDelegate(), tableName, temp, false, 0);
            continue;
        }
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (fieldMapper.isForeignKey()) {
            boolean b = false;
            if (value == null) {
                value = fieldMapper.getFieldType();
                b = true;
            }
            dealMapperAnnotationIterationForCount(b, value, sqlBuilders, tableName, fieldMapper, temp, index, tableName, indexStr);
        } else {
            if (value == null) {
                continue;
            }
            dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
        }
    }
    /*
		 * Handle the "and" condition in the queryMapper
		 */
    for (ConditionMapper conditionMapper : queryMapper.getConditionMapperCache().values()) {
        Object value = dtoFieldMap.get(conditionMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionMapper(conditionMapper, value, whereSql, tableName, temp, false, 0);
    }
    /*
		 * Handle the "or" condition in the queryMapper
		 */
    for (OrMapper orMapper : queryMapper.getOrMapperCache().values()) {
        Object value = dtoFieldMap.get(orMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionOrMapper(orMapper, value, whereSql, tableName, temp);
    }
}
Also used : ForeignAssociationMapper(indi.mybatis.flying.models.ForeignAssociationMapper) TableName(indi.mybatis.flying.models.TableName) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) OrMapper(indi.mybatis.flying.models.OrMapper) JSONObject(com.alibaba.fastjson.JSONObject) TableMapper(indi.mybatis.flying.models.TableMapper) QueryMapper(indi.mybatis.flying.models.QueryMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Example 2 with ForeignAssociationMapper

use of indi.mybatis.flying.models.ForeignAssociationMapper in project mybatis.flying by limeng32.

the class SqlBuilder method dealMapperAnnotationIterationForSelectAll.

private static void dealMapperAnnotationIterationForSelectAll(boolean objectIsClass, Object object, StringBuilder selectSql, StringBuilder fromSql, StringBuilder whereSql, StringBuilder groupBySql, AtomicInteger index, FlyingModel flyingModel, TableName tn, Mapperable originFieldMapper, String fieldPerfix, TableName lastTableName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException, InstantiationException {
    Class<?> objectType = objectIsClass ? (Class<?>) object : object.getClass();
    String ignoreTag = null;
    String prefix = null;
    String indexStr = null;
    String whiteListTag = null;
    Map<String, Set<AggregateModel>> aggregateMap = null;
    Set<String> groupBySet = null;
    boolean useWhiteList = false;
    if (flyingModel != null) {
        ignoreTag = flyingModel.getIgnoreTag();
        prefix = flyingModel.getPrefix();
        indexStr = flyingModel.getIndex();
        whiteListTag = flyingModel.getWhiteListTag();
        if (whiteListTag != null) {
            useWhiteList = true;
        }
        if (!flyingModel.getAggregate().isEmpty()) {
            aggregateMap = flyingModel.getAggregate();
        }
        if (!flyingModel.getGroupBy().isEmpty()) {
            groupBySet = flyingModel.getGroupBy();
        }
    }
    if (aggregateMap != null) {
    // prepare for v1.1.0 System.out.println("aggregateJson::" + JSONObject.toJSONString(aggregateMap));
    }
    if (groupBySet != null) {
    // prepare for v1.1.0 System.out.println("groupBySet::" + JSONObject.toJSONString(groupBySet));
    }
    Map<String, Object> dtoFieldMap = objectIsClass ? Collections.emptyMap() : PropertyUtils.describe(object);
    Class<?> tempClass = getTableMappedClass(objectType);
    TableMapper tableMapper = buildTableMapper(tempClass);
    QueryMapper queryMapper = buildQueryMapper(objectType, tempClass);
    TableName tableName = null;
    int indexValue = index.getAndIncrement();
    if (lastTableName == null) {
        tableName = new TableName(tableMapper, indexValue, null);
    } else {
        tableName = new TableName(tableMapper, indexValue, lastTableName.getMap());
    }
    /*
		 * If originFieldMapper is null, it is considered to be the first traversal. In
		 * the first iteration, handle fromSql.
		 * 
		 */
    if (originFieldMapper == null) {
        fromSql.append(tableName.sqlSelect());
        if (indexStr != null) {
            fromSql.append(" ").append(indexStr);
        }
    }
    if (flyingModel != null) {
        for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
            if ((!useWhiteList || fieldMapper.getWhiteListTagSet().contains(whiteListTag)) && (!fieldMapper.getIgnoreTagSet().contains(ignoreTag))) {
                dtoFieldMap = dealSelectSql(flyingModel, fieldMapper, dtoFieldMap, index, selectSql, tableName, prefix);
            }
            if (aggregateMap != null) {
                // TODO 在这里处理聚合函数
                if (aggregateMap.containsKey(fieldMapper.getDbFieldName())) {
                    Set<AggregateModel> set = aggregateMap.get(fieldMapper.getDbFieldName());
                    for (AggregateModel am : set) {
                        selectSql.append(am.getFunction()).append("(").append(tableName.sqlWhere()).append(am.getColumn()).append(") as ").append(am.getAlias()).append(COMMA);
                    }
                }
            }
            if (groupBySet != null) {
                // TODO 在这里处理group by
                if (groupBySet.contains(fieldMapper.getDbFieldName())) {
                    if (groupBySql.length() == 0) {
                        groupBySql.append(GROUP_BY_BLANK);
                    }
                    groupBySql.append(tableName.sqlWhere()).append(fieldMapper.getDbFieldName()).append(COMMA);
                }
            }
        }
    }
    /*
		 * If the originFieldMapper and originTableName are not null, it can be
		 * considered a non-first traversal.In the non-first traversal, process
		 * fieldPerfix and fromSql.
		 * 
		 */
    String temp = null;
    if (originFieldMapper != null && tn != null) {
        /* Processing fieldPerfix */
        if (fieldPerfix == null) {
            temp = originFieldMapper.getFieldName();
        } else {
            temp = new StringBuilder(fieldPerfix).append(DOT).append(originFieldMapper.getFieldName()).toString();
        }
        /* Processing fromSql */
        fromSql.append(originFieldMapper.getAssociationType().value()).append(tableName.sqlSelect()).append(BLANK_ON_BLANK).append(tn.sqlWhere()).append(originFieldMapper.getDbFieldNameForJoin()).append(BLANK_EQUAL_BLANK).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
        ForeignAssociationMapper[] fams = originFieldMapper.getForeignAssociationMappers();
        if (fams != null && fams.length > 0) {
            for (ForeignAssociationMapper fam : fams) {
                fromSql.append(BLANK_AND_BLANK).append(tn.sqlWhere()).append(fam.getDbFieldName()).append(fam.getCondition().value()).append(tableName.sqlWhere()).append(fam.getDbAssociationFieldName());
            }
        }
    }
    /* Handle the conditions in the fieldMapper */
    for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
        if (fieldMapper.isHasDelegate() && dtoFieldMap.get(fieldMapper.getDelegate().getFieldName()) != null) {
            dealConditionEqual(whereSql, fieldMapper.getDelegate(), tableName, temp, false, 0);
            continue;
        }
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            continue;
        }
        if (fieldMapper.isForeignKey()) {
            dealMapperAnnotationIterationForSelectAll(value instanceof Class<?>, value, selectSql, fromSql, whereSql, null, index, flyingModel == null ? (null) : (flyingModel.getProperties().get(fieldMapper.getFieldName())), tableName, fieldMapper, temp, tableName);
        } else {
            dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
        }
    }
    /* Handle the "and" condition in the queryMapper */
    for (ConditionMapper conditionMapper : queryMapper.getConditionMapperCache().values()) {
        Object value = dtoFieldMap.get(conditionMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionMapper(conditionMapper, value, whereSql, tableName, temp, false, 0);
    }
    /* Handle the "or" condition in the queryMapper */
    for (OrMapper orMapper : queryMapper.getOrMapperCache().values()) {
        Object value = dtoFieldMap.get(orMapper.getFieldName());
        if (value == null) {
            continue;
        }
        dealConditionOrMapper(orMapper, value, whereSql, tableName, temp);
    }
}
Also used : AggregateModel(indi.mybatis.flying.models.AggregateModel) Set(java.util.Set) ForeignAssociationMapper(indi.mybatis.flying.models.ForeignAssociationMapper) TableName(indi.mybatis.flying.models.TableName) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) OrMapper(indi.mybatis.flying.models.OrMapper) JSONObject(com.alibaba.fastjson.JSONObject) TableMapper(indi.mybatis.flying.models.TableMapper) QueryMapper(indi.mybatis.flying.models.QueryMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)2 ConditionMapper (indi.mybatis.flying.models.ConditionMapper)2 FieldMapper (indi.mybatis.flying.models.FieldMapper)2 ForeignAssociationMapper (indi.mybatis.flying.models.ForeignAssociationMapper)2 OrMapper (indi.mybatis.flying.models.OrMapper)2 QueryMapper (indi.mybatis.flying.models.QueryMapper)2 TableMapper (indi.mybatis.flying.models.TableMapper)2 TableName (indi.mybatis.flying.models.TableName)2 AggregateModel (indi.mybatis.flying.models.AggregateModel)1 Set (java.util.Set)1