Search in sources :

Example 1 with MethodParameterNotFoundException

use of com.abubusoft.kripton.processor.exceptions.MethodParameterNotFoundException in project kripton by xcesco.

the class SqlAnalyzer method execute.

/**
 * Extract from value string every placeholder ${}, replace it with ? and then convert every field typeName with column typeName. The result is a pair: the first value is the elaborated string. The second is the list of parameters associated to
 * ?. This second parameter is the list of parameters and replaced with ?.
 */
public void execute(Elements elementUtils, SQLiteModelMethod method, String sqlStatement) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    usedMethodParameters = new HashSet<String>();
    paramNames = new ArrayList<String>();
    paramGetters = new ArrayList<String>();
    usedBeanPropertyNames = new ArrayList<String>();
    paramTypeNames = new ArrayList<TypeName>();
    // replace placeholder ${ } with ?
    {
        Matcher matcher = PARAMETER.matcher(sqlStatement);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "?");
            paramNames.add(matcher.group(1));
        }
        matcher.appendTail(buffer);
        sqlStatement = buffer.toString();
    }
    // replace property typeName to column typeName
    {
        Matcher matcher = WORD.matcher(sqlStatement);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            SQLProperty property = entity.findPropertyByName(matcher.group(1));
            if (property != null) {
                matcher.appendReplacement(buffer, property.columnName);
            }
        }
        matcher.appendTail(buffer);
        sqlStatement = buffer.toString();
    }
    TypeName rawNameType;
    // analyze parametersName
    String effectiveName;
    for (String rawName : paramNames) {
        JQLParameterName pName = JQLParameterName.parse(rawName);
        if (!pName.isNested()) {
            effectiveName = method.findParameterNameByAlias(pName.getValue());
            rawNameType = method.findParameterTypeByAliasOrName(effectiveName);
            if (rawNameType == null) {
                throw new MethodParameterNotFoundException(method, effectiveName);
            }
            paramGetters.add(effectiveName);
            paramTypeNames.add(rawNameType);
            usedMethodParameters.add(effectiveName);
            usedBeanPropertyNames.add(null);
        } else {
            if (method.findParameterTypeByAliasOrName(pName.getBeanName()) == null) {
                throw new MethodParameterNotFoundException(method, pName.getBeanName());
            }
            if (TypeUtility.isEquals(method.findParameterTypeByAliasOrName(pName.getBeanName()), entity) && entity.contains(pName.getValue())) {
                // there are nested property invocation
                paramGetters.add(method.findParameterNameByAlias(pName.getBeanName()) + "." + getter(entity.findPropertyByName(pName.getValue())));
                usedBeanPropertyNames.add(pName.getValue());
                paramTypeNames.add(TypeUtility.typeName(entity.findPropertyByName(pName.getValue()).getElement().asType()));
                usedMethodParameters.add(method.findParameterNameByAlias(pName.getBeanName()));
            } else {
                throw (new PropertyInAnnotationNotFoundException(method, pName.getValue()));
            }
        }
    // } else {
    // throw (new PropertyInAnnotationNotFoundException(method, rawName));
    // }
    }
    this.sqlStatement = sqlStatement;
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLParameterName(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker.JQLParameterName) Matcher(java.util.regex.Matcher) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) PropertyInAnnotationNotFoundException(com.abubusoft.kripton.processor.exceptions.PropertyInAnnotationNotFoundException) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) MethodParameterNotFoundException(com.abubusoft.kripton.processor.exceptions.MethodParameterNotFoundException)

Aggregations

MethodParameterNotFoundException (com.abubusoft.kripton.processor.exceptions.MethodParameterNotFoundException)1 PropertyInAnnotationNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyInAnnotationNotFoundException)1 JQLParameterName (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker.JQLParameterName)1 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)1 TypeName (com.squareup.javapoet.TypeName)1 Matcher (java.util.regex.Matcher)1