package com.skyline.energy.definition;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.ClassUtils;
import com.skyline.energy.annotation.ReturnId;
import com.skyline.energy.annotation.Update;
import com.skyline.energy.exception.DaoGenerateException;
import com.skyline.energy.utils.CommonUtils;
public class JdbcUpdateDefinition extends BaseJdbcDefinition {
private boolean isReturnId = false;
public boolean isReturnId() {
return isReturnId;
}
public JdbcUpdateDefinition(Method method) throws DaoGenerateException {
init(method);
}
private void init(Method method) throws DaoGenerateException {
checkReturnType(method);
Map<String, Integer> paramIndexes = new HashMap<String, Integer>(8, 1f);
Annotation[][] annotations = method.getParameterAnnotations();
Class<?>[] paramTypes = method.getParameterTypes();
parseParameterAnnotations(annotations, paramIndexes, null, paramTypes);
Update update = method.getAnnotation(Update.class);
String sql = update.value();
parseSql(sql, paramTypes, paramIndexes);
parseShardBy(method, paramIndexes, paramTypes);
}
private void checkReturnType(Method method) throws DaoGenerateException {
Class<?> returnType = method.getReturnType();
isReturnId = (method.getAnnotation(ReturnId.class) != null);
if (isReturnId) {
if (CommonUtils.isTypePrimitive(returnType)) {
returnType = ClassUtils.primitiveToWrapper(returnType);
}
if (!CommonUtils.isTypeNumber(returnType)) {
throw new DaoGenerateException("if use @ReturnId: " + returnType + " must instance of Number");
}
} else {
if (CommonUtils.isTypePrimitive(returnType)) {
returnType = ClassUtils.primitiveToWrapper(returnType);
}
if (CommonUtils.isTypeVoid(returnType) || Integer.class.equals(returnType)) {
return;
}
throw new DaoGenerateException("if use @Update only be used on a method return void,int,Integer");
}
}
}