|
17 | 17 | import apijson.*;
|
18 | 18 | import com.alibaba.druid.pool.DruidDataSource;
|
19 | 19 | import com.alibaba.fastjson.JSONObject;
|
| 20 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 21 | +import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
| 22 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 23 | +import com.datastax.oss.driver.api.core.cql.Row; |
20 | 24 | import com.vesoft.nebula.jdbc.impl.NebulaDriver;
|
21 | 25 | import com.zaxxer.hikari.HikariDataSource;
|
22 | 26 |
|
23 | 27 | import java.io.Serializable;
|
| 28 | +import java.net.URI; |
| 29 | +import java.net.URL; |
| 30 | +import java.nio.file.Paths; |
24 | 31 | import java.sql.Connection;
|
25 | 32 | import java.sql.SQLException;
|
26 | 33 | import java.util.Collection;
|
|
37 | 44 | import org.influxdb.BatchOptions;
|
38 | 45 | import org.influxdb.InfluxDB;
|
39 | 46 | import org.influxdb.InfluxDBFactory;
|
40 |
| -import org.influxdb.dto.Point; |
41 | 47 | import org.influxdb.dto.Query;
|
42 | 48 | import org.influxdb.dto.QueryResult;
|
43 | 49 | import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
@@ -94,7 +100,7 @@ public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig c
|
94 | 100 | super.putCache(sql, list, config);
|
95 | 101 |
|
96 | 102 | String table = config != null && config.isMain() ? config.getTable() : null;
|
97 |
| - if (table != null && DemoSQLConfig.CONFIG_TABLE_LIST.contains(table) == false) { |
| 103 | + if (table != null && ! DemoSQLConfig.CONFIG_TABLE_LIST.contains(table)) { |
98 | 104 | try {
|
99 | 105 | if (config.isExplain() || RequestMethod.isHeadMethod(config.getMethod(), true)) {
|
100 | 106 | REDIS_TEMPLATE.opsForValue().set(sql, JSON.toJSONString(list), 10 * 60, TimeUnit.SECONDS);
|
@@ -124,6 +130,8 @@ public synchronized void removeCache(String sql, SQLConfig config) {
|
124 | 130 | // Redis 缓存 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
125 | 131 |
|
126 | 132 | public static final String DATABASE_NEBULA = "NEBULA";
|
| 133 | + public static final String DATABASE_SNOWFLAKE = "SNOWFLAKE"; |
| 134 | + public static final String DATABASE_CASSANDRA = "CASSANDRA"; |
127 | 135 |
|
128 | 136 | // 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
|
129 | 137 | @Override
|
@@ -196,82 +204,123 @@ public Connection getConnection(SQLConfig config) throws Exception {
|
196 | 204 |
|
197 | 205 | @Override
|
198 | 206 | public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception {
|
199 |
| - if (DemoSQLConfig.DATABASE_INFLUXDB.equals(config.getDatabase())) { |
200 |
| - InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword()); |
201 |
| - influxDB.setDatabase(config.getSchema()); |
| 207 | + if (DATABASE_CASSANDRA.equals(config.getDatabase()) || DemoSQLConfig.DATABASE_INFLUXDB.equals(config.getDatabase())) { |
202 | 208 |
|
203 | 209 | String sql = config.getSQL(config.isPrepared());
|
204 | 210 |
|
205 | 211 | RequestMethod method = config.getMethod();
|
206 |
| - boolean isWrite = ! RequestMethod.isQueryMethod(method); |
207 |
| - if (method == null && ! isWrite) { |
| 212 | + boolean isWrite = !RequestMethod.isQueryMethod(method); |
| 213 | + if (method == null && !isWrite) { |
208 | 214 | String trimmedSQL = sql == null ? null : sql.trim();
|
209 | 215 | String sqlPrefix = trimmedSQL == null || trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
|
210 | 216 | isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
|
211 | 217 | }
|
212 | 218 |
|
213 |
| - if (isWrite) { |
214 |
| - influxDB.enableBatch( |
215 |
| - BatchOptions.DEFAULTS |
216 |
| - .threadFactory(runnable -> { |
217 |
| - Thread thread = new Thread(runnable); |
218 |
| - thread.setDaemon(true); |
219 |
| - return thread; |
220 |
| - }) |
221 |
| - ); |
222 | 219 |
|
223 |
| - Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close)); |
| 220 | + if (DATABASE_CASSANDRA.equals(config.getDatabase())) { |
| 221 | + CqlSession session = CqlSession.builder() |
| 222 | +// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip")) |
| 223 | + .withCloudSecureConnectBundle(new URL(config.getDBUri())) |
| 224 | + .withAuthCredentials(config.getDBAccount(), config.getDBPassword()) |
| 225 | + .withKeyspace(config.getSchema()) |
| 226 | + .build(); |
| 227 | + |
| 228 | + // if (config.isPrepared()) { |
| 229 | + // PreparedStatement stt = session.prepare(sql); |
| 230 | + // |
| 231 | + // List<Object> pl = config.getPreparedValueList(); |
| 232 | + // if (pl != null) { |
| 233 | + // for (Object o : pl) { |
| 234 | + // stt.bind(pl.toArray()); |
| 235 | + // } |
| 236 | + // } |
| 237 | + // sql = stt.getQuery(); |
| 238 | + // } |
| 239 | + |
| 240 | + ResultSet rs = session.execute(sql); |
| 241 | + |
| 242 | + List<Row> list = rs.all(); |
| 243 | + if (list == null || list.isEmpty()) { |
| 244 | + return new JSONObject(true); |
| 245 | + } |
224 | 246 |
|
225 |
| - influxDB.write(sql); |
| 247 | + JSONObject result = JSON.parseObject(list.get(0)); |
| 248 | + if (list.size() > 1) { |
| 249 | + result.put(KEY_RAW_LIST, list); |
| 250 | + } |
226 | 251 |
|
227 |
| - JSONObject result = DemoParser.newSuccessResult(); |
| 252 | + return result; |
| 253 | + } |
228 | 254 |
|
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 | 255 |
|
243 |
| - if (method == RequestMethod.PUT) { |
244 |
| - Map<String, Object> content = config.getContent(); |
245 |
| - result.put(JSONResponse.KEY_COUNT, content == null ? 0 : content.size()); |
| 256 | + if (DemoSQLConfig.DATABASE_INFLUXDB.equals(config.getDatabase())) { |
| 257 | + InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword()); |
| 258 | + influxDB.setDatabase(config.getSchema()); |
| 259 | + |
| 260 | + |
| 261 | + if (isWrite) { |
| 262 | + influxDB.enableBatch( |
| 263 | + BatchOptions.DEFAULTS |
| 264 | + .threadFactory(runnable -> { |
| 265 | + Thread thread = new Thread(runnable); |
| 266 | + thread.setDaemon(true); |
| 267 | + return thread; |
| 268 | + }) |
| 269 | + ); |
| 270 | + |
| 271 | + Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close)); |
| 272 | + |
| 273 | + influxDB.write(sql); |
| 274 | + |
| 275 | + JSONObject result = DemoParser.newSuccessResult(); |
| 276 | + |
| 277 | + if (method == RequestMethod.POST) { |
| 278 | + List<List<Object>> values = config.getValues(); |
| 279 | + result.put(JSONResponse.KEY_COUNT, values == null ? 0 : values.size()); |
246 | 280 | } else {
|
247 |
| - result.put(JSONResponse.KEY_COUNT, id == null && idIn instanceof Collection ? ((Collection<?>) idIn).size() : 1); // FIXME 直接 SQLAuto 传 Flux/InfluxQL INSERT 如何取数量? |
| 281 | + String idKey = config.getIdKey(); |
| 282 | + Object id = config.getId(); |
| 283 | + Object idIn = config.getIdIn(); |
| 284 | + if (id != null) { |
| 285 | + result.put(idKey, id); |
| 286 | + } |
| 287 | + if (idIn != null) { |
| 288 | + result.put(idKey + "[]", idIn); |
| 289 | + } |
| 290 | + |
| 291 | + if (method == RequestMethod.PUT) { |
| 292 | + Map<String, Object> content = config.getContent(); |
| 293 | + result.put(JSONResponse.KEY_COUNT, content == null ? 0 : content.size()); |
| 294 | + } else { |
| 295 | + result.put(JSONResponse.KEY_COUNT, id == null && idIn instanceof Collection ? ((Collection<?>) idIn).size() : 1); // FIXME 直接 SQLAuto 传 Flux/InfluxQL INSERT 如何取数量? |
| 296 | + } |
248 | 297 | }
|
| 298 | + |
| 299 | + return result; |
249 | 300 | }
|
250 | 301 |
|
251 |
| - return result; |
252 |
| - } |
| 302 | + QueryResult qr = influxDB.query(new Query(sql)); |
253 | 303 |
|
254 |
| - QueryResult qr = influxDB.query(new Query(sql)); |
| 304 | + String err = qr == null ? null : qr.getError(); |
| 305 | + if (StringUtil.isNotEmpty(qr, true)) { |
| 306 | + throw new SQLException(err); |
| 307 | + } |
255 | 308 |
|
256 |
| - String err = qr == null ? null : qr.getError(); |
257 |
| - if (StringUtil.isNotEmpty(qr, true)) { |
258 |
| - throw new SQLException(err); |
259 |
| - } |
| 309 | + List<QueryResult.Result> list = qr == null ? null : qr.getResults(); |
| 310 | + if (list == null || list.isEmpty()) { |
| 311 | + return new JSONObject(true); |
| 312 | + } |
260 | 313 |
|
261 |
| - List<QueryResult.Result> list = qr == null ? null : qr.getResults(); |
262 |
| - if (list == null || list.isEmpty()) { |
263 |
| - return new JSONObject(true); |
264 |
| - } |
| 314 | + JSONObject result = JSON.parseObject(list.get(0)); |
| 315 | + if (list.size() > 1) { |
| 316 | + result.put(KEY_RAW_LIST, list); |
| 317 | + } |
265 | 318 |
|
266 |
| - JSONObject result = JSON.parseObject(list.get(0)); |
267 |
| - if (list.size() > 1) { |
268 |
| - result.put(KEY_RAW_LIST, list); |
| 319 | + return result; |
269 | 320 | }
|
270 | 321 |
|
271 |
| - return result; |
272 | 322 | }
|
273 | 323 |
|
274 |
| - |
275 | 324 | return super.execute(config, unknownType);
|
276 | 325 | }
|
277 | 326 |
|
|
0 commit comments