Search in sources :

Example 1 with FieldMapper

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

the class SqlBuilder method buildConditionMapper.

private static void buildConditionMapper(ConditionMapper conditionMapper, ConditionMapperAnnotation conditionMapperAnnotation, Class<?> pojoClass, Field field) {
    conditionMapper.setFieldName(field.getName());
    conditionMapper.setDbFieldName(conditionMapperAnnotation.dbFieldName());
    conditionMapper.setConditionType(conditionMapperAnnotation.conditionType());
    conditionMapper.setSubTarget(conditionMapperAnnotation.subTarget());
    conditionMapper.setTypeHandlerPath(conditionMapperAnnotation.dbAssociationTypeHandler());
    for (Field pojoField : pojoClass.getDeclaredFields()) {
        for (Annotation oan : pojoField.getDeclaredAnnotations()) {
            boolean b1 = oan instanceof FieldMapperAnnotation && ((FieldMapperAnnotation) oan).dbFieldName().equalsIgnoreCase(conditionMapperAnnotation.dbFieldName());
            boolean b2 = oan instanceof Column && (FieldMapper.getColumnName((Column) oan, pojoField)).equalsIgnoreCase(conditionMapperAnnotation.dbFieldName());
            boolean b3 = (conditionMapper.getSubTarget() != null) && (!Void.class.equals(conditionMapper.getSubTarget()));
            if (b1 || b2 || b3) {
                FieldMapper fieldMapper = new FieldMapper();
                if (b3) {
                    if (!tableMapperCache.containsKey(conditionMapper.getSubTarget())) {
                        buildTableMapper(conditionMapper.getSubTarget());
                    }
                    TableMapper tableMapper = tableMapperCache.get(conditionMapper.getSubTarget());
                    Map<String, FieldMapper> fieldMapperCache = tableMapper.getFieldMapperCache();
                    for (Map.Entry<String, FieldMapper> e : fieldMapperCache.entrySet()) {
                        if (conditionMapper.getDbFieldName().equalsIgnoreCase(e.getValue().getDbFieldName())) {
                            fieldMapper = e.getValue();
                            break;
                        }
                    }
                } else {
                    fieldMapper = new FieldMapper();
                    fieldMapper.buildMapper(pojoField);
                }
                conditionMapper.setFieldType(fieldMapper.getFieldType());
                conditionMapper.setJdbcType(fieldMapper.getJdbcType());
                if (!"".equals(fieldMapper.getDbAssociationUniqueKey())) {
                    conditionMapper.setDbAssociationUniqueKey(fieldMapper.getDbAssociationUniqueKey());
                    conditionMapper.setForeignKey(true);
                }
                if (conditionMapper.isForeignKey() && (!ConditionType.NullOrNot.equals(conditionMapper.getConditionType()))) {
                    if (!tableMapperCache.containsKey(pojoField.getType())) {
                        buildTableMapper(pojoField.getType());
                    }
                    TableMapper tm = tableMapperCache.get(pojoField.getType());
                    String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbAssociationUniqueKey()).getFieldName();
                    conditionMapper.setForeignFieldName(foreignFieldName);
                }
                if (!"".equals(fieldMapper.getDbCrossedAssociationUniqueKey())) {
                    conditionMapper.setDbCrossedAssociationUniqueKey(fieldMapper.getDbCrossedAssociationUniqueKey());
                    fieldMapper.setCrossDbForeignKey(true);
                }
                if (fieldMapper.isCrossDbForeignKey()) {
                    if (!tableMapperCache.containsKey(pojoField.getType())) {
                        buildTableMapper(pojoField.getType());
                    }
                    TableMapper tm = tableMapperCache.get(pojoField.getType());
                    String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbCrossedAssociationUniqueKey()).getFieldName();
                    conditionMapper.setForeignFieldName(foreignFieldName);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Column(javax.persistence.Column) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ConditionMapperAnnotation(indi.mybatis.flying.annotations.ConditionMapperAnnotation)

Example 2 with FieldMapper

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

the class SqlBuilder method buildSelectSql.

/**
 * 由传入的对象生成query sql语句
 *
 * @param clazz
 *            pojo Class
 * @return sql
 */
public static String buildSelectSql(Class<?> clazz, FlyingModel flyingModel) {
    String ignoreTag = flyingModel.getIgnoreTag();
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(clazz));
    String tableName = tableMapper.getTableName();
    selectSql = new StringBuffer(SELECT_);
    for (Mapperable fieldMapper : tableMapper.getFieldMapperCache().values()) {
        if ((!fieldMapper.getIgnoreTagSet().contains(ignoreTag))) {
            selectSql.append(fieldMapper.getDbFieldName()).append(COMMA);
        }
    }
    if (selectSql.indexOf(COMMA) > -1) {
        selectSql.delete(selectSql.lastIndexOf(COMMA), selectSql.lastIndexOf(COMMA) + 1);
    }
    selectSql.append(FROM).append(tableName);
    whereSql = new StringBuffer(WHERE_);
    for (FieldMapper fieldMapper : tableMapper.getUniqueKeyNames()) {
        whereSql.append(fieldMapper.getDbFieldName());
        whereSql.append(EQUAL_POUND_OPENBRACE).append(fieldMapper.getFieldName()).append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString()).append(CLOSEBRACE_AND_);
    }
    whereSql.delete(whereSql.lastIndexOf(AND), whereSql.lastIndexOf(AND) + 3);
    return selectSql.append(whereSql).toString();
}
Also used : TableMapper(indi.mybatis.flying.models.TableMapper) Mapperable(indi.mybatis.flying.models.Mapperable) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Example 3 with FieldMapper

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

the class SqlBuilder method buildTableMapper.

/**
 * 由传入的dto对象的class构建TableMapper对象,构建好的对象存入缓存中,以后使用时直接从缓存中获取
 *
 * @param dtoClass
 * @return TableMapper
 */
private static TableMapper buildTableMapper(Class<?> dtoClass) {
    Map<String, FieldMapper> fieldMapperCache = null;
    Field[] fields = dtoClass.getDeclaredFields();
    FieldMapper fieldMapper = null;
    TableMapper tableMapper = null;
    tableMapper = tableMapperCache.get(dtoClass);
    if (tableMapper != null) {
        return tableMapper;
    }
    tableMapper = new TableMapper();
    tableMapper.setClazz(dtoClass);
    List<FieldMapper> uniqueKeyList = new ArrayList<FieldMapper>();
    List<FieldMapper> opVersionLockList = new ArrayList<FieldMapper>();
    Annotation[] classAnnotations = dtoClass.getDeclaredAnnotations();
    for (Annotation an : classAnnotations) {
        if (an instanceof TableMapperAnnotation) {
            tableMapper.setTableMapperAnnotation((TableMapperAnnotation) an);
        } else if (an instanceof Table) {
            tableMapper.setTable((Table) an);
        }
    }
    fieldMapperCache = new WeakHashMap<String, FieldMapper>(16);
    for (Field field : fields) {
        fieldMapper = new FieldMapper();
        boolean b = fieldMapper.buildMapper(field);
        if (!b) {
            continue;
        }
        switch(fieldMapper.getOpLockType()) {
            case Version:
                fieldMapper.setOpVersionLock(true);
                break;
            default:
                break;
        }
        if (fieldMapper.isUniqueKey()) {
            uniqueKeyList.add(fieldMapper);
        }
        if (fieldMapper.getIgnoreTag().length > 0) {
            for (String t : fieldMapper.getIgnoreTag()) {
                fieldMapper.getIgnoreTagSet().add(t);
            }
        }
        if (!"".equals(fieldMapper.getDbAssociationUniqueKey())) {
            fieldMapper.setForeignKey(true);
        }
        if (fieldMapper.isForeignKey()) {
            if (!tableMapperCache.containsKey(field.getType())) {
                buildTableMapper(field.getType());
            }
            TableMapper tm = tableMapperCache.get(field.getType());
            String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbAssociationUniqueKey()).getFieldName();
            fieldMapper.setForeignFieldName(foreignFieldName);
        }
        if (!"".equals(fieldMapper.getDbCrossedAssociationUniqueKey())) {
            fieldMapper.setCrossDbForeignKey(true);
        }
        if (fieldMapper.isCrossDbForeignKey()) {
            if (!tableMapperCache.containsKey(field.getType())) {
                buildTableMapper(field.getType());
            }
            TableMapper tm = tableMapperCache.get(field.getType());
            String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbCrossedAssociationUniqueKey()).getFieldName();
            fieldMapper.setForeignFieldName(foreignFieldName);
        }
        if (fieldMapper.isOpVersionLock()) {
            opVersionLockList.add(fieldMapper);
        }
        fieldMapperCache.put(field.getName(), fieldMapper);
    }
    tableMapper.setFieldMapperCache(fieldMapperCache);
    tableMapper.setUniqueKeyNames(uniqueKeyList.toArray(new FieldMapper[uniqueKeyList.size()]));
    tableMapper.setOpVersionLocks(opVersionLockList.toArray(new FieldMapper[opVersionLockList.size()]));
    tableMapper.buildTableName();
    tableMapperCache.put(dtoClass, tableMapper);
    return tableMapper;
}
Also used : Table(javax.persistence.Table) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ArrayList(java.util.ArrayList) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ConditionMapperAnnotation(indi.mybatis.flying.annotations.ConditionMapperAnnotation) Field(java.lang.reflect.Field) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Example 4 with FieldMapper

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

the class SqlBuilder method buildUpdatePersistentSql.

/**
 * 由传入的对象生成update持久态对象的 sql语句
 *
 * @param object
 *            pojo
 * @return sql
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws RuntimeException
 */
public static String buildUpdatePersistentSql(Object object, FlyingModel flyingModel) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (null == object) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
    }
    String ignoreTag = flyingModel.getIgnoreTag();
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    String tableName = tableMapper.getTableName();
    tableSql = new StringBuffer();
    whereSql = new StringBuffer(WHERE_);
    tableSql.append(UPDATE_).append(tableName).append(_SET_);
    boolean allFieldNull = true;
    for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
        if (!fieldMapper.isUpdateAble() || (fieldMapper.getIgnoreTagSet().contains(ignoreTag))) {
            continue;
        }
        allFieldNull = false;
        tableSql.append(fieldMapper.getDbFieldName()).append(EQUAL_POUND_OPENBRACE);
        if (fieldMapper.isForeignKey() || fieldMapper.isCrossDbForeignKey()) {
            tableSql.append(fieldMapper.getFieldName()).append(DOT).append(fieldMapper.getForeignFieldName());
        } else {
            tableSql.append(fieldMapper.getFieldName());
        }
        tableSql.append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString());
        if (fieldMapper.getTypeHandlerPath() != null) {
            tableSql.append(COMMA_TYPEHANDLER_EQUAL).append(fieldMapper.getTypeHandlerPath());
        }
        tableSql.append(CLOSEBRACE);
        if (fieldMapper.isOpVersionLock()) {
            tableSql.append(PLUS_1);
        }
        tableSql.append(COMMA);
    }
    if (allFieldNull) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullField);
    }
    tableSql.delete(tableSql.lastIndexOf(COMMA), tableSql.lastIndexOf(COMMA) + 1);
    for (FieldMapper fieldMapper : tableMapper.getUniqueKeyNames()) {
        whereSql.append(fieldMapper.getDbFieldName());
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            throw new BuildSqlException(new StringBuffer(BuildSqlExceptionEnum.updatePersistentUniqueKeyIsNull.toString()).append(fieldMapper.getDbFieldName()).toString());
        }
        whereSql.append(EQUAL_POUND_OPENBRACE).append(fieldMapper.getFieldName()).append(COMMA_JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString()).append(CLOSEBRACE_AND_);
    }
    for (FieldMapper f : tableMapper.getOpVersionLocks()) {
        whereSql.append(f.getDbFieldName()).append(EQUAL_POUND_OPENBRACE).append(f.getFieldName()).append(CLOSEBRACE_AND_);
    }
    whereSql.delete(whereSql.lastIndexOf(AND), whereSql.lastIndexOf(AND) + 3);
    return tableSql.append(whereSql).toString();
}
Also used : BuildSqlException(indi.mybatis.flying.exception.BuildSqlException) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Example 5 with FieldMapper

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

the class SqlBuilder method buildUpdateSql.

/**
 * 由传入的对象生成update sql语句
 *
 * @param object
 *            pojo
 * @return sql
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws RuntimeException
 */
public static String buildUpdateSql(Object object, FlyingModel flyingModel) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (null == object) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
    }
    String ignoreTag = flyingModel.getIgnoreTag();
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    String tableName = tableMapper.getTableName();
    tableSql = new StringBuffer();
    whereSql = new StringBuffer(WHERE_);
    tableSql.append(UPDATE_).append(tableName).append(_SET_);
    boolean allFieldNull = true;
    for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (!fieldMapper.isUpdateAble() || (value == null || (fieldMapper.getIgnoreTagSet().contains(ignoreTag)))) {
            continue;
        }
        allFieldNull = false;
        tableSql.append(fieldMapper.getDbFieldName()).append(EQUAL_POUND_OPENBRACE);
        if (fieldMapper.isForeignKey() || fieldMapper.isCrossDbForeignKey()) {
            tableSql.append(fieldMapper.getFieldName()).append(DOT).append(fieldMapper.getForeignFieldName());
        } else {
            tableSql.append(fieldMapper.getFieldName());
        }
        tableSql.append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString());
        if (fieldMapper.getTypeHandlerPath() != null) {
            tableSql.append(COMMA_TYPEHANDLER_EQUAL).append(fieldMapper.getTypeHandlerPath());
        }
        tableSql.append(CLOSEBRACE);
        if (fieldMapper.isOpVersionLock()) {
            tableSql.append(PLUS_1);
        }
        tableSql.append(COMMA);
    }
    if (allFieldNull) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullField);
    }
    tableSql.delete(tableSql.lastIndexOf(COMMA), tableSql.lastIndexOf(COMMA) + 1);
    for (FieldMapper fieldMapper : tableMapper.getUniqueKeyNames()) {
        whereSql.append(fieldMapper.getDbFieldName());
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            throw new BuildSqlException(new StringBuffer(BuildSqlExceptionEnum.updateUniqueKeyIsNull.toString()).append(fieldMapper.getDbFieldName()).toString());
        }
        whereSql.append(EQUAL_POUND_OPENBRACE).append(fieldMapper.getFieldName()).append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString()).append(CLOSEBRACE_AND_);
    }
    for (FieldMapper f : tableMapper.getOpVersionLocks()) {
        whereSql.append(f.getDbFieldName()).append(EQUAL_POUND_OPENBRACE).append(f.getFieldName()).append(CLOSEBRACE_AND_);
    }
    whereSql.delete(whereSql.lastIndexOf(AND), whereSql.lastIndexOf(AND) + 3);
    return tableSql.append(whereSql).toString();
}
Also used : BuildSqlException(indi.mybatis.flying.exception.BuildSqlException) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Aggregations

FieldMapper (indi.mybatis.flying.models.FieldMapper)7 TableMapper (indi.mybatis.flying.models.TableMapper)7 BuildSqlException (indi.mybatis.flying.exception.BuildSqlException)4 ConditionMapperAnnotation (indi.mybatis.flying.annotations.ConditionMapperAnnotation)2 FieldMapperAnnotation (indi.mybatis.flying.annotations.FieldMapperAnnotation)2 TableMapperAnnotation (indi.mybatis.flying.annotations.TableMapperAnnotation)2 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 Mapperable (indi.mybatis.flying.models.Mapperable)1 KeyHandler (indi.mybatis.flying.type.KeyHandler)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Column (javax.persistence.Column)1 Table (javax.persistence.Table)1