Skip to content

Commit 112d8dd

Browse files
fuziranaolin-zhao
authored andcommitted
feat: add Kingbase compatibility support
1 parent 3fc69f8 commit 112d8dd

6 files changed

Lines changed: 514 additions & 117 deletions

File tree

‎APIJSONORM/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
</properties>
2222

2323
<dependencies>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.13.2</version>
28+
<scope>test</scope>
29+
</dependency>
2430
</dependencies>
2531

2632
<build>

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ public String getUserIdKey() {
897897
private boolean main = true;
898898

899899
private Object id; // Table 的 id
900+
private boolean idGeneratedByAPIJSON;
900901
private Object idIn; // User Table 的 id IN
901902
private Object userId; // Table 的 userId
902903
private Object userIdIn; // Table 的 userId IN
@@ -1014,6 +1015,15 @@ public AbstractSQLConfig<T, M, L> setId(Object id) {
10141015
this.id = id;
10151016
return this;
10161017
}
1018+
@Override
1019+
public boolean isIdGeneratedByAPIJSON() {
1020+
return idGeneratedByAPIJSON;
1021+
}
1022+
@Override
1023+
public AbstractSQLConfig<T, M, L> setIdGeneratedByAPIJSON(boolean generated) {
1024+
this.idGeneratedByAPIJSON = generated;
1025+
return this;
1026+
}
10171027

10181028
@Override
10191029
public Object getIdIn() {
@@ -2101,7 +2111,7 @@ public String gainOrderString(boolean hasPrefix) {
21012111
// return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(order, joinOrder, ", ");
21022112
// }
21032113

2104-
if (getCount() > 0 && (isSQLServer() || isDb2())) {
2114+
if (getCount() > 0 && (isSQLServer() || isKingBaseSQLServer() || isDb2())) {
21052115
// Oracle, SQL Server, DB2 的 OFFSET 必须加 ORDER BY.去掉Oracle,Oracle里面没有offset关键字
21062116

21072117
// String[] ss = StringUtil.split(order);
@@ -4041,7 +4051,7 @@ public String gainCompareString(String key, String column, Object value, String
40414051
public String gainKey(@NotNull String key) {
40424052
String lenFun = "";
40434053
if (key.endsWith("[")) {
4044-
lenFun = isSQLServer() ? "datalength" : "length";
4054+
lenFun = isSQLServer() || isKingBaseSQLServer() ? "datalength" : "length";
40454055
key = key.substring(0, key.length() - 1);
40464056
}
40474057
else if (key.endsWith("{")) {
@@ -4332,7 +4342,8 @@ public String gainRegExpString(String key, String column, String value, boolean
43324342
if (isPSQL() || isKingBaseSQLServer()) {
43334343
return gainKey(column) + " ~" + (ignoreCase ? "* " : " ") + gainValue(key, column, value);
43344344
}
4335-
if (isOracle() || isDameng() || isKingBaseOracle() || isKingBaseMySQL() || (isMySQL() && gainDBVersionNums()[0] >= 8)) {
4345+
if (isOracle() || isDameng() || DATABASE_KINGBASE.equals(gainSQLDatabase())
4346+
|| isKingBaseOracle() || isKingBaseMySQL() || (isMySQL() && gainDBVersionNums()[0] >= 8)) {
43364347
return "regexp_like(" + gainKey(column) + ", " + gainValue(key, column, value) + (ignoreCase ? ", 'i'" : ", 'c'") + ")";
43374348
}
43384349
if (isPresto() || isTrino()) {
@@ -4651,10 +4662,10 @@ public String gainContainString(String key, String column, Object[] childs, int
46514662
condition += (gainKey(column) + " @> " + gainValue(key, column, newJSONArray(c)));
46524663
// operator does not exist: jsonb @> character varying "[" + c + "]");
46534664
}
4654-
else if (isKingBaseSQLServer()) {
4665+
else if (isKingBase()) {
46554666
condition += (gainKey(column) + "::jsonb @> " + gainValue(key, column, newJSONArray(c)) + "::jsonb");
46564667
}
4657-
else if (isOracle() || isDameng() || isKingBaseOracle()) {
4668+
else if (isOracle() || isDameng()) {
46584669
condition += ("json_textcontains(" + gainKey(column) + ", " + (StringUtil.isEmpty(path, true)
46594670
? "'$'" : gainValue(key, column, path)) + ", " + gainValue(key, column, c == null ? null : c.toString()) + ")");
46604671
}
@@ -4701,7 +4712,7 @@ public List<String> getWithAsExprSQLList() {
47014712
}
47024713
private void clearWithAsExprListIfNeed() {
47034714
// mysql8版本以上,子查询支持with as表达式
4704-
if(this.isMySQL() && this.gainDBVersionNums()[0] >= 8) {
4715+
if((this.isMySQL() || this.isKingBaseMySQL()) && this.gainDBVersionNums()[0] >= 8) {
47054716
this.withAsExprSQLList = new ArrayList<>();
47064717
}
47074718
}
@@ -4755,7 +4766,7 @@ private String withAsExprSubqueryString(SQLConfig<T, M, L> cfg, Subquery<T, M, L
47554766
} else {
47564767
withAsExpreSql = cfg.gainSQL(isPrepared());
47574768
// mysql 才存在这个问题, 主表和子表是一张表
4758-
if (isWithAsEnable && isMySQL() && StringUtil.equals(getTable(), subquery.gainFrom())) {
4769+
if (isWithAsEnable && (isMySQL() || isKingBaseMySQL()) && StringUtil.equals(getTable(), subquery.gainFrom())) {
47594770
withAsExpreSql = " SELECT * FROM (" + withAsExpreSql + ")" + as + quote + subquery.gainKey() + quote;
47604771
}
47614772
}
@@ -5018,15 +5029,15 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> String
50185029
return "ALTER TABLE " + tablePath + " UPDATE" + config.gainSetString() + config.gainWhereString(true);
50195030
}
50205031
cSql = "UPDATE " + tablePath + config.gainSetString() + config.gainWhereString(true)
5021-
+ (config.isMySQL() ? config.gainLimitString() : "");
5032+
+ (config.isMySQL() || config.isKingBaseMySQL() ? config.gainLimitString() : "");
50225033
cSql = buildWithAsExprSql(config, cSql);
50235034
return cSql;
50245035
case DELETE:
50255036
if(config.isClickHouse()){
50265037
return "ALTER TABLE " + tablePath + " DELETE" + config.gainWhereString(true);
50275038
}
50285039
cSql = "DELETE FROM " + tablePath + config.gainWhereString(true)
5029-
+ (config.isMySQL() ? config.gainLimitString() : ""); // PostgreSQL 不允许 LIMIT
5040+
+ (config.isMySQL() || config.isKingBaseMySQL() ? config.gainLimitString() : ""); // PostgreSQL 不允许 LIMIT
50305041
cSql = buildWithAsExprSql(config, cSql);
50315042
return cSql;
50325043
default:
@@ -5388,7 +5399,11 @@ else if (rt.endsWith("~")) {
53885399
if (isPSQL()) {
53895400
sql += (first ? ON : AND) + lk + (isNot ? NOT : "") + " ~" + (ignoreCase ? "* " : " ") + rk;
53905401
}
5391-
else if (isOracle() || isDameng() || isKingBase()) {
5402+
else if (isKingBaseSQLServer()) {
5403+
sql += (first ? ON : AND) + lk + (isNot ? NOT : "") + " ~" + (ignoreCase ? "* " : " ") + rk;
5404+
}
5405+
else if (isOracle() || isDameng() || DATABASE_KINGBASE.equals(gainSQLDatabase())
5406+
|| isKingBaseOracle() || isKingBaseMySQL()) {
53925407
sql += (first ? ON : AND) + "regexp_like(" + lk + ", " + rk + (ignoreCase ? ", 'i'" : ", 'c'") + ")";
53935408
}
53945409
else if (isPresto() || isTrino()) {
@@ -5444,7 +5459,11 @@ else if ("{}".equals(rt) || "<>".equals(rt)) {
54445459
sql += (first ? ON : AND) + (isNot ? "( " : "") + gainCondition(isNot, arrKeyPath
54455460
+ " IS NOT NULL AND " + arrKeyPath + " @> " + itemKeyPath) + (isNot ? ") " : "");
54465461
}
5447-
else if (isOracle() || isDameng() || isKingBase()) {
5462+
else if (isKingBase()) {
5463+
sql += (first ? ON : AND) + (isNot ? "( " : "") + gainCondition(isNot, arrKeyPath
5464+
+ " IS NOT NULL AND " + arrKeyPath + "::jsonb @> " + itemKeyPath + "::jsonb") + (isNot ? ") " : "");
5465+
}
5466+
else if (isOracle() || isDameng()) {
54485467
sql += (first ? ON : AND) + (isNot ? "( " : "") + gainCondition(isNot, arrKeyPath
54495468
+ " IS NOT NULL AND json_textcontains(" + arrKeyPath
54505469
+ ", '$', " + itemKeyPath + ")") + (isNot ? ") " : "");
@@ -5575,8 +5594,10 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> SQLConf
55755594
}
55765595

55775596
Object id = request.get(idKey);
5597+
boolean idGeneratedByAPIJSON = false;
55785598
if (id == null && method == POST) {
55795599
id = callback.newId(method, database, datasource, namespace, catalog, schema, table); // null 表示数据库自增 id
5600+
idGeneratedByAPIJSON = id != null;
55805601
}
55815602

55825603
if (id != null) { // null 无效
@@ -6207,6 +6228,7 @@ else if (keyMap != null) {
62076228

62086229
config.setRole(role);
62096230
config.setId(id);
6231+
config.setIdGeneratedByAPIJSON(idGeneratedByAPIJSON);
62106232
config.setIdIn(idIn);
62116233
config.setUserId(userId);
62126234
config.setUserIdIn(userIdIn);

0 commit comments

Comments
 (0)