use of indi.mybatis.flying.type.KeyHandler in project mybatis.flying by limeng32.
the class CookOriginalSql method fetchFlyingFeature.
public static FlyingModel fetchFlyingFeature(String originalSql) {
if (flyingModelCache.get(originalSql) != null) {
return flyingModelCache.get(originalSql);
}
FlyingModel ret = new FlyingModel();
String extension = null;
if (null != originalSql && originalSql.startsWith(FLYING) && originalSql.indexOf(':') > -1) {
String dataSourceIdAndConnectionCatalog = null;
String s1 = null;
if ((originalSql.startsWith(FLYING_LEFTBRACKET) || originalSql.startsWith(FLYING_QUESTIONMARK_LEFTBRACKET)) && originalSql.indexOf(')') > 0) {
String s0 = originalSql.substring(0, originalSql.indexOf(')') + 1);
dataSourceIdAndConnectionCatalog = s0.substring(s0.indexOf('(') + 1, s0.lastIndexOf(')'));
s1 = originalSql.substring(0, originalSql.indexOf(':', originalSql.indexOf(')')));
} else {
s1 = originalSql.substring(0, originalSql.indexOf(':'));
}
if (FLYING.equals(s1) || FLYING_QUESTIONMARK.equals(s1) || s1.startsWith(FLYING_LEFTBRACKET) || originalSql.startsWith(FLYING_QUESTIONMARK_LEFTBRACKET)) {
String s2 = null;
if (s1.startsWith(FLYING_LEFTBRACKET) || originalSql.startsWith(FLYING_QUESTIONMARK_LEFTBRACKET)) {
s2 = originalSql.substring(originalSql.indexOf(":", originalSql.indexOf(')')) + 1, originalSql.length());
} else {
s2 = originalSql.substring(originalSql.indexOf(':') + 1, originalSql.length());
}
String actionTypeStr = null;
if (s2.indexOf(':') > -1) {
actionTypeStr = s2.substring(0, s2.indexOf(':'));
} else {
actionTypeStr = s2;
}
if (actionTypeStr.endsWith(")") && actionTypeStr.indexOf('(') != -1) {
extension = actionTypeStr.substring(actionTypeStr.lastIndexOf('(') + 1, actionTypeStr.length() - 1);
actionTypeStr = actionTypeStr.substring(0, actionTypeStr.lastIndexOf('('));
}
ActionType actionType = ActionType.valueOf(actionTypeStr);
if (actionType != null) {
ret.setHasFlyingFeature(true);
if (dataSourceIdAndConnectionCatalog != null && dataSourceIdAndConnectionCatalog.indexOf(':') != -1) {
ret.setDataSourceId(dataSourceIdAndConnectionCatalog.substring(0, dataSourceIdAndConnectionCatalog.indexOf(':')));
ret.setConnectionCatalog(dataSourceIdAndConnectionCatalog.substring(dataSourceIdAndConnectionCatalog.indexOf(':') + 1, dataSourceIdAndConnectionCatalog.length()));
}
ret.setActionType(actionType);
if (s2.indexOf(':') > -1) {
String s3 = s2.substring(s2.indexOf(':') + 1, s2.length());
String ignoreTag = null;
if (s3.indexOf(':') > -1) {
ignoreTag = s3.substring(0, s3.indexOf(':'));
} else {
ignoreTag = s3;
}
ret.setIgnoreTag(ignoreTag);
}
if (ActionType.insert.equals(actionType) && extension != null) {
KeyGeneratorType keyGeneratorType = null;
if (extension.indexOf(".") == -1) {
try {
keyGeneratorType = KeyGeneratorType.valueOf(extension);
} catch (IllegalArgumentException e) {
logger.error(new StringBuffer(AutoMapperExceptionEnum.wrongKeyGeneratorType.description()).append(originalSql).append(" because of ").append(e).toString());
}
ret.setKeyGeneratorType(keyGeneratorType);
if (keyGeneratorType != null) {
KeyHandler keyHandler;
switch(keyGeneratorType) {
case uuid:
keyHandler = UuidKeyHandler.getInstance();
break;
case uuid_no_line:
keyHandler = UuidWithoutLineKeyHandler.getInstance();
break;
case millisecond:
keyHandler = MilliSecondKeyHandler.getInstance();
break;
case snowflake:
keyHandler = SnowFlakeKeyHandler.getInstance();
break;
default:
keyHandler = null;
break;
}
ret.setKeyHandler(keyHandler);
}
} else {
try {
@SuppressWarnings("unchecked") Class<? extends KeyHandler> clazz = (Class<? extends KeyHandler>) Class.forName(extension);
ret.setKeyGeneratorType(KeyGeneratorType.custom);
ret.setKeyHandler(clazz.newInstance());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
logger.error(new StringBuffer(AutoMapperExceptionEnum.wrongCustomKeyGenerator.description()).append(originalSql).append(" because of ").append(e).toString());
}
}
}
flyingModelCache.put(originalSql, ret);
return ret;
}
}
}
ret.setHasFlyingFeature(false);
flyingModelCache.put(originalSql, ret);
return ret;
}
use of indi.mybatis.flying.type.KeyHandler 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();
}
Aggregations