Skip to content

Commit c663fb2

Browse files
authored
Merge pull request #290 from chenyanlann/master
Add:ORM's support for ClickHouse
2 parents cd9827f + 9b5f3f2 commit c663fb2

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
124124
DATABASE_LIST.add(DATABASE_SQLSERVER);
125125
DATABASE_LIST.add(DATABASE_ORACLE);
126126
DATABASE_LIST.add(DATABASE_DB2);
127+
DATABASE_LIST.add(DATABASE_CLICKHOUSE);
127128

128129

129130
RAW_MAP = new LinkedHashMap<>(); // 保证顺序,避免配置冲突等意外情况
@@ -508,10 +509,17 @@ public boolean isDb2() {
508509
public static boolean isDb2(String db) {
509510
return DATABASE_DB2.equals(db);
510511
}
512+
@Override
513+
public boolean isClickHouse() {
514+
return isClickHouse(getSQLDatabase());
515+
}
516+
public static boolean isClickHouse(String db) {
517+
return DATABASE_CLICKHOUSE.equals(db);
518+
}
511519

512520
@Override
513521
public String getQuote() {
514-
return isMySQL() ? "`" : "\"";
522+
return isMySQL() ? "`" : ( isClickHouse()? "" : "\"");
515523
}
516524

517525
@Override
@@ -2158,6 +2166,9 @@ public String getRegExpString(String key, String value, boolean ignoreCase) {
21582166
if (isOracle()) {
21592167
return "regexp_like(" + getKey(key) + ", " + getValue(value) + (ignoreCase ? ", 'i'" : ", 'c'") + ")";
21602168
}
2169+
if (isClickHouse()) {
2170+
return "match(" + (ignoreCase ? "lower(" : "") + getKey(key) + (ignoreCase ? ")" : "") + ", " + (ignoreCase ? "lower(" : "") + getValue(value) + (ignoreCase ? ")" : "") + ")";
2171+
}
21612172
return getKey(key) + " REGEXP " + (ignoreCase ? "" : "BINARY ") + getValue(value);
21622173
}
21632174
//~ regexp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@@ -2448,7 +2459,12 @@ else if (isOracle()) {
24482459
else {
24492460
boolean isNum = c instanceof Number;
24502461
String v = (isNum ? "" : "\"") + childs[i] + (isNum ? "" : "\"");
2451-
condition += ("json_contains(" + getKey(key) + ", " + getValue(v) + ")");
2462+
if (isClickHouse()) {
2463+
condition += condition + "has(JSONExtractArrayRaw(assumeNotNull(" + getKey(key) + "))" + ", " + getValue(v) + ")";
2464+
}
2465+
else {
2466+
condition += ("json_contains(" + getKey(key) + ", " + getValue(v) + ")");
2467+
}
24522468
}
24532469
}
24542470
}
@@ -2649,9 +2665,9 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
26492665
case POST:
26502666
return "INSERT INTO " + tablePath + config.getColumnString() + " VALUES" + config.getValuesString();
26512667
case PUT:
2652-
return "UPDATE " + tablePath + config.getSetString() + config.getWhereString(true) + (config.isMySQL() ? config.getLimitString() : "");
2668+
return "UPDATE " + tablePath + config.getSetString() + config.getWhereString(true) + (config.isMySQL()||config.isClickHouse() ? config.getLimitString() : "");
26532669
case DELETE:
2654-
return "DELETE FROM " + tablePath + config.getWhereString(true) + (config.isMySQL() ? config.getLimitString() : ""); // PostgreSQL 不允许 LIMIT
2670+
return "DELETE FROM " + tablePath + config.getWhereString(true) + (config.isMySQL()||config.isClickHouse() ? config.getLimitString() : ""); // PostgreSQL 不允许 LIMIT
26552671
default:
26562672
String explain = (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : "");
26572673
if (config.isTest() && RequestMethod.isGetMethod(config.getMethod(), true)) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface SQLConfig {
2222
String DATABASE_SQLSERVER = "SQLSERVER";
2323
String DATABASE_ORACLE = "ORACLE";
2424
String DATABASE_DB2 = "DB2";
25+
String DATABASE_CLICKHOUSE = "CLICKHOUSE";
2526

2627
String SCHEMA_INFORMATION = "information_schema"; //MySQL, PostgreSQL, SQL Server 都有的系统模式
2728
String SCHEMA_SYS = "sys"; //SQL Server 系统模式
@@ -37,6 +38,7 @@ public interface SQLConfig {
3738
boolean isSQLServer();
3839
boolean isOracle();
3940
boolean isDb2();
41+
boolean isClickHouse();
4042
//暂时只兼容以上 5 种
4143
// boolean isSQL();
4244
// boolean isTSQL();

0 commit comments

Comments
 (0)