use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class SqlSelectBuilder method generateSelectForContentProvider.
/**
* <p>
* Generate select used in content provider class.
* </p>
*
* @param elementUtils
* @param builder
* @param method
* @param selectResultType
*/
private static void generateSelectForContentProvider(Builder builder, final SQLiteModelMethod method, SelectType selectResultType) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
final SQLiteEntity entity = daoDefinition.getEntity();
final Set<String> columns = new LinkedHashSet<>();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.contentProviderMethodName);
// params
methodBuilder.addParameter(ParameterSpec.builder(Uri.class, "uri").build());
methodBuilder.addParameter(ParameterSpec.builder(ArrayTypeName.of(String.class), "projection").build());
methodBuilder.addParameter(ParameterSpec.builder(String.class, "selection").build());
methodBuilder.addParameter(ParameterSpec.builder(ArrayTypeName.of(String.class), "selectionArgs").build());
methodBuilder.addParameter(ParameterSpec.builder(String.class, "sortOrder").build());
methodBuilder.returns(Cursor.class);
SqlBuilderHelper.generateLogForContentProviderBeginning(method, methodBuilder);
JQLChecker jqlChecker = JQLChecker.getInstance();
SplittedSql splittedSql = generateSQL(method, methodBuilder, true);
List<JQLPlaceHolder> placeHolders = jqlChecker.extractFromVariableStatement(method, splittedSql.sqlWhereStatement);
// remove placeholder for dynamic where, we are not interested here
placeHolders = SqlBuilderHelper.removeDynamicPlaceHolder(placeHolders);
AssertKripton.assertTrue(placeHolders.size() == method.contentProviderUriVariables.size(), "In '%s.%s' content provider URI path variables and variables in where conditions are different. If SQL uses parameters, they must be defined in URI path.", daoDefinition.getName(), method.getName());
Set<JQLProjection> projectedColumns = jqlChecker.extractProjections(method, method.jql.value, entity);
for (JQLProjection item : projectedColumns) {
if (item.type == ProjectionType.COLUMN) {
columns.add(entity.get(item.column.trim()).columnName);
} else {
columns.add(item.expression.trim());
}
}
methodBuilder.addStatement("$T _contentValues=contentValues()", KriptonContentValues.class);
methodBuilder.addStatement("$T _sqlBuilder=sqlBuilder()", StringBuilder.class);
SqlModifyBuilder.generateInitForDynamicWhereVariables(method, methodBuilder, "selection", "selectionArgs");
methodBuilder.addStatement("$T _projectionBuffer=new $T()", StringBuilder.class, StringBuilder.class);
if (method.jql.isOrderBy()) {
methodBuilder.addStatement("String _sortOrder=sortOrder");
}
methodBuilder.addStatement("_sqlBuilder.append($S)", splittedSql.sqlBasic);
SqlBuilderHelper.generateWhereCondition(methodBuilder, method, false);
generateDynamicPartOfQuery(method, methodBuilder, splittedSql);
// generate and check columns
{
methodBuilder.addCode("\n// manage projected columns\n");
methodBuilder.addStatement("String _columnSeparator=\"\"");
methodBuilder.beginControlFlow("if (projection!=null && projection.length>0)");
// generate projected column check
String columnCheckSetName = SqlBuilderHelper.generateColumnCheckSet(builder, method, columns);
SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "projection", true, new OnColumnListener() {
@Override
public void onColumnCheck(MethodSpec.Builder methodBuilder, String projectedColumnVariable) {
methodBuilder.addStatement("_projectionBuffer.append(_columnSeparator + $L)", projectedColumnVariable);
methodBuilder.addStatement("_columnSeparator=\", \"");
}
});
methodBuilder.nextControlFlow("else");
methodBuilder.beginControlFlow("for (String column: $L)", columnCheckSetName);
methodBuilder.addStatement("_projectionBuffer.append(_columnSeparator + column)");
methodBuilder.addStatement("_columnSeparator=\", \"");
methodBuilder.endControlFlow();
methodBuilder.endControlFlow();
int i = 0;
// every controls was done in constructor of SQLiteModelMethod
for (ContentUriPlaceHolder variable : method.contentProviderUriVariables) {
AssertKripton.assertTrue(SqlBuilderHelper.validate(variable.value, placeHolders, i), "In '%s.%s' content provider URI path and where conditions must use same set of variables", daoDefinition.getName(), method.getName());
SQLProperty entityProperty = entity.get(variable.value);
TypeName methodParameterType = method.findParameterTypeByAliasOrName(variable.value);
methodBuilder.addCode("// Add parameter $L at path segment $L\n", variable.value, variable.pathSegmentIndex);
// methodBuilder.addStatement("_sqlWhereParams.add(uri.getPathSegments().get($L))",
// variable.pathSegmentIndex);
methodBuilder.addStatement("_contentValues.addWhereArgs(uri.getPathSegments().get($L))", variable.pathSegmentIndex);
if (entityProperty != null) {
AssertKripton.assertTrue(TypeUtility.isTypeIncludedIn(entityProperty.getPropertyType().getTypeName(), String.class, Long.class, Long.TYPE), "In '%s.%s' content provider URI path variables %s must be String of Long type", daoDefinition.getName(), method.getName(), entityProperty.getName());
} else if (methodParameterType != null) {
AssertKripton.assertTrue(TypeUtility.isTypeIncludedIn(methodParameterType, String.class, Long.class, Long.TYPE), "In '%s.%s' content provider URI path variables %s must be String of Long type", daoDefinition.getName(), method.getName(), method.findParameterNameByAlias(variable.value));
}
i++;
}
}
// _sql must be always defined
methodBuilder.addStatement("String _sql=String.format(_sqlBuilder.toString(), _projectionBuffer.toString())");
SqlBuilderHelper.generateLogForSQL(method, methodBuilder);
SqlBuilderHelper.generateLogForWhereParameters(method, methodBuilder);
methodBuilder.addCode("\n// execute query\n");
// methodBuilder.addStatement("Cursor _result =
// database().rawQuery(_sql, _sqlWhereParams.toArray(new
// String[_sqlWhereParams.size()]))");
methodBuilder.addStatement("Cursor _result = database().rawQuery(_sql, _contentValues.whereArgsAsArray())");
methodBuilder.addStatement("return _result");
// we add at last javadoc, because need info is built at last.
SqlBuilderHelper.generateJavaDocForContentProvider(method, methodBuilder);
methodBuilder.addJavadoc("@param uri $S\n", method.contentProviderUriTemplate.replace("*", "[*]"));
methodBuilder.addJavadoc("@param selection dynamic part of <code>where</code> statement $L\n", method.hasDynamicWhereConditions() ? "" : "<b>NOT USED</b>");
methodBuilder.addJavadoc("@param selectionArgs arguments of dynamic part of <code>where</code> statement $L\n", method.hasDynamicWhereConditions() ? "" : "<b>NOT USED</b>");
methodBuilder.addJavadoc("@return number of effected rows\n");
builder.addMethod(methodBuilder.build());
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class SqlSelectBuilder method convertJQL2SQL.
/**
* @param schema
* @param entity
* @param method
* @param jqlChecker
* @return
*/
public static String convertJQL2SQL(final SQLiteModelMethod method, final boolean replaceWithQuestion) {
JQLChecker jqlChecker = JQLChecker.getInstance();
// convert jql to sql
String sql = jqlChecker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onBindParameter(String bindParameterName) {
if (replaceWithQuestion) {
return "?";
}
return "${" + bindParameterName + "}";
}
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
});
return sql;
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker 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.jql.JQLChecker in project kripton by xcesco.
the class SqlBuilderHelper method generateSQLForInsertDynamic.
/**
* <p>
* Generate log for INSERT operations
* </p>
*
* @param method
* @param methodBuilder
*/
public static void generateSQLForInsertDynamic(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
methodBuilder.addComment("generate SQL for insert");
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 onBindParameter(String bindParameterName) {
return "?";
}
});
final One<Integer> counter = new One<Integer>(0);
sql = checker.replaceVariableStatements(method, sql, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onColumnNameSet(String statement) {
counter.value0++;
return "%s";
}
@Override
public String onColumnValueSet(String statement) {
counter.value0++;
return "%s";
}
});
if (counter.value0 == 2) {
methodBuilder.addStatement("String _sql=String.format($S, _contentValues.keyList(), _contentValues.keyValueList())", sql);
} else {
methodBuilder.addStatement("String _sql=String.format($S, _contentValues.keyList())", sql);
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class SqlBuilderHelper method orderContentValues.
public static List<Pair<String, TypeName>> orderContentValues(final SQLiteModelMethod method, final List<Pair<String, TypeName>> updateableParams) {
final List<Pair<String, TypeName>> result = new ArrayList<Pair<String, TypeName>>();
JQLChecker checker = JQLChecker.getInstance();
final One<Boolean> inserMode = new One<Boolean>(false);
checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {
// used in update
@Override
public String onColumnNameToUpdate(String columnName) {
String column = currentEntity.findPropertyByName(columnName).columnName;
for (Pair<String, TypeName> item : updateableParams) {
String paramNameInQuery = method.findParameterAliasByName(item.value0);
if (paramNameInQuery.equalsIgnoreCase(columnName)) {
result.add(item);
break;
}
}
return column;
}
// used in insert
@Override
public void onColumnNameSetBegin(Column_name_setContext ctx) {
inserMode.value0 = true;
}
@Override
public void onColumnNameSetEnd(Column_name_setContext ctx) {
inserMode.value0 = false;
}
@Override
public String onColumnName(String columnName) {
if (!inserMode.value0)
return columnName;
String column = currentEntity.findPropertyByName(columnName).columnName;
for (Pair<String, TypeName> item : updateableParams) {
String paramNameInQuery = method.findParameterAliasByName(item.value0);
if (paramNameInQuery.equalsIgnoreCase(columnName)) {
result.add(item);
break;
}
}
return column;
}
});
return result;
}
Aggregations