fixed:" "in" logic is not correct

This commit is contained in:
开源海哥 2023-03-29 10:25:11 +08:00
parent fe65f3803e
commit 02a5e4b84c
2 changed files with 44 additions and 77 deletions

View File

@ -19,6 +19,7 @@ package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -98,48 +99,6 @@ public class QueryCondition implements Serializable {
this.logic = logic; this.logic = logic;
} }
/**
* 计算问号?的数量
*
* @return 问号的数量
*/
public int calculateQuestionMarkCount() {
if (LOGIC_IS_NULL.equals(logic)
|| LOGIC_IS_NOT_NULL.equals(logic)
|| value instanceof QueryColumn
|| value instanceof QueryWrapper) {
return 0;
}
//between, not between
else if (LOGIC_BETWEEN.equals(logic) || LOGIC_NOT_BETWEEN.equals(logic)) {
return 2;
}
//in, not in
else if (LOGIC_IN.equals(logic) || LOGIC_NOT_IN.equals(logic)) {
return calculateValueArrayCount();
}
//
else {
return 1;
}
}
private int calculateValueArrayCount() {
Object[] values = (Object[]) value;
int paramsCount = 0;
for (Object v : values) {
if (v.getClass() == int[].class) {
paramsCount += ((int[]) v).length;
} else if (v.getClass() == long[].class) {
paramsCount += ((long[]) v).length;
} else if (v.getClass() == short[].class) {
paramsCount += ((short[]) v).length;
} else {
paramsCount++;
}
}
return paramsCount;
}
public QueryCondition when(boolean effective) { public QueryCondition when(boolean effective) {
this.effective = effective; this.effective = effective;
@ -194,7 +153,7 @@ public class QueryCondition implements Serializable {
} }
//正常查询构建问号 //正常查询构建问号
else { else {
appendQuestionMark(sql, calculateQuestionMarkCount()); appendQuestionMark(sql);
} }
} }
@ -217,16 +176,21 @@ public class QueryCondition implements Serializable {
} }
protected static void appendQuestionMark(StringBuilder sqlBuilder, int paramsCount) { protected void appendQuestionMark(StringBuilder sqlBuilder) {
if (paramsCount == 1) { if (LOGIC_IS_NULL.equals(logic)
sqlBuilder.append(" ? "); || LOGIC_IS_NOT_NULL.equals(logic)
|| value instanceof QueryColumn
|| value instanceof QueryWrapper) {
//do nothing
} }
//between, not between //between, not between
else if (paramsCount == 2) { else if (LOGIC_BETWEEN.equals(logic) || LOGIC_NOT_BETWEEN.equals(logic)) {
sqlBuilder.append(" ? AND ? "); sqlBuilder.append(" ? AND ? ");
} }
//in, not in //in, not in
else if (paramsCount > 0) { else if (LOGIC_IN.equals(logic) || LOGIC_NOT_IN.equals(logic)) {
int paramsCount = calculateValueArrayCount();
sqlBuilder.append('('); sqlBuilder.append('(');
for (int i = 0; i < paramsCount; i++) { for (int i = 0; i < paramsCount; i++) {
sqlBuilder.append('?'); sqlBuilder.append('?');
@ -236,10 +200,29 @@ public class QueryCondition implements Serializable {
} }
sqlBuilder.append(')'); sqlBuilder.append(')');
} else { } else {
// paramsCount == 0, ignore sqlBuilder.append(" ? ");
} }
} }
private int calculateValueArrayCount() {
Object[] values = (Object[]) value;
int paramsCount = 0;
for (Object object : values) {
if (object != null && (object.getClass().isArray()
|| object.getClass() == int[].class
|| object.getClass() == long[].class
|| object.getClass() == short[].class
|| object.getClass() == float[].class
|| object.getClass() == double[].class)) {
paramsCount += Array.getLength(object);
} else {
paramsCount++;
}
}
return paramsCount;
}
@Override @Override
public String toString() { public String toString() {
return "QueryCondition{" + return "QueryCondition{" +

View File

@ -19,6 +19,7 @@ package com.mybatisflex.core.query;
import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import java.lang.reflect.Array;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -52,15 +53,18 @@ class WrapperUtil {
if (value != null) { if (value != null) {
if (value.getClass().isArray()) { if (value.getClass().isArray()) {
Object[] values = (Object[]) value; Object[] values = (Object[]) value;
for (Object v : values) { for (Object object : values) {
if (v.getClass() == int[].class) { if (object != null && (object.getClass().isArray()
addAll(paras, (int[]) v); || object.getClass() == int[].class
} else if (v.getClass() == long[].class) { || object.getClass() == long[].class
addAll(paras, (long[]) v); || object.getClass() == short[].class
} else if (v.getClass() == short[].class) { || object.getClass() == float[].class
addAll(paras, (short[]) v); || object.getClass() == double[].class)) {
for (int i = 0; i < Array.getLength(object); i++) {
paras.add(Array.get(object, i));
}
} else { } else {
paras.add(v); paras.add(object);
} }
} }
} else if (value instanceof QueryWrapper) { } else if (value instanceof QueryWrapper) {
@ -75,26 +79,6 @@ class WrapperUtil {
} }
private static void addAll(List<Object> paras, int[] ints) {
for (int i : ints) {
paras.add(i);
}
}
private static void addAll(List<Object> paras, long[] longs) {
for (long i : longs) {
paras.add(i);
}
}
private static void addAll(List<Object> paras, short[] shorts) {
for (short i : shorts) {
paras.add(i);
}
}
public static String getColumnTableName(List<QueryTable> queryTables, QueryTable queryTable) { public static String getColumnTableName(List<QueryTable> queryTables, QueryTable queryTable) {
if (queryTables == null) { if (queryTables == null) {
return ""; return "";