Skip to content

Commit 5cb87bc

Browse files
committed
Java:为 APIJSON 完善 InfluxDB 增删改 返回的 count, id, id{} 等通用字段 以及 SQLConfig 配置
1 parent 8f0976a commit 5cb87bc

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ public String execute(@RequestBody String request, HttpSession session) {
13921392
prefix = prefix.substring(mid + 1);
13931393
}
13941394

1395-
if (DemoSQLExecutor.DATABASE_INFLUXDB.equalsIgnoreCase(prefix)) {
1395+
if (DemoSQLConfig.DATABASE_INFLUXDB.equalsIgnoreCase(prefix)) {
13961396
database = prefix.toUpperCase();
13971397

13981398
int end = uri.lastIndexOf("/");
@@ -1405,8 +1405,8 @@ public String execute(@RequestBody String request, HttpSession session) {
14051405

14061406
uri = "http" + uri.substring(start);
14071407

1408-
account = "root";
1409-
password = "apijson@123";
1408+
// account = "root";
1409+
// password = "apijson@123";
14101410
} else if (DemoSQLExecutor.DATABASE_NEBULA.equalsIgnoreCase(prefix)) {
14111411
database = prefix.toUpperCase();
14121412
}
@@ -1440,7 +1440,7 @@ public String execute(@RequestBody String request, HttpSession session) {
14401440
long rsDuration = 0;
14411441
long executeStartTime = System.currentTimeMillis();
14421442

1443-
if (DemoSQLExecutor.DATABASE_INFLUXDB.equals(database)) {
1443+
if (DemoSQLConfig.DATABASE_INFLUXDB.equals(database)) {
14441444
JSONObject result = executor.execute(config, false);
14451445
if (isWrite) {
14461446
updateCount = result == null ? 0 : result.getIntValue(JSONResponse.KEY_COUNT);

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ public String getDBUri() {
213213
// return "jdbc:TAOS://localhost:6030"; //TODO 改成你自己的
214214
return "jdbc:TAOS-RS://localhost:6041"; //TODO 改成你自己的
215215
}
216+
if (isInfluxDB()) {
217+
return "http://localhost:8086";
218+
}
219+
216220
return "";
217221
}
218222

@@ -252,6 +256,10 @@ public String getDBAccount() {
252256
if (isTDengine()) {
253257
return "root"; //TODO 改成你自己的
254258
}
259+
if (isInfluxDB()) {
260+
return "root";
261+
}
262+
255263
return "";
256264
}
257265

@@ -291,6 +299,10 @@ public String getDBPassword() {
291299
if (isTDengine()) {
292300
return "taosdata"; //TODO 改成你自己的
293301
}
302+
if (isInfluxDB()) {
303+
return "apijson@123"; //TODO 改成你自己的
304+
}
305+
294306
return "";
295307
}
296308

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLExecutor.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.Serializable;
2424
import java.sql.Connection;
2525
import java.sql.SQLException;
26+
import java.util.Collection;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Properties;
@@ -123,7 +124,6 @@ public synchronized void removeCache(String sql, SQLConfig config) {
123124
// Redis 缓存 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
124125

125126
public static final String DATABASE_NEBULA = "NEBULA";
126-
public static final String DATABASE_INFLUXDB = "INFLUXDB";
127127

128128
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
129129
@Override
@@ -195,16 +195,20 @@ public Connection getConnection(SQLConfig config) throws Exception {
195195

196196

197197
@Override
198-
public JSONObject execute(SQLConfig config, boolean unknownType) throws Exception {
199-
if (DATABASE_INFLUXDB.equals(config.getDatabase())) {
198+
public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception {
199+
if (DemoSQLConfig.DATABASE_INFLUXDB.equals(config.getDatabase())) {
200200
InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword());
201-
202201
influxDB.setDatabase(config.getSchema());
203202

204203
String sql = config.getSQL(config.isPrepared());
205-
String trimmedSQL = sql == null ? null : sql.trim();
206-
String sqlPrefix = trimmedSQL == null || trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
207-
boolean isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
204+
205+
RequestMethod method = config.getMethod();
206+
boolean isWrite = ! RequestMethod.isQueryMethod(method);
207+
if (method == null && ! isWrite) {
208+
String trimmedSQL = sql == null ? null : sql.trim();
209+
String sqlPrefix = trimmedSQL == null || trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
210+
isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
211+
}
208212

209213
if (isWrite) {
210214
influxDB.enableBatch(
@@ -220,8 +224,30 @@ public JSONObject execute(SQLConfig config, boolean unknownType) throws Exceptio
220224

221225
influxDB.write(sql);
222226

223-
JSONObject result = new JSONObject(true);
224-
result.put(JSONResponse.KEY_COUNT, 1); // FIXME
227+
JSONObject result = DemoParser.newSuccessResult();
228+
229+
if (method == RequestMethod.POST) {
230+
List<List<Object>> values = config.getValues();
231+
result.put(JSONResponse.KEY_COUNT, values == null ? 0 : values.size());
232+
} else {
233+
String idKey = config.getIdKey();
234+
Object id = config.getId();
235+
Object idIn = config.getIdIn();
236+
if (id != null) {
237+
result.put(idKey, id);
238+
}
239+
if (idIn != null) {
240+
result.put(idKey + "[]", idIn);
241+
}
242+
243+
if (method == RequestMethod.PUT) {
244+
Map<String, Object> content = config.getContent();
245+
result.put(JSONResponse.KEY_COUNT, content == null ? 0 : content.size());
246+
} else {
247+
result.put(JSONResponse.KEY_COUNT, id == null && idIn instanceof Collection ? ((Collection<?>) idIn).size() : 1); // FIXME 直接 SQLAuto 传 Flux/InfluxQL INSERT 如何取数量?
248+
}
249+
}
250+
225251
return result;
226252
}
227253

@@ -238,7 +264,7 @@ public JSONObject execute(SQLConfig config, boolean unknownType) throws Exceptio
238264
}
239265

240266
JSONObject result = JSON.parseObject(list.get(0));
241-
if (list.size() > 0) {
267+
if (list.size() > 1) {
242268
result.put(KEY_RAW_LIST, list);
243269
}
244270

0 commit comments

Comments
 (0)