use of indi.mybatis.flying.models.TableMapper 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);
}
}
}
}
}
use of indi.mybatis.flying.models.TableMapper 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();
}
use of indi.mybatis.flying.models.TableMapper 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;
}
use of indi.mybatis.flying.models.TableMapper 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();
}
use of indi.mybatis.flying.models.TableMapper 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();
}
Aggregations