Search in sources :

Example 6 with FieldMapper

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

the class SqlBuilder method buildInsertSql.

/**
 * 由传入的对象生成insert sql语句
 *
 * @param object
 *            pojo @return String @throws IllegalAccessException @throws
 *            IllegalArgumentException @throws NoSuchFieldException @throws
 *            SecurityException @throws NoSuchMethodException @throws
 *            InvocationTargetException @throws Exception
 *            RuntimeException @throws
 */
public static String buildInsertSql(Object object, FlyingModel flyingModel) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    String ignoreTag = flyingModel.getIgnoreTag();
    KeyHandler keyHandler = flyingModel.getKeyHandler();
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    String tableName = tableMapper.getTableName();
    tableSql = new StringBuffer();
    StringBuffer valueSql = new StringBuffer();
    tableSql.append(INSERT_INTO_).append(tableName).append(_OPENPAREN);
    valueSql.append(VALUES_OPENPAREN);
    boolean allFieldNull = true;
    boolean uniqueKeyHandled = false;
    for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (!fieldMapper.isInsertAble() || ((value == null && !fieldMapper.isOpVersionLock()) || (fieldMapper.getIgnoreTagSet().contains(ignoreTag)))) {
            continue;
        } else if (((FieldMapper) fieldMapper).isOpVersionLock()) {
            value = 0;
            ReflectHelper.setValueByFieldName(object, fieldMapper.getFieldName(), value);
        }
        allFieldNull = false;
        tableSql.append(fieldMapper.getDbFieldName()).append(COMMA);
        valueSql.append(POUND_OPENBRACE);
        if (fieldMapper.isForeignKey() || fieldMapper.isCrossDbForeignKey()) {
            valueSql.append(fieldMapper.getFieldName()).append(DOT).append(fieldMapper.getForeignFieldName());
        } else {
            valueSql.append(fieldMapper.getFieldName());
        }
        valueSql.append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString());
        if (fieldMapper.getTypeHandlerPath() != null) {
            valueSql.append(COMMA_TYPEHANDLER_EQUAL).append(fieldMapper.getTypeHandlerPath());
        }
        if (fieldMapper.isUniqueKey()) {
            uniqueKeyHandled = true;
            if (keyHandler != null) {
                handleInsertSql(keyHandler, valueSql, fieldMapper, object, uniqueKeyHandled);
            }
        }
        valueSql.append(CLOSEBRACE_COMMA);
    }
    if (keyHandler != null && !uniqueKeyHandled) {
        FieldMapper temp = tableMapper.getUniqueKeyNames()[0];
        tableSql.append(temp.getDbFieldName()).append(COMMA);
        handleInsertSql(keyHandler, valueSql, temp, object, uniqueKeyHandled);
    }
    if (allFieldNull) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullField);
    }
    tableSql.delete(tableSql.lastIndexOf(COMMA), tableSql.lastIndexOf(COMMA) + 1);
    valueSql.delete(valueSql.lastIndexOf(COMMA), valueSql.lastIndexOf(COMMA) + 1);
    return tableSql.append(CLOSEPAREN_).append(valueSql).append(CLOSEPAREN).toString();
}
Also used : BuildSqlException(indi.mybatis.flying.exception.BuildSqlException) TableMapper(indi.mybatis.flying.models.TableMapper) KeyHandler(indi.mybatis.flying.type.KeyHandler) FieldMapper(indi.mybatis.flying.models.FieldMapper)

Example 7 with FieldMapper

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

the class SqlBuilder method buildDeleteSql.

/**
 * 由传入的对象生成delete sql语句
 *
 * @param object
 *            pojo
 * @return sql
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws RuntimeException
 */
public static String buildDeleteSql(Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (null == object) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
    }
    Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
    TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
    String tableName = tableMapper.getTableName();
    StringBuffer sql = new StringBuffer();
    sql.append(DELETE_FROM_).append(tableName).append(WHERE_);
    for (FieldMapper fieldMapper : tableMapper.getUniqueKeyNames()) {
        sql.append(fieldMapper.getDbFieldName());
        Object value = dtoFieldMap.get(fieldMapper.getFieldName());
        if (value == null) {
            throw new BuildSqlException(new StringBuffer(BuildSqlExceptionEnum.deleteUniqueKeyIsNull.toString()).append(fieldMapper.getDbFieldName()).toString());
        }
        sql.append(EQUAL_POUND_OPENBRACE).append(fieldMapper.getFieldName()).append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString()).append(CLOSEBRACE_AND_);
    }
    for (FieldMapper f : tableMapper.getOpVersionLocks()) {
        sql.append(f.getDbFieldName()).append(EQUAL_POUND_OPENBRACE).append(f.getFieldName()).append(CLOSEBRACE_AND_);
    }
    sql.delete(sql.lastIndexOf(AND), sql.lastIndexOf(AND) + 3);
    return sql.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