forked from pagehelper/Mybatis-PageHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageAutoDialect.java
355 lines (325 loc) · 14.6 KB
/
PageAutoDialect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.pagehelper.page;
import com.github.pagehelper.AutoDialect;
import com.github.pagehelper.Dialect;
import com.github.pagehelper.PageException;
import com.github.pagehelper.dialect.AbstractHelperDialect;
import com.github.pagehelper.dialect.auto.*;
import com.github.pagehelper.dialect.helper.*;
import com.github.pagehelper.util.ClassUtil;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.mapping.MappedStatement;
import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
/**
* 基础方言信息
*
* @author liuzh
*/
public class PageAutoDialect {
private static Map<String, Class<? extends Dialect>> dialectAliasMap = new LinkedHashMap<>();
private static Map<String, Class<? extends AutoDialect>> autoDialectMap = new LinkedHashMap<>();
public static void registerDialectAlias(String alias, Class<? extends Dialect> dialectClass) {
dialectAliasMap.put(alias, dialectClass);
}
static {
//注册别名
registerDialectAlias("hsqldb", HsqldbDialect.class);
registerDialectAlias("h2", HsqldbDialect.class);
registerDialectAlias("phoenix", HsqldbDialect.class);
registerDialectAlias("postgresql", PostgreSqlDialect.class);
registerDialectAlias("mysql", MySqlDialect.class);
registerDialectAlias("mariadb", MySqlDialect.class);
registerDialectAlias("sqlite", MySqlDialect.class);
registerDialectAlias("herddb", HerdDBDialect.class);
registerDialectAlias("oracle", OracleDialect.class);
registerDialectAlias("oracle9i", Oracle9iDialect.class);
registerDialectAlias("db2", Db2Dialect.class);
registerDialectAlias("as400", AS400Dialect.class);
registerDialectAlias("informix", InformixDialect.class);
//解决 informix-sqli #129,仍然保留上面的
registerDialectAlias("informix-sqli", InformixDialect.class);
registerDialectAlias("sqlserver", SqlServerDialect.class);
registerDialectAlias("sqlserver2012", SqlServer2012Dialect.class);
registerDialectAlias("derby", SqlServer2012Dialect.class);
//达梦数据库,https://github.com/mybatis-book/book/issues/43
registerDialectAlias("dm", OracleDialect.class);
//阿里云PPAS数据库,https://github.com/pagehelper/Mybatis-PageHelper/issues/281
registerDialectAlias("edb", OracleDialect.class);
//神通数据库
registerDialectAlias("oscar", OscarDialect.class);
registerDialectAlias("clickhouse", MySqlDialect.class);
//瀚高数据库
registerDialectAlias("highgo", HsqldbDialect.class);
//虚谷数据库
registerDialectAlias("xugu", HsqldbDialect.class);
registerDialectAlias("impala", HsqldbDialect.class);
registerDialectAlias("firebirdsql", FirebirdDialect.class);
//人大金仓数据库
registerDialectAlias("kingbase", PostgreSqlDialect.class);
// 人大金仓新版本kingbase8
registerDialectAlias("kingbase8", PostgreSqlDialect.class);
//行云数据库
registerDialectAlias("xcloud", CirroDataDialect.class);
//openGauss数据库
registerDialectAlias("opengauss", PostgreSqlDialect.class);
//注册 AutoDialect
//想要实现和以前版本相同的效果时,可以配置 autoDialectClass=old
registerAutoDialectAlias("old", DefaultAutoDialect.class);
registerAutoDialectAlias("hikari", HikariAutoDialect.class);
registerAutoDialectAlias("druid", DruidAutoDialect.class);
registerAutoDialectAlias("tomcat-jdbc", TomcatAutoDialect.class);
registerAutoDialectAlias("dbcp", DbcpAutoDialect.class);
registerAutoDialectAlias("c3p0", C3P0AutoDialect.class);
//不配置时,默认使用 DataSourceNegotiationAutoDialect
registerAutoDialectAlias("default", DataSourceNegotiationAutoDialect.class);
}
public static void registerAutoDialectAlias(String alias, Class<? extends AutoDialect> autoDialectClass) {
autoDialectMap.put(alias, autoDialectClass);
}
/**
* 自动获取dialect,如果没有setProperties或setSqlUtilConfig,也可以正常进行
*/
private boolean autoDialect = true;
/**
* 属性配置
*/
private Properties properties;
/**
* 缓存 dialect 实现,key 有两种,分别为 jdbcurl 和 dialectClassName
*/
private Map<Object, AbstractHelperDialect> urlDialectMap = new ConcurrentHashMap<Object, AbstractHelperDialect>();
private ReentrantLock lock = new ReentrantLock();
private AbstractHelperDialect delegate;
private ThreadLocal<AbstractHelperDialect> dialectThreadLocal = new ThreadLocal<AbstractHelperDialect>();
private AutoDialect autoDialectDelegate;
public static String fromJdbcUrl(String jdbcUrl) {
final String url = jdbcUrl.toLowerCase();
for (String dialect : dialectAliasMap.keySet()) {
if (url.contains(":" + dialect.toLowerCase() + ":")) {
return dialect;
}
}
return null;
}
//获取当前的代理对象
public AbstractHelperDialect getDelegate() {
if (delegate != null) {
return delegate;
}
return dialectThreadLocal.get();
}
//移除代理对象
public void clearDelegate() {
dialectThreadLocal.remove();
}
public AbstractHelperDialect getDialectThreadLocal() {
return dialectThreadLocal.get();
}
public void setDialectThreadLocal(AbstractHelperDialect delegate) {
this.dialectThreadLocal.set(delegate);
}
/**
* 反射类
*
* @param className
* @return
* @throws Exception
*/
public static Class resloveDialectClass(String className) throws Exception {
if (dialectAliasMap.containsKey(className.toLowerCase())) {
return dialectAliasMap.get(className.toLowerCase());
} else {
return Class.forName(className);
}
}
/**
* 初始化 helper
*
* @param dialectClass
* @param properties
*/
public static AbstractHelperDialect instanceDialect(String dialectClass, Properties properties) {
AbstractHelperDialect dialect;
if (StringUtil.isEmpty(dialectClass)) {
throw new PageException("When you use the PageHelper pagination plugin, you must set the helper property");
}
try {
Class sqlDialectClass = resloveDialectClass(dialectClass);
if (AbstractHelperDialect.class.isAssignableFrom(sqlDialectClass)) {
dialect = (AbstractHelperDialect) sqlDialectClass.newInstance();
} else {
throw new PageException("When using PageHelper, the dialect must be an implementation class that implements the " + AbstractHelperDialect.class.getCanonicalName() + " interface!");
}
} catch (Exception e) {
throw new PageException("error initializing helper dialectclass[" + dialectClass + "]" + e.getMessage(), e);
}
dialect.setProperties(properties);
return dialect;
}
/**
* 多数据动态获取时,每次需要初始化,还可以运行时指定具体的实现
*
* @param ms
* @param dialectClass 分页实现,必须是 {@link AbstractHelperDialect} 实现类,可以使用当前类中注册的别名,例如 "mysql", "oracle"
*/
public void initDelegateDialect(MappedStatement ms, String dialectClass) {
if (StringUtil.isNotEmpty(dialectClass)) {
AbstractHelperDialect dialect = urlDialectMap.get(dialectClass);
if (dialect == null) {
lock.lock();
try {
if ((dialect = urlDialectMap.get(dialectClass)) == null) {
dialect = instanceDialect(dialectClass, properties);
urlDialectMap.put(dialectClass, dialect);
}
} finally {
lock.unlock();
}
}
dialectThreadLocal.set(dialect);
} else if (delegate == null) {
if (autoDialect) {
this.delegate = autoGetDialect(ms);
} else {
dialectThreadLocal.set(autoGetDialect(ms));
}
}
}
/**
* 自动获取分页方言实现
*
* @param ms
* @return
*/
public AbstractHelperDialect autoGetDialect(MappedStatement ms) {
DataSource dataSource = ms.getConfiguration().getEnvironment().getDataSource();
Object dialectKey = autoDialectDelegate.extractDialectKey(ms, dataSource, properties);
if (dialectKey == null) {
return autoDialectDelegate.extractDialect(dialectKey, ms, dataSource, properties);
} else if (!urlDialectMap.containsKey(dialectKey)) {
lock.lock();
try {
if (!urlDialectMap.containsKey(dialectKey)) {
urlDialectMap.put(dialectKey, autoDialectDelegate.extractDialect(dialectKey, ms, dataSource, properties));
}
} finally {
lock.unlock();
}
}
return urlDialectMap.get(dialectKey);
}
/**
* 初始化自定义 AutoDialect
*
* @param properties
*/
private void initAutoDialectClass(Properties properties) {
String autoDialectClassStr = properties.getProperty("autoDialectClass");
if (StringUtil.isNotEmpty(autoDialectClassStr)) {
try {
Class<? extends AutoDialect> autoDialectClass;
if (autoDialectMap.containsKey(autoDialectClassStr)) {
autoDialectClass = autoDialectMap.get(autoDialectClassStr);
} else {
autoDialectClass = (Class<AutoDialect>) Class.forName(autoDialectClassStr);
}
this.autoDialectDelegate = ClassUtil.newInstance(autoDialectClass, properties);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Make sure that the AutoDialect implementation class ("
+ autoDialectClassStr + ") for the autoDialectClass configuration exists!", e);
} catch (Exception e) {
throw new RuntimeException(autoDialectClassStr + "Class must provide a constructor without parameters", e);
}
} else {
this.autoDialectDelegate = new DataSourceNegotiationAutoDialect();
}
}
/**
* 初始化方言别名
*
* @param properties
*/
private void initDialectAlias(Properties properties) {
String dialectAlias = properties.getProperty("dialectAlias");
if (StringUtil.isNotEmpty(dialectAlias)) {
String[] alias = dialectAlias.split(";");
for (int i = 0; i < alias.length; i++) {
String[] kv = alias[i].split("=");
if (kv.length != 2) {
throw new IllegalArgumentException("dialectAlias parameter misconfigured," +
"Please follow alias1=xx.dialectClass; alias2=dialectClass2!");
}
for (int j = 0; j < kv.length; j++) {
try {
//允许配置如 dm=oracle, 直接引用oracle实现
if (dialectAliasMap.containsKey(kv[1])) {
registerDialectAlias(kv[0], dialectAliasMap.get(kv[1]));
} else {
Class<? extends Dialect> diallectClass = (Class<? extends Dialect>) Class.forName(kv[1]);
//允许覆盖已有的实现
registerDialectAlias(kv[0], diallectClass);
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Make sure the Dialect implementation class configured by dialectAlias exists!", e);
}
}
}
}
}
public void setProperties(Properties properties) {
this.properties = properties;
//初始化自定义AutoDialect
initAutoDialectClass(properties);
//使用 sqlserver2012 作为默认分页方式,这种情况在动态数据源时方便使用
String useSqlserver2012 = properties.getProperty("useSqlserver2012");
if (StringUtil.isNotEmpty(useSqlserver2012) && Boolean.parseBoolean(useSqlserver2012)) {
registerDialectAlias("sqlserver", SqlServer2012Dialect.class);
registerDialectAlias("sqlserver2008", SqlServerDialect.class);
}
initDialectAlias(properties);
//指定的 Helper 数据库方言,和 不同
String dialect = properties.getProperty("helperDialect");
//运行时获取数据源
String runtimeDialect = properties.getProperty("autoRuntimeDialect");
//1.动态多数据源
if (StringUtil.isNotEmpty(runtimeDialect) && "TRUE".equalsIgnoreCase(runtimeDialect)) {
this.autoDialect = false;
}
//2.动态获取方言
else if (StringUtil.isEmpty(dialect)) {
autoDialect = true;
}
//3.指定方言
else {
autoDialect = false;
this.delegate = instanceDialect(dialect, properties);
}
}
}