Fix SQLChecker for bind in lists and Bindable sql params

This commit is contained in:
Travis Burtrum 2019-03-12 00:34:23 -04:00
parent 1766a9af6d
commit 168186d43f
3 changed files with 21 additions and 5 deletions

View File

@ -654,7 +654,20 @@ public class JdbcMapperProcessor extends AbstractProcessor {
}
}
}
sqlChecker.checkSql(processingEnv, genClass, mapper, databaseType, eeMethod, sqlStatement, bindParams, arrayInList);
// todo: fix these awful hacks...
String sqlStatementToCheck = sqlStatement;
if (bindInList) {
// remove this bit entirely
sqlStatementToCheck = sqlStatementToCheck.replace("REPLACEMEWITHUNQUOTEDQUOTEPLZ + com.moparisthebest.jdbc.util.InListUtil.toInList(REPLACEMEWITHUNQUOTEDQUOTEPLZ", "");
// replace this with a bind in list that expects 1 param...
sqlStatementToCheck = sqlStatementToCheck.replaceAll("REPLACEMEWITHUNQUOTEDQUOTEPLZ, [^)]+\\) \\+ REPLACEMEWITHUNQUOTEDQUOTEPLZ", " IN (?) ");
}
if (sqlParam) {
// remove this entirely, like a blank string being sent in
sqlStatementToCheck = sqlStatementToCheck.replaceAll("REPLACEMEWITHUNQUOTEDQUOTEPLZ \\+ [^ ]+ \\+ REPLACEMEWITHUNQUOTEDQUOTEPLZ", "");
}
// end awful hacks
sqlChecker.checkSql(processingEnv, genClass, mapper, databaseType, eeMethod, sqlStatementToCheck, bindParams, arrayInList);
}
}

View File

@ -2,6 +2,7 @@ package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.ArrayInList;
import com.moparisthebest.jdbc.QueryMapper;
import com.moparisthebest.jdbc.util.Bindable;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
@ -104,6 +105,7 @@ public class SimpleSQLChecker implements SQLChecker {
if (param instanceof SpecialVariableElement) {
final SpecialVariableElement specialParam = (SpecialVariableElement) param;
switch (specialParam.specialType) {
case BIND_IN_LIST:
case IN_LIST: {
final TypeMirror componentType;
if (o.getKind() == TypeKind.ARRAY) {
@ -115,10 +117,11 @@ public class SimpleSQLChecker implements SQLChecker {
getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "invalid in-list-variable type, returning null", ((SpecialVariableElement) param).delegate);
return null;
}
// if arrayInList is null here, it's a BindInList, in which case we need to return 1 object to bind...
if (types.isAssignable(componentType, numberType)) {
return arrayInList.toArray(conn, true, new Long[]{0L}); // todo: not quite right, oh well for now
return arrayInList == null ? 0L : arrayInList.toArray(conn, true, new Long[]{0L}); // todo: not quite right, oh well for now
} else if (types.isAssignable(componentType, stringType) || types.isAssignable(componentType, enumType)) {
return arrayInList.toArray(conn, false, new String[]{defaultString});
return arrayInList == null ? defaultString : arrayInList.toArray(conn, false, new String[]{defaultString});
} else {
getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "invalid in-list-variable type, returning null", ((SpecialVariableElement) param).delegate);
return null;
@ -129,7 +132,7 @@ public class SimpleSQLChecker implements SQLChecker {
case CLOB:
return new StringReader(defaultString);
case SQL:
return specialParam.blobStringCharset == null ? "" : specialParam.blobStringCharset;
return Bindable.empty;
}
}
// end special behavior

View File

@ -136,7 +136,7 @@ class SpecialVariableElement implements VariableElement {
@Override
public String toString() {
return "InListVariableElement{" +
return "SpecialVariableElement{" +
"delegate=" + delegate +
'}';
}