refactor: 优化SQL逻辑

This commit is contained in:
junjun 2024-03-14 10:14:33 +08:00
parent 2f197e8024
commit 97c4a43c67
3 changed files with 9 additions and 17 deletions

View File

@ -49,24 +49,16 @@ public class SQLConstants {
public static final String FIELD_DOT = "`%s`";
public static final String UNIX_TIMESTAMP = "UNIX_TIMESTAMP(%s)";
public static final String DATE_FORMAT = "DATE_FORMAT(%s,'%s')";
public static final String UNIX_TIMESTAMP = "DE_UNIX_TIMESTAMP(%s)";
public static final String DE_DATE_FORMAT = "DE_DATE_FORMAT(%s,'%s')";
public static final String CAST_DATE_FORMAT = "CAST_DATE_FORMAT(%s,'%s','%s')";
public static final String DE_CAST_DATE_FORMAT = "DE_CAST_DATE_FORMAT(%s,'%s','%s')";
public static final String FROM_UNIXTIME = "FROM_UNIXTIME(%s,'%s')";
public static final String STR_TO_DATE = "STR_TO_DATE(%s,'%s')";
public static final String FROM_UNIXTIME = "DE_FROM_UNIXTIME(%s,'%s')";
public static final String DE_STR_TO_DATE = "DE_STR_TO_DATE(%s,'%s')";
public static final String GET_DATE_FORMAT = "GET_DATE_FORMAT(%s)";
public static final String CAST = "CAST(%s AS %s)";
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

View File

@ -67,10 +67,10 @@ public class Dimension2SQLObj {
String format = Utils.transDateFormat(x.getDateStyle(), x.getDatePattern());
if (StringUtils.equalsIgnoreCase(x.getDateStyle(), "y_Q")) {
fieldName = String.format(format,
String.format(SQLConstants.DATE_FORMAT, originField, "yyyy"),
String.format(SQLConstants.DE_DATE_FORMAT, originField, "yyyy"),
String.format(SQLConstants.QUARTER, originField));
} else {
fieldName = String.format(SQLConstants.CAST_DATE_FORMAT, originField,
fieldName = String.format(SQLConstants.DE_CAST_DATE_FORMAT, originField,
SQLConstants.DEFAULT_DATE_FORMAT,
format);
}
@ -94,13 +94,13 @@ public class Dimension2SQLObj {
String from_unixtime = String.format(SQLConstants.FROM_UNIXTIME, cast, SQLConstants.DEFAULT_DATE_FORMAT);
if (StringUtils.equalsIgnoreCase(x.getDateStyle(), "y_Q")) {
fieldName = String.format(format,
String.format(SQLConstants.DATE_FORMAT, from_unixtime, "yyyy"),
String.format(SQLConstants.DE_DATE_FORMAT, from_unixtime, "yyyy"),
String.format(SQLConstants.QUARTER, from_unixtime));
} else {
fieldName = String.format(SQLConstants.CAST_DATE_FORMAT, from_unixtime, SQLConstants.DEFAULT_DATE_FORMAT, format);
fieldName = String.format(SQLConstants.DE_CAST_DATE_FORMAT, from_unixtime, SQLConstants.DEFAULT_DATE_FORMAT, format);
}
} else {
fieldName = String.format(SQLConstants.DATE_FORMAT, originField, format);
fieldName = String.format(SQLConstants.DE_DATE_FORMAT, originField, format);
}
} else if (Objects.equals(x.getDeType(), DeTypeConstants.DE_STRING) && Objects.equals(x.getDeExtractType(), DeTypeConstants.DE_STRING)) {
fieldName = originField;

View File

@ -61,8 +61,8 @@ public class Order2SQLObj {
} else if (Objects.equals(f.getDeType(), DeTypeConstants.DE_FLOAT)) {
fieldName = String.format(SQLConstants.CAST, originField, SQLConstants.DEFAULT_FLOAT_FORMAT);
} else if (Objects.equals(f.getDeType(), DeTypeConstants.DE_TIME)) {
fieldName = StringUtils.isEmpty(f.getDateFormat()) ? String.format(SQLConstants.STR_TO_DATE, originField, SQLConstants.DEFAULT_DATE_FORMAT) :
String.format(SQLConstants.DATE_FORMAT, String.format(SQLConstants.STR_TO_DATE, originField, f.getDateFormat()), SQLConstants.DEFAULT_DATE_FORMAT);
fieldName = StringUtils.isEmpty(f.getDateFormat()) ? String.format(SQLConstants.DE_STR_TO_DATE, originField, SQLConstants.DEFAULT_DATE_FORMAT) :
String.format(SQLConstants.DE_DATE_FORMAT, String.format(SQLConstants.DE_STR_TO_DATE, originField, f.getDateFormat()), SQLConstants.DEFAULT_DATE_FORMAT);
} else {
fieldName = originField;
}