use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext in project kripton by xcesco.
the class ModifyBeanHelper method extractSQLForJavaDoc.
private String extractSQLForJavaDoc(final SQLiteModelMethod method) {
final One<Boolean> usedInWhere = new One<>(false);
String sqlForJavaDoc = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnNameToUpdate(String columnName) {
return currentEntity.findPropertyByName(columnName).columnName;
}
@Override
public String onColumnName(String columnName) {
return currentEntity.findPropertyByName(columnName).columnName;
}
@Override
public String onBindParameter(String bindParameterName) {
if (!usedInWhere.value0) {
if (bindParameterName.contains(".")) {
String[] a = bindParameterName.split("\\.");
if (a.length == 2) {
bindParameterName = a[1];
}
}
return ":" + bindParameterName;
} else {
return null;
}
}
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
usedInWhere.value0 = true;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
usedInWhere.value0 = false;
}
});
return sqlForJavaDoc;
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext in project kripton by xcesco.
the class SqlBuilderHelper method generateLog.
/**
* <p>
* Generate log for INSERT operations
* </p>
*
* @param method
* @param methodBuilder
*/
public static void generateLog(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
SQLiteDaoDefinition daoDefinition = method.getParent();
// log is enabled
if (daoDefinition.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (_context.isLogEnabled())");
methodBuilder.addCode("// log for insert -- BEGIN \n");
JQLChecker checker = JQLChecker.getInstance();
final One<Boolean> inWhere = new One<Boolean>(false);
String sql = checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onBindParameter(String bindParameterName) {
if (inWhere.value0) {
return "?";
}
return null;
}
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
super.onWhereStatementBegin(ctx);
inWhere.value0 = true;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
super.onWhereStatementEnd(ctx);
inWhere.value0 = false;
}
});
if (method.jql.containsSelectOperation) {
// log
// manage log
methodBuilder.addCode("\n");
methodBuilder.addStatement("$T.info($S)", Logger.class, sql);
} else {
sql = checker.replaceVariableStatements(method, sql, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onColumnNameSet(String statement) {
return "%s";
}
@Override
public String onColumnValueSet(String statement) {
return "%s";
}
});
methodBuilder.addStatement("$T _columnNameBuffer=new $T()", StringBuffer.class, StringBuffer.class);
methodBuilder.addStatement("$T _columnValueBuffer=new $T()", StringBuffer.class, StringBuffer.class);
methodBuilder.addStatement("String _columnSeparator=$S", "");
SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "_contentValues.keys()", false, new OnColumnListener() {
@Override
public void onColumnCheck(MethodSpec.Builder methodBuilder, String columNameVariable) {
methodBuilder.addStatement("_columnNameBuffer.append(_columnSeparator+$L)", columNameVariable);
methodBuilder.addStatement("_columnValueBuffer.append(_columnSeparator+$S+$L)", ":", columNameVariable);
methodBuilder.addStatement("_columnSeparator=$S", ", ");
}
});
methodBuilder.addStatement("$T.info($S, _columnNameBuffer.toString(), _columnValueBuffer.toString())", Logger.class, sql);
}
generateLogForContentValues(method, methodBuilder);
methodBuilder.addCode("// log for insert -- END \n\n");
SqlBuilderHelper.generateLogForWhereParameters(method, methodBuilder);
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext in project kripton by xcesco.
the class GenericSQLHelper method generateGenericExecSQL.
/**
* @param methodBuilder
* @param method
* @param daoDefinition
* @param schema
*/
public static void generateGenericExecSQL(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
boolean nullable;
final List<String> paramsList = new ArrayList<String>();
final List<String> contentValueList = new ArrayList<String>();
final One<Boolean> columnsToUpdate = new One<Boolean>(true);
String sql = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
super.onWhereStatementBegin(ctx);
columnsToUpdate.value0 = false;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
super.onWhereStatementEnd(ctx);
columnsToUpdate.value0 = true;
}
@Override
public String onColumnName(String columnName) {
String resolvedName = currentSchema.findColumnNameByPropertyName(method, columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(resolvedName != null, method, columnName);
return resolvedName;
}
@Override
public String onBindParameter(String bindParameterName) {
String propertyName = method.findParameterAliasByName(bindParameterName);
if (columnsToUpdate.value0) {
contentValueList.add(propertyName);
} else {
paramsList.add(propertyName);
}
return "?";
}
});
// update/insert columns
final SQLiteEntity entity = daoDefinition.getEntity();
for (String item : contentValueList) {
// ASSERT: property is always in entity
String propertyName = method.findParameterNameByAlias(item);
TypeName paramType = method.findParameterTypeByAliasOrName(item);
SQLProperty property = entity.get(item);
if (propertyName == null)
throw (new PropertyNotFoundException(method, propertyName, paramType));
Pair<String, TypeName> methodParam = new Pair<String, TypeName>(propertyName, paramType);
// check same type
TypeUtility.checkTypeCompatibility(method, methodParam, property);
if (method.isLogEnabled()) {
methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
} else {
methodBuilder.addCode("_contentValues.put(");
}
// it does not need to be converted in string
SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, methodParam.value0, methodParam.value1, property);
methodBuilder.addCode(");\n");
// if (nullable) {
// methodBuilder.nextControlFlow("else");
//
// if (method.isLogEnabled()) {
// methodBuilder.addStatement("_contentValues.putNull($S)", property.columnName);
// } else {
// methodBuilder.addStatement("_contentValues.putNull()");
// }
//
// methodBuilder.endControlFlow();
// }
}
// where condition
methodBuilder.addComment("build where condition");
{
// String separator = "";
TypeName paramType;
String realName;
for (String item : paramsList) {
methodBuilder.addCode("_contentValues.addWhereArgs(");
paramType = method.findParameterTypeByAliasOrName(item);
realName = method.findParameterNameByAlias(item);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(paramType != null, method, item);
// code for query arguments
nullable = TypeUtility.isNullable(paramType);
if (nullable) {
methodBuilder.addCode("($L==null?\"\":", realName);
}
// check for string conversion
TypeUtility.beginStringConversion(methodBuilder, paramType);
SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, realName, paramType, null);
// check for string conversion
TypeUtility.endStringConversion(methodBuilder, paramType);
if (nullable) {
methodBuilder.addCode(")");
}
methodBuilder.addCode(");\n");
}
}
// log for where parames
SqlBuilderHelper.generateLog(method, methodBuilder);
// log
methodBuilder.addCode("\n");
methodBuilder.addStatement("database().execSQL($S, _contentValues.whereArgsAsArray())", sql);
}
Aggregations