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();
}
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();
}
Aggregations