|
25 | 25 | //import apijson.milvus.MilvusUtil; |
26 | 26 | //import apijson.mongodb.MongoUtil; |
27 | 27 | import apijson.orm.SQLConfig; |
28 | | -//import com.alibaba.druid.pool.DruidDataSource; |
| 28 | +import com.alibaba.druid.pool.DruidDataSource; |
29 | 29 | import com.alibaba.fastjson.JSONObject; |
30 | 30 | //import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
31 | 31 | //import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
@@ -124,155 +124,155 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> { |
124 | 124 | public static final String DATABASE_NEBULA = "NEBULA"; |
125 | 125 |
|
126 | 126 | // 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig<Long> 不需要配置 dbVersion, dbUri, dbAccount, dbPassword |
127 | | -// @Override |
128 | | -// public Connection getConnection(SQLConfig<Long> config) throws Exception { |
129 | | -//// if (DATABASE_NEBULA.equals(config.getDatabase())) { // 3.0.0 及以下要这样连接 |
130 | | -//// String uri = config.getDBUri(); |
131 | | -//// |
132 | | -//// int start = uri.indexOf("://"); |
133 | | -//// String prefix = uri.substring(0, start); |
134 | | -//// |
135 | | -//// uri = uri.substring(start + "://".length()); |
136 | | -//// int end = uri.indexOf("/"); |
137 | | -//// String space = uri.substring(end + 1); |
138 | | -//// |
139 | | -//// Properties props = new Properties(); |
140 | | -//// props.put("url", prefix + "://" + space); |
141 | | -//// props.put("graphSpace", space); |
142 | | -//// |
143 | | -//// NebulaDriver driver = new NebulaDriver(uri.substring(0, end)); |
144 | | -//// return driver.connect(prefix + "://" + space, props); |
145 | | -//// // return DriverManager.getConnection("jdbc:nebula://JDBC_TEST_SPACE", "root", "nebula"); |
146 | | -//// } |
| 127 | + @Override |
| 128 | + public Connection getConnection(SQLConfig<Long> config) throws Exception { |
| 129 | +// if (DATABASE_NEBULA.equals(config.getDatabase())) { // 3.0.0 及以下要这样连接 |
| 130 | +// String uri = config.getDBUri(); |
147 | 131 | // |
148 | | -// String datasource = config.getDatasource(); |
149 | | -// Log.d(TAG, "getConnection config.getDatasource() = " + datasource); |
| 132 | +// int start = uri.indexOf("://"); |
| 133 | +// String prefix = uri.substring(0, start); |
150 | 134 | // |
151 | | -// String key = datasource + "-" + config.getDatabase(); |
152 | | -// Connection c = connectionMap.get(key); |
153 | | -// if (datasource != null && (c == null || c.isClosed())) { |
154 | | -// try { |
155 | | -// DataSource ds; |
156 | | -// switch (datasource) { |
157 | | -//// case "HIKARICP": |
158 | | -//// ds = DemoApplication.getApplicationContext().getBean(HikariDataSource.class); |
159 | | -//// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_HIKARICP 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_HIKARICP.getConnection(); |
160 | | -//// break; |
161 | | -// default: |
162 | | -// Map<String, DruidDataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DruidDataSource.class); |
163 | | -// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_DRUID 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_DRUID.getConnection(); |
164 | | -// switch (datasource) { |
165 | | -// case "DRUID-TEST": |
166 | | -// ds = dsMap.get("druidTestDataSource"); |
167 | | -// break; |
168 | | -// case "DRUID-ONLINE": |
169 | | -// ds = dsMap.get("druidOnlineDataSource"); |
170 | | -// break; |
171 | | -// case "DRUID": |
172 | | -// ds = dsMap.get("druidDataSource"); |
173 | | -// break; |
174 | | -// default: |
175 | | -// ds = null; |
176 | | -// break; |
177 | | -// } |
| 135 | +// uri = uri.substring(start + "://".length()); |
| 136 | +// int end = uri.indexOf("/"); |
| 137 | +// String space = uri.substring(end + 1); |
| 138 | +// |
| 139 | +// Properties props = new Properties(); |
| 140 | +// props.put("url", prefix + "://" + space); |
| 141 | +// props.put("graphSpace", space); |
| 142 | +// |
| 143 | +// NebulaDriver driver = new NebulaDriver(uri.substring(0, end)); |
| 144 | +// return driver.connect(prefix + "://" + space, props); |
| 145 | +// // return DriverManager.getConnection("jdbc:nebula://JDBC_TEST_SPACE", "root", "nebula"); |
| 146 | +// } |
| 147 | + |
| 148 | + String datasource = config.getDatasource(); |
| 149 | + Log.d(TAG, "getConnection config.getDatasource() = " + datasource); |
| 150 | + |
| 151 | + String key = datasource + "-" + config.getDatabase(); |
| 152 | + Connection c = connectionMap.get(key); |
| 153 | + if (datasource != null && (c == null || c.isClosed())) { |
| 154 | + try { |
| 155 | + DataSource ds; |
| 156 | + switch (datasource) { |
| 157 | +// case "HIKARICP": |
| 158 | +// ds = DemoApplication.getApplicationContext().getBean(HikariDataSource.class); |
| 159 | +// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_HIKARICP 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_HIKARICP.getConnection(); |
178 | 160 | // break; |
| 161 | + default: |
| 162 | + Map<String, DruidDataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DruidDataSource.class); |
| 163 | + // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_DRUID 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_DRUID.getConnection(); |
| 164 | + switch (datasource) { |
| 165 | + case "DRUID-TEST": |
| 166 | + ds = dsMap.get("druidTestDataSource"); |
| 167 | + break; |
| 168 | + case "DRUID-ONLINE": |
| 169 | + ds = dsMap.get("druidOnlineDataSource"); |
| 170 | + break; |
| 171 | + case "DRUID": |
| 172 | + ds = dsMap.get("druidDataSource"); |
| 173 | + break; |
| 174 | + default: |
| 175 | + ds = null; |
| 176 | + break; |
| 177 | + } |
| 178 | + break; |
| 179 | + } |
| 180 | + |
| 181 | + connectionMap.put(key, ds == null ? null : ds.getConnection()); |
| 182 | + } catch (Exception e) { |
| 183 | + Log.e(TAG, "getConnection try { " |
| 184 | + + "DataSource ds = DemoApplication.getApplicationContext().getBean(DataSource.class); .." |
| 185 | + + "} catch (Exception e) = " + e.getMessage()); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // 必须最后执行 super 方法,因为里面还有事务相关处理。 |
| 190 | + // 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表 |
| 191 | + return super.getConnection(config); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception { |
| 196 | + boolean isMilvus = DATABASE_MILVUS.equals(config.getDatabase()); // APIJSON 6.4.0+ 可用 config.isMilvus(); |
| 197 | + boolean isCassandra = config.isCassandra(); |
| 198 | + boolean isInfluxDB = config.isInfluxDB(); |
| 199 | + |
| 200 | + if (isMilvus || isCassandra || isInfluxDB) { |
| 201 | + // TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数 |
| 202 | + String sql = config.getSQL(false); // config.isPrepared()); |
| 203 | + if (sql != null && config.getMethod() == null) { |
| 204 | + String trimmedSQL = sql.trim(); |
| 205 | + String sqlPrefix = trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase(); |
| 206 | + if (sqlPrefix.startsWith("INSERT ")) { |
| 207 | + config.setMethod(RequestMethod.POST); |
| 208 | + } |
| 209 | + else if (sqlPrefix.startsWith("UPDATE ")) { |
| 210 | + config.setMethod(RequestMethod.PUT); |
| 211 | + } |
| 212 | + else if (sqlPrefix.startsWith("DELETE ")) { |
| 213 | + config.setMethod(RequestMethod.DELETE); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + boolean isWrite = ! RequestMethod.isQueryMethod(config.getMethod()); |
| 218 | + |
| 219 | + List<JSONObject> cache = isWrite ? null : getCache(sql, config); |
| 220 | + int position = config.getPosition(); |
| 221 | + JSONObject result = getCacheItem(cache, position, config); |
| 222 | + if (result != null) { |
| 223 | + if (position == 0 && cache != null && cache.size() > 1) { |
| 224 | + result.put(KEY_RAW_LIST, cache); |
| 225 | + } |
| 226 | + return result; |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | + List<JSONObject> resultList = new ArrayList<>(); |
| 231 | + |
| 232 | +// if (isMilvus) { |
| 233 | +// if (isWrite) { |
| 234 | +// return MilvusUtil.executeUpdate(config, sql); |
179 | 235 | // } |
180 | 236 | // |
181 | | -// connectionMap.put(key, ds == null ? null : ds.getConnection()); |
182 | | -// } catch (Exception e) { |
183 | | -// Log.e(TAG, "getConnection try { " |
184 | | -// + "DataSource ds = DemoApplication.getApplicationContext().getBean(DataSource.class); .." |
185 | | -// + "} catch (Exception e) = " + e.getMessage()); |
| 237 | +// resultList = MilvusUtil.executeQuery(config, sql, unknownType); |
186 | 238 | // } |
187 | | -// } |
188 | | -// |
189 | | -// // 必须最后执行 super 方法,因为里面还有事务相关处理。 |
190 | | -// // 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表 |
191 | | -// return super.getConnection(config); |
192 | | -// } |
193 | | -// |
194 | | -// @Override |
195 | | -// public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception { |
196 | | -// boolean isMilvus = DATABASE_MILVUS.equals(config.getDatabase()); // APIJSON 6.4.0+ 可用 config.isMilvus(); |
197 | | -// boolean isCassandra = config.isCassandra(); |
198 | | -// boolean isInfluxDB = config.isInfluxDB(); |
199 | | -// |
200 | | -// if (isMilvus || isCassandra || isInfluxDB) { |
201 | | -// // TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数 |
202 | | -// String sql = config.getSQL(false); // config.isPrepared()); |
203 | | -// if (sql != null && config.getMethod() == null) { |
204 | | -// String trimmedSQL = sql.trim(); |
205 | | -// String sqlPrefix = trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase(); |
206 | | -// if (sqlPrefix.startsWith("INSERT ")) { |
207 | | -// config.setMethod(RequestMethod.POST); |
208 | | -// } |
209 | | -// else if (sqlPrefix.startsWith("UPDATE ")) { |
210 | | -// config.setMethod(RequestMethod.PUT); |
| 239 | +// else if (isCassandra) { |
| 240 | +// if (isWrite) { |
| 241 | +// return CassandraUtil.executeUpdate(config, sql); |
211 | 242 | // } |
212 | | -// else if (sqlPrefix.startsWith("DELETE ")) { |
213 | | -// config.setMethod(RequestMethod.DELETE); |
214 | | -// } |
215 | | -// } |
216 | | -// |
217 | | -// boolean isWrite = ! RequestMethod.isQueryMethod(config.getMethod()); |
218 | 243 | // |
219 | | -// List<JSONObject> cache = isWrite ? null : getCache(sql, config); |
220 | | -// int position = config.getPosition(); |
221 | | -// JSONObject result = getCacheItem(cache, position, config); |
222 | | -// if (result != null) { |
223 | | -// if (position == 0 && cache != null && cache.size() > 1) { |
224 | | -// result.put(KEY_RAW_LIST, cache); |
225 | | -// } |
226 | | -// return result; |
| 244 | +// resultList = CassandraUtil.executeQuery(config, sql, unknownType); |
227 | 245 | // } |
| 246 | +// else if (isInfluxDB) { |
| 247 | +// if (isWrite) { |
| 248 | +// return InfluxDBUtil.executeUpdate(config, sql); |
| 249 | +// } |
228 | 250 | // |
229 | | -// |
230 | | -// List<JSONObject> resultList = new ArrayList<>(); |
231 | | -// |
232 | | -//// if (isMilvus) { |
233 | | -//// if (isWrite) { |
234 | | -//// return MilvusUtil.executeUpdate(config, sql); |
235 | | -//// } |
236 | | -//// |
237 | | -//// resultList = MilvusUtil.executeQuery(config, sql, unknownType); |
238 | | -//// } |
239 | | -//// else if (isCassandra) { |
240 | | -//// if (isWrite) { |
241 | | -//// return CassandraUtil.executeUpdate(config, sql); |
242 | | -//// } |
243 | | -//// |
244 | | -//// resultList = CassandraUtil.executeQuery(config, sql, unknownType); |
245 | | -//// } |
246 | | -//// else if (isInfluxDB) { |
247 | | -//// if (isWrite) { |
248 | | -//// return InfluxDBUtil.executeUpdate(config, sql); |
249 | | -//// } |
250 | | -//// |
251 | | -//// resultList = InfluxDBUtil.executeQuery(config, sql, unknownType); |
252 | | -//// } |
253 | | -// |
254 | | -// // TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数 |
255 | | -// result = resultList.isEmpty() ? new JSONObject() : resultList.get(0); |
256 | | -// if (resultList.size() > 1) { |
257 | | -// result.put(KEY_RAW_LIST, resultList); |
| 251 | +// resultList = InfluxDBUtil.executeQuery(config, sql, unknownType); |
258 | 252 | // } |
259 | | -// |
260 | | -// putCache(sql, resultList, config); |
261 | | -// |
262 | | -// return result; |
263 | | -// } |
264 | | -// |
265 | | -// return super.execute(config, unknownType); |
266 | | -// } |
267 | | -// |
268 | | -// @Override |
269 | | -// public void close() { |
270 | | -// super.close(); |
271 | | -// |
272 | | -//// MilvusUtil.closeAllClient(); |
273 | | -//// CassandraUtil.closeAllSession(); |
274 | | -//// InfluxDBUtil.closeAllClient(); |
275 | | -// } |
| 253 | + |
| 254 | + // TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数 |
| 255 | + result = resultList.isEmpty() ? new JSONObject() : resultList.get(0); |
| 256 | + if (resultList.size() > 1) { |
| 257 | + result.put(KEY_RAW_LIST, resultList); |
| 258 | + } |
| 259 | + |
| 260 | + putCache(sql, resultList, config); |
| 261 | + |
| 262 | + return result; |
| 263 | + } |
| 264 | + |
| 265 | + return super.execute(config, unknownType); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public void close() { |
| 270 | + super.close(); |
| 271 | + |
| 272 | +// MilvusUtil.closeAllClient(); |
| 273 | +// CassandraUtil.closeAllSession(); |
| 274 | +// InfluxDBUtil.closeAllClient(); |
| 275 | + } |
276 | 276 |
|
277 | 277 | // 不需要隐藏字段这个功能时,取消注释来提升性能 |
278 | 278 | // @Override |
|
0 commit comments