use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker 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.JQLChecker 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.JQLChecker 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);
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class TestJqlChecker method testOK.
/**
* <p>
* OK
* </p>
*
* @throws Throwable
*/
@Test
public void testOK() throws Throwable {
String sql = "SELECT id, action, number, countryCode, contactName, contactId FROM phone_number WHERE number = ${bean.number} and number like ${bean.number} || '%' and #{" + JQLDynamicStatementType.DYNAMIC_WHERE + "}";
log(sql);
JQL jql = new JQL();
jql.value = sql;
JQLChecker jsqChecker = JQLChecker.getInstance();
jsqChecker.analyze(dummyContext, jql, new JqlBaseListener() {
@Override
public void enterBind_parameter(Bind_parameterContext ctx) {
TestJqlChecker.this.log("xx parameter name %s", ctx.bind_parameter_name().getText());
}
@Override
public void enterBind_dynamic_sql(Bind_dynamic_sqlContext ctx) {
TestJqlChecker.this.log("xx dynamic %s", ctx.bind_parameter_name().getText());
}
@Override
public void enterColumn_name(Column_nameContext ctx) {
super.enterColumn_name(ctx);
log("column " + ctx.getText());
}
});
jsqChecker.extractPlaceHoldersAsList(new JQLContext() {
@Override
public String getContextDescription() {
return "test context";
}
}, jql.value);
log("replaced " + jsqChecker.replace(dummyContext, jql, new JQLReplacerListenerImpl(null) {
@Override
public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
return String.format("\"+%s+\"", dynamicStatement);
}
@Override
public String onBindParameter(String bindParameterName) {
return "?";
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
// TODO Auto-generated method stub
return null;
}
}));
log("aa");
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class TestJqlChecker method testUpdate01.
@Test
public void testUpdate01() {
String sql = "UPDATE channel SET uid=${ bean.field1}, owner_uid=${bean.field2}, update_time=${bean.field3}, name=${field4} WHERE id=${bean.field1}";
String sqlForLog = "UPDATE channel SET uid=?, owner_uid=?, update_time=?, name=? WHERE id=?";
JQL jql = new JQL();
jql.value = sql;
JQLChecker checker = JQLChecker.getInstance();
// verify sql
checker.verify(dummyContext, jql);
// check bind parameters
{
List<JQLPlaceHolder> aspected = new ArrayList<>();
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field2"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field3"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field4"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
checkCollectionExactly(actual, aspected);
}
// prepare for log
String sqlLogResult = checker.replace(dummyContext, jql, new JQLReplacerListenerImpl(null) {
@Override
public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
return String.format("\"+%s+\"", dynamicStatement);
}
@Override
public String onBindParameter(String bindParameterName) {
return "?";
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
// TODO Auto-generated method stub
return null;
}
});
assertEquals("sql for log generation failed", sqlForLog, sqlLogResult);
}
Aggregations