|
| 1 | +/** |
| 2 | + * Copyright 2016-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package examples.paging; |
| 17 | + |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import org.mybatis.dynamic.sql.render.RenderingStrategy; |
| 24 | +import org.mybatis.dynamic.sql.select.SelectModel; |
| 25 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 26 | + |
| 27 | +/** |
| 28 | + * This adapter modifies the generated SQL by adding a LIMIT and OFFSET clause at the end |
| 29 | + * of the generated SQL. This can be used to create a paginated query. |
| 30 | + * |
| 31 | + * LIMIT and OFFSET has limited support in relational databases, so this cannot be considered |
| 32 | + * a general solution for all paginated queries (and that is why this adapter lives only in the |
| 33 | + * test source tree and is not packaged with the core library code). |
| 34 | + * |
| 35 | + * I believe it works in MySQL, HSQLDB, and Postgres. |
| 36 | + * |
| 37 | + * @author Jeff Butler |
| 38 | + */ |
| 39 | +public class LimitAndOffsetAdapter<R> { |
| 40 | + private SelectModel selectModel; |
| 41 | + private Function<SelectStatementProvider, R> mapperMethod; |
| 42 | + private int limit; |
| 43 | + private int offset; |
| 44 | + |
| 45 | + private LimitAndOffsetAdapter(SelectModel selectModel, Function<SelectStatementProvider, R> mapperMethod, |
| 46 | + int limit, int offset) { |
| 47 | + this.selectModel = Objects.requireNonNull(selectModel); |
| 48 | + this.mapperMethod = Objects.requireNonNull(mapperMethod); |
| 49 | + this.limit = limit; |
| 50 | + this.offset = offset; |
| 51 | + } |
| 52 | + |
| 53 | + public R execute() { |
| 54 | + return mapperMethod.apply(selectStatement()); |
| 55 | + } |
| 56 | + |
| 57 | + private SelectStatementProvider selectStatement() { |
| 58 | + return new LimitAndOffsetDecorator( |
| 59 | + selectModel.render(RenderingStrategy.MYBATIS3)); |
| 60 | + } |
| 61 | + |
| 62 | + public static <R> LimitAndOffsetAdapter<R> of(SelectModel selectModel, |
| 63 | + Function<SelectStatementProvider, R> mapperMethod, int limit, int offset) { |
| 64 | + return new LimitAndOffsetAdapter<>(selectModel, mapperMethod, limit, offset); |
| 65 | + } |
| 66 | + |
| 67 | + public class LimitAndOffsetDecorator implements SelectStatementProvider { |
| 68 | + private Map<String, Object> parameters = new HashMap<>(); |
| 69 | + private String selectStatement; |
| 70 | + |
| 71 | + public LimitAndOffsetDecorator(SelectStatementProvider delegate) { |
| 72 | + parameters.putAll(delegate.getParameters()); |
| 73 | + parameters.put("limit", limit); |
| 74 | + parameters.put("offset", offset); |
| 75 | + |
| 76 | + selectStatement = delegate.getSelectStatement() + |
| 77 | + " LIMIT #{parameters.limit} OFFSET #{parameters.offset}"; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Map<String, Object> getParameters() { |
| 82 | + return parameters; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getSelectStatement() { |
| 87 | + return selectStatement; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments