Skip to content

Commit 03259d7

Browse files
committed
Java:MultiDataSource 转换 influxdb-java 结果集格式
1 parent bb006e0 commit 03259d7

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ public String getDBUri() {
190190
return "";
191191
}
192192

193+
@Override
194+
public String getSQLTable() {
195+
String t = super.getSQLTable();
196+
return isInfluxDB() ? t.toLowerCase() : t;
197+
}
198+
199+
// TODO 迁移到 APIJSON 主项目 <<<<<<<<<<<<<<<<<<<<
200+
@Override
201+
public String getSchema() {
202+
String sch = super.getSchema();
203+
if (StringUtil.isEmpty(sch) && isInfluxDB()) {
204+
sch = DEFAULT_SCHEMA;
205+
}
206+
return sch;
207+
}
208+
209+
@Override
210+
public String getSQLSchema() {
211+
return isInfluxDB() ? null : super.getSQLSchema();
212+
}
213+
214+
// TODO 迁移到 APIJSON 主项目 >>>>>>>>>>>>>>>>>>>>>>
215+
193216
private String dbAccount;
194217
public DemoSQLConfig setDBAccount(String dbAccount) {
195218
this.dbAccount = dbAccount;

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

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package apijson.demo;
1616

1717
import apijson.*;
18+
import apijson.orm.AbstractSQLConfig;
1819
import com.alibaba.druid.pool.DruidDataSource;
20+
import com.alibaba.fastjson.JSONArray;
1921
import com.alibaba.fastjson.JSONObject;
2022
import com.datastax.oss.driver.api.core.CqlSession;
2123
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
@@ -30,10 +32,7 @@
3032
import java.nio.file.Paths;
3133
import java.sql.Connection;
3234
import java.sql.SQLException;
33-
import java.util.Collection;
34-
import java.util.List;
35-
import java.util.Map;
36-
import java.util.Properties;
35+
import java.util.*;
3736
import java.util.concurrent.TimeUnit;
3837

3938
import javax.sql.DataSource;
@@ -202,21 +201,31 @@ public Connection getConnection(SQLConfig config) throws Exception {
202201

203202
@Override
204203
public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception {
205-
String db = config.getDatabase();
206-
if (DemoSQLConfig.DATABASE_CASSANDRA.equals(db) || DemoSQLConfig.DATABASE_INFLUXDB.equals(db)) {
207-
208-
String sql = config.getSQL(config.isPrepared());
204+
boolean isCassandra = config.isCassandra();
205+
boolean isInfluxDB = config.isInfluxDB();
206+
207+
if (isCassandra || isInfluxDB) {
208+
String sql = config.getSQL(false); // config.isPrepared());
209+
List<JSONObject> cache = getCache(sql, config);
210+
int position = config.getPosition();
211+
JSONObject result = getCacheItem(cache, position, config);
212+
if (result != null) {
213+
if (position == 0 && cache != null && cache.size() > 1) {
214+
result.put(KEY_RAW_LIST, cache);
215+
}
216+
return result;
217+
}
209218

210219
RequestMethod method = config.getMethod();
211-
boolean isWrite = !RequestMethod.isQueryMethod(method);
212-
if (method == null && !isWrite) {
220+
boolean isWrite = ! RequestMethod.isQueryMethod(method);
221+
if (method == null && ! isWrite) {
213222
String trimmedSQL = sql == null ? null : sql.trim();
214223
String sqlPrefix = trimmedSQL == null || trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
215224
isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
216225
}
217226

218227

219-
if (DemoSQLConfig.DATABASE_CASSANDRA.equals(db)) {
228+
if (isCassandra) {
220229
CqlSession session = CqlSession.builder()
221230
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
222231
.withCloudSecureConnectBundle(new URL(config.getDBUri()))
@@ -243,7 +252,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
243252
return new JSONObject(true);
244253
}
245254

246-
JSONObject result = JSON.parseObject(list.get(0));
255+
result = JSON.parseObject(list.get(0));
247256
if (list.size() > 1) {
248257
result.put(KEY_RAW_LIST, list);
249258
}
@@ -252,11 +261,10 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
252261
}
253262

254263

255-
if (DemoSQLConfig.DATABASE_INFLUXDB.equals(db)) {
264+
if (isInfluxDB) {
256265
InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword());
257266
influxDB.setDatabase(config.getSchema());
258267

259-
260268
if (isWrite) {
261269
influxDB.enableBatch(
262270
BatchOptions.DEFAULTS
@@ -271,7 +279,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
271279

272280
influxDB.write(sql);
273281

274-
JSONObject result = DemoParser.newSuccessResult();
282+
result = DemoParser.newSuccessResult();
275283

276284
if (method == RequestMethod.POST) {
277285
List<List<Object>> values = config.getValues();
@@ -301,7 +309,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
301309
QueryResult qr = influxDB.query(new Query(sql));
302310

303311
String err = qr == null ? null : qr.getError();
304-
if (StringUtil.isNotEmpty(qr, true)) {
312+
if (StringUtil.isNotEmpty(err, true)) {
305313
throw new SQLException(err);
306314
}
307315

@@ -310,10 +318,43 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
310318
return new JSONObject(true);
311319
}
312320

313-
JSONObject result = JSON.parseObject(list.get(0));
314-
if (list.size() > 1) {
315-
result.put(KEY_RAW_LIST, list);
321+
List<JSONObject> resultList = new ArrayList<>();
322+
323+
for (int i = 0; i < list.size(); i++) {
324+
QueryResult.Result qyrt = list.get(i);
325+
List<QueryResult.Series> seriesList = qyrt.getSeries();
326+
if (seriesList == null || seriesList.isEmpty()) {
327+
continue;
328+
}
329+
330+
for (int j = 0; j < seriesList.size(); j++) {
331+
QueryResult.Series series = seriesList.get(j);
332+
List<List<Object>> valuesList = series.getValues();
333+
if (valuesList == null || valuesList.isEmpty()) {
334+
continue;
335+
}
336+
337+
List<String> columns = series.getColumns();
338+
for (int k = 0; k < valuesList.size(); k++) {
339+
340+
List<Object> values = valuesList.get(k);
341+
JSONObject obj = new JSONObject(true);
342+
if (values != null) {
343+
for (int l = 0; l < values.size(); l++) {
344+
obj.put(columns.get(l), values.get(l));
345+
}
346+
}
347+
resultList.add(obj);
348+
}
349+
}
350+
}
351+
352+
result = resultList.isEmpty() ? new JSONObject() : resultList.get(0);
353+
if (resultList.size() > 1) {
354+
result.put(KEY_RAW_LIST, resultList);
316355
}
356+
357+
putCache(sql, resultList, config);
317358

318359
return result;
319360
}

0 commit comments

Comments
 (0)