Search in sources :

Example 1 with TableName

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

the class SqlBuilder method handleWhereSql.

private static void handleWhereSql(StringBuffer whereSql, Mapperable mapper, TableName tableName, String fieldNamePrefix) {
    if (tableName != null) {
        if (mapper.getSubTarget() == null || Void.class.equals(mapper.getSubTarget())) {
            whereSql.append(tableName.sqlWhere());
        } else {
            TableName temp = tableName.getMap().get(mapper.getSubTarget());
            whereSql.append(new StringBuffer(temp.getTableMapper().getTableName()).append("_").append(temp.getIndex()).append("."));
        }
    }
    whereSql.append(mapper.getDbFieldName());
}
Also used : TableName(indi.mybatis.flying.models.TableName)

Example 2 with TableName

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

the class SqlBuilder method dealMapperAnnotationIterationForCount.

private static void dealMapperAnnotationIterationForCount(Object object, StringBuffer fromSql, StringBuffer whereSql, TableName originTableName, Mapperable originFieldMapper, String fieldPerfix, AtomicInteger index, TableName lastTableName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    QueryMapper queryMapper = buildQueryMapper(object.getClass(), getTableMappedClass(object.getClass()));
    TableName tableName = new TableName(tableMapper, index.getAndIncrement(), lastTableName.getMap());
    /*
		 * 在第一次遍历中,处理好fromSql。 如果originFieldMapper为null则可认为是第一次遍历
		 */
    if (originFieldMapper == null) {
        fromSql.append(tableName.sqlSelect());
    }
    /*
		 * 在非第一次遍历中,处理fieldPerfix和fromSql。
		 * 如果originFieldMapper和originTableName均不为null则可认为是非第一次遍历
		 */
    String temp = null;
    if (originFieldMapper != null && originTableName != null) {
        /* 处理fieldPerfix */
        temp = originFieldMapper.getFieldName();
        if (fieldPerfix != null) {
            temp = fieldPerfix + DOT + temp;
        }
        /* 处理fromSql */
        fromSql.append(_LEFT_JOIN_).append(tableName.sqlSelect()).append(_ON_).append(originTableName.sqlWhere()).append(originFieldMapper.getDbFieldName()).append(_EQUAL_).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
    }
    /* 处理fieldMapper中的条件 */
    for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            continue;
        }
        /* 此处当value拥有TableMapper或QueryMapper标注时,开始进行迭代 */
        if (fieldMapper.isForeignKey()) {
            dealMapperAnnotationIterationForCount(value, fromSql, whereSql, tableName, fieldMapper, temp, index, tableName);
        } else {
            dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
        }
    }
    /* 处理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);
    }
    /* 处理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 : TableName(indi.mybatis.flying.models.TableName) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) OrMapper(indi.mybatis.flying.models.OrMapper) TableMapper(indi.mybatis.flying.models.TableMapper) Mapperable(indi.mybatis.flying.models.Mapperable) QueryMapper(indi.mybatis.flying.models.QueryMapper)

Example 3 with TableName

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

the class SqlBuilder method buildCountSql.

/**
 * 由传入的对象生成count sql语句
 *
 * @param object
 *            pojo
 * @return sql
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws RuntimeException
 */
public static String buildCountSql(Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (null == object) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
    }
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    ai = new AtomicInteger(0);
    TableName tableName = new TableName(tableMapper, 0, null);
    selectSql = new StringBuffer();
    selectSql.append(SELECT_COUNT_OPENPAREN).append(tableName.sqlWhere());
    /*
		 * 如果有且只有一个主键,采用select count("主键")的方式;如果无主键或有多个主键(联合主键),采用select
		 * count(*)的方式。
		 */
    if (tableMapper.getUniqueKeyNames().length == 1) {
        selectSql.append(tableMapper.getUniqueKeyNames()[0].getDbFieldName());
    } else {
        selectSql.append(ASTERISK);
    }
    selectSql.append(CLOSEPAREN);
    fromSql = new StringBuffer(FROM);
    whereSql = new StringBuffer(WHERE_);
    dealMapperAnnotationIterationForCount(object, fromSql, whereSql, null, null, null, ai, tableName);
    if (selectSql.indexOf(COMMA) > -1) {
        selectSql.delete(selectSql.lastIndexOf(COMMA), selectSql.lastIndexOf(COMMA) + 1);
    }
    if (WHERE_.equals(whereSql.toString())) {
        whereSql = new StringBuffer();
    } else if (whereSql.indexOf(AND) > -1) {
        whereSql.delete(whereSql.lastIndexOf(AND), whereSql.lastIndexOf(AND) + 3);
    }
    return selectSql.append(fromSql).append(whereSql).toString();
}
Also used : TableName(indi.mybatis.flying.models.TableName) BuildSqlException(indi.mybatis.flying.exception.BuildSqlException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TableMapper(indi.mybatis.flying.models.TableMapper)

Example 4 with TableName

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

the class SqlBuilder method dealMapperAnnotationIterationForSelectAll.

private static void dealMapperAnnotationIterationForSelectAll(Object object, StringBuffer selectSql, StringBuffer fromSql, StringBuffer whereSql, TableName originTableName, Mapperable originFieldMapper, String fieldPerfix, AtomicInteger index, TableName lastTableName, String ignoreTag) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    QueryMapper queryMapper = buildQueryMapper(object.getClass(), getTableMappedClass(object.getClass()));
    TableName tableName = null;
    if (lastTableName == null) {
        tableName = new TableName(tableMapper, index.getAndIncrement(), null);
    } else {
        tableName = new TableName(tableMapper, index.getAndIncrement(), lastTableName.getMap());
    }
    /*
		 * 在第一次遍历中,处理好selectSql和fromSql。 如果originFieldMapper为null则可认为是第一次遍历
		 */
    if (originFieldMapper == null) {
        fromSql.append(tableName.sqlSelect());
        for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
            if ((!fieldMapper.getIgnoreTagSet().contains(ignoreTag))) {
                selectSql.append(tableName.sqlWhere()).append(fieldMapper.getDbFieldName()).append(COMMA);
            }
        }
    }
    /*
		 * 在非第一次遍历中,处理fieldPerfix和fromSql。
		 * 如果originFieldMapper和originTableName均不为null则可认为是非第一次遍历
		 */
    String temp = null;
    if (originFieldMapper != null && originTableName != null) {
        /* 处理fieldPerfix */
        temp = originFieldMapper.getFieldName();
        if (fieldPerfix != null) {
            temp = fieldPerfix + DOT + temp;
        }
        /* 处理fromSql */
        fromSql.append(_LEFT_JOIN_).append(tableName.sqlSelect()).append(_ON_).append(originTableName.sqlWhere()).append(originFieldMapper.getDbFieldName()).append(_EQUAL_).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
    }
    /* 处理fieldMapper中的条件 */
    for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            continue;
        }
        /* 此处当value拥有TableMapper或QueryMapper标注时,开始进行迭代 */
        if (fieldMapper.isForeignKey()) {
            dealMapperAnnotationIterationForSelectAll(value, selectSql, fromSql, whereSql, tableName, fieldMapper, temp, index, tableName, null);
        } else {
            dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
        }
    }
    /* 处理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);
    }
    /* 处理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 : TableName(indi.mybatis.flying.models.TableName) ConditionMapper(indi.mybatis.flying.models.ConditionMapper) OrMapper(indi.mybatis.flying.models.OrMapper) TableMapper(indi.mybatis.flying.models.TableMapper) Mapperable(indi.mybatis.flying.models.Mapperable) QueryMapper(indi.mybatis.flying.models.QueryMapper)

Aggregations

TableName (indi.mybatis.flying.models.TableName)4 TableMapper (indi.mybatis.flying.models.TableMapper)3 ConditionMapper (indi.mybatis.flying.models.ConditionMapper)2 Mapperable (indi.mybatis.flying.models.Mapperable)2 OrMapper (indi.mybatis.flying.models.OrMapper)2 QueryMapper (indi.mybatis.flying.models.QueryMapper)2 BuildSqlException (indi.mybatis.flying.exception.BuildSqlException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1