TSK-1728: Refactored SqlProviderUtil (#1705)

This commit is contained in:
Tristan2357 2021-09-14 11:47:34 +02:00 committed by GitHub
parent 92fb63784d
commit aa725edb83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 62 deletions

View File

@ -1,5 +1,7 @@
package pro.taskana.common.internal.util;
import java.util.stream.IntStream;
public class SqlProviderUtil {
public static final String OPENING_SCRIPT_TAG = "<script>";
public static final String CLOSING_SCRIPT_TAG = "</script>";
@ -9,8 +11,8 @@ public class SqlProviderUtil {
private SqlProviderUtil() {}
public static void whereIn(String collection, String column, StringBuilder sb) {
sb.append("<if test='")
public static StringBuilder whereIn(String collection, String column, StringBuilder sb) {
return sb.append("<if test='")
.append(collection)
.append(" != null'>AND ")
.append(column)
@ -19,8 +21,12 @@ public class SqlProviderUtil {
.append("' separator=',' >#{item}</foreach>)</if> ");
}
public static void whereNotIn(String collection, String column, StringBuilder sb) {
sb.append("<if test='")
public static StringBuilder whereIn(String collection, String column) {
return whereIn(collection, column, new StringBuilder());
}
public static StringBuilder whereNotIn(String collection, String column, StringBuilder sb) {
return sb.append("<if test='")
.append(collection)
.append(" != null'>AND ")
.append(column)
@ -29,8 +35,12 @@ public class SqlProviderUtil {
.append("' separator=',' >#{item}</foreach>)</if> ");
}
public static void whereInTime(String collection, String column, StringBuilder sb) {
sb.append("<if test='")
public static StringBuilder whereNotIn(String collection, String column) {
return whereNotIn(collection, column, new StringBuilder());
}
public static StringBuilder whereInTime(String collection, String column, StringBuilder sb) {
return sb.append("<if test='")
.append(collection)
.append(" !=null'> AND (<foreach item='item' collection='")
.append(collection)
@ -43,8 +53,12 @@ public class SqlProviderUtil {
.append(" &lt;=#{item.end} </if>)</foreach>)</if> ");
}
public static void whereLike(String collection, String column, StringBuilder sb) {
sb.append("<if test='")
public static StringBuilder whereInTime(String collection, String column) {
return whereInTime(collection, column, new StringBuilder());
}
public static StringBuilder whereLike(String collection, String column, StringBuilder sb) {
return sb.append("<if test='")
.append(collection)
.append(" != null'>AND (<foreach item='item' collection='")
.append(collection)
@ -52,4 +66,26 @@ public class SqlProviderUtil {
.append(column)
.append(") LIKE #{item}</foreach>)</if> ");
}
public static StringBuilder whereLike(String collection, String column) {
return whereLike(collection, column, new StringBuilder());
}
public static StringBuilder whereCustomStatements(
String baseCollection, String baseColumn, int customBound, StringBuilder sb) {
IntStream.rangeClosed(1, customBound)
.forEach(
x -> {
String column = baseColumn + "_" + x;
whereIn(baseCollection + x + "In", column, sb);
whereLike(baseCollection + x + "Like", column, sb);
whereNotIn(baseCollection + x + "NotIn", column, sb);
});
return sb;
}
public static StringBuilder whereCustomStatements(
String baseCollection, String baseColumn, int customBound) {
return whereCustomStatements(baseCollection, baseColumn, customBound, new StringBuilder());
}
}

View File

@ -4,12 +4,10 @@ import static pro.taskana.common.internal.util.SqlProviderUtil.CLOSING_SCRIPT_TA
import static pro.taskana.common.internal.util.SqlProviderUtil.CLOSING_WHERE_TAG;
import static pro.taskana.common.internal.util.SqlProviderUtil.OPENING_SCRIPT_TAG;
import static pro.taskana.common.internal.util.SqlProviderUtil.OPENING_WHERE_TAG;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereCustomStatements;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereIn;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereLike;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereNotIn;
import java.util.stream.IntStream;
import pro.taskana.common.internal.util.SqlProviderUtil;
public class MonitorMapperSqlProvider {
@ -195,15 +193,13 @@ public class MonitorMapperSqlProvider {
@SuppressWarnings("unused")
public static String getTasksCountByState() {
StringBuilder whereStatements = new StringBuilder();
whereIn("domains", "DOMAIN", whereStatements);
whereIn("states", "STATE", whereStatements);
whereIn("workbasketIds", "WORKBASKET_ID", whereStatements);
return OPENING_SCRIPT_TAG
+ "SELECT WORKBASKET_KEY, STATE, COUNT(STATE) as COUNT "
+ "FROM TASK "
+ OPENING_WHERE_TAG
+ whereStatements
+ whereIn("domains", "DOMAIN")
+ whereIn("states", "STATE")
+ whereIn("workbasketIds", "WORKBASKET_ID")
+ "<if test='priorityMinimum != null'>"
+ "AND priority >= #{priorityMinimum} "
+ "</if>"
@ -214,13 +210,6 @@ public class MonitorMapperSqlProvider {
@SuppressWarnings("unused")
public static String getTasksCountForStatusGroupedByOrgLevel() {
StringBuilder whereStatements = new StringBuilder();
whereIn("report.classificationCategory", "T.CLASSIFICATION_CATEGORY", whereStatements);
whereIn("report.domains", "T.DOMAIN", whereStatements);
whereIn("report.classificationIds", "T.CLASSIFICATION_ID", whereStatements);
whereNotIn("report.excludedClassificationIds", "T.CLASSIFICATION_ID", whereStatements);
whereCustomStatements(whereStatements);
return OPENING_SCRIPT_TAG
+ "SELECT A.AGE_IN_DAYS, A.ORG_LEVEL_1, A.ORG_LEVEL_2, A.ORG_LEVEL_3, A.ORG_LEVEL_4, "
+ "'${status}' AS STATUS, COUNT(A.AGE_IN_DAYS) AS COUNT FROM ("
@ -246,7 +235,11 @@ public class MonitorMapperSqlProvider {
+ "<if test=\"status.name() == 'COMPLETED'\">"
+ "T.COMPLETED IS NOT NULL "
+ "</if>"
+ whereStatements
+ whereIn("report.classificationCategory", "T.CLASSIFICATION_CATEGORY")
+ whereIn("report.domains", "T.DOMAIN")
+ whereIn("report.classificationIds", "T.CLASSIFICATION_ID")
+ whereNotIn("report.excludedClassificationIds", "T.CLASSIFICATION_ID")
+ whereCustomStatements("report.custom", "T.CUSTOM", 16)
+ CLOSING_WHERE_TAG
+ ") AS A "
+ "GROUP BY A.AGE_IN_DAYS, A.ORG_LEVEL_1, A.ORG_LEVEL_2, A.ORG_LEVEL_3, A.ORG_LEVEL_4 "
@ -275,21 +268,20 @@ public class MonitorMapperSqlProvider {
+ CLOSING_SCRIPT_TAG;
}
@SuppressWarnings("unused")
public static String getTaskCountByPriority() {
StringBuilder whereStatements = new StringBuilder();
whereIn("report.workbasketType", "W.TYPE", whereStatements);
return OPENING_SCRIPT_TAG
+ "SELECT T.WORKBASKET_KEY, T.PRIORITY, COUNT(T.PRIORITY) as COUNT "
+ "FROM TASK as T "
+ "INNER JOIN WORKBASKET as W ON W.ID = T.WORKBASKET_ID "
+ OPENING_WHERE_TAG
+ whereStatements
+ whereIn("report.workbasketType", "W.TYPE")
+ CLOSING_WHERE_TAG
+ "GROUP BY T.WORKBASKET_KEY, T.PRIORITY"
+ CLOSING_SCRIPT_TAG;
}
private static String timeIntervalWhereStatements() {
private static StringBuilder timeIntervalWhereStatements() {
StringBuilder sb = new StringBuilder();
SqlProviderUtil.whereIn("report.workbasketIds", "T.WORKBASKET_ID", sb);
SqlProviderUtil.whereIn("report.states", "T.STATE", sb);
@ -297,22 +289,7 @@ public class MonitorMapperSqlProvider {
SqlProviderUtil.whereIn("report.domains", "T.DOMAIN", sb);
SqlProviderUtil.whereIn("report.classificationIds", "T.CLASSIFICATION_ID", sb);
SqlProviderUtil.whereNotIn("report.excludedClassificationIds", "T.CLASSIFICATION_ID", sb);
whereCustomStatements(sb);
return sb.toString();
}
private static void whereCustomStatements(StringBuilder sb) {
IntStream.rangeClosed(1, 16)
.forEach(
x -> {
String collectionIn = "report.custom" + x + "In";
String collectionNotIn = "report.custom" + x + "NotIn";
String collectionLike = "report.custom" + x + "Like";
String column = "T.CUSTOM_" + x;
whereIn(collectionIn, column, sb);
whereLike(collectionLike, column, sb);
whereNotIn(collectionNotIn, column, sb);
});
SqlProviderUtil.whereCustomStatements("report.custom", "T.CUSTOM", 16, sb);
return sb;
}
}

View File

@ -5,6 +5,7 @@ import static pro.taskana.common.internal.util.SqlProviderUtil.CLOSING_WHERE_TAG
import static pro.taskana.common.internal.util.SqlProviderUtil.DB2_WITH_UR;
import static pro.taskana.common.internal.util.SqlProviderUtil.OPENING_SCRIPT_TAG;
import static pro.taskana.common.internal.util.SqlProviderUtil.OPENING_WHERE_TAG;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereCustomStatements;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereIn;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereInTime;
import static pro.taskana.common.internal.util.SqlProviderUtil.whereLike;
@ -12,7 +13,6 @@ import static pro.taskana.common.internal.util.SqlProviderUtil.whereNotIn;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import pro.taskana.task.api.TaskQueryColumnName;
@ -363,7 +363,7 @@ public class TaskQuerySqlProvider {
whereLike("attachmentChannelLike", "a.CHANNEL", sb);
whereLike("attachmentReferenceLike", "a.REF_VALUE", sb);
whereLike("description", "DESCRIPTION", sb);
whereCustomStatements(sb);
whereCustomStatements("custom", "t.CUSTOM", 16, sb);
sb.append("<if test='isRead != null'>AND IS_READ = #{isRead}</if> ");
sb.append("<if test='isTransferred != null'>AND IS_TRANSFERRED = #{isTransferred}</if> ");
sb.append(
@ -372,18 +372,4 @@ public class TaskQuerySqlProvider {
+ " AND t.DOMAIN = #{item.domain})</foreach>)</if> ");
return sb.toString();
}
private static void whereCustomStatements(StringBuilder sb) {
IntStream.rangeClosed(1, 16)
.forEach(
x -> {
String collectionIn = "custom" + x + "In";
String collectionNotIn = "custom" + x + "NotIn";
String collectionLike = "custom" + x + "Like";
String column = "CUSTOM_" + x;
whereIn(collectionIn, column, sb);
whereLike(collectionLike, column, sb);
whereNotIn(collectionNotIn, column, sb);
});
}
}

View File

@ -22,7 +22,7 @@ public enum WorkbasketQueryColumnName implements QueryColumnName {
ORG_LEVEL_3("w.org_level_3"),
ORG_LEVEL_4("w.org_level_4");
private String name;
private final String name;
WorkbasketQueryColumnName(String name) {
this.name = name;