001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.row;
017
018import com.mybatisflex.core.FlexGlobalConfig;
019import com.mybatisflex.core.exception.FlexExceptions;
020import com.mybatisflex.core.paginate.Page;
021import com.mybatisflex.core.query.CPI;
022import com.mybatisflex.core.query.QueryCondition;
023import com.mybatisflex.core.query.QueryTable;
024import com.mybatisflex.core.query.QueryWrapper;
025import com.mybatisflex.core.transaction.Propagation;
026import com.mybatisflex.core.transaction.TransactionalManager;
027import com.mybatisflex.core.util.CollectionUtil;
028import org.apache.ibatis.session.SqlSessionFactory;
029import org.apache.ibatis.util.MapUtil;
030
031import java.util.Arrays;
032import java.util.Collection;
033import java.util.List;
034import java.util.Map;
035import java.util.concurrent.ConcurrentHashMap;
036import java.util.function.BiConsumer;
037import java.util.function.Supplier;
038
039/**
040 * 针对 RowMapper 的静态方法进行封装
041 */
042public class Db {
043
044    private Db() {}
045
046    private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>();
047    static RowMapperInvoker defaultRowMapperInvoker;
048
049    public static RowMapperInvoker invoker() {
050        if (defaultRowMapperInvoker == null) {
051            FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
052            SqlSessionFactory sqlSessionFactory = defaultConfig.getSqlSessionFactory();
053            defaultRowMapperInvoker = new RowMapperInvoker(sqlSessionFactory);
054        }
055        return defaultRowMapperInvoker;
056    }
057
058    public static RowMapperInvoker invoker(String environmentId) {
059        return MapUtil.computeIfAbsent(INVOKER_MAP, environmentId, key -> {
060            SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getConfig(key).getSqlSessionFactory();
061            return new RowMapperInvoker(sqlSessionFactory);
062        });
063    }
064
065
066    /**
067     * 往 schema.tableName 插入一条 row 数据
068     *
069     * @param schema    模式
070     * @param tableName 表名
071     * @param row       数据
072     */
073    public static int insert(String schema,String tableName, Row row) {
074        return invoker().insert(schema,tableName, row);
075    }
076    /**
077     * 往 tableName 插入一条 row 数据
078     *
079     * @param tableName 表名
080     * @param row       数据
081     */
082    public static int insert(String tableName, Row row) {
083        return invoker().insert(null,tableName, row);
084    }
085
086
087    /**
088     * 直接编写 sql 插入数据
089     *
090     * @param sql  sql 内容
091     * @param args sql 参数
092     */
093    public static int insertBySql(String sql, Object... args) {
094        return invoker().insertBySql(sql, args);
095    }
096
097
098    /**
099     * 批量插入数据
100     *
101     * @param schema    模式
102     * @param tableName 表名
103     * @param rows      数据
104     */
105    public static int[] insertBatch(String schema,String tableName, Collection<Row> rows) {
106        return insertBatch(schema,tableName, rows, rows.size());
107    }
108
109    /**
110     * 批量插入数据
111     *
112     * @param tableName 表名
113     * @param rows      数据
114     */
115    public static int[] insertBatch(String tableName, Collection<Row> rows) {
116        return insertBatch(null,tableName, rows, rows.size());
117    }
118
119    /**
120     * 批量插入数据
121     *
122     * @param schema    模式
123     * @param tableName 表名
124     * @param rows      数据
125     * @param batchSize 每次提交的数据量
126     */
127    public static int[] insertBatch(String schema,String tableName, Collection<Row> rows, int batchSize) {
128        List<Row> list = CollectionUtil.toList(rows);
129        return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> {
130            Row row = list.get(index);
131            mapper.insert(schema,tableName, row);
132        });
133    }
134    /**
135     * 批量插入数据
136     *
137     * @param tableName 表名
138     * @param rows      数据
139     * @param batchSize 每次提交的数据量
140     */
141    public static int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) {
142        List<Row> list = CollectionUtil.toList(rows);
143        return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> {
144            Row row = list.get(index);
145            mapper.insert(null,tableName, row);
146        });
147    }
148
149    /**
150     * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String ,String, Collection, int)} 高
151     *
152     * @param schema    模式
153     * @param tableName 表名
154     * @param rows      数据
155     */
156    public static int insertBatchWithFirstRowColumns(String schema,String tableName, List<Row> rows) {
157        return invoker().insertBatchWithFirstRowColumns(schema,tableName, rows);
158    }
159
160    /**
161     * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String ,String, Collection, int)} 高
162     *
163     * @param tableName 表名
164     * @param rows      数据
165     */
166    public static int insertBatchWithFirstRowColumns(String tableName, List<Row> rows) {
167        return invoker().insertBatchWithFirstRowColumns(null,tableName, rows);
168    }
169
170    /**
171     * 编写 delete sql 来删除数据
172     *
173     * @param sql  sql 内容
174     * @param args 参数
175     */
176    public static int deleteBySql(String sql, Object... args) {
177        return invoker().deleteBySql(sql, args);
178    }
179
180    /**
181     * 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的
182     *
183     * @param schema    模式
184     * @param tableName 表名
185     * @param row       主键 和 id值
186     */
187    public static int deleteById(String schema,String tableName, Row row) {
188        return invoker().deleteById(schema,tableName, row);
189    }
190
191    /**
192     * 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的
193     *
194     * @param tableName 表名
195     * @param row       主键 和 id值
196     */
197    public static int deleteById(String tableName, Row row) {
198        return invoker().deleteById(null,tableName, row);
199    }
200
201
202    /**
203     * 根据主键来删除 1 条数据
204     *
205     * @param schema    模式
206     * @param tableName  表名
207     * @param primaryKey 主键字段名称
208     * @param id         主键值
209     */
210    public static int deleteById(String schema,String tableName, String primaryKey, Object id) {
211        return invoker().deleteById(schema,tableName, primaryKey, id);
212    }
213
214    /**
215     * 根据主键来删除 1 条数据
216     *
217     * @param tableName  表名
218     * @param primaryKey 主键字段名称
219     * @param id         主键值
220     */
221    public static int deleteById(String tableName, String primaryKey, Object id) {
222        return invoker().deleteById(null,tableName, primaryKey, id);
223    }
224
225
226    /**
227     * 根据 id 集合来批量删除数据
228     *
229     * @param schema    模式
230     * @param tableName  表名
231     * @param primaryKey 主键字段名称
232     * @param ids        id 集合
233     */
234    public static int deleteBatchByIds(String schema,String tableName, String primaryKey, Collection<?> ids) {
235        return invoker().deleteBatchByIds(schema,tableName, primaryKey, ids);
236    }
237
238    /**
239     * 根据 id 集合来批量删除数据
240     *
241     * @param tableName  表名
242     * @param primaryKey 主键字段名称
243     * @param ids        id 集合
244     */
245    public static int deleteBatchByIds(String tableName, String primaryKey, Collection<?> ids) {
246        return invoker().deleteBatchByIds(null,tableName, primaryKey, ids);
247    }
248
249    /**
250     * 根据 map 构建的 where 条件来删除数据
251     *
252     * @param schema    模式
253     * @param tableName    表名
254     * @param whereColumns where 条件
255     */
256    public static int deleteByMap(String schema,String tableName, Map<String, Object> whereColumns) {
257        return invoker().deleteByQuery(schema,tableName, new QueryWrapper().where(whereColumns));
258    }
259
260    /**
261     * 根据 map 构建的 where 条件来删除数据
262     *
263     * @param tableName    表名
264     * @param whereColumns where 条件
265     */
266    public static int deleteByMap(String tableName, Map<String, Object> whereColumns) {
267        return invoker().deleteByQuery(null,tableName, new QueryWrapper().where(whereColumns));
268    }
269
270    /**
271     * 根据 condition 条件删除数据
272     *
273     * @param schema    模式
274     * @param tableName 表名
275     * @param condition 条件内容
276     */
277    public static int deleteByCondition(String schema,String tableName, QueryCondition condition) {
278        return invoker().deleteByQuery(schema,tableName, new QueryWrapper().where(condition));
279    }
280
281    /**
282     * 根据 condition 条件删除数据
283     *
284     * @param tableName 表名
285     * @param condition 条件内容
286     */
287    public static int deleteByCondition(String tableName, QueryCondition condition) {
288        return invoker().deleteByQuery(null,tableName, new QueryWrapper().where(condition));
289    }
290
291
292    /**
293     * 根据 query 构建的条件来删除数据
294     *
295     * @param schema    模式
296     * @param tableName    表名
297     * @param queryWrapper query
298     */
299    public static int deleteByQuery(String schema,String tableName, QueryWrapper queryWrapper) {
300        return invoker().deleteByQuery(schema,tableName, queryWrapper);
301    }
302
303    /**
304     * 根据 query 构建的条件来删除数据
305     *
306     * @param tableName    表名
307     * @param queryWrapper query
308     */
309    public static int deleteByQuery(String tableName, QueryWrapper queryWrapper) {
310        return invoker().deleteByQuery(null,tableName, queryWrapper);
311    }
312
313    /**
314     * 根据原生 sql 来更新数据
315     *
316     * @param sql  sql 内容
317     * @param args sql 参数
318     */
319    public static int updateBySql(String sql, Object... args) {
320        return invoker().updateBySql(sql, args);
321    }
322
323
324    /**
325     * @param sql
326     * @param batchArgsSetter
327     * @return
328     */
329    public static int[] updateBatch(String sql, BatchArgsSetter batchArgsSetter) {
330        int batchSize = batchArgsSetter.getBatchSize();
331        return executeBatch(batchSize, batchSize, RowMapper.class
332                , (mapper, index) -> mapper.updateBySql(sql, batchArgsSetter.getSqlArgs(index)));
333    }
334
335
336    /**
337     * 根据 id 来更新数据
338     *
339     * @param schema    模式
340     * @param tableName 表名
341     * @param row       id 及其内容
342     */
343    public static int updateById(String schema,String tableName, Row row) {
344        return invoker().updateById(schema,tableName, row);
345    }
346
347
348    /**
349     * 根据 id 来更新数据
350     *
351     * @param tableName 表名
352     * @param row       id 及其内容
353     */
354    public static int updateById(String tableName, Row row) {
355        return invoker().updateById(null,tableName, row);
356    }
357
358    /**
359     * 根据 map 构建的条件来更新数据
360     *
361     * @param schema    模式
362     * @param tableName    表名
363     * @param data         数据内容
364     * @param whereColumns where 条件
365     */
366    public static int updateByMap(String schema,String tableName, Row data, Map<String, Object> whereColumns) {
367        return invoker().updateByQuery(schema,tableName, data, new QueryWrapper().where(whereColumns));
368    }
369
370
371    /**
372     * 根据 map 构建的条件来更新数据
373     *
374     * @param tableName    表名
375     * @param data         数据内容
376     * @param whereColumns where 条件
377     */
378    public static int updateByMap(String tableName, Row data, Map<String, Object> whereColumns) {
379        return invoker().updateByQuery(null,tableName, data, new QueryWrapper().where(whereColumns));
380    }
381
382    /**
383     * 根据 condition 来更新数据
384     *
385     * @param schema    模式
386     * @param tableName 表名
387     * @param data      数据
388     * @param condition 条件
389     */
390    public static int updateByCondition(String schema,String tableName, Row data, QueryCondition condition) {
391        return invoker().updateByQuery(schema,tableName, data, new QueryWrapper().where(condition));
392    }
393
394    /**
395     * 根据 condition 来更新数据
396     *
397     * @param tableName 表名
398     * @param data      数据
399     * @param condition 条件
400     */
401    public static int updateByCondition(String tableName, Row data, QueryCondition condition) {
402        return invoker().updateByQuery(null,tableName, data, new QueryWrapper().where(condition));
403    }
404
405
406    /**
407     * 根据 query 构建的条件来更新数据
408     *
409     * @param schema    模式
410     * @param tableName    表名
411     * @param data         数据内容
412     * @param queryWrapper queryWrapper 条件
413     */
414    public static int updateByQuery(String schema,String tableName, Row data, QueryWrapper queryWrapper) {
415        return invoker().updateByQuery(schema,tableName, data, queryWrapper);
416    }
417
418    /**
419     * 根据 query 构建的条件来更新数据
420     *
421     * @param tableName    表名
422     * @param data         数据内容
423     * @param queryWrapper queryWrapper 条件
424     */
425    public static int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) {
426        return invoker().updateByQuery(null,tableName, data, queryWrapper);
427    }
428
429
430    /**
431     * 根据主键来批量更新数据
432     *
433     * @param schema    模式
434     * @param tableName 表名
435     * @param rows      还有主键的数据
436     */
437    public static int updateBatchById(String schema,String tableName, List<Row> rows) {
438        return invoker().updateBatchById(schema,tableName, rows);
439    }
440
441    /**
442     * 根据主键来批量更新数据
443     *
444     * @param tableName 表名
445     * @param rows      还有主键的数据
446     */
447    public static int updateBatchById(String tableName, List<Row> rows) {
448        return invoker().updateBatchById(null,tableName, rows);
449    }
450
451
452    /**
453     * 根据主键来批量更新数据
454     *
455     * @param entities  实体
456     * @param batchSize 批次大小
457     * @return int
458     */
459    public static <T> int updateEntitiesBatch(Collection<T> entities, int batchSize) {
460        List<T> list = CollectionUtil.toList(entities);
461        return Arrays.stream(executeBatch(list.size(), batchSize, RowMapper.class, (mapper, index) -> {
462            T entity = list.get(index);
463            mapper.updateEntity(entity);
464        })).sum();
465    }
466
467
468    /**
469     * 根据主键来批量更新数据
470     *
471     * @param entities 实体
472     * @return int 影响行数
473     */
474    public static <T> int updateEntitiesBatch(Collection<T> entities) {
475        return updateEntitiesBatch(entities, RowMapper.DEFAULT_BATCH_SIZE);
476    }
477
478
479    /**
480     * 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
481     *
482     *
483     * @param schema    模式
484     * @param tableName  表名
485     * @param fieldName 字段名
486     * @param value     递增值
487     * @param queryWrapper  条件
488     * @return  受影响行数
489     */
490    public static int updateNumberAddByQuery(String schema,String tableName, String fieldName, Number value, QueryWrapper queryWrapper){
491        return invoker().updateNumberAddByQuery(schema,tableName, fieldName, value, queryWrapper);
492    }
493
494    /**
495     * 通过 update table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
496     *
497     * @param tableName
498     * @param fieldName
499     * @param value
500     * @param queryWrapper
501     * @return
502     */
503    public static int updateNumberAddByQuery(String tableName, String fieldName, Number value, QueryWrapper queryWrapper){
504        return invoker().updateNumberAddByQuery(null,tableName, fieldName, value, queryWrapper);
505    }
506
507
508    /**
509     * 批量执行工具方法
510     *
511     * @param totalSize   执行总量
512     * @param batchSize   每一批次的数据量
513     * @param mapperClass 通过那个 Mapper 来执行
514     * @param consumer    执行内容
515     * @param <M>         Mapper
516     * @return 执行影响的行数
517     */
518    public static <M> int[] executeBatch(int totalSize, int batchSize, Class<M> mapperClass, BiConsumer<M, Integer> consumer) {
519        return invoker().executeBatch(totalSize, batchSize, mapperClass, consumer);
520    }
521
522    /**
523     * 根据 sql 来查询 1 条数据
524     *
525     * @param sql  sql 内容
526     * @param args sql 参数
527     */
528    public static Row selectOneBySql(String sql, Object... args) {
529        return invoker().selectOneBySql(sql, args);
530    }
531
532
533    /**
534     * 根据 id 来查询 1 条数据
535     *
536     * @param schema    模式
537     * @param tableName 表名
538     * @param row       主键和 id 值
539     */
540    public static Row selectOneById(String schema,String tableName, Row row) {
541        return invoker().selectOneById(schema,tableName, row);
542    }
543
544    /**
545     * 根据 id 来查询 1 条数据
546     *
547     * @param tableName 表名
548     * @param row       主键和 id 值
549     */
550    public static Row selectOneById(String tableName, Row row) {
551        return invoker().selectOneById(null,tableName, row);
552    }
553
554
555    /**
556     * 根据主键来查询 1 条数据
557     *
558     * @param schema    模式
559     * @param tableName  表名
560     * @param primaryKey 主键字段名称
561     * @param id         主键值
562     */
563    public static Row selectOneById(String schema,String tableName, String primaryKey, Object id) {
564        return invoker().selectOneById(schema,tableName, primaryKey, id);
565    }
566
567    /**
568     * 根据主键来查询 1 条数据
569     *
570     * @param tableName  表名
571     * @param primaryKey 主键字段名称
572     * @param id         主键值
573     */
574    public static Row selectOneById(String tableName, String primaryKey, Object id) {
575        return invoker().selectOneById(null,tableName, primaryKey, id);
576    }
577
578    /**
579     * 根据 map 来查询 1 条数据
580     *
581     * @param schema    模式
582     * @param tableName    表名
583     * @param whereColumns where条件
584     */
585    public static Row selectOneByMap(String schema,String tableName, Map whereColumns) {
586        return invoker().selectOneByQuery(schema,tableName, new QueryWrapper().where(whereColumns));
587    }
588
589
590
591    /**
592     * 根据 map 来查询 1 条数据
593     *
594     * @param tableName    表名
595     * @param whereColumns where条件
596     */
597    public static Row selectOneByMap(String tableName, Map whereColumns) {
598        return invoker().selectOneByQuery(null,tableName, new QueryWrapper().where(whereColumns));
599    }
600
601    /**
602     * 根据 condition 来查询数据
603     *
604     * @param schema    模式
605     * @param tableName 表名
606     * @param condition 条件
607     */
608    public static Row selectOneByCondition(String schema,String tableName, QueryCondition condition) {
609        return invoker().selectOneByQuery(schema,tableName, new QueryWrapper().where(condition));
610    }
611
612    /**
613     * 根据 condition 来查询数据
614     *
615     * @param tableName 表名
616     * @param condition 条件
617     */
618    public static Row selectOneByCondition(String tableName, QueryCondition condition) {
619        return invoker().selectOneByQuery(null,tableName, new QueryWrapper().where(condition));
620    }
621
622
623    /**
624     * 根据 queryWrapper 来查询 1 条数据
625     *
626     * @param schema    模式
627     * @param tableName    表名
628     * @param queryWrapper queryWrapper
629     */
630    public static Row selectOneByQuery(String schema,String tableName, QueryWrapper queryWrapper) {
631        return invoker().selectOneByQuery(schema,tableName, queryWrapper);
632    }
633
634    /**
635     * 根据 queryWrapper 来查询 1 条数据
636     *
637     * @param tableName    表名
638     * @param queryWrapper queryWrapper
639     */
640    public static Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) {
641        return invoker().selectOneByQuery(null,tableName, queryWrapper);
642    }
643
644
645    /**
646     * 直接根据 queryWrapper 查询 1 条数据
647     *
648     * @param queryWrapper 必须带有 from 的 queryWrapper
649     */
650    public static Row selectOneByQuery(QueryWrapper queryWrapper) {
651        List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
652        if (queryTables == null || queryTables.isEmpty()) {
653            throw FlexExceptions.wrap("table must not be null or empty in Db.selectOneByQuery");
654        }
655        return invoker().selectOneByQuery(null,null, queryWrapper);
656    }
657
658
659    /**
660     * 通过 sql 来查询多条数据
661     *
662     * @param sql  sql 内容
663     * @param args sql 参数
664     */
665    public static List<Row> selectListBySql(String sql, Object... args) {
666        return invoker().selectListBySql(sql, args);
667    }
668
669
670    /**
671     * 通过 map 构建的条件来查询数据列表
672     *
673     * @param schema       模式
674     * @param tableName    表名
675     * @param whereColumns where 条件
676     */
677    public static List<Row> selectListByMap(String schema,String tableName, Map<String, Object> whereColumns) {
678        return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(whereColumns));
679    }
680
681    /**
682     * 通过 map 构建的条件来查询数据列表
683     *
684     * @param tableName    表名
685     * @param whereColumns where 条件
686     */
687    public static List<Row> selectListByMap(String tableName, Map<String, Object> whereColumns) {
688        return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(whereColumns));
689    }
690
691
692    /**
693     * 根据 map 构建的条件来查询数据列表
694     *
695     * @param schema    模式
696     * @param tableName    表名
697     * @param whereColumns 条件
698     * @param count        数据量
699     */
700    public static List<Row> selectListByMap(String schema,String tableName, Map<String, Object> whereColumns, int count) {
701        return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(whereColumns).limit(count));
702    }
703
704    /**
705     * 根据 map 构建的条件来查询数据列表
706     *
707     * @param tableName    表名
708     * @param whereColumns 条件
709     * @param count        数据量
710     */
711    public static List<Row> selectListByMap(String tableName, Map<String, Object> whereColumns, int count) {
712        return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(whereColumns).limit(count));
713    }
714
715
716    /**
717     * 通过 condition 条件来查询数据列表
718     *
719     * @param schema    模式
720     * @param tableName 表名
721     * @param condition where 条件
722     */
723    public static List<Row> selectListByCondition(String schema,String tableName, QueryCondition condition) {
724        return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(condition));
725    }
726
727
728    /**
729     * 通过 condition 条件来查询数据列表
730     *
731     * @param tableName 表名
732     * @param condition where 条件
733     */
734    public static List<Row> selectListByCondition(String tableName, QueryCondition condition) {
735        return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(condition));
736    }
737    /**
738     * 根据 condition 条件来查询数据列表
739     *
740     * @param schema    模式
741     * @param tableName 表名
742     * @param condition 条件
743     * @param count     数据量
744     */
745    public static List<Row> selectListByCondition(String schema,String tableName, QueryCondition condition, int count) {
746        return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(condition).limit(count));
747    }
748
749    /**
750     * 根据 condition 条件来查询数据列表
751     *
752     * @param tableName 表名
753     * @param condition 条件
754     * @param count     数据量
755     */
756    public static List<Row> selectListByCondition(String tableName, QueryCondition condition, int count) {
757        return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(condition).limit(count));
758    }
759
760
761    /**
762     * 通过 query 来查询数据列表
763     *
764     * @param schema    模式
765     * @param tableName    表名
766     * @param queryWrapper query 条件
767     */
768    public static List<Row> selectListByQuery(String schema,String tableName, QueryWrapper queryWrapper) {
769        return invoker().selectListByQuery(schema,tableName, queryWrapper);
770    }
771
772
773    /**
774     * 通过 query 来查询数据列表
775     *
776     * @param tableName    表名
777     * @param queryWrapper query 条件
778     */
779    public static List<Row> selectListByQuery(String tableName, QueryWrapper queryWrapper) {
780        return invoker().selectListByQuery(null,tableName, queryWrapper);
781    }
782
783
784    /**
785     * 通过 query 来查询数据列表
786     *
787     * @param queryWrapper 必须带有 from 的 queryWrapper
788     */
789    public static List<Row> selectListByQuery(QueryWrapper queryWrapper) {
790        List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
791        if (queryTables == null || queryTables.isEmpty()) {
792            throw FlexExceptions.wrap("table must not be null or empty in Db.selectListByQuery");
793        }
794        return invoker().selectListByQuery(null,null, queryWrapper);
795    }
796
797    /**
798     * 查询某张表的所有数据
799     *
800     * @param schema    模式
801     * @param tableName 表名
802     */
803    public static List<Row> selectAll(String schema,String tableName) {
804        return invoker().selectAll(schema,tableName);
805    }
806
807    /**
808     * 查询某张表的所有数据
809     *
810     * @param tableName 表名
811     */
812    public static List<Row> selectAll(String tableName) {
813        return invoker().selectAll(null,tableName);
814    }
815
816    /**
817     * 查询某个内容,数据返回的应该只有 1 行 1 列
818     *
819     * @param sql  sql 内容
820     * @param args sql 参数
821     */
822    public static Object selectObject(String sql, Object... args) {
823        return invoker().selectObject(sql, args);
824    }
825
826
827    /**
828     * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
829     *
830     * @param schema    模式
831     * @param tableName    表名
832     * @param queryWrapper query 封装
833     * @return 数据内容
834     */
835    public static Object selectObject(String schema,String tableName, QueryWrapper queryWrapper) {
836        return invoker().selectObjectByQuery(schema,tableName, queryWrapper);
837    }
838
839    /**
840     * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
841     *
842     * @param tableName    表名
843     * @param queryWrapper query 封装
844     * @return 数据内容
845     */
846    public static Object selectObject(String tableName, QueryWrapper queryWrapper) {
847        return invoker().selectObjectByQuery(null,tableName, queryWrapper);
848    }
849
850
851    /**
852     * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
853     *
854     * @param queryWrapper query 封装
855     * @return 数据内容
856     */
857    public static Object selectObject(QueryWrapper queryWrapper) {
858        return invoker().selectObjectByQuery(null, null, queryWrapper);
859    }
860
861
862
863    /**
864     * 查询某列内容,数据返回应该有 多行 1 列
865     *
866     * @param sql  sql 内容
867     * @param args sql 参数
868     */
869    public static List<Object> selectObjectList(String sql, Object... args) {
870        return invoker().selectObjectList(sql, args);
871    }
872
873
874    /**
875     * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
876     *
877     * @param schema    模式
878     * @param tableName    表名
879     * @param queryWrapper query 封装
880     * @return 数据内容
881     */
882    public static Object selectObjectList(String schema,String tableName, QueryWrapper queryWrapper) {
883        return invoker().selectObjectListByQuery(schema,tableName, queryWrapper);
884    }
885
886    /**
887     * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
888     *
889     * @param tableName    表名
890     * @param queryWrapper query 封装
891     * @return 数据内容
892     */
893    public static Object selectObjectList(String tableName, QueryWrapper queryWrapper) {
894        return invoker().selectObjectListByQuery(null,tableName, queryWrapper);
895    }
896
897
898    /**
899     * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
900     *
901     * @param queryWrapper query 封装
902     * @return 数据内容
903     */
904    public static Object selectObjectList(QueryWrapper queryWrapper) {
905        return invoker().selectObjectListByQuery(null, null, queryWrapper);
906    }
907
908
909    /**
910     * 查收 count 数据,一般用于 select count(*)...
911     * 或者返回的内容是一行1列,且是数值类型的也可以用此方法
912     *
913     * @param sql  sql 内容
914     * @param args sql 参数
915     */
916    public static long selectCount(String sql, Object... args) {
917        return invoker().selectCount(sql, args);
918    }
919
920
921    /**
922     * 根据 condition 条件来查询数量
923     *
924     * @param schema    模式
925     * @param tableName 表名
926     * @param condition 条件
927     */
928    public static long selectCountByCondition(String schema,String tableName, QueryCondition condition) {
929        return invoker().selectCountByQuery(schema,tableName, new QueryWrapper().where(condition));
930    }
931
932    /**
933     * 根据 condition 条件来查询数量
934     *
935     * @param tableName 表名
936     * @param condition 条件
937     */
938    public static long selectCountByCondition(String tableName, QueryCondition condition) {
939        return invoker().selectCountByQuery(null,tableName, new QueryWrapper().where(condition));
940    }
941
942
943    /**
944     * 根据 query 构建的条件来查询数据量
945     *
946     * @param schema    模式
947     * @param tableName    表名
948     * @param queryWrapper query 条件
949     */
950    public static long selectCountByQuery(String schema,String tableName, QueryWrapper queryWrapper) {
951        return invoker().selectCountByQuery(schema,tableName, queryWrapper);
952    }
953
954    /**
955     * 根据 query 构建的条件来查询数据量
956     *
957     * @param tableName    表名
958     * @param queryWrapper query 条件
959     */
960    public static long selectCountByQuery(String tableName, QueryWrapper queryWrapper) {
961        return invoker().selectCountByQuery(null,tableName, queryWrapper);
962    }
963
964
965    /**
966     * 直接根据 query 来查询数据量
967     *
968     * @param queryWrapper 必须带有表名的 queryWrapper
969     * @return 数据量
970     */
971    public static long selectCountByQuery(QueryWrapper queryWrapper) {
972        List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
973        if (queryTables == null || queryTables.isEmpty()) {
974            throw FlexExceptions.wrap("Query tables must not be null or empty in Db.selectCountByQuery");
975        }
976        return invoker().selectCountByQuery(null,null, queryWrapper);
977    }
978
979
980    /**
981     * 分页查询
982     *
983     * @param schema    模式
984     * @param tableName  表名
985     * @param pageNumber 当前的页码
986     * @param pageSize   每页的数据量
987     * @param condition  条件
988     */
989    public static Page<Row> paginate(String schema,String tableName, int pageNumber, int pageSize, QueryCondition condition) {
990        return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition));
991    }
992
993
994    /**
995     * 分页查询
996     *
997     * @param tableName  表名
998     * @param pageNumber 当前的页码
999     * @param pageSize   每页的数据量
1000     * @param condition  条件
1001     */
1002    public static Page<Row> paginate(String tableName, int pageNumber, int pageSize, QueryCondition condition) {
1003        return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition));
1004    }
1005
1006
1007    /**
1008     * 分页查询
1009     *
1010     * @param schema    模式
1011     * @param tableName  表名
1012     * @param pageNumber 当前的页码
1013     * @param pageSize   每页的数据量
1014     * @param totalRow   数据总量
1015     * @param condition  条件
1016     */
1017    public static Page<Row> paginate(String schema,String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) {
1018        return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition));
1019    }
1020
1021    /**
1022     * 分页查询
1023     *
1024     * @param tableName  表名
1025     * @param pageNumber 当前的页码
1026     * @param pageSize   每页的数据量
1027     * @param totalRow   数据总量
1028     * @param condition  条件
1029     */
1030    public static Page<Row> paginate(String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) {
1031        return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition));
1032    }
1033
1034
1035    /**
1036     * 分页查询
1037     *
1038     * @param schema    模式
1039     * @param tableName    表名
1040     * @param pageNumber   当前的页码
1041     * @param pageSize     每页的数据量
1042     * @param queryWrapper 条件
1043     */
1044    public static Page<Row> paginate(String schema,String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) {
1045        return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize), queryWrapper);
1046    }
1047
1048    /**
1049     * 分页查询
1050     *
1051     * @param tableName    表名
1052     * @param pageNumber   当前的页码
1053     * @param pageSize     每页的数据量
1054     * @param queryWrapper 条件
1055     */
1056    public static Page<Row> paginate(String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) {
1057        return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize), queryWrapper);
1058    }
1059
1060
1061    /**
1062     * 分页查询
1063     *
1064     * @param schema    模式
1065     * @param tableName    表名
1066     * @param pageNumber   当前的页码
1067     * @param pageSize     每页的数据量
1068     * @param totalRow     数据总量
1069     * @param queryWrapper 条件
1070     */
1071    public static Page<Row> paginate(String schema,String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) {
1072        return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper);
1073    }
1074
1075    /**
1076     * 分页查询
1077     *
1078     * @param tableName    表名
1079     * @param pageNumber   当前的页码
1080     * @param pageSize     每页的数据量
1081     * @param totalRow     数据总量
1082     * @param queryWrapper 条件
1083     */
1084    public static Page<Row> paginate(String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) {
1085        return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper);
1086    }
1087
1088
1089    /**
1090     * 分页查询
1091     *
1092     * @param schema    模式
1093     * @param tableName    表名
1094     * @param page         page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量
1095     * @param queryWrapper 条件
1096     */
1097    public static Page<Row> paginate(String schema,String tableName, Page<Row> page, QueryWrapper queryWrapper) {
1098        return invoker().paginate(schema,tableName, page, queryWrapper);
1099    }
1100
1101    /**
1102     * 分页查询
1103     *
1104     * @param tableName    表名
1105     * @param page         page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量
1106     * @param queryWrapper 条件
1107     */
1108    public static Page<Row> paginate(String tableName, Page<Row> page, QueryWrapper queryWrapper) {
1109        return invoker().paginate(null,tableName, page, queryWrapper);
1110    }
1111
1112
1113    /**
1114     * 进行事务操作
1115     *
1116     * @param supplier
1117     */
1118    public static boolean tx(Supplier<Boolean> supplier) {
1119        return tx(supplier, Propagation.REQUIRED);
1120    }
1121
1122
1123    public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) {
1124        Boolean result = TransactionalManager.exec(supplier, propagation);
1125        return result != null && result;
1126    }
1127}