Skip to content

Commit 0063721

Browse files
committed
JOIN ON 新增支持 {} IN 和 <> json_contains 两种关联方式
1 parent 3ea3e61 commit 0063721

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

APIJSONORM/src/main/java/apijson/SQL.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
public class SQL {
1212

13+
public static final String JOIN = " JOIN ";
14+
public static final String ON = " ON ";
1315
public static final String OR = " OR ";
1416
public static final String AND = " AND ";
1517
public static final String NOT = " NOT ";

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static apijson.SQL.AND;
3434
import static apijson.SQL.NOT;
3535
import static apijson.SQL.OR;
36+
import static apijson.SQL.ON;
3637

3738
import java.util.ArrayList;
3839
import java.util.Arrays;
@@ -3803,8 +3804,25 @@ public String getJoinString() throws Exception {
38033804
if (onList != null) {
38043805
boolean first = true;
38053806
for (On on : onList) {
3806-
sql += (first ? " ON " : " AND ") + quote + jt + quote + "." + quote + on.getKey() + quote + " = "
3807-
+ quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote;
3807+
String rt = on.getRelateType();
3808+
if (StringUtil.isEmpty(rt, false)) {
3809+
sql += (first ? ON : AND) + quote + jt + quote + "." + quote + on.getKey() + quote + " = "
3810+
+ quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote;
3811+
}
3812+
else if ("{}".equals(rt)) {
3813+
sql += (first ? ON : AND) + "json_contains(" + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote
3814+
// + ", concat('\\'', " + quote + jt + quote + "." + quote + on.getKey() + quote + ", '\\''), '$')";
3815+
+ ", cast(" + quote + jt + quote + "." + quote + on.getKey() + quote + " AS CHAR), '$')";
3816+
}
3817+
else if ("<>".equals(rt)) {
3818+
sql += (first ? ON : AND) + "json_contains(" + quote + jt + quote + "." + quote + on.getKey() + quote
3819+
// + ", concat('\\'', " + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote + ", '\\''), '$')";
3820+
+ ", cast(" + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote + " AS CHAR), '$')";
3821+
}
3822+
else {
3823+
throw new IllegalArgumentException("join:value 中 value 里的 " + jt + "/" + j.getPath()
3824+
+ " 中 JOIN ON 条件关联类型 " + rt + " 不合法!只支持 =, {}, <> 这几种!");
3825+
}
38083826
first = false;
38093827
}
38103828
}
@@ -3826,8 +3844,26 @@ public String getJoinString() throws Exception {
38263844
if (onList != null) {
38273845
boolean first = true;
38283846
for (On on : onList) {
3829-
sql += (first ? " ON " : " AND ") + quote + jt + quote + "." + quote + on.getKey() + quote + " = "
3830-
+ quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote;
3847+
String rt = on.getRelateType();
3848+
if (StringUtil.isEmpty(rt, false)) {
3849+
sql += (first ? ON : AND) + quote + jt + quote + "." + quote + on.getKey() + quote + " = "
3850+
+ quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote;
3851+
}
3852+
else if ("{}".equals(rt)) {
3853+
sql += (first ? ON : AND) + "json_contains(" + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote
3854+
// + ", concat('\\'', " + quote + jt + quote + "." + quote + on.getKey() + quote + ", '\\''), '$')";
3855+
+ ", cast(" + quote + jt + quote + "." + quote + on.getKey() + quote + " AS CHAR), '$')";
3856+
}
3857+
else if ("<>".equals(rt)) {
3858+
sql += (first ? ON : AND) + "json_contains(" + quote + jt + quote + "." + quote + on.getKey() + quote
3859+
// + ", concat('\\'', " + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote + ", '\\''), '$')";
3860+
+ ", cast(" + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote + " AS CHAR), '$')";
3861+
}
3862+
else {
3863+
throw new IllegalArgumentException("join:value 中 value 里的 " + jt + "/" + j.getPath()
3864+
+ " 中 JOIN ON 条件关联类型 " + rt + " 不合法!只支持 =, {}, <> 这几种!");
3865+
}
3866+
38313867
first = false;
38323868
}
38333869
}

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Arrays;
2424
import java.util.Collection;
2525
import java.util.HashMap;
26-
import java.util.Iterator;
2726
import java.util.List;
2827
import java.util.Map;
2928
import java.util.Map.Entry;
@@ -537,7 +536,8 @@ else if (config.isClickHouse() && (sqlTable.startsWith("`") || sqlTable.startsWi
537536
if (onList != null) {
538537
for (On on : onList) {
539538
if (on != null) {
540-
viceConfig.putWhere(on.getKey(), item.get(on.getTargetKey()), true);
539+
String ok = on.getOriginKey();
540+
viceConfig.putWhere(ok.substring(0, ok.length() - 1), item.get(on.getTargetKey()), true);
541541
}
542542
}
543543
}
@@ -743,6 +743,8 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
743743
//缓存到 childMap
744744
if (onList != null) {
745745
for (On on : onList) {
746+
String ok = on.getOriginKey();
747+
String vk = ok.substring(0, ok.length() - 1);
746748
cc.putWhere(on.getKey(), result.get(on.getKey()), true);
747749
}
748750
}

0 commit comments

Comments
 (0)