Search in sources :

Example 1 with BuildSqlException

use of indi.mybatis.flying.exception.BuildSqlException 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 2 with BuildSqlException

use of indi.mybatis.flying.exception.BuildSqlException 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)

Example 3 with BuildSqlException

use of indi.mybatis.flying.exception.BuildSqlException 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 4 with BuildSqlException

use of indi.mybatis.flying.exception.BuildSqlException in project mybatis.flying by limeng32.

the class SqlBuilder method buildSelectOneSql.

/**
 * 由传入的对象生成query sql语句
 *
 * @param object
 *            pojo
 * @return sql
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws RuntimeException
 */
public static String buildSelectOneSql(Object object, FlyingModel flyingModel) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (null == object) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
    }
    String ignoreTag = flyingModel.getIgnoreTag();
    if (object instanceof Conditionable) {
        ((Conditionable) object).setLimiter(null);
    }
    selectSql = new StringBuffer(SELECT_);
    fromSql = new StringBuffer(FROM);
    whereSql = new StringBuffer(WHERE_);
    ai = new AtomicInteger(0);
    dealMapperAnnotationIterationForSelectAll(object, selectSql, fromSql, whereSql, null, null, null, ai, null, ignoreTag);
    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).append(_LIMIT_1).toString();
}
Also used : Conditionable(indi.mybatis.flying.models.Conditionable) BuildSqlException(indi.mybatis.flying.exception.BuildSqlException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with BuildSqlException

use of indi.mybatis.flying.exception.BuildSqlException in project mybatis.flying by limeng32.

the class SqlBuilder method buildSelectAllSql.

/**
 * 由传入的对象生成query sql语句
 *
 * @param object
 *            pojo
 * @return sql
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws RuntimeException
 */
public static String buildSelectAllSql(Object object, FlyingModel flyingModel) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (null == object) {
        throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
    }
    String ignoreTag = flyingModel.getIgnoreTag();
    selectSql = new StringBuffer(SELECT_);
    fromSql = new StringBuffer(FROM);
    whereSql = new StringBuffer(WHERE_);
    ai = new AtomicInteger(0);
    dealMapperAnnotationIterationForSelectAll(object, selectSql, fromSql, whereSql, null, null, null, ai, null, ignoreTag);
    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 : BuildSqlException(indi.mybatis.flying.exception.BuildSqlException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

BuildSqlException (indi.mybatis.flying.exception.BuildSqlException)7 TableMapper (indi.mybatis.flying.models.TableMapper)5 FieldMapper (indi.mybatis.flying.models.FieldMapper)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Conditionable (indi.mybatis.flying.models.Conditionable)1 TableName (indi.mybatis.flying.models.TableName)1 KeyHandler (indi.mybatis.flying.type.KeyHandler)1