Skip to content

Commit e550426

Browse files
committed
适配华为云GaussDb数据库.
#860
1 parent 4db191c commit e550426

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.rowbounds;
26+
27+
import com.github.pagehelper.dialect.AbstractRowBoundsDialect;
28+
import org.apache.ibatis.cache.CacheKey;
29+
import org.apache.ibatis.session.RowBounds;
30+
31+
/**
32+
* GaussDB 基于 RowBounds 的分页.
33+
*
34+
* @author nieqiurong
35+
* @since 6.1.2
36+
*/
37+
public class GaussDBRowBoundsDialect extends AbstractRowBoundsDialect {
38+
39+
/**
40+
* 构建 <a href="https://support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-1702.html">GaussDB</a>分页查询语句
41+
*/
42+
@Override
43+
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
44+
StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
45+
sqlBuilder.append(sql);
46+
if (rowBounds.getOffset() == 0) {
47+
sqlBuilder.append(" LIMIT ");
48+
sqlBuilder.append(rowBounds.getLimit());
49+
} else {
50+
sqlBuilder.append(" LIMIT ");
51+
sqlBuilder.append(rowBounds.getOffset());
52+
sqlBuilder.append(",");
53+
sqlBuilder.append(rowBounds.getLimit());
54+
pageKey.update(rowBounds.getOffset());
55+
}
56+
pageKey.update(rowBounds.getLimit());
57+
return sqlBuilder.toString();
58+
}
59+
60+
}

src/main/java/com/github/pagehelper/page/PageAutoDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public static void registerDialectAlias(String alias, Class<? extends Dialect> d
103103

104104
//openGauss数据库
105105
registerDialectAlias("opengauss", PostgreSqlDialect.class);
106+
registerDialectAlias("gaussdb", GaussDBDialect.class);
106107
registerDialectAlias("sundb", OracleDialect.class);
107108

108109
//注册 AutoDialect

0 commit comments

Comments
 (0)