com.javaoffers.batis.modelhelper.anno.fun.parse.FunAnnoParser Maven / Gradle / Ivy
package com.javaoffers.batis.modelhelper.anno.fun.parse;
import com.javaoffers.batis.modelhelper.anno.ColName;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.math.Rand;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.time.Curdate;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.time.CurrentDate;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.time.CurrentTime;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.time.Curtime;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.time.Now;
import com.javaoffers.batis.modelhelper.anno.fun.noneparam.time.UnixTimestamp;
import com.javaoffers.batis.modelhelper.anno.fun.params.If;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfEq;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfGt;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfGte;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfLt;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfLte;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfNeq;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfNotNull;
import com.javaoffers.batis.modelhelper.anno.fun.params.IfNull;
import com.javaoffers.batis.modelhelper.anno.fun.params.Left;
import com.javaoffers.batis.modelhelper.anno.fun.params.Right;
import com.javaoffers.batis.modelhelper.anno.fun.params.math.Abs;
import com.javaoffers.batis.modelhelper.anno.fun.params.math.Ceil;
import com.javaoffers.batis.modelhelper.anno.fun.params.math.Floor;
import com.javaoffers.batis.modelhelper.anno.fun.params.math.Mod;
import com.javaoffers.batis.modelhelper.anno.fun.params.math.Round;
import com.javaoffers.batis.modelhelper.anno.fun.params.math.Truncate;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.DateFormat;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Dayname;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.FromDays;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Hour;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Makedate;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Maketime;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Minute;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Month;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Monthname;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.SecToTime;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.StrToDate;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.TimeFormat;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.TimeToSec;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.ToDays;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Week;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Weekday;
import com.javaoffers.batis.modelhelper.anno.fun.params.time.Year;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.CharLength;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Concat;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.ConcatWs;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.GroupConcat;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.LTrim;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Length;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Lower;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.RTrim;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Repeat;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Replace;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Strcmp;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.SubString;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Trim;
import com.javaoffers.batis.modelhelper.anno.fun.params.varchar.Upper;
import com.javaoffers.batis.modelhelper.utils.TableInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author mingJie
*/
public class FunAnnoParser {
public static final String DEFAULT_VALUE = "";
public static ParseSqlFunResult parse(TableInfo tableInfo, Class modelClass, Field field, String defaultColName){
Assert.isTrue(field != null,"the field is null");
Annotation[] allAnno = field.getDeclaredAnnotations();
if(allAnno == null || allAnno.length == 0){
return null;
}
//先查询出@ColName注解
Appender appender = new Appender(modelClass,field,defaultColName);
ColName colNameAnno = null;
String coreSql = null;
boolean isFunSql = false;
boolean isExcludeColAll = false;
for(Annotation anno : allAnno){
if(anno instanceof ColName){
colNameAnno = (ColName) anno;
isExcludeColAll = colNameAnno.excludeColAll();
continue;
}else if(anno instanceof GroupConcat){
isExcludeColAll = ((GroupConcat) anno).excludeColAll();
}
//This will be optimized for strategy mode later. to avoid a lot of if statements
isFunSql =
parseParamCommon(appender, anno)||
parseParamVarchar(appender, anno)||
parseParamTime(appender, anno)||
parseParamMath(appender, anno)||
parseNoneParamTime(appender, anno)||
parseNoneParamMath(appender, anno) || isFunSql;
}
if(colNameAnno != null){
coreSql = colNameAnno.value();
//a and b cannot be used together
Assert.isTrue(colNameAnno != null && !appender.isNoneParam, "@ColName and" +
" "+appender.getNoParamFunName() + " cannot be used together");
}
if(isFunSql && tableInfo.getColNames().containsKey(coreSql)){
coreSql = tableInfo.getTableName()+"."+coreSql;
}
String finishSql = appender.toSqlString(coreSql);
// only one @ColName(" 'xxx' ") on field
if(!isFunSql && !tableInfo.getColNames().containsKey(finishSql)){
isFunSql = true;
}
return new ParseSqlFunResult(finishSql, isFunSql, isExcludeColAll);
}
private static boolean parseNoneParamMath(Appender appender, Annotation anno) {
boolean status = false;
if(anno instanceof Rand){
appender.appenderNoneParam(Rand.TAG);
status = true;
}
return status;
}
private static boolean parseNoneParamTime(Appender appender, Annotation anno) {
boolean status = false;
if(anno instanceof CurrentDate){
appender.appenderNoneParam(CurrentDate.TAG);
status = true;
}
else if(anno instanceof Curdate){
appender.appenderNoneParam(Curdate.TAG);
status = true;
}
else if(anno instanceof CurrentTime){
appender.appenderNoneParam(CurrentTime.TAG);
status = true;
}
else if(anno instanceof Curtime){
appender.appenderNoneParam(Curtime.TAG);
status = true;
}
else if(anno instanceof Now){
appender.appenderNoneParam(Now.TAG);
status = true;
}
else if(anno instanceof UnixTimestamp){
appender.appender(UnixTimestamp.TAG);
status = true;
}
return status;
}
private static boolean parseParamMath(Appender appender, Annotation anno) {
boolean status = false;
if(anno instanceof Abs){
Abs abs = (Abs) anno;
appender.appender(abs.TAG);
status = true;
}
else if(anno instanceof Ceil){
Ceil ceil = (Ceil) anno;
appender.appender(ceil.TAG);
status = true;
}
else if(anno instanceof Floor){
Floor floor = (Floor) anno;
appender.appender(floor.TAG);
status = true;
}
else if(anno instanceof Mod){
Mod mod = (Mod) anno;
int value = mod.value();
appender.appender(mod.TAG, value);
status = true;
}
else if(anno instanceof Round){
Round round = (Round) anno;
appender.appender(round.TAG,round.precision());
status = true;
}
else if(anno instanceof Truncate){
Truncate truncate = (Truncate) anno;
appender.appender(truncate.TAG, truncate.value());
status = true;
}
return status;
}
private static boolean parseParamTime(Appender appender, Annotation anno) {
boolean status = false;
if(anno instanceof DateFormat){
DateFormat dateFormat = (DateFormat)anno;
String format = dateFormat.value();
appender.appender(dateFormat.TAG, format);
status = true;
}else if(anno instanceof Dayname){
Dayname dayname = (Dayname) anno;
appender.appender(dayname.TAG);
status = true;
}
else if(anno instanceof FromDays){
appender.appender(FromDays.TAG);
status = true;
}
else if(anno instanceof Hour){
Hour hour = (Hour) anno;
appender.appender(hour.TAG);
status = true;
}
else if(anno instanceof Maketime){
int ep1 = ((Maketime) anno).ep1();
int ep2 = ((Maketime) anno).ep2();
appender.appender(Maketime.TAG, ep1, ep2);
status = true;
}
else if(anno instanceof Makedate){
int value = ((Makedate) anno).value();
appender.appender(Makedate.TAG, value);
status = true;
}
else if(anno instanceof Minute){
Minute minute = (Minute) anno;
appender.appender(minute.TAG);
status = true;
}
else if(anno instanceof Month){
Month month = (Month) anno;
appender.appender(month.TAG);
status = true;
}
else if(anno instanceof Monthname){
Monthname monthname = (Monthname) anno;
appender.appender(monthname.TAG);
status = true;
}
else if(anno instanceof SecToTime){
appender.appender(SecToTime.TAG);
status = true;
}
else if(anno instanceof StrToDate){
String format = ((StrToDate) anno).value();
appender.appender(StrToDate.TAG, format);
status = true;
}
else if(anno instanceof TimeFormat){
TimeFormat timeFormat = (TimeFormat) anno;
String format = timeFormat.value();
appender.appender(timeFormat.TAG, format);
status = true;
}
else if(anno instanceof ToDays){
appender.appender(ToDays.TAG);
status = true;
}
else if(anno instanceof TimeToSec){
appender.appender(TimeToSec.TAG);
status = true;
}
else if(anno instanceof Week){
Week week = (Week) anno;
appender.appender(week.TAG);
status = true;
}
else if(anno instanceof Weekday){
Weekday weekday = (Weekday) anno;
appender.appender(weekday.TAG);
status = true;
}
else if(anno instanceof Year){
Year year = (Year) anno;
appender.appender(year.TAG);
status = true;
}
return status;
}
private static boolean parseParamVarchar(Appender appender, Annotation anno) {
boolean status = false;
if(anno instanceof CharLength){
CharLength charLength = ((CharLength) anno);
appender.appender(charLength.TAG);
status = true;
}
else if(anno instanceof Concat){
Concat concat = (Concat) anno;
appender.appenderPosition(concat.TAG, concat.position(), concat.value());
status = true;
}
else if(anno instanceof ConcatWs){
ConcatWs concat = (ConcatWs) anno;
String separator = concat.separator();
String[] value = concat.value();
LinkedList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy