use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl in project kripton by xcesco.
the class ModifyRawHelper method generateJavaDoc.
/**
* @param daoDefinition
* @param method
* @param methodBuilder
* @param updateMode
* @param whereCondition
* @param where
* @param methodParams
* @param updateableParams
*
* @return sql generated
*/
private void generateJavaDoc(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder, boolean updateMode, String whereCondition, Pair<String, List<Pair<String, TypeName>>> where, List<Pair<String, TypeName>> methodParams) {
final List<SQLProperty> updatedProperties = new ArrayList<>();
final One<Boolean> onWhereStatement = new One<Boolean>(false);
String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
onWhereStatement.value0 = true;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
onWhereStatement.value0 = false;
}
@Override
public String onColumnNameToUpdate(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
updatedProperties.add(tempProperty);
return tempProperty.columnName;
}
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
@Override
public String onBindParameter(String bindParameterName) {
String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
if (onWhereStatement.value0) {
return "${" + bindParameterName + "}";
} else {
return ":" + bindParameterName;
}
}
});
if (updateMode) {
methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
methodBuilder.addJavadoc("\n");
// list of updated fields
methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
methodBuilder.addJavadoc("<ul>\n");
for (SQLProperty property : updatedProperties) {
methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
// methodBuilder.addJavadoc("<dd>is binded to query's parameter
// <strong>$L</strong> and method's parameter
// <strong>$L</strong></dd>\n", "${" + resolvedName + "}",
// property.value0);
}
methodBuilder.addJavadoc("</ul>");
methodBuilder.addJavadoc("\n\n");
} else {
methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
methodBuilder.addJavadoc("\n\n");
}
// list of where parameter
methodBuilder.addJavadoc("<h2>Where parameters:</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
for (Pair<String, TypeName> property : where.value1) {
String rawName = method.findParameterNameByAlias(property.value0);
methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
}
methodBuilder.addJavadoc("</dl>");
methodBuilder.addJavadoc("\n\n");
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<dl>\n");
methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
methodBuilder.addJavadoc("\n</dl>");
methodBuilder.addJavadoc("\n\n");
}
// dynamic conditions
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
}
methodBuilder.addJavadoc("</dl>");
methodBuilder.addJavadoc("\n\n");
}
// method parameters
if (methodParams.size() > 0) {
for (Pair<String, TypeName> param : methodParams) {
String resolvedName = method.findParameterAliasByName(param.value0);
methodBuilder.addJavadoc("@param $L", param.value0);
if (method.isThisDynamicWhereConditionsName(param.value0)) {
methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
} else if (where.value1.contains(new Pair<>(resolvedName, param.value1))) {
methodBuilder.addJavadoc("\n\tis used as where parameter <strong>$L</strong>\n", "${" + resolvedName + "}");
} else {
methodBuilder.addJavadoc("\n\tis used as updated field <strong>$L</strong>\n", resolvedName);
}
}
}
// if true, field must be associate to ben attributes
TypeName returnType = method.getReturnClass();
// define return value
if (returnType == TypeName.VOID) {
} else {
methodBuilder.addJavadoc("\n");
if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
if (updateMode) {
methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
} else {
methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
}
// methodBuilder.addCode("return result!=0;\n");
} else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
if (updateMode) {
methodBuilder.addJavadoc("@return number of updated records");
} else {
methodBuilder.addJavadoc("@return number of deleted records");
}
// methodBuilder.addCode("return result;\n");
} else {
// more than one listener found
throw (new InvalidMethodSignException(method, "invalid return type"));
}
methodBuilder.addJavadoc("\n");
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl in project kripton by xcesco.
the class ModifyRawHelper method generateJavaDoc.
private void generateJavaDoc(final SQLiteModelMethod method, Builder methodBuilder, boolean updateMode) {
List<Pair<String, TypeName>> methodParams = method.getParameters();
final List<SQLProperty> updatedProperties = new ArrayList<>();
final List<Pair<String, TypeName>> methodParamsUsedAsParameter = new ArrayList<>();
// new
String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnNameToUpdate(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
updatedProperties.add(tempProperty);
return tempProperty.columnName;
}
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
@Override
public String onBindParameter(String bindParameterName) {
String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
methodParamsUsedAsParameter.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
return "${" + bindParameterName + "}";
}
});
if (updateMode) {
methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
methodBuilder.addJavadoc("\n");
// list of updated fields
methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
methodBuilder.addJavadoc("<ul>\n");
for (SQLProperty property : updatedProperties) {
methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
}
methodBuilder.addJavadoc("</ul>");
methodBuilder.addJavadoc("\n\n");
} else {
methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
methodBuilder.addJavadoc("\n\n");
}
// list of where parameter
methodBuilder.addJavadoc("<h2>Parameters:</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
for (Pair<String, TypeName> property : methodParamsUsedAsParameter) {
String rawName = method.findParameterNameByAlias(property.value0);
methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
}
methodBuilder.addJavadoc("</dl>");
methodBuilder.addJavadoc("\n\n");
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<dl>\n");
methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
methodBuilder.addJavadoc("\n</dl>");
methodBuilder.addJavadoc("\n\n");
}
// dynamic conditions
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
}
methodBuilder.addJavadoc("</dl>");
methodBuilder.addJavadoc("\n\n");
}
// method parameters
if (methodParams.size() > 0) {
for (Pair<String, TypeName> param : methodParams) {
String resolvedName = method.findParameterAliasByName(param.value0);
methodBuilder.addJavadoc("@param $L", param.value0);
if (method.isThisDynamicWhereConditionsName(param.value0)) {
methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
} else {
methodBuilder.addJavadoc("\n\tis used as for parameter <strong>$L</strong>\n", resolvedName);
}
}
}
// if true, field must be associate to ben attributes
TypeName returnType = method.getReturnClass();
// define return value
if (returnType == TypeName.VOID) {
} else {
methodBuilder.addJavadoc("\n");
if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
if (updateMode) {
methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
} else {
methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
}
methodBuilder.addCode("return result!=0;\n");
} else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
if (updateMode) {
methodBuilder.addJavadoc("@return number of updated records");
} else {
methodBuilder.addJavadoc("@return number of deleted records");
}
// methodBuilder.addCode("return result;\n");
} else {
// more than one listener found
throw (new InvalidMethodSignException(method, "invalid return type"));
}
methodBuilder.addJavadoc("\n");
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl in project kripton by xcesco.
the class SqlBuilderHelper method generateWhereCondition.
/**
* <p>
* Generate where management code.
* </p>
*
* <h2>pre condition</h2>
* <dl>
* <dt>_sqlDynamicWhere</dt>
* <dd>dynamic where</dd>
* </dl>
*
* <h2>post conditions</h2>
* <dl>
* <dt>_sqlWhereParams</dt>
* <dd>ArraList</dd>
* <dt>_sqlWhereStatement</dt>
* <dd>String</dd>
* </dl>
*
* @param methodBuilder
* @param method
* @param jql
* @param jqlChecker
* @param sqlWhereStatement
*/
public static void generateWhereCondition(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method, boolean sqlWhereParamsAlreadyDefined) {
final JQL jql = method.jql;
final JQLChecker jqlChecker = JQLChecker.getInstance();
// we need always this
if (!sqlWhereParamsAlreadyDefined) {
// methodBuilder.addStatement("$T<String>
// _sqlWhereParams=getWhereParamsArray()", ArrayList.class);
}
if (jql.isWhereConditions()) {
// parameters extracted from query
final One<String> whereStatement = new One<>();
final One<Boolean> alreadyFoundWhereStatement = new One<>(false);
// put in whereStatement value of where statement.
jqlChecker.replaceVariableStatements(method, method.jql.value, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onWhere(String statement) {
if (alreadyFoundWhereStatement.value0 == false) {
whereStatement.value0 = statement;
alreadyFoundWhereStatement.value0 = true;
return "";
} else {
// DO NOTHING
return null;
}
}
});
methodBuilder.addCode("\n// manage WHERE arguments -- BEGIN\n");
String sqlWhere = jqlChecker.replaceFromVariableStatement(method, whereStatement.value0, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
@Override
public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
return null;
}
@Override
public String onBindParameter(String bindParameterName) {
return "?";
}
});
methodBuilder.addCode("\n// manage WHERE statement\n");
String value = sqlWhere;
String valueToReplace = jql.dynamicReplace.get(JQLDynamicStatementType.DYNAMIC_WHERE);
if (method.jql.operationType == JQLType.SELECT) {
// we have to include WHERE keywords
if (jql.isStaticWhereConditions() && !jql.isDynamicWhereConditions()) {
// case static statement and NO dynamic
methodBuilder.addStatement("String _sqlWhereStatement=$S", value);
} else if (jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
methodBuilder.addStatement("String _sqlWhereStatement=$S+$T.ifNotEmptyAppend($L,\" $L \")", value.replace(valueToReplace, ""), StringUtils.class, "_sqlDynamicWhere", method.dynamicWherePrepend);
} else if (!jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
methodBuilder.addStatement("String _sqlWhereStatement=$T.ifNotEmptyAppend($L, \" $L \")", StringUtils.class, "_sqlDynamicWhere", JQLKeywords.WHERE_KEYWORD);
}
} else {
// we DON'T have to include WHERE keywords
value = value.replace(" " + JQLKeywords.WHERE_KEYWORD, "");
if (jql.isStaticWhereConditions() && !jql.isDynamicWhereConditions()) {
// case static statement and NO dynamic
methodBuilder.addStatement("String _sqlWhereStatement=$S", value);
} else if (jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
methodBuilder.addStatement("String _sqlWhereStatement=$S+$T.ifNotEmptyAppend($L,\" $L \")", value.replace(valueToReplace, ""), StringUtils.class, "_sqlDynamicWhere", method.dynamicWherePrepend);
} else if (!jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
methodBuilder.addStatement("String _sqlWhereStatement=$T.ifNotEmptyAppend($L, \" \")", StringUtils.class, "_sqlDynamicWhere");
}
}
methodBuilder.addStatement("_sqlBuilder.append($L)", "_sqlWhereStatement");
methodBuilder.addCode("\n// manage WHERE arguments -- END\n");
} else {
// in every situation we need it
methodBuilder.addStatement("String _sqlWhereStatement=\"\"");
}
// manage where arguments
if (method.hasDynamicWhereConditions() && method.hasDynamicWhereArgs()) {
// ASSERT: only with dynamic where conditions
methodBuilder.beginControlFlow("if ($T.hasText(_sqlDynamicWhere) && _sqlDynamicWhereArgs!=null)", StringUtils.class);
if (method.hasDynamicWhereConditions()) {
methodBuilder.beginControlFlow("for (String _arg: _sqlDynamicWhereArgs)");
// methodBuilder.addStatement("_sqlWhereParams.add(_arg)");
methodBuilder.addStatement("_contentValues.addWhereArgs(_arg)");
methodBuilder.endControlFlow();
}
methodBuilder.endControlFlow();
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl in project kripton by xcesco.
the class SqlBuilderHelper method generateSQLForStaticQuery.
/**
* <p>
* Generate log for INSERT operations
* </p>
*
* @param method
* @param methodBuilder
*/
public static void generateSQLForStaticQuery(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
methodBuilder.addComment("generate static SQL for statement");
JQLChecker checker = JQLChecker.getInstance();
// replace the table name, other pieces will be removed
String sql = checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnNameToUpdate(String columnName) {
return onColumnName(columnName);
}
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = method.getParent().getEntity().get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
@Override
public String onBindParameter(String bindParameterName) {
return "?";
}
});
methodBuilder.addStatement("String _sql=$S", sql);
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl in project kripton by xcesco.
the class SqlModifyBuilder method generateLogForModifiers.
/**
* generate sql log
*
* @param method
* @param methodBuilder
* @param schema
* @param entity
* @param jqlChecker
*/
public static void generateLogForModifiers(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
JQLChecker jqlChecker = JQLChecker.getInstance();
final One<Boolean> usedInWhere = new One<Boolean>(false);
methodBuilder.addCode("\n// display log\n");
String sqlForLog = jqlChecker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnNameToUpdate(String columnName) {
// only entity's columns
return currentEntity.findPropertyByName(columnName).columnName;
}
@Override
public String onColumnName(String columnName) {
// return entity.findByName(columnName).columnName;
return currentSchema.findColumnNameByPropertyName(method, columnName);
}
@Override
public String onBindParameter(String bindParameterName) {
if (usedInWhere.value0) {
return "?";
} else {
String paramName = bindParameterName;
if (paramName.contains(".")) {
String[] a = paramName.split("\\.");
if (a.length == 2) {
paramName = a[1];
}
}
SQLProperty property = currentEntity.findPropertyByName(paramName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(property != null, method, bindParameterName);
return ":" + property.columnName;
}
}
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
usedInWhere.value0 = true;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
usedInWhere.value0 = false;
}
});
if (method.jql.dynamicReplace.containsKey(JQLDynamicStatementType.DYNAMIC_WHERE)) {
methodBuilder.addStatement("$T.info($S, $L)", Logger.class, sqlForLog.replace(method.jql.dynamicReplace.get(JQLDynamicStatementType.DYNAMIC_WHERE), "%s"), "StringUtils.ifNotEmptyAppend(_sqlDynamicWhere,\" AND \")");
} else {
methodBuilder.addStatement("$T.info($S)", Logger.class, sqlForLog);
}
}
Aggregations