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