23
23
import java .io .Serializable ;
24
24
import java .sql .Connection ;
25
25
import java .sql .SQLException ;
26
+ import java .util .Collection ;
26
27
import java .util .List ;
27
28
import java .util .Map ;
28
29
import java .util .Properties ;
@@ -123,7 +124,6 @@ public synchronized void removeCache(String sql, SQLConfig config) {
123
124
// Redis 缓存 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
124
125
125
126
public static final String DATABASE_NEBULA = "NEBULA" ;
126
- public static final String DATABASE_INFLUXDB = "INFLUXDB" ;
127
127
128
128
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
129
129
@ Override
@@ -195,16 +195,20 @@ public Connection getConnection(SQLConfig config) throws Exception {
195
195
196
196
197
197
@ 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 ())) {
200
200
InfluxDB influxDB = InfluxDBFactory .connect (config .getDBUri (), config .getDBAccount (), config .getDBPassword ());
201
-
202
201
influxDB .setDatabase (config .getSchema ());
203
202
204
203
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
+ }
208
212
209
213
if (isWrite ) {
210
214
influxDB .enableBatch (
@@ -220,8 +224,30 @@ public JSONObject execute(SQLConfig config, boolean unknownType) throws Exceptio
220
224
221
225
influxDB .write (sql );
222
226
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
+
225
251
return result ;
226
252
}
227
253
@@ -238,7 +264,7 @@ public JSONObject execute(SQLConfig config, boolean unknownType) throws Exceptio
238
264
}
239
265
240
266
JSONObject result = JSON .parseObject (list .get (0 ));
241
- if (list .size () > 0 ) {
267
+ if (list .size () > 1 ) {
242
268
result .put (KEY_RAW_LIST , list );
243
269
}
244
270
0 commit comments