# 数据查询执行流程

提示

所有的回调如果走/data/save/public/query接口,则自动注入回调,如果走自定义的接口并遵循传输结构体规范,则需要后端程序员写程序注入。

# 查询结构体

package sei.cloud.datasource.entity;

public class DataQueryEntity implements DataEntity {
	public enum SQLType {	/* 操作类型 */
		TABLE, VIEWS, SOURCE, SQL
	}
	private SQLType sqlType = SQLType.TABLE;  /* 查询类型 */
	private DatabaseType databaseType; /* 选配,数据库类型(如果为null则使用表进行判断) */
	private String module; /* 选配,模块,用于权限判断(如果为null则使用表进行权限判断) */
	private String table;	/* 选配,操作的表或视图 */
	private String originalSQL; /* 操作的原始SQL(针对全队head中配置为source数据源情况或前端写的SQL语句) */
	private int subTable = 0; /* 是否将关联平行表或子表的数据传回前端,0:不传,1:传第1级子表,2:传第1级和第2级,以此类推 */
	private String keyFieldName;	/* 选配,主键字段名 */
	private ArrayFieldList fields;	/* 查询字段 */
	private Map<String, ArrayFieldList> tablesAndFields;	/* 本次查询所有涉及的表及对应的表字段。注意: 只有多表连接查询(即merge不为null)时才有值) */
	private String merge;	/* 多表连接查询 */
	private String group;	/* 分组(包括having) */
	private String order;	/* 排序 */
	private Integer page;	/* 第几页 */
	private int size = 15;	/* 每页行数 */

	private boolean export = false;  /* 是否导出数据 */
	private List<Object> titles;   /* 导出字段的标题 */

	private boolean onlyField = false; /* 是否只拿查询结果字段,不拿数据,true:只拿字段,不拿数据 false:数据与字段都要拿,默认为false */
	private boolean onlyCount = false; /* 是否只拿总记录数,不拿数据,true:只拿记录总数,不拿数据,默认为false */
	private Object privilegeButton;	/* 是否需要权限按钮按钮列表,可设置类型:Boolean, String, Array */
	private boolean load = true;	/* 是否装载数据 */
	private boolean keyField = false;	/* 是否需要回传主键字段名字 */
	private String lastId;	/* 选配, 当前页的最后一条数据的_id值,作用为针对MongoDB的分页查询优化 */
	private Map<String, Object> att;	/* 前端传回用于字典翻译的数据 */
	private JSONObject attVar; /* 自定义的附加临时数据,用于操作前后临时变量数据的存放 */
	private JSON filter;	/* 选配,操作条件 */
	private JSONObject dict;	/* 字典翻译选项  */

	private boolean next = true; /* 是否进行下一步操作 */
	private boolean isConvert = false; /* 是否已经做了转换 */
	private SQLVo doSQL;	/* 转换后的执行语句(即针对不同数据库的可执行语句) */

	private List<Predicate<? super DataQueryEntity>> beforeConverts; /* 将 JSON 转换为 SQLQueryEntry 实体 前 的回调 */
	private List<Predicate<? super DataQueryEntity>> afterConverts; /* 将 JSON 转换为 SQLQueryEntry 实体 后 的回调 */

	private List<Predicate<? super DataQueryEntity>> beforeQuerys; /* 执行询 前 的回调 */
	private List<Consumer<? super DataQueryEntity>> afterQuerys; /* 执行查询 后 的回调 */

	private List<Predicate<? super SQLVo>> privileges; /* 给SQL添加权限的回调函数 */
	private List<Function<DataQueryEntity, Map<String, Object>>> buttonsFunctions; /* 获得前端操作按钮的回调函数 */

	/**
	 * 注册转换前回调函数
	 * @param beforeConvert: 转换前回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity registerBeforeConvert(Predicate<? super DataQueryEntity> beforeConvert);

	/**
	 * 执行转换前回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity execBeforeConvert();

	/**
	 * 注册转换后回调函数
	 * @param afterConvert: 转换后回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity registerAfterConvert(Predicate<? super DataQueryEntity> afterConvert);

	/**
	 * 执行转换后回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity execAfterConvert();

	/**
	 * 注册查询前回调函数
	 * @param beforeQuery: 查询执行前回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity registerBeforeQuery(Predicate<? super DataQueryEntity> beforeQuery);

	/**
	 * 执行查询前回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity execBeforeQuery();

	/**
	 * 注册查询后回调函数
	 * @param afterQuery: 查询执行后回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity registerAfterQuery(Consumer<? super DataQueryEntity> afterQuery);

	/**
	 * 执行查询后回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity execAfterQuery();

	/**
	 * 注册权限回调函数
	 * @param privilege: 权限回调函数
	 * @return DataEntity
	 */
	public DataQueryEntity registerPrivilege(Predicate<? super SQLVo> privilege);

	/**
	 * 执行权限回调函数
	 * @param sqlVo: 要执行的SQLVo
	 * @return DataQueryEntity
	 */
	public DataQueryEntity execPrivilege(SQLVo sqlVo);

	/**
	 * 注册获得权限按钮回调函数
	 * @param buttonsFunction: 权限按钮回调函数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity registerButtonsPrivilege(Function<DataQueryEntity, Map<String, Object>> buttonsFunction);

	/**
	 * 检查表所属数据库类型
	 * @return DataQueryEntity
	 */
	public DataQueryEntity checkDatabaseType();

	/**
	 * 获取指定字段的值
	 * @param fieldName: 字段名
	 * @return Object | null
	 */
	public Object getFieldValue(String fieldName);

	/**
	 * 将当前SQL实体转换为可执行SQL语句
	 * 如果设置了有执行转换前后的回调则会调用
	 * @return 转换后的SQLQueryEntry
	 * @throws Exception 异常
	 */
	public DataQueryEntity convert()  throws Exception;

	/**
	 * 将source操作类型的转换为可执行的SQL语句
	 * @param isCallBack: 是否执行转换前后的回调
	 * @return DataQueryEntity
	 */
	public DataQueryEntity convertSource(boolean isCallBack);

	/**
	 * 将前端编写的SQL语句转换为可执行的SQL
	 * @param isCallBack: 是否执行转换前后的回调
	 * @return DataQueryEntity
	 * @throws IllegalArgumentException 异常
	 */
	public DataQueryEntity convertSQL(boolean isCallBack)  throws IllegalArgumentException;

	/**
	 * 将表或试图查询转换为可执行的SQL
	 * @param isCallBack: 是否执行转换前后的回调
	 * @return DataQueryEntity
	 * @throws Exception 异常
	 */
	public DataQueryEntity convertTableView(boolean isCallBack) throws Exception;

	/**
	 * 执行查询并获得查询结果
	 * @return ResMsg
	 * @throws Exception 异常
	 */
	public ResMsg doQuery() throws Exception;

	/**
	 * 解析字典配置
	 * @param dictConfig: 字典配置信息
	 * @param isPrivilege: 是否需要权限判断
	 * @return JSONObject
	 * @throws SQLException 异常
	 * @throws IllegalArgumentException 异常
	 */
	public JSONObject getDictPars(JSONObject dictConfig, boolean isPrivilege) throws SQLException, IllegalArgumentException;

	/**
	 * 字典翻译
	 * @param list: 要翻译的数据列表
	 * @param dictConfig: 字典配置信息
	 * @param isPrivilege: 判断对字典表是否有查询权限
	 * @return List
	 * @throws SQLException 异常
	 * @throws IllegalArgumentException 异常
	 */
	public List<XJSONObject> dicTranslate(List<XJSONObject> list, JSONObject dictConfig, boolean isPrivilege) throws SQLException, IllegalArgumentException;

	/**
	 * 添加查询条件
	 * @param fieldName: 字段名
	 * @param value: 字段值
	 * @return DataQueryEntity
	 */
	public DataQueryEntity addFilter(String fieldName, Object value);

	/**
	 * 添加查询条件
	 * @param filter: 查询条件
	 * @return DataQueryEntity
	 */
	public DataQueryEntity addFilter(JSON filter);

	/**
	 * 获得SQL类型
	 * @return SQLType
	 */
	public SQLType getSqlType();

	/**
	 * 设置SQL类型
	 * @param sqlType: SQLType枚举中的一种
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setSqlType(SQLType sqlType);

	/**
	 * 获得要操作的数据库类型
	 * @return DatabaseType
	 */
	public DatabaseType getDatabaseType();

	/**
	 * 设置要操作的数据库类型
	 * @param databaseType: 枚举DatabaseType中的一种
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setDatabaseType(DatabaseType databaseType);

	/**
	 * 获得模块名称
	 * @return String
	 */
	public String getModule();

	/**
	 * 设置模块名称
	 * @param module: 模块名称
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setModule(String module);

	/**
	 * 获得表名称
	 * @return String
	 */
	public String getTable();

	/**
	 * 设置表名称
	 * @param table: 表名称
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setTable(String table);

	/**
	 * 获得前端编写的SQL语句或者后端设置的数据源SQL语句
	 * @return String
	 */
	public String getOriginalSQL();

	/**
	 * 设置前端编写的SQL语句或者后端设置的数据源SQL语句
	 * @param originalSQL: 前端编写的SQL语句或者后端设置的数据源SQL语句
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setOriginalSQL(String originalSQL);

	/**
	 * 获得是否将关联平行表或子表的数据传回前端
	 * @return
	 */
	public int getSubTable();

	/**
	 * 设置是否将关联平行表或子表的数据传回
	 * @param subTable: 0:不传,1:传第1级子表,2:传第1级和第2级,以此类推
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setSubTable(int subTable);

	/**
	 * 获得主键字段名
	 * @return String
	 */
	public String getKeyFieldName();

	/**
	 * 设置主键字段名
	 * @param keyFieldName: 主键字段名
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setKeyFieldName(String keyFieldName);

	/**
	 * 获得要返回前端的字段列表
	 * @return ArrayFieldList
	 */
	public ArrayFieldList getFields();

	/**
	 * 设置要返回前端的字段列表
	 * @param fields: 要返回前端的字段列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setFields(ArrayFieldList fields);

	/**
	 * 获得本次查询所有涉及的表及对应的表字段。注意: 只有多表连接查询(即merge不为null)时才有值)
	 * @return Map
	 */
	public Map<String, ArrayFieldList> getTablesAndFields();

	/**
	 * 设置本次查询所有涉及的表及对应的表字段。注意: 只有多表连接查询(即merge不为null)时才有值)
	 * @param tablesAndFields: 本次查询所有涉及的表及对应的表字段
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setTablesAndFields(Map<String, ArrayFieldList> tablesAndFields);

	/**
	 * 获得要连接查询的条件
	 * @return String
	 */
	public String getMerge();

	/**
	 * 设置要连接查询的条件
	 * @param merge: 连接查询的条件
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setMerge(String merge);

	/**
	 * 获得分组条件
	 * @return String
	 */
	public String getGroup();

	/**
	 * 设置分组条件
	 * @param group: 分组条件
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setGroup(String group);

	/**
	 * 获得排序条件
	 * @return String
	 */
	public String getOrder();

	/**
	 * 设置排序条件
	 * @param order: 排序条件,如: name asc,sex desc
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setOrder(String order);

	/**
	 * 获得第几页
	 * @return Integer
	 */
	public Integer getPage();

	/**
	 * 设置第几页
	 * @param page: 第几页
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setPage(Integer page);

	/**
	 * 获得每页行数
	 * @return int
	 */
	public int getSize();

	/**
	 * 设置每页行数
	 * @param size: 每页行数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setSize(int size);

	/**
	 * 获得是否要导出
	 * @return boolean
	 */
	public boolean isExport();

	/**
	 * 设置是否要导出
	 * @param export: 是否要导出
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setExport(boolean export);

	/**
	 * 获得导出列的标题列表
	 * @return List
	 */
	public List<Object> getTitles();

	/**
	 * 设置导出列的标题列表
	 * @param titles: 导出列的标题列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setTitles(List<Object> titles);

	/**
	 * 获得是否只查询字段名
	 * @return boolean
	 */
	public boolean isOnlyField();

	/**
	 * 设置是否只查询字段名
	 * @param onlyField: 是否只查询字段名
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setOnlyField(boolean onlyField);

	/**
	 * 获得是否只要查询总行数
	 * @return boolean
	 */
	public boolean isOnlyCount();

	/**
	 * 设置是否只要查询总行数
	 * @param onlyCount: 是否只要查询总行数
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setOnlyCount(boolean onlyCount);

	/**
	 * 获得是否需要权限按钮按钮列表,可设置类型:Boolean, String, Array
	 * @return Object
	 */
	public Object getPrivilegeButton();

	/**
	 * 设置是否需要权限按钮按钮列表,可设置类型:Boolean, String, Array
	 * @param privilegeButton: 权限按钮按钮列表,可设置类型:Boolean, String, Array
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setPrivilegeButton(Object privilegeButton);

	/**
	 * 获得是否需要查询数据
	 * @return boolean
	 */
	public boolean isLoad();

	/**
	 * 设置是否需要查询数据
	 * @param load: 是否需要查询数据
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setLoad(boolean load);

	/**
	 * 获得是否需要回传主键字段名字
	 * @return boolean
	 */
	public boolean isKeyField();

	/**
	 * 设置是否需要回传主键字段名字
	 * @param keyField: 是否需要回传主键字段名字
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setKeyField(boolean keyField);

	/**
	 * 获得当前页的最后一条数据的_id值,作用为针对MongoDB的分页查询优化
	 * @return String
	 */
	public String getLastId();

	/**
	 * 设置当前页的最后一条数据的_id值,作用为针对MongoDB的分页查询优化
	 * @param lastId:  当前页的最后一条数据的_id值,作用为针对MongoDB的分页查询优化
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setLastId(String lastId);

	/**
	 * 获得前端传回用于字典翻译的数据
	 * @return Map
	 */
	public Map<String, Object> getAtt();

	/**
	 * 设置前端传回用于字典翻译的数据
	 * @param att: 前端传回用于字典翻译的数据
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setAtt(Map<String, Object> att);

	/**
	 * 获得自定义的附加临时数据,用于操作前后临时变量数据的存放
	 * @return JSONObject
	 */
	public JSONObject getAttVar();

	/**
	 * 设置自定义的附加临时数据,用于操作前后临时变量数据的存放
	 * @param attVar: 自定义的附加临时数据,用于操作前后临时变量数据的存放
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setAttVar(JSONObject attVar);

	/**
	 * 获得查询条件
	 * @return JSON
	 */
	public JSON getFilter();

	/**
	 * 设置查询条件
	 * @param filter: 查询条件
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setFilter(JSON filter);

	/**
	 * 获得字典翻译选项
	 * @return JSONObject
	 */
	public JSONObject getDict();

	/**
	 * 设置字典翻译选项
	 * @param dict: 字典翻译选项
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setDict(JSONObject dict);

	/**
	 * 获得是否继续下一步操作
	 * @return boolean
	 */
	public boolean isNext();

	/**
	 * 设置是否继续下一步操作
	 * @param next: 是否继续下一步操作
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setNext(boolean next);

	/**
	 * 获得是否已经转换
	 * @return boolean
	 */
	public boolean isConvert();

	/**
	 * 设置是否已经转换
	 * @param convert: 是否已经转换
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setConvert(boolean convert);
	/**
	 * 获得转换结果
	 * @return SQLVo
	 */
	public SQLVo getDoSQL();

	/**
	 * 设置转换结果
	 * @param doSQL: 转换结果
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setDoSQL(SQLVo doSQL);

	/**
	 * 获得转换前回调函数列表
	 * @return List
	 */
	public List<Predicate<? super DataQueryEntity>> getBeforeConverts();

	/**
	 * 设置转换前回调函数列表
	 * @param beforeConverts: 转换前回调函数列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setBeforeConverts(List<Predicate<? super DataQueryEntity>> beforeConverts);

	/**
	 * 获得转换后回调函数列表
	 * @return List
	 */
	public List<Predicate<? super DataQueryEntity>> getAfterConverts();

	/**
	 * 设置转换后回调函数列表
	 * @param afterConverts: 转换后回调函数列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setAfterConverts(List<Predicate<? super DataQueryEntity>> afterConverts);

	/**
	 * 获得查询前回调函数列表
	 * @return List
	 */
	public List<Predicate<? super DataQueryEntity>> getBeforeQuerys();

	/**
	 * 设置查询前回调函数列表
	 * @param beforeQuerys: 查询前回调函数列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setBeforeQuerys(List<Predicate<? super DataQueryEntity>> beforeQuerys);

	/**
	 * 获得查询后回调函数列表
	 * @return List
	 */
	public List<Consumer<? super DataQueryEntity>> getAfterQuerys();

	/**
	 * 设置查询后回调函数列表
	 * @param afterQuerys: 查询后回调函数列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setAfterQuerys(List<Consumer<? super DataQueryEntity>> afterQuerys);

	/**
	 * 获得给SQL添加权限的回调函数列表
	 * @return List
	 */
	public List<Predicate<? super SQLVo>> getPrivileges();

	/**
	 * 设置给SQL添加权限的回调函数
	 * @param privileges: 给SQL添加权限的回调函数列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setPrivileges(List<Predicate<? super SQLVo>> privileges);

	/**
	 * 获得前端操作按钮的回调函数列表
	 * @return List
	 */
	public List<Function<DataQueryEntity, Map<String, Object>>> getButtonsFunctions();

	/**
	 * 设置前端操作按钮的回调函数列表
	 * @param buttonsFunctions: 前端操作按钮的回调函数列表
	 * @return DataQueryEntity
	 */
	public DataQueryEntity setButtonsFunctions(List<Function<DataQueryEntity, Map<String, Object>>> buttonsFunctions);
}
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662