@@ -1371,99 +1371,147 @@ public String execute(@RequestBody String request, HttpSession session) {
1371
1371
long startTime = System .currentTimeMillis ();
1372
1372
1373
1373
JSONObject req = JSON .parseObject (request );
1374
+ String sql = req == null ? null : req .getString ("sql" );
1375
+ if (StringUtil .isEmpty (sql )) {
1376
+ throw new IllegalArgumentException ("SQL 不能为空!" );
1377
+ }
1378
+
1374
1379
String database = req .getString ("database" );
1380
+ String schema = req .getString ("schema" );
1375
1381
String uri = req .getString ("uri" );
1376
1382
String account = req .getString ("account" );
1377
1383
String password = req .getString ("password" );
1378
- String sql = req .getString ("sql" );
1379
1384
JSONArray arg = req .getJSONArray ("args" );
1380
1385
try {
1381
- String trimmedSQL = sql .trim ();
1386
+ if (StringUtil .isEmpty (database , true )) {
1387
+
1388
+ int start = uri .indexOf ("://" );
1389
+ String prefix = uri .substring (0 , start );
1390
+ int mid = prefix .lastIndexOf (":" );
1391
+ if (mid >= 0 ) {
1392
+ prefix = prefix .substring (mid + 1 );
1393
+ }
1394
+
1395
+ if (DemoSQLExecutor .DATABASE_INFLUXDB .equalsIgnoreCase (prefix )) {
1396
+ database = prefix .toUpperCase ();
1397
+
1398
+ int end = uri .lastIndexOf ("/" );
1399
+ if (end >= 0 ) {
1400
+ if (StringUtil .isEmpty (schema , true )) {
1401
+ schema = uri .substring (end + 1 );
1402
+ }
1403
+ uri = uri .substring (0 , end );
1404
+ }
1405
+
1406
+ uri = "http" + uri .substring (start );
1407
+
1408
+ account = "root" ;
1409
+ password = "apijson@123" ;
1410
+ } else if (DemoSQLExecutor .DATABASE_NEBULA .equalsIgnoreCase (prefix )) {
1411
+ database = prefix .toUpperCase ();
1412
+ }
1413
+ }
1414
+
1415
+
1416
+ String trimmedSQL = sql == null ? null : sql .trim ();
1382
1417
1383
1418
List <Object > valueList = arg ;
1384
1419
1385
1420
DemoSQLExecutor executor = new DemoSQLExecutor ();
1386
1421
DemoSQLConfig config = new DemoSQLConfig ();
1387
1422
1388
- config .setDatabase (database ); // "NEBULA"); //
1423
+ config .setDatabase (database );
1424
+ config .setSchema (schema );
1389
1425
config .setDBUri (uri );
1390
1426
config .setDBAccount (account );
1391
1427
config .setDBPassword (password );
1392
1428
config .setPrepared (true );
1393
1429
config .setPreparedValueList (valueList );
1430
+ config .setSql (sql );
1394
1431
1395
- String sqlPrefix = trimmedSQL .length () >= 7 ? trimmedSQL .substring (0 , 7 ).toUpperCase () : "" ;
1432
+ String sqlPrefix = trimmedSQL == null || trimmedSQL .length () < 7 ? "" : trimmedSQL .substring (0 , 7 ).toUpperCase ();
1396
1433
boolean isWrite = sqlPrefix .startsWith ("INSERT " ) || sqlPrefix .startsWith ("UPDATE " ) || sqlPrefix .startsWith ("DELETE " );
1397
1434
1398
- long executeStartTime = System . currentTimeMillis () ;
1435
+ JSONArray arr = null ;
1399
1436
1437
+ long updateCount = 0 ;
1438
+ long executeDuration = 0 ;
1439
+ long cursorDuration = 0 ;
1440
+ long rsDuration = 0 ;
1441
+ long executeStartTime = System .currentTimeMillis ();
1400
1442
1401
- Statement statement = executor .getStatement (config , trimmedSQL );
1402
- if (statement instanceof PreparedStatement ) {
1403
- if (EXECUTE_STRICTLY ) {
1404
- if (isWrite ) {
1405
- ((PreparedStatement ) statement ).executeUpdate ();
1443
+ if (DemoSQLExecutor .DATABASE_INFLUXDB .equals (database )) {
1444
+ JSONObject result = executor .execute (config , false );
1445
+ if (isWrite ) {
1446
+ updateCount = result == null ? 0 : result .getIntValue (JSONResponse .KEY_COUNT );
1447
+ } else {
1448
+ arr = result == null ? null : result .getJSONArray (DemoSQLExecutor .KEY_RAW_LIST );
1449
+ }
1450
+ } else {
1451
+ Statement statement = executor .getStatement (config , trimmedSQL );
1452
+ if (statement instanceof PreparedStatement ) {
1453
+ if (EXECUTE_STRICTLY ) {
1454
+ if (isWrite ) {
1455
+ ((PreparedStatement ) statement ).executeUpdate ();
1456
+ } else {
1457
+ ((PreparedStatement ) statement ).executeQuery ();
1458
+ }
1406
1459
} else {
1407
- ((PreparedStatement ) statement ).executeQuery ();
1460
+ ((PreparedStatement ) statement ).execute ();
1461
+ }
1462
+ } else {
1463
+ if (arg != null && !arg .isEmpty ()) {
1464
+ throw new UnsupportedOperationException ("非预编译模式不允许传参 arg !" );
1408
1465
}
1409
- }
1410
- else {
1411
- ((PreparedStatement ) statement ).execute ();
1412
- }
1413
- } else {
1414
- if (arg != null && ! arg .isEmpty ()) {
1415
- throw new UnsupportedOperationException ("非预编译模式不允许传参 arg !" );
1416
- }
1417
1466
1418
- if (EXECUTE_STRICTLY ) {
1419
- if (isWrite ) {
1420
- statement .executeUpdate (sql );
1467
+ if (EXECUTE_STRICTLY ) {
1468
+ if (isWrite ) {
1469
+ statement .executeUpdate (sql );
1470
+ } else {
1471
+ statement .executeQuery (sql );
1472
+ }
1421
1473
} else {
1422
- statement .executeQuery (sql );
1474
+ statement .execute (sql );
1423
1475
}
1424
1476
}
1425
- else {
1426
- statement .execute (sql );
1427
- }
1428
- }
1429
1477
1430
- long executeDuration = System .currentTimeMillis () - executeStartTime ;
1478
+ executeDuration = System .currentTimeMillis () - executeStartTime ;
1431
1479
1432
- ResultSet rs = statement .getResultSet ();
1433
- ResultSetMetaData rsmd = rs == null ? null : rs .getMetaData ();
1434
- int length = rsmd == null ? 0 : rsmd .getColumnCount ();
1480
+ arr = new JSONArray ();
1481
+ ResultSet rs = statement .getResultSet ();
1482
+ ResultSetMetaData rsmd = rs == null ? null : rs .getMetaData ();
1483
+ int length = rsmd == null ? 0 : rsmd .getColumnCount ();
1435
1484
1436
- JSONArray arr = new JSONArray ();
1437
1485
1438
- long cursorDuration = 0 ;
1439
- long rsDuration = 0 ;
1486
+ long cursorStartTime = System .currentTimeMillis ();
1487
+ while (rs != null && rs .next ()) {
1488
+ cursorDuration += System .currentTimeMillis () - cursorStartTime ;
1440
1489
1441
- long cursorStartTime = System .currentTimeMillis ();
1442
- while (rs != null && rs .next ()) {
1443
- cursorDuration += System .currentTimeMillis () - cursorStartTime ;
1490
+ JSONObject obj = new JSONObject (true );
1491
+ for (int i = 1 ; i <= length ; i ++) {
1492
+ long sqlStartTime = System .currentTimeMillis ();
1493
+ String name = rsmd .getColumnName (i ); // rsmd.getColumnLable(i);
1494
+ Object value = rs .getObject (i );
1495
+ rsDuration += System .currentTimeMillis () - sqlStartTime ;
1444
1496
1445
- JSONObject obj = new JSONObject (true );
1446
- for (int i = 1 ; i <= length ; i ++) {
1447
- long sqlStartTime = System .currentTimeMillis ();
1448
- String name = rsmd .getColumnName (i ); // rsmd.getColumnLable(i);
1449
- Object value = rs .getObject (i );
1450
- rsDuration += System .currentTimeMillis () - sqlStartTime ;
1497
+ obj .put (name , value );
1498
+ }
1451
1499
1452
- obj . put ( name , value );
1500
+ arr . add ( obj );
1453
1501
}
1454
1502
1455
- arr .add (obj );
1503
+ // try {
1504
+ updateCount = statement .getUpdateCount ();
1505
+ // } catch (Throwable e) {
1506
+ // e.printStackTrace();
1507
+ // }
1456
1508
}
1457
1509
1458
1510
JSONObject result = DemoParser .newSuccessResult ();
1459
1511
result .put ("sql" , sql );
1460
1512
result .put ("args" , arg );
1461
1513
if (isWrite ) {
1462
- // try {
1463
- result .put ("count" , statement .getUpdateCount ());
1464
- // } catch (Throwable e) {
1465
- // e.printStackTrace();
1466
- // }
1514
+ result .put ("count" , updateCount );
1467
1515
}
1468
1516
result .put ("list" , arr );
1469
1517
@@ -1475,7 +1523,10 @@ public String execute(@RequestBody String request, HttpSession session) {
1475
1523
1476
1524
result .put ("time:start|duration|end|parse|sql" , startTime + "|" + duration + "|" + endTime + "|" + parseDuration + "|" + sqlDuration );
1477
1525
1478
- // return result.toJSONString();
1526
+ if (DemoSQLExecutor .DATABASE_NEBULA .equalsIgnoreCase (database ) == false ) {
1527
+ return result .toJSONString ();
1528
+ }
1529
+
1479
1530
return com .alibaba .fastjson .JSON .toJSONString (result , new ValueFilter () {
1480
1531
@ Override
1481
1532
public Object process (Object o , String key , Object val ) {
0 commit comments