15
15
package apijson .demo ;
16
16
17
17
import apijson .*;
18
+ import apijson .orm .AbstractSQLConfig ;
18
19
import com .alibaba .druid .pool .DruidDataSource ;
20
+ import com .alibaba .fastjson .JSONArray ;
19
21
import com .alibaba .fastjson .JSONObject ;
20
22
import com .datastax .oss .driver .api .core .CqlSession ;
21
23
import com .datastax .oss .driver .api .core .cql .PreparedStatement ;
30
32
import java .nio .file .Paths ;
31
33
import java .sql .Connection ;
32
34
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 .*;
37
36
import java .util .concurrent .TimeUnit ;
38
37
39
38
import javax .sql .DataSource ;
@@ -202,21 +201,31 @@ public Connection getConnection(SQLConfig config) throws Exception {
202
201
203
202
@ Override
204
203
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
+ }
209
218
210
219
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 ) {
213
222
String trimmedSQL = sql == null ? null : sql .trim ();
214
223
String sqlPrefix = trimmedSQL == null || trimmedSQL .length () < 7 ? "" : trimmedSQL .substring (0 , 7 ).toUpperCase ();
215
224
isWrite = sqlPrefix .startsWith ("INSERT " ) || sqlPrefix .startsWith ("UPDATE " ) || sqlPrefix .startsWith ("DELETE " );
216
225
}
217
226
218
227
219
- if (DemoSQLConfig . DATABASE_CASSANDRA . equals ( db ) ) {
228
+ if (isCassandra ) {
220
229
CqlSession session = CqlSession .builder ()
221
230
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
222
231
.withCloudSecureConnectBundle (new URL (config .getDBUri ()))
@@ -243,7 +252,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
243
252
return new JSONObject (true );
244
253
}
245
254
246
- JSONObject result = JSON .parseObject (list .get (0 ));
255
+ result = JSON .parseObject (list .get (0 ));
247
256
if (list .size () > 1 ) {
248
257
result .put (KEY_RAW_LIST , list );
249
258
}
@@ -252,11 +261,10 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
252
261
}
253
262
254
263
255
- if (DemoSQLConfig . DATABASE_INFLUXDB . equals ( db ) ) {
264
+ if (isInfluxDB ) {
256
265
InfluxDB influxDB = InfluxDBFactory .connect (config .getDBUri (), config .getDBAccount (), config .getDBPassword ());
257
266
influxDB .setDatabase (config .getSchema ());
258
267
259
-
260
268
if (isWrite ) {
261
269
influxDB .enableBatch (
262
270
BatchOptions .DEFAULTS
@@ -271,7 +279,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
271
279
272
280
influxDB .write (sql );
273
281
274
- JSONObject result = DemoParser .newSuccessResult ();
282
+ result = DemoParser .newSuccessResult ();
275
283
276
284
if (method == RequestMethod .POST ) {
277
285
List <List <Object >> values = config .getValues ();
@@ -301,7 +309,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
301
309
QueryResult qr = influxDB .query (new Query (sql ));
302
310
303
311
String err = qr == null ? null : qr .getError ();
304
- if (StringUtil .isNotEmpty (qr , true )) {
312
+ if (StringUtil .isNotEmpty (err , true )) {
305
313
throw new SQLException (err );
306
314
}
307
315
@@ -310,10 +318,43 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
310
318
return new JSONObject (true );
311
319
}
312
320
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 );
316
355
}
356
+
357
+ putCache (sql , resultList , config );
317
358
318
359
return result ;
319
360
}
0 commit comments