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.paginate.Page; 019import com.mybatisflex.core.query.QueryWrapper; 020import org.apache.ibatis.executor.BatchResult; 021import org.apache.ibatis.session.ExecutorType; 022import org.apache.ibatis.session.SqlSession; 023import org.apache.ibatis.session.SqlSessionFactory; 024 025import java.util.Collection; 026import java.util.List; 027import java.util.function.BiConsumer; 028import java.util.function.Function; 029 030public class RowMapperInvoker { 031 032 private final SqlSessionFactory sqlSessionFactory; 033 034 public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) { 035 this.sqlSessionFactory = sqlSessionFactory; 036 } 037 038 private <R> R execute(Function<RowMapper, R> function) { 039 try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) { 040 RowMapper mapper = sqlSession.getMapper(RowMapper.class); 041 return function.apply(mapper); 042 } 043 } 044 045 public int insert(String schema, String tableName, Row row) { 046 return execute(mapper -> mapper.insert(schema, tableName, row)); 047 } 048 049 050 public int insertBySql(String sql, Object... args) { 051 return execute(mapper -> mapper.insertBySql(sql, args)); 052 } 053 054 public int insertBatchWithFirstRowColumns(String schema, String tableName, List<Row> rows) { 055 return execute(mapper -> mapper.insertBatchWithFirstRowColumns(schema, tableName, rows)); 056 } 057 058 public int deleteBySql(String sql, Object... args) { 059 return execute(mapper -> mapper.deleteBySql(sql, args)); 060 } 061 062 public int deleteById(String schema, String tableName, Row row) { 063 return execute(mapper -> mapper.deleteById(schema, tableName, row)); 064 } 065 066 public int deleteById(String schema, String tableName, String primaryKey, Object id) { 067 return execute(mapper -> mapper.deleteById(schema, tableName, primaryKey, id)); 068 } 069 070 public int deleteBatchByIds(String schema, String tableName, String primaryKey, Collection<?> ids) { 071 return execute(mapper -> mapper.deleteBatchByIds(schema, tableName, primaryKey, ids)); 072 } 073 074 075 public int deleteByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 076 return execute(mapper -> mapper.deleteByQuery(schema, tableName, queryWrapper)); 077 } 078 079 public int updateBySql(String sql, Object... args) { 080 return execute(mapper -> mapper.updateBySql(sql, args)); 081 } 082 083 084 public <M> int[] executeBatch(int totalSize, int batchSize, Class<M> mapperClass, BiConsumer<M, Integer> consumer) { 085 int[] results = new int[totalSize]; 086 try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, true)) { 087 M mapper = sqlSession.getMapper(mapperClass); 088 int counter = 0; 089 int resultsPos = 0; 090 for (int i = 0; i < totalSize; i++) { 091 consumer.accept(mapper, i); 092 if (++counter == batchSize) { 093 counter = 0; 094 List<BatchResult> batchResults = sqlSession.flushStatements(); 095 for (BatchResult batchResult : batchResults) { 096 int[] updateCounts = batchResult.getUpdateCounts(); 097 for (int updateCount : updateCounts) { 098 results[resultsPos++] = updateCount; 099 } 100 } 101 } 102 103 } 104 105 if (counter != 0) { 106 List<BatchResult> batchResults = sqlSession.flushStatements(); 107 for (BatchResult batchResult : batchResults) { 108 int[] updateCounts = batchResult.getUpdateCounts(); 109 for (int updateCount : updateCounts) { 110 results[resultsPos++] = updateCount; 111 } 112 } 113 } 114 } 115 return results; 116 } 117 118 public int updateById(String schema, String tableName, Row row) { 119 return execute(mapper -> mapper.updateById(schema, tableName, row)); 120 } 121 122 public int updateByQuery(String schema, String tableName, Row data, QueryWrapper queryWrapper) { 123 return execute(mapper -> mapper.updateByQuery(schema, tableName, data, queryWrapper)); 124 } 125 126 public int updateBatchById(String schema, String tableName, List<Row> rows) { 127 return execute(mapper -> mapper.updateBatchById(schema, tableName, rows)); 128 } 129 130 public Row selectOneBySql(String sql, Object... args) { 131 return execute(mapper -> mapper.selectOneBySql(sql, args)); 132 } 133 134 public Row selectOneById(String schema, String tableName, Row row) { 135 return execute(mapper -> mapper.selectOneById(schema, tableName, row)); 136 } 137 138 public Row selectOneById(String schema, String tableName, String primaryKey, Object id) { 139 return execute(mapper -> mapper.selectOneById(schema, tableName, primaryKey, id)); 140 } 141 142 public Row selectOneByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 143 return execute(mapper -> mapper.selectOneByQuery(schema, tableName, queryWrapper)); 144 } 145 146 public List<Row> selectListBySql(String sql, Object... args) { 147 return execute(mapper -> mapper.selectListBySql(sql, args)); 148 } 149 150 public List<Row> selectListByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 151 return execute(mapper -> mapper.selectListByQuery(schema, tableName, queryWrapper)); 152 } 153 154 public List<Row> selectAll(String schema, String tableName) { 155 return execute(mapper -> mapper.selectAll(schema, tableName)); 156 } 157 158 public Object selectObjectByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 159 return execute(mapper -> mapper.selectObjectByQuery(schema, tableName, queryWrapper)); 160 } 161 162 public List<Object> selectObjectListByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 163 return execute(mapper -> mapper.selectObjectListByQuery(schema, tableName, queryWrapper)); 164 } 165 166 public Object selectObject(String sql, Object... args) { 167 return execute(mapper -> mapper.selectObject(sql, args)); 168 } 169 170 public List<Object> selectObjectList(String sql, Object... args) { 171 return execute(mapper -> mapper.selectObjectList(sql, args)); 172 } 173 174 public long selectCount(String sql, Object... args) { 175 return execute(mapper -> mapper.selectCount(sql, args)); 176 } 177 178 179 public long selectCountByQuery(String schema, String tableName, QueryWrapper queryWrapper) { 180 return execute(mapper -> mapper.selectCountByQuery(schema, tableName, queryWrapper)); 181 } 182 183 public Page<Row> paginate(String schema, String tableName, Page<Row> page, QueryWrapper queryWrapper) { 184 return execute(mapper -> mapper.paginate(schema, tableName, page, queryWrapper)); 185 } 186 187 188 public int updateNumberAddByQuery(String schema, String tableName, String fieldName, Number value, QueryWrapper queryWrapper) { 189 return execute(mapper -> mapper.updateNumberAddByQuery(schema, tableName, fieldName, value, queryWrapper)); 190 } 191 192}