|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2014-2023 abel533@gmail.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.github.pagehelper.dialect.helper; |
| 26 | + |
| 27 | +import com.github.pagehelper.Page; |
| 28 | +import com.github.pagehelper.dialect.AbstractHelperDialect; |
| 29 | +import com.github.pagehelper.util.MetaObjectUtil; |
| 30 | +import org.apache.ibatis.cache.CacheKey; |
| 31 | +import org.apache.ibatis.mapping.BoundSql; |
| 32 | +import org.apache.ibatis.mapping.MappedStatement; |
| 33 | +import org.apache.ibatis.mapping.ParameterMapping; |
| 34 | +import org.apache.ibatis.reflection.MetaObject; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +/** |
| 41 | + * GaussDB 方言 |
| 42 | + * |
| 43 | + * @author nieqiurong |
| 44 | + * @since 6.1.2 |
| 45 | + */ |
| 46 | +public class GaussDBDialect extends AbstractHelperDialect { |
| 47 | + |
| 48 | + @Override |
| 49 | + public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) { |
| 50 | + paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow()); |
| 51 | + paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize()); |
| 52 | + pageKey.update(page.getStartRow()); |
| 53 | + pageKey.update(page.getPageSize()); |
| 54 | + if (boundSql.getParameterMappings() != null) { |
| 55 | + List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings()); |
| 56 | + if (page.getStartRow() == 0) { |
| 57 | + newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build()); |
| 58 | + } else { |
| 59 | + newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, long.class).build()); |
| 60 | + newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build()); |
| 61 | + } |
| 62 | + MetaObject metaObject = MetaObjectUtil.forObject(boundSql); |
| 63 | + metaObject.setValue("parameterMappings", newParameterMappings); |
| 64 | + } |
| 65 | + return paramMap; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 构建 <a href="https://support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-1702.html">GaussDB</a>分页查询语句 |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public String getPageSql(String sql, Page page, CacheKey pageKey) { |
| 73 | + StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14); |
| 74 | + sqlBuilder.append(sql); |
| 75 | + if (page.getStartRow() == 0) { |
| 76 | + sqlBuilder.append(" LIMIT ? "); |
| 77 | + } else { |
| 78 | + sqlBuilder.append(" LIMIT ?, ? "); |
| 79 | + } |
| 80 | + return sqlBuilder.toString(); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments