|
| 1 | +/*Copyright ©2024 APIJSON(https://github.com/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.cassandra; |
| 16 | + |
| 17 | +import apijson.JSON; |
| 18 | +import apijson.JSONResponse; |
| 19 | +import apijson.NotNull; |
| 20 | +import apijson.RequestMethod; |
| 21 | +import apijson.orm.SQLConfig; |
| 22 | +import com.alibaba.fastjson.JSONObject; |
| 23 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 24 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 25 | +import com.datastax.oss.driver.api.core.cql.Row; |
| 26 | + |
| 27 | +import java.net.MalformedURLException; |
| 28 | +import java.net.URL; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +import static apijson.orm.AbstractSQLExecutor.KEY_RAW_LIST; |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Lemon |
| 36 | + * @see DemoSQLExecutor 重写 execute 方法: |
| 37 | + * \@Override |
| 38 | + * public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception { |
| 39 | + * if (config.isMilvus()) { |
| 40 | + * return CassandraUtil.execute(config, null, unknownType); |
| 41 | + * } |
| 42 | + * |
| 43 | + * return super.execute(config, unknownType); |
| 44 | + * } |
| 45 | + */ |
| 46 | +public class CassandraUtil { |
| 47 | + public static final String TAG = "CassandraUtil"; |
| 48 | + |
| 49 | + public static final Map<String, CqlSession> CLIENT_MAP = new LinkedHashMap<>(); |
| 50 | + public static <T> CqlSession getSession(@NotNull SQLConfig<T> config) throws MalformedURLException { |
| 51 | + String uri = config.getDBUri(); |
| 52 | + String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount(); |
| 53 | + |
| 54 | + CqlSession session = CLIENT_MAP.get(key); |
| 55 | + if (session == null) { |
| 56 | + session = CqlSession.builder() |
| 57 | +// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip")) |
| 58 | + .withCloudSecureConnectBundle(new URL(config.getDBUri())) |
| 59 | +// .withAuthCredentials(config.getDBAccount(), config.getDBPassword()) |
| 60 | + .withKeyspace(config.getSchema()) |
| 61 | + .build(); |
| 62 | + |
| 63 | + CLIENT_MAP.put(key, session); |
| 64 | + } |
| 65 | + return session; |
| 66 | + } |
| 67 | + |
| 68 | + public static <T> void closeSession(@NotNull SQLConfig<T> config) throws MalformedURLException { |
| 69 | + CqlSession conn = getSession(config); |
| 70 | + if (conn != null) { |
| 71 | + String uri = config.getDBUri(); |
| 72 | + String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount(); |
| 73 | + CLIENT_MAP.remove(key); |
| 74 | + |
| 75 | +// try { |
| 76 | + conn.close(); |
| 77 | +// } |
| 78 | +// catch (Throwable e) { |
| 79 | +// e.printStackTrace(); |
| 80 | +// } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static <T> void closeAllSession() { |
| 85 | + Collection<CqlSession> cs = CLIENT_MAP.values(); |
| 86 | + for (CqlSession c : cs) { |
| 87 | + try { |
| 88 | + c.close(); |
| 89 | + } |
| 90 | + catch (Throwable e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + CLIENT_MAP.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + public static <T> JSONObject execute(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception { |
| 100 | + if (RequestMethod.isQueryMethod(config.getMethod())) { |
| 101 | + return execQuery(config, sql, unknownType); |
| 102 | + } |
| 103 | + |
| 104 | + return executeUpdate(config, sql); |
| 105 | + } |
| 106 | + |
| 107 | + public static <T> int execUpdate(SQLConfig<T> config, String sql) throws Exception { |
| 108 | + JSONObject result = executeUpdate(config, sql); |
| 109 | + return result.getIntValue(JSONResponse.KEY_COUNT); |
| 110 | + } |
| 111 | + |
| 112 | + public static <T> JSONObject executeUpdate(SQLConfig<T> config, String sql) throws Exception { |
| 113 | + return executeUpdate(null, config, sql); |
| 114 | + } |
| 115 | + public static <T> JSONObject executeUpdate(CqlSession session, SQLConfig<T> config, String sql) throws Exception { |
| 116 | + if (session == null) { |
| 117 | + session = getSession(config); |
| 118 | + } |
| 119 | + |
| 120 | + if (session == null) { |
| 121 | + session = getSession(config); |
| 122 | + } |
| 123 | + |
| 124 | + ResultSet rs = session.execute(sql); |
| 125 | + |
| 126 | + Row row = rs.one(); |
| 127 | + if (row == null) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + JSONObject result = new JSONObject(true); |
| 132 | + result.put(JSONResponse.KEY_COUNT, row.get(JSONResponse.KEY_COUNT, Integer.class)); |
| 133 | + if (config.getId() != null) { |
| 134 | + result.put(JSONResponse.KEY_ID, config.getId()); |
| 135 | + } |
| 136 | + |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + public static <T> JSONObject execQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception { |
| 142 | + List<JSONObject> list = executeQuery(config, sql, unknownType); |
| 143 | + JSONObject result = list == null || list.isEmpty() ? null : list.get(0); |
| 144 | + if (result == null) { |
| 145 | + result = new JSONObject(true); |
| 146 | + } |
| 147 | + |
| 148 | + if (list != null && list.size() > 1) { |
| 149 | + result.put(KEY_RAW_LIST, list); |
| 150 | + } |
| 151 | + |
| 152 | + return result; |
| 153 | + } |
| 154 | + |
| 155 | + public static <T> List<JSONObject> executeQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception { |
| 156 | + return executeQuery(null, config, sql, unknownType); |
| 157 | + } |
| 158 | + public static <T> List<JSONObject> executeQuery(CqlSession session, @NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception { |
| 159 | + if (session == null) { |
| 160 | + session = getSession(config); |
| 161 | + } |
| 162 | + |
| 163 | + // if (config.isPrepared()) { |
| 164 | + // PreparedStatement stt = session.prepare(sql); |
| 165 | + // |
| 166 | + // List<Object> pl = config.getPreparedValueList(); |
| 167 | + // if (pl != null) { |
| 168 | + // for (Object o : pl) { |
| 169 | + // stt.bind(pl.toArray()); |
| 170 | + // } |
| 171 | + // } |
| 172 | + // sql = stt.getQuery(); |
| 173 | + // } |
| 174 | + |
| 175 | + ResultSet rs = session.execute(sql); |
| 176 | + |
| 177 | + List<Row> list = rs.all(); |
| 178 | + if (list == null) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + |
| 182 | + List<JSONObject> resultList = new ArrayList<>(list.size()); |
| 183 | + for (int i = 0; i < list.size(); i++) { |
| 184 | + resultList.add(JSON.parseObject(list.get(i))); |
| 185 | + } |
| 186 | + |
| 187 | + return resultList; |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments