|
| 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.mongodb; |
| 16 | + |
| 17 | +import com.mongodb.jdbc.MongoBsonValue; |
| 18 | +import org.bson.*; |
| 19 | +import org.bson.conversions.Bson; |
| 20 | +import org.bson.types.ObjectId; |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Lemon |
| 27 | + * @see DemoSQLExecutor 重写 getValue 方法: |
| 28 | + * \@Override |
| 29 | + * protected Object getValue(SQLConfig<Long> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition |
| 30 | + * , JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception { |
| 31 | + * Object v = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap); |
| 32 | + * return MongoUtil.getValue(v); |
| 33 | + * } |
| 34 | + */ |
| 35 | +public class MongoUtil { |
| 36 | + public static final String TAG = "MongoUtil"; |
| 37 | + |
| 38 | + /**去除多余的包装 |
| 39 | + * @param value |
| 40 | + * @return |
| 41 | + */ |
| 42 | + public static Object getValue(Object value) { |
| 43 | + if (value instanceof Bson) { |
| 44 | + try { |
| 45 | + value = ((Bson) value).toBsonDocument(); |
| 46 | + } catch (Throwable e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (value instanceof MongoBsonValue) { |
| 52 | + value = ((MongoBsonValue) value).getBsonValue(); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + if (value instanceof BsonValue) { |
| 58 | + BsonValue nv = (BsonValue) value; |
| 59 | + if (nv.isNull()) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + if (nv.isArray()) { |
| 64 | + List<BsonValue> vs = nv.asArray().getValues(); |
| 65 | + if (vs == null) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + List<Object> l = new ArrayList<>(); |
| 70 | + for (BsonValue v : vs) { |
| 71 | + Object cv = getValue(v); |
| 72 | + l.add(cv); |
| 73 | + } |
| 74 | + |
| 75 | + return l; |
| 76 | + } |
| 77 | + |
| 78 | + if (nv.isDocument()) { |
| 79 | + BsonDocument bd = nv.asDocument(); |
| 80 | + Set<Map.Entry<String, BsonValue>> set = bd == null ? null : bd.entrySet(); |
| 81 | + if (set == null) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + Map<Object, Object> m = new LinkedHashMap<>(); |
| 86 | + for (Map.Entry<String, BsonValue> ety : set) { |
| 87 | + Object cv = getValue(ety.getValue()); |
| 88 | + m.put(ety.getKey(), cv); |
| 89 | + } |
| 90 | + |
| 91 | + return m; |
| 92 | + } |
| 93 | + |
| 94 | + if (nv.isDBPointer()) { |
| 95 | + value = nv.asDBPointer().getId(); |
| 96 | + } |
| 97 | + |
| 98 | + if (nv.isBoolean()) { |
| 99 | + return nv.asBoolean().getValue(); |
| 100 | + } |
| 101 | + if (nv.isBinary()) { |
| 102 | + return nv.asBinary().getData(); |
| 103 | + } |
| 104 | + if (nv.isInt32()) { |
| 105 | + return nv.asInt32().getValue(); |
| 106 | + } |
| 107 | + if (nv.isInt64()) { |
| 108 | + return nv.asInt64().getValue(); |
| 109 | + } |
| 110 | + if (nv.isDouble()) { |
| 111 | + return nv.asDouble().getValue(); |
| 112 | + } |
| 113 | + if (nv.isNumber()) { |
| 114 | + return nv.asNumber().doubleValue(); |
| 115 | + } |
| 116 | + if (nv.isDecimal128()) { |
| 117 | + return nv.asDecimal128().doubleValue(); |
| 118 | + } |
| 119 | + if (nv.isObjectId()) { |
| 120 | + ObjectId v = nv.asObjectId().getValue(); |
| 121 | + return v == null ? null : v.toString(); |
| 122 | + } |
| 123 | + if (nv.isDateTime()) { |
| 124 | + long v = nv.asDateTime().getValue(); |
| 125 | + return v; // new Timestamp(v); |
| 126 | + } |
| 127 | + if (nv.isString()) { |
| 128 | + return nv.asString().getValue(); |
| 129 | + } |
| 130 | + if (nv.isSymbol()) { |
| 131 | + return nv.asSymbol().getSymbol(); |
| 132 | + } |
| 133 | + |
| 134 | + if (nv.isJavaScript()) { |
| 135 | + BsonJavaScript v = nv.asJavaScript(); |
| 136 | + if (v == null) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + Map<Object, Object> m = new LinkedHashMap<>(); |
| 141 | + m.put("type", BsonType.JAVASCRIPT); |
| 142 | + m.put("code", v.getCode()); |
| 143 | + return m; |
| 144 | + } |
| 145 | + |
| 146 | + if (nv.isJavaScriptWithScope()) { |
| 147 | + BsonJavaScriptWithScope v = nv.asJavaScriptWithScope(); |
| 148 | + if (v == null) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + Map<Object, Object> m = new LinkedHashMap<>(); |
| 153 | + m.put("type", BsonType.JAVASCRIPT); |
| 154 | + m.put("scope", getValue(v.getScope())); |
| 155 | + m.put("code", v.getCode()); |
| 156 | + return m; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return value; |
| 161 | + } |
| 162 | +} |
0 commit comments