Skip to content

Commit e2fb0a1

Browse files
committed
完善数据库版本判断,兼容 MySQL 8.0 以上和以下版本,适配正则匹配、窗口函数等
1 parent 4efcfd3 commit e2fb0a1

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,16 @@ public abstract class AbstractSQLConfig implements SQLConfig {
728728

729729
}
730730

731+
private int[] dbVersionNums = null;
732+
@Override
733+
public int[] getDBVersionNums() {
734+
if (dbVersionNums == null || dbVersionNums.length <= 0) {
735+
dbVersionNums = SQLConfig.super.getDBVersionNums();
736+
}
737+
return dbVersionNums;
738+
}
731739

732-
@Override
740+
@Override
733741
public boolean limitSQLCount() {
734742
return Log.DEBUG == false || AbstractVerifier.SYSTEM_ACCESS_MAP.containsKey(getTable()) == false;
735743
}
@@ -3412,7 +3420,7 @@ public String getRegExpString(String key, String column, String value, boolean i
34123420
if (isPostgreSQL()) {
34133421
return getKey(column) + " ~" + (ignoreCase ? "* " : " ") + getValue(key, column, value);
34143422
}
3415-
if (isOracle()) {
3423+
if (isOracle() || (isMySQL() && getDBVersionNums()[0] >= 8)) {
34163424
return "regexp_like(" + getKey(column) + ", " + getValue(key, column, value) + (ignoreCase ? ", 'i'" : ", 'c'") + ")";
34173425
}
34183426
if (isClickHouse()) {
@@ -3425,7 +3433,9 @@ public String getRegExpString(String key, String column, String value, boolean i
34253433
}
34263434
return getKey(column) + " REGEXP " + (ignoreCase ? "" : "BINARY ") + getValue(key, column, value);
34273435
}
3428-
//~ regexp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3436+
3437+
3438+
//~ regexp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
34293439

34303440

34313441

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
719719
int allChildCount = childCount*config.getCount(); // 所有分组子项数量总和
720720

721721
String sql2 = null;
722-
if (childCount > 0 && (childCount != 1 || join.isOne2Many())) { // TODO 判断 MySQL >= 8.0
722+
if (childCount > 0 && (childCount != 1 || join.isOne2Many()) && (jc.isMySQL() == false || jc.getDBVersionNums()[0] >= 8)) {
723723
String q = jc.getQuote();
724724
sql2 = prepared ? jc.getSQL(true) : sql;
725725

APIJSONORM/src/main/java/apijson/orm/SQLConfig.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import apijson.NotNull;
1212
import apijson.RequestMethod;
13+
import apijson.StringUtil;
1314

1415
/**SQL配置
1516
* @author Lemon
@@ -46,8 +47,8 @@ public interface SQLConfig {
4647
// boolean isPLSQL();
4748
// boolean isAnsiSQL();
4849

49-
boolean limitSQLCount(); //用来给 Table, Column 等系统属性表来绕过 MAX_SQL_COUNT 等限制
50-
50+
boolean limitSQLCount(); //用来给 Table, Column 等系统属性表来绕过 MAX_SQL_COUNT 等限制
51+
5152
@NotNull
5253
String getIdKey();
5354
@NotNull
@@ -60,6 +61,27 @@ public interface SQLConfig {
6061
*/
6162
String getDBVersion();
6263

64+
@NotNull
65+
default int[] getDBVersionNums() {
66+
String dbVersion = StringUtil.getNoBlankString(getDBVersion());
67+
if (dbVersion.isEmpty()) {
68+
return new int[]{0};
69+
}
70+
71+
int index = dbVersion.indexOf("-");
72+
if (index > 0) {
73+
dbVersion = dbVersion.substring(0, index);
74+
}
75+
76+
String[] ss = dbVersion.split("[.]");
77+
int[] nums = new int[Math.max(1, ss.length)];
78+
for (int i = 0; i < ss.length; i++) {
79+
nums[i] = Integer.valueOf(ss[i]);
80+
}
81+
82+
return nums;
83+
}
84+
6385
/**获取数据库地址
6486
* @return
6587
*/
@@ -116,10 +138,10 @@ public interface SQLConfig {
116138

117139
Object getId();
118140
SQLConfig setId(Object id);
119-
141+
120142
Object getIdIn();
121143
SQLConfig setIdIn(Object idIn);
122-
144+
123145
Object getUserId();
124146
SQLConfig setUserId(Object userId);
125147

@@ -137,21 +159,21 @@ public interface SQLConfig {
137159

138160
String getSchema();
139161
SQLConfig setSchema(String schema);
140-
162+
141163
String getDatasource();
142164
SQLConfig setDatasource(String datasource);
143165

144166
String getQuote();
145167

146168
List<String> getJson();
147169
SQLConfig setJson(List<String> json);
148-
170+
149171
/**请求传进来的Table名
150172
* @return
151173
* @see {@link #getSQLTable()}
152174
*/
153175
String getTable();
154-
176+
155177
SQLConfig setTable(String table);
156178

157179
/**数据库里的真实Table名
@@ -182,13 +204,13 @@ public interface SQLConfig {
182204

183205
String getCombine();
184206
SQLConfig setCombine(String combine);
185-
207+
186208
Map<String, String> getCast();
187209
SQLConfig setCast(Map<String, String> cast);
188-
210+
189211
List<String> getNull();
190212
SQLConfig setNull(List<String> nulls);
191-
213+
192214
Map<String, Object> getWhere();
193215
SQLConfig setWhere(Map<String, Object> where);
194216

@@ -197,7 +219,7 @@ public interface SQLConfig {
197219

198220
Map<String, Object> getHaving();
199221
SQLConfig setHaving(Map<String, Object> having);
200-
222+
201223
String getHavingCombine();
202224
SQLConfig setHavingCombine(String havingCombine);
203225

0 commit comments

Comments
 (0)