Repository navigation
Expand file tree
/
Copy pathAbstractSQLExecutor.java
More file actions
executable file
·370 lines (295 loc) · 11.3 KB
/
Copy pathAbstractSQLExecutor.java
File metadata and controls
executable file
·370 lines (295 loc) · 11.3 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/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 zuo.biao.apijson.server;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import zuo.biao.apijson.JSONResponse;
import zuo.biao.apijson.Log;
import zuo.biao.apijson.StringUtil;
/**executor for query(read) or update(write) MySQL database
* @author Lemon
*/
public abstract class AbstractSQLExecutor implements SQLExecutor {
private static final String TAG = "SQLExecutor";
//访问一次后丢失,可能因为static导致内存共享,别的地方改变了内部对象的值
// private static final Map<String, Map<Integer, JSONObject>> staticCacheMap;
// static {
// staticCacheMap = new HashMap<String, Map<Integer, JSONObject>>();
// }
/**
* 缓存map
*/
protected Map<String, Map<Integer, JSONObject>> cacheMap = new HashMap<String, Map<Integer, JSONObject>>();
/**保存缓存
* @param sql
* @param map
* @param isStatic
*/
@Override
public synchronized void putCache(String sql, Map<Integer, JSONObject> map, boolean isStatic) {
if (sql == null || map == null) { //空map有效,说明查询过sql了 || map.isEmpty()) {
Log.i(TAG, "saveList sql == null || map == null >> return;");
return;
}
// if (isStatic) {
// staticCacheMap.put(sql, map);
// } else {
cacheMap.put(sql, map);
// }
}
/**移除缓存
* @param sql
* @param isStatic
*/
@Override
public synchronized void removeCache(String sql, boolean isStatic) {
if (sql == null) {
Log.i(TAG, "removeList sql == null >> return;");
return;
}
// if (isStatic) {
// staticCacheMap.remove(sql);
// } else {
cacheMap.remove(sql);
// }
}
/**获取缓存
* @param sql
* @param position
* @param isStatic
* @return
*/
@Override
public JSONObject getCache(String sql, int position, boolean isStatic) {
Map<Integer, JSONObject> map = /** isStatic ? staticCacheMap.get(sql) : */ cacheMap.get(sql);
//只要map不为null,则如果 map.get(position) == null,则返回 {} ,避免再次SQL查询
if (map == null) {
return null;
}
JSONObject result = map.get(position);
return result != null ? result : new JSONObject();
}
/**关闭连接,释放资源
*/
@Override
public void close() {
cacheMap.clear();
cacheMap = null;
}
/**执行SQL
* @param config
* @return
* @throws Exception
*/
@Override
public JSONObject execute(SQLConfig config) throws Exception {
if (config == null) {
Log.e(TAG, "select config==null >> return null;");
return null;
}
boolean prepared = config.isPrepared();
final String sql = config.getSQL(false);
config.setPrepared(prepared);
if (StringUtil.isNotEmpty(sql, true) == false) {
Log.e(TAG, "select StringUtil.isNotEmpty(sql, true) == false >> return null;");
return null;
}
JSONObject result = null;
long startTime = System.currentTimeMillis();
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
+ "\n select startTime = " + startTime
+ "\n sql = \n " + sql
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
ResultSet rs = null;
switch (config.getMethod()) {
case HEAD:
case HEADS:
rs = executeQuery(config);
result = rs.next() ? AbstractParser.newSuccessResult()
: AbstractParser.newErrorResult(new SQLException("数据库错误, rs.next() 失败!"));
result.put(JSONResponse.KEY_COUNT, rs.getLong(1));
rs.close();
return result;
case POST:
case PUT:
case DELETE:
long updateCount = executeUpdate(config);
result = AbstractParser.newResult(updateCount > 0 ? JSONResponse.CODE_SUCCESS : JSONResponse.CODE_NOT_FOUND
, updateCount > 0 ? JSONResponse.MSG_SUCCEED : "可能对象不存在!");
//id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
if (config.getId() > 0) {
result.put(JSONResponse.KEY_ID, config.getId());
} else {
result.put(JSONResponse.KEY_ID + "[]", config.getWhere(JSONResponse.KEY_ID_IN, true));
}
result.put(JSONResponse.KEY_COUNT, updateCount);//返回修改的记录数
return result;
case GET:
case GETS:
break;
default://OPTIONS, TRACE等
Log.e(TAG, "select sql = " + sql + " ; method = " + config.getMethod() + " >> return null;");
return null;
}
final int position = config.getPosition();
result = getCache(sql, position, config.isCacheStatic());
Log.i(TAG, ">>> select result = getCache('" + sql + "', " + position + ") = " + result);
if (result != null) {
Log.d(TAG, "\n\n select result != null >> return result;" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
return result;
}
rs = executeQuery(config);
// final boolean cache = config.getCount() != 1;
Map<Integer, JSONObject> resultMap = new HashMap<Integer, JSONObject>();
// Log.d(TAG, "select cache = " + cache + "; resultMap" + (resultMap == null ? "=" : "!=") + "null");
int index = -1;
ResultSetMetaData rsmd = rs.getMetaData();
final int length = rsmd.getColumnCount();
//<SELECT * FROM Comment WHERE momentId = '470', { id: 1, content: "csdgs" }>
Map<String, JSONObject> childMap = new HashMap<>(); //要存到cacheMap
// WHERE id = ? AND ... 或 WHERE ... AND id = ? 强制排序 remove 再 put,还是重新 getSQL吧
while (rs.next()){
index ++;
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n select while (rs.next()){ index = " + index + "\n\n");
result = new JSONObject(true);
for (int i = 1; i <= length; i++) {
result = onPutColumn(config, rs, rsmd, index, result, i, childMap);
}
resultMap = onPutTable(config, rs, rsmd, resultMap, index, result);
Log.d(TAG, "\n select while (rs.next()) { resultMap.put( " + index + ", result); "
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n");
}
rs.close();
//子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
Set<Entry<String, JSONObject>> set = childMap.entrySet();
//<sql, Table>
for (Entry<String, JSONObject> entry : set) {
Map<Integer, JSONObject> m = new HashMap<Integer, JSONObject>();
m.put(0, entry.getValue());
putCache(entry.getKey(), m, false);
}
putCache(sql, resultMap, config.isCacheStatic());
Log.i(TAG, ">>> select putCache('" + sql + "', resultMap); resultMap.size() = " + resultMap.size());
long endTime = System.currentTimeMillis();
Log.d(TAG, "\n\n select endTime = " + endTime + "; duration = " + (endTime - startTime)
+ "\n return resultMap.get(" + position + ");" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
return resultMap.get(position);
}
/**table.put(rsmd.getColumnLabel(i), rs.getObject(i));
* @param config
* @param rs
* @param rsmd
* @param tablePosition 从0开始
* @param table
* @param columnIndex 从1开始
* @param childMap
* @return result
* @throws Exception
*/
protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd
, final int tablePosition, @NotNull JSONObject table, final int columnIndex, Map<String, JSONObject> childMap) throws Exception {
if (rsmd.getColumnName(columnIndex).startsWith("_")) {
Log.i(TAG, "select while (rs.next()){ ..."
+ " >> rsmd.getColumnName(i).startsWith(_) >> continue;");
return table;
}
//TODO 改为 rsmd.getTableName(columnIndex) 支持副表不传 @column , 但如何判断是副表?传 mainTableMain进来
String lable = rsmd.getColumnLabel(columnIndex);
int dotIndex = lable.indexOf(".");
String column = dotIndex < 0 ? lable : lable.substring(dotIndex + 1);
String childTable = dotIndex < 0 ? null : lable.substring(0, dotIndex);
JSONObject finalTable = null;
String childSql = null;
SQLConfig childConfig = null;
if (childTable == null) {
finalTable = table;
}
else {
lable = column;
//<sql, Table>
List<Join> joinList = config.getJoinList();
if (joinList != null) {
for (Join j : joinList) {
if (childTable.equals(j.getName())) {
childConfig = j.getCacheConfig(); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好
childConfig.putWhere(j.getKey(), table.get(j.getTargetKey()), false);
childSql = childConfig.getSQL(false);
if (StringUtil.isEmpty(childSql, true)) {
return table;
}
finalTable = (JSONObject) childMap.get(childSql);
break;
}
}
}
if (finalTable == null) {
finalTable = new JSONObject(true);
childMap.put(childSql, finalTable);
}
}
Object value = rs.getObject(columnIndex);
// Log.d(TAG, "name:" + rsmd.getColumnName(i));
// Log.d(TAG, "lable:" + rsmd.getColumnLabel(i));
// Log.d(TAG, "type:" + rsmd.getColumnType(i));
// Log.d(TAG, "typeName:" + rsmd.getColumnTypeName(i));
// Log.i(TAG, "select while (rs.next()) { >> for (int i = 0; i < length; i++) {"
// + "\n >>> value = " + value);
if (value != null) { //数据库查出来的null和empty值都有意义,去掉会导致 Moment:{ @column:"content" } 部分无结果及中断数组查询!
if (value instanceof Timestamp) {
value = ((Timestamp) value).toString();
}
else if (value instanceof String && isJSONType(rsmd, columnIndex)) { //json String
value = JSON.parse((String) value);
}
}
finalTable.put(lable, value);
return table;
}
/**resultMap.put(position, table);
* @param config
* @param rs
* @param rsmd
* @param resultMap
* @param position
* @param table
* @return resultMap
*/
protected Map<Integer, JSONObject> onPutTable(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd
, @NotNull Map<Integer, JSONObject> resultMap, int position, @NotNull JSONObject table) {
resultMap.put(position, table);
return resultMap;
}
/**判断是否为JSON类型
* @param rsmd
* @param position
* @return
*/
@Override
public boolean isJSONType(ResultSetMetaData rsmd, int position) {
try {
//TODO CHAR和JSON类型的字段,getColumnType返回值都是1 ,如果不用CHAR,改用VARCHAR,则可以用上面这行来提高性能。
//return rsmd.getColumnType(position) == 1;
return rsmd.getColumnTypeName(position).toLowerCase().contains("json");
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}