Search in sources :

Example 21 with JQLReplacerListenerImpl

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl in project kripton by xcesco.

the class InsertRawHelper method generateJavaDoc.

/**
 * @param methodBuilder
 * @param method
 * @param returnType
 * @return string sql
 */
public String generateJavaDoc(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method, TypeName returnType) {
    final SQLiteDaoDefinition daoDefinition = method.getParent();
    final SQLiteEntity entity = daoDefinition.getEntity();
    final One<Boolean> inColumnValues = new One<Boolean>(false);
    final List<Pair<String, TypeName>> methodParamsUsedAsColumnValue = new ArrayList<>();
    final List<Pair<String, TypeName>> methodParamsUsedAsParameter = new ArrayList<>();
    // transform JQL to SQL
    String sqlInsert = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public void onColumnValueSetBegin(Column_value_setContext ctx) {
            inColumnValues.value0 = true;
        }

        @Override
        public void onColumnValueSetEnd(Column_value_setContext ctx) {
            inColumnValues.value0 = false;
        }

        @Override
        public String onColumnName(String columnName) {
            Set<SQLProperty> property = currentSchema.getPropertyBySimpleName(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(property != null, method, columnName);
            SQLProperty tempProperty = property.iterator().next();
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
            if (inColumnValues.value0) {
                methodParamsUsedAsColumnValue.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
            } else {
                methodParamsUsedAsParameter.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
            }
            return "${" + resolvedParamName + "}";
        }
    });
    methodBuilder.addJavadoc("<h2>SQL insert</h2>\n");
    methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlInsert);
    methodBuilder.addJavadoc("\n");
    if (methodParamsUsedAsColumnValue.size() > 0) {
        // list of inserted fields
        methodBuilder.addJavadoc("<h2>Inserted columns:</strong></h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (Pair<String, TypeName> property : methodParamsUsedAsColumnValue) {
            // String resolvedName = method.findParameterAliasByName(property.value0);
            String resolvedName = method.findParameterNameByAlias(property.value0);
            /*SQLProperty prop = entity.get(resolvedName);
								
				if (prop == null)
					throw (new PropertyNotFoundException(method, property.value0, property.value1));*/
            methodBuilder.addJavadoc("\t<dt>$L</dt>", property.value0);
            methodBuilder.addJavadoc("<dd>is binded to query's parameter <strong>$L</strong> and method's parameter <strong>$L</strong></dd>\n", "${" + property.value0 + "}", resolvedName);
        }
        methodBuilder.addJavadoc("</dl>\n\n");
    }
    // list of parameters
    if (methodParamsUsedAsParameter.size() > 0) {
        methodBuilder.addJavadoc("<h2>Method parameters used as sql parameters</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (Pair<String, TypeName> property : methodParamsUsedAsParameter) {
            String resolvedName = method.findParameterNameByAlias(property.value0);
            methodBuilder.addJavadoc("\t<dt>$L</dt>", resolvedName);
            methodBuilder.addJavadoc("<dd>is binded to query's parameter <strong>$${$L}</strong></dd>\n", property.value0);
        }
        methodBuilder.addJavadoc("</dl>\n\n");
    }
    for (Pair<String, TypeName> param : method.getParameters()) {
        if (methodParamsUsedAsColumnValue.contains(param)) {
            methodBuilder.addJavadoc("@param $L\n", param.value0);
            if (entity.get(method.findParameterAliasByName(param.value0)) != null) {
                methodBuilder.addJavadoc("\tis binded to column value <strong>$L</strong>\n", entity.get(method.findParameterAliasByName(param.value0)).columnName);
            } else {
                // in case of JQL explicit, you can declare name of parameter
                methodBuilder.addJavadoc("\tis binded to query parameter <strong>$L</strong>\n", param.value0);
            }
        }
        if (methodParamsUsedAsParameter.contains(param)) {
            methodBuilder.addJavadoc("@param $L\n", param.value0);
            methodBuilder.addJavadoc("\tis used as parameter\n");
        }
    }
    generateJavaDocReturnType(methodBuilder, returnType);
    return sqlInsert;
}
Also used : TypeName(com.squareup.javapoet.TypeName) Set(java.util.Set) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) Column_value_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) Pair(com.abubusoft.kripton.common.Pair)

Aggregations

JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)21 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)13 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)13 One (com.abubusoft.kripton.common.One)12 ArrayList (java.util.ArrayList)9 TypeName (com.squareup.javapoet.TypeName)7 Pair (com.abubusoft.kripton.common.Pair)6 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)6 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)5 JQLDynamicStatementType (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType)5 JQLReplaceVariableStatementListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl)5 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)5 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)5 BaseProcessorTest (base.BaseProcessorTest)4 JQLPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder)4 Test (org.junit.Test)4 JQLContext (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3