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        } finally {
076            sqlSession.close();
077        }
078        return results;
079    }
080
081    public int insertBatchWithFirstRowColumns(String tableName, List<Row> rows) {
082        return execute(mapper -> mapper.insertBatchWithFirstRowColumns(tableName, rows));
083    }
084
085    public int deleteBySql(String sql, Object... args) {
086        return execute(mapper -> mapper.deleteBySql(sql, args));
087    }
088
089    public int deleteById(String tableName, Row row) {
090        return execute(mapper -> mapper.deleteById(tableName, row));
091    }
092
093    public int deleteById(String tableName, String primaryKey, Object id) {
094        return execute(mapper -> mapper.deleteById(tableName, primaryKey, id));
095    }
096
097    public int deleteBatchByIds(String tableName, String primaryKey, Collection<?> ids) {
098        return execute(mapper -> mapper.deleteBatchByIds(tableName, primaryKey, ids));
099    }
100
101
102    public int deleteByQuery(String tableName, QueryWrapper queryWrapper) {
103        return execute(mapper -> mapper.deleteByQuery(tableName, queryWrapper));
104    }
105
106    public int updateBySql(String sql, Object... args) {
107        return execute(mapper -> mapper.updateBySql(sql, args));
108    }
109
110    public int updateById(String tableName, Row row) {
111        return execute(mapper -> mapper.updateById(tableName, row));
112    }
113
114    public int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) {
115        return execute(mapper -> mapper.updateByQuery(tableName, data, queryWrapper));
116    }
117
118    public int updateBatchById(String tableName, List<Row> rows) {
119        return execute(mapper -> mapper.updateBatchById(tableName, rows));
120    }
121
122    public Row selectOneBySql(String sql, Object... args) {
123        return execute(mapper -> mapper.selectOneBySql(sql, args));
124    }
125
126    public Row selectOneById(String tableName, Row row) {
127        return execute(mapper -> mapper.selectOneById(tableName, row));
128    }
129
130    public Row selectOneById(String tableName, String primaryKey, Object id) {
131        return execute(mapper -> mapper.selectOneById(tableName, primaryKey, id));
132    }
133
134    public Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) {
135        return execute(mapper -> mapper.selectOneByQuery(tableName, queryWrapper));
136    }
137
138    public List<Row> selectListBySql(String sql, Object... args) {
139        return execute(mapper -> mapper.selectListBySql(sql, args));
140    }
141
142    public List<Row> selectListByQuery(String tableName, QueryWrapper queryWrapper) {
143        return execute(mapper -> mapper.selectListByQuery(tableName, queryWrapper));
144    }
145
146    public List<Row> selectAll(String tableName) {
147        return execute(mapper -> mapper.selectAll(tableName));
148    }
149
150    public Object selectObject(String sql, Object... args) {
151        return execute(mapper -> mapper.selectObject(sql, args));
152    }
153
154    public List<Object> selectObjectList(String sql, Object... args) {
155        return execute(mapper -> mapper.selectObjectList(sql, args));
156    }
157
158    public long selectCount(String sql, Object... args) {
159        return execute(mapper -> mapper.selectCount(sql, args));
160    }
161
162
163    public long selectCountByQuery(String tableName, QueryWrapper queryWrapper) {
164        return execute(mapper -> mapper.selectCountByQuery(tableName, queryWrapper));
165    }
166
167    public Page<Row> paginate(String tableName, Page<Row> page, QueryWrapper queryWrapper) {
168        return execute(mapper -> mapper.paginate(tableName, page, queryWrapper));
169    }
170
171
172}