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