Skip to content

Commit 4886ee9

Browse files
committed
新增支持物联网时序数据库 TDengine
1 parent 3f748f1 commit 4886ee9

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
148148
DATABASE_LIST.add(DATABASE_DB2);
149149
DATABASE_LIST.add(DATABASE_CLICKHOUSE);
150150
DATABASE_LIST.add(DATABASE_HIVE);
151+
DATABASE_LIST.add(DATABASE_TDENGINE);
151152

152153

153154
RAW_MAP = new LinkedHashMap<>(); // 保证顺序,避免配置冲突等意外情况
@@ -726,6 +727,13 @@ public abstract class AbstractSQLConfig implements SQLConfig {
726727
SQL_FUNCTION_MAP.put("bitAnd", ""); //bitAnd(a,b)
727728
SQL_FUNCTION_MAP.put("bitOr", ""); //bitOr(a,b)
728729

730+
// PostgreSQL 表结构相关 SQL 函数
731+
SQL_FUNCTION_MAP.put("obj_description", "");
732+
SQL_FUNCTION_MAP.put("col_description", "");
733+
734+
// SQLServer 相关 SQL 函数
735+
SQL_FUNCTION_MAP.put("datalength", "");
736+
729737
}
730738

731739
private int[] dbVersionNums = null;
@@ -991,7 +999,7 @@ public static boolean isHive(String db) {
991999

9921000
@Override
9931001
public String getQuote() {
994-
return isMySQL()||isClickHouse() ? "`" : "\"";
1002+
return isMySQL() || isClickHouse() || isTDengine() ? "`" : "\"";
9951003
}
9961004

9971005
@Override

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,8 @@ public Connection getConnection(@NotNull SQLConfig config) throws Exception {
11141114
connectionMap.put(connectionKey, connection);
11151115
}
11161116

1117-
int ti = getTransactionIsolation();
1117+
// TDengine 驱动内部事务处理方法都是空实现,手动 commit 无效
1118+
int ti = config.isTDengine() ? Connection.TRANSACTION_NONE : getTransactionIsolation();
11181119
if (ti != Connection.TRANSACTION_NONE) { //java.sql.SQLException: Transaction isolation level NONE not supported by MySQL
11191120
begin(ti);
11201121
}
@@ -1143,7 +1144,7 @@ public void begin(int transactionIsolation) throws SQLException {
11431144
// }
11441145
if (! isIsolationStatusSet) { //只设置一次Isolation等级 PG重复设置事务等级会报错
11451146
isIsolationStatusSet = true;
1146-
connection.setTransactionIsolation(transactionIsolation);
1147+
connection.setTransactionIsolation(transactionIsolation); // 这句导致 TDengine 驱动报错
11471148
}
11481149
connection.setAutoCommit(false); //java.sql.SQLException: Can''t call commit when autocommit=true
11491150
}
@@ -1212,6 +1213,12 @@ public void close() {
12121213

12131214
@Override
12141215
public ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exception {
1216+
if (config.isTDengine()) {
1217+
Connection conn = getConnection(config);
1218+
Statement stt = conn.createStatement();
1219+
return executeQuery(stt, StringUtil.isEmpty(sql) ? config.getSQL(false) : sql);
1220+
}
1221+
12151222
PreparedStatement stt = getStatement(config, sql);
12161223
ResultSet rs = stt.executeQuery(); //PreparedStatement 不用传 SQL
12171224
// if (config.isExplain() && (config.isSQLServer() || config.isOracle())) {
@@ -1221,10 +1228,20 @@ public ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exce
12211228
return rs;
12221229
}
12231230

1231+
12241232
@Override
12251233
public int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception {
1226-
PreparedStatement stt = getStatement(config);
1227-
int count = stt.executeUpdate(); // PreparedStatement 不用传 SQL
1234+
Statement stt;
1235+
int count;
1236+
if (config.isTDengine()) {
1237+
Connection conn = getConnection(config);
1238+
stt = conn.createStatement();
1239+
count = stt.executeUpdate(StringUtil.isEmpty(sql) ? config.getSQL(false) : sql);
1240+
}
1241+
else {
1242+
stt = getStatement(config);
1243+
count = ((PreparedStatement) stt).executeUpdate(); // PreparedStatement 不用传 SQL
1244+
}
12281245

12291246
if (count <= 0 && config.isHive()) {
12301247
count = 1;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public interface SQLConfig {
2424
String DATABASE_DB2 = "DB2";
2525
String DATABASE_CLICKHOUSE = "CLICKHOUSE";
2626
String DATABASE_HIVE = "HIVE";
27+
String DATABASE_TDENGINE = "TDENGINE";
2728

2829
String SCHEMA_INFORMATION = "information_schema"; //MySQL, PostgreSQL, SQL Server 都有的系统模式
2930
String SCHEMA_SYS = "sys"; //SQL Server 系统模式
@@ -41,7 +42,15 @@ public interface SQLConfig {
4142
boolean isDb2();
4243
boolean isClickHouse();
4344
boolean isHive();
44-
//暂时只兼容以上 5 种
45+
default boolean isTDengine() {
46+
return isTDengine(getDatabase());
47+
}
48+
49+
static boolean isTDengine(String db) {
50+
return DATABASE_TDENGINE.equals(db);
51+
}
52+
53+
//暂时只兼容以上几种
4554
// boolean isSQL();
4655
// boolean isTSQL();
4756
// boolean isPLSQL();
@@ -177,7 +186,7 @@ default int[] getDBVersionNums() {
177186
SQLConfig setTable(String table);
178187

179188
/**数据库里的真实Table名
180-
* 通过 {@link #TABLE_KEY_MAP} 映射
189+
* 通过 {@link AbstractSQLConfig.TABLE_KEY_MAP} 映射
181190
* @return
182191
*/
183192
String getSQLTable();
@@ -285,4 +294,5 @@ default int[] getDBVersionNums() {
285294
SQLConfig setProcedure(String procedure);
286295

287296

297+
288298
}

0 commit comments

Comments
 (0)