Skip to content

Commit 5840470

Browse files
committed
解决误在子查询拼接 LIMIT;解决 @sample SAMPLE BY @fill FILL 的 SQL 拼接 bug
1 parent 9a45d33 commit 5840470

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

APIJSONORM/src/main/java/apijson/StringUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ public static boolean isNotEmpty(String s, boolean trim) {
343343
public static final Pattern PATTERN_PHONE;
344344
public static final Pattern PATTERN_EMAIL;
345345
public static final Pattern PATTERN_ID_CARD;
346+
public static final Pattern PATTERN_NUM_OR_ALPHA;
346347
public static final Pattern PATTERN_ALPHA;
347348
public static final Pattern PATTERN_PASSWORD; //TODO
348349
public static final Pattern PATTERN_NAME;
@@ -351,6 +352,7 @@ public static boolean isNotEmpty(String s, boolean trim) {
351352
public static final Pattern PATTERN_BRANCH_URL;
352353
static {
353354
PATTERN_NUMBER = Pattern.compile("^[0-9]+$");
355+
PATTERN_NUM_OR_ALPHA = Pattern.compile("^[0-9a-zA-Z_.:]+$");
354356
PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$");
355357
PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$");
356358
PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$");
@@ -442,6 +444,19 @@ public static boolean isNumberOrAlpha(String s) {
442444
return isNumer(s) || isAlpha(s);
443445
}
444446

447+
/**判断是否全是数字或字母
448+
* @param s
449+
* @return
450+
*/
451+
public static boolean isCombineOfNumOrAlpha(String s) {
452+
if (isEmpty(s, true)) {
453+
return false;
454+
}
455+
456+
currentString = s;
457+
return PATTERN_NUM_OR_ALPHA.matcher(s).matches();
458+
}
459+
445460
/**判断是否为代码名称,只能包含字母,数字或下划线
446461
* @param s
447462
* @return

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,17 +1792,18 @@ public String getSampleString(boolean hasPrefix) {
17921792

17931793
for (int i = 0; i < keys.length; i++) {
17941794
String item = keys[i];
1795-
//if ("fill(null)".equals(item) || "fill(linear)".equals(item) || "fill(prev)".equals(item) || "fill(previous)".equals(item)) {
1796-
// continue;
1797-
//}
17981795

17991796
String origin = item;
18001797

18011798
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
18021799
//这里既不对origin trim,也不对 ASC/DESC ignoreCase,希望前端严格传没有任何空格的字符串过来,减少传输数据量,节约服务器性能
1803-
if (StringUtil.isNumberOrAlpha(origin) == false) {
1800+
if (StringUtil.isName(origin)) {}
1801+
else if (StringUtil.isCombineOfNumOrAlpha(origin)) {
1802+
continue;
1803+
}
1804+
else {
18041805
throw new IllegalArgumentException("预编译模式下 @sample:value 中 " + item + " 不合法! value 里面用 , 分割的"
1805-
+ "每一项必须是 column 且其中 column 必须是 字母或数字组合!并且不要有多余的空格!");
1806+
+ "每一项必须是 column 且其中 column 必须是 数字或英语字母组合!并且不要有多余的空格!");
18061807
}
18071808
}
18081809

@@ -1994,13 +1995,21 @@ public String getFillString(boolean hasPrefix) {
19941995

19951996
for (int i = 0; i < keys.length; i++) {
19961997
String item = keys[i];
1998+
if ("NULL".equals(item) || "LINEAR".equals(item) || "PREV".equals(item) || "PREVIOUS".equals(item)) {
1999+
continue;
2000+
}
2001+
19972002
String origin = item;
19982003

19992004
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
20002005
//这里既不对origin trim,也不对 ASC/DESC ignoreCase,希望前端严格传没有任何空格的字符串过来,减少传输数据量,节约服务器性能
2001-
if (StringUtil.isName(origin) == false) {
2006+
if (StringUtil.isName(origin)) {}
2007+
else if (StringUtil.isCombineOfNumOrAlpha(origin)) {
2008+
continue;
2009+
}
2010+
else {
20022011
throw new IllegalArgumentException("预编译模式下 @fill:value 中 " + item + " 不合法! value 里面用 , 分割的"
2003-
+ "每一项必须是 column 且其中 column 必须是 英语单词!并且不要有多余的空格!");
2012+
+ "每一项必须是 column 且其中 column 必须是 数字或英语字母组合!并且不要有多余的空格!");
20042013
}
20052014
}
20062015

@@ -3035,39 +3044,35 @@ public static int getOffset(int page, int count) {
30353044
@JSONField(serialize = false)
30363045
public String getLimitString() {
30373046
int count = getCount();
3047+
int page = getPage();
3048+
3049+
boolean isMilvus = isMilvus();
3050+
if ((count <= 0 && ! (isMilvus && isMain())) || RequestMethod.isHeadMethod(getMethod(), true)) { // TODO HEAD 真的不需要 LIMIT ?
3051+
return "";
3052+
}
30383053

30393054
boolean isSurrealDB = isSurrealDB();
30403055
boolean isQuestDB = isQuestDB();
3041-
if (isSurrealDB || isQuestDB || isMilvus()) {
3056+
if (isSurrealDB || isQuestDB || isMilvus) {
30423057
if (count == 0) {
30433058
Parser<T> parser = getParser();
30443059
count = parser == null ? AbstractParser.MAX_QUERY_COUNT : parser.getMaxQueryCount();
30453060
}
30463061

3047-
int offset = getOffset(getPage(), count);
3062+
int offset = getOffset(page, count);
30483063
if (isQuestDB()) {
30493064
return " LIMIT " + offset + ", " + (offset + count);
30503065
}
3051-
else if (isSurrealDB()) {
3066+
else if (isSurrealDB()) {
30523067
return " START " + offset + " LIMIT " + count;
30533068
}
30543069
else {
30553070
return " LIMIT " + offset + ", " + count; // 目前 moql-transx 的限制
30563071
}
30573072
}
30583073

3059-
if (count <= 0 || RequestMethod.isHeadMethod(getMethod(), true)) { // TODO HEAD 真的不需要 LIMIT ?
3060-
return "";
3061-
}
3062-
30633074
boolean isOracle = isOracle();
3064-
return getLimitString(
3065-
getPage()
3066-
, count
3067-
, isTSQL()
3068-
, isOracle || isDameng() || isKingBase()
3069-
, isPresto() || isTrino()
3070-
);
3075+
return getLimitString(page, count, isTSQL(), isOracle || isDameng() || isKingBase(), isPresto() || isTrino());
30713076
}
30723077
/**获取限制数量及偏移量
30733078
* @param page

0 commit comments

Comments
 (0)