-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoUtil.java
More file actions
162 lines (136 loc) · 5.09 KB
/
MongoUtil.java
File metadata and controls
162 lines (136 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*Copyright ©2024 APIJSON(https://github.com/APIJSON)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.mongodb;
import com.mongodb.jdbc.MongoBsonValue;
import org.bson.*;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.util.*;
/**
* @author Lemon
* @see DemoSQLExecutor 重写 getValue 方法:
* \@Override
* protected Object getValue(SQLConfig<Long> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition
* , JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception {
* Object v = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
* return MongoUtil.getValue(v);
* }
*/
public class MongoUtil {
public static final String TAG = "MongoUtil";
/**去除多余的包装
* @param value
* @return
*/
public static Object getValue(Object value) {
if (value instanceof Bson) {
try {
value = ((Bson) value).toBsonDocument();
} catch (Throwable e) {
e.printStackTrace();
}
}
if (value instanceof MongoBsonValue) {
value = ((MongoBsonValue) value).getBsonValue();
}
if (value instanceof BsonValue) {
BsonValue nv = (BsonValue) value;
if (nv.isNull()) {
return null;
}
if (nv.isArray()) {
List<BsonValue> vs = nv.asArray().getValues();
if (vs == null) {
return null;
}
List<Object> l = new ArrayList<>();
for (BsonValue v : vs) {
Object cv = getValue(v);
l.add(cv);
}
return l;
}
if (nv.isDocument()) {
BsonDocument bd = nv.asDocument();
Set<Map.Entry<String, BsonValue>> set = bd == null ? null : bd.entrySet();
if (set == null) {
return null;
}
Map<Object, Object> m = new LinkedHashMap<>();
for (Map.Entry<String, BsonValue> ety : set) {
Object cv = getValue(ety.getValue());
m.put(ety.getKey(), cv);
}
return m;
}
if (nv.isDBPointer()) {
value = nv.asDBPointer().getId();
}
if (nv.isBoolean()) {
return nv.asBoolean().getValue();
}
if (nv.isBinary()) {
return nv.asBinary().getData();
}
if (nv.isInt32()) {
return nv.asInt32().getValue();
}
if (nv.isInt64()) {
return nv.asInt64().getValue();
}
if (nv.isDouble()) {
return nv.asDouble().getValue();
}
if (nv.isNumber()) {
return nv.asNumber().doubleValue();
}
if (nv.isDecimal128()) {
return nv.asDecimal128().doubleValue();
}
if (nv.isObjectId()) {
ObjectId v = nv.asObjectId().getValue();
return v == null ? null : v.toString();
}
if (nv.isDateTime()) {
long v = nv.asDateTime().getValue();
return v; // new Timestamp(v);
}
if (nv.isString()) {
return nv.asString().getValue();
}
if (nv.isSymbol()) {
return nv.asSymbol().getSymbol();
}
if (nv.isJavaScript()) {
BsonJavaScript v = nv.asJavaScript();
if (v == null) {
return null;
}
Map<Object, Object> m = new LinkedHashMap<>();
m.put("type", BsonType.JAVASCRIPT);
m.put("code", v.getCode());
return m;
}
if (nv.isJavaScriptWithScope()) {
BsonJavaScriptWithScope v = nv.asJavaScriptWithScope();
if (v == null) {
return null;
}
Map<Object, Object> m = new LinkedHashMap<>();
m.put("type", BsonType.JAVASCRIPT);
m.put("scope", getValue(v.getScope()));
m.put("code", v.getCode());
return m;
}
}
return value;
}
}