use of com.abubusoft.kripton.processor.sqlite.transform.SQLTransform in project kripton by xcesco.
the class SelectScalarHelper method generateSpecializedPart.
/*
* (non-Javadoc)
*
* @see com.abubusoft.kripton.processor.sqlite.SQLiteSelectBuilder.
* SelectCodeGenerator#generate(com.squareup.javapoet.MethodSpec.Builder)
*/
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
TypeName returnTypeName = method.getReturnClass();
// ASSERT: returnType is a supported type
// no column or too many columns
AssertKripton.assertTrueOrInvalidMethodSignException(fieldList.size() == 1, method, "no way to understand which field is the result for this method");
SQLTransform t = SQLTransformer.lookup(returnTypeName);
methodBuilder.addCode("$T result=", returnTypeName);
t.generateDefaultValue(methodBuilder);
methodBuilder.addCode(";\n");
methodBuilder.addCode("\n");
// methodBuilder.beginControlFlow("try");
methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
// generate index from columns
methodBuilder.addCode("\n");
if (TypeUtility.isNullable(returnTypeName)) {
methodBuilder.addCode("if (_cursor.isNull(0)) { return null; }\n");
} else {
methodBuilder.addCode("if (_cursor.isNull(0)) { return ");
t.generateDefaultValue(methodBuilder);
methodBuilder.addCode("; }\n", t);
}
methodBuilder.addCode("result=");
t.generateReadValueFromCursor(methodBuilder, method.getParent(), returnTypeName, "_cursor", "0");
methodBuilder.addCode(";\n");
// end cursor
methodBuilder.endControlFlow();
methodBuilder.addCode("return result;\n");
// end method
methodBuilder.endControlFlow();
}
use of com.abubusoft.kripton.processor.sqlite.transform.SQLTransform in project kripton by xcesco.
the class SelectScalarListHelper method generateSpecializedPart.
/*
* (non-Javadoc)
*
* @see com.abubusoft.kripton.processor.sqlite.SQLiteSelectBuilder.
* SelectCodeGenerator#generate(com.squareup.javapoet.MethodSpec.Builder)
*/
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
// ASSERT: returnType is a supported type
// no column or too many columns
AssertKripton.assertTrueOrInvalidMethodSignException(fieldList.size() == 1, method, "only one field can be defined as result for this method");
ParameterizedTypeName returnListName = (ParameterizedTypeName) method.getReturnClass();
;
TypeName collectionClass;
ClassName listClazzName = returnListName.rawType;
TypeName elementName = returnListName.typeArguments.get(0);
collectionClass = SelectBeanListHelper.defineCollection(listClazzName);
methodBuilder.addCode("\n");
if (TypeUtility.isTypeEquals(collectionClass, TypeUtility.typeName(ArrayList.class))) {
methodBuilder.addStatement("$T<$T> resultList=new $T<$T>(_cursor.getCount())", collectionClass, elementName, collectionClass, elementName);
} else {
methodBuilder.addStatement("$T<$T> resultList=new $T<$T>()", collectionClass, elementName, collectionClass, elementName);
}
methodBuilder.addCode("\n");
SQLTransform t = SQLTransformer.lookup(returnListName);
// @formatter:off
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("do\n");
methodBuilder.beginControlFlow("if (!_cursor.isNull(0))");
methodBuilder.addCode("resultList.add(");
t.generateReadValueFromCursor(methodBuilder, method.getParent(), elementName, "_cursor", "0");
methodBuilder.addCode(");\n");
methodBuilder.nextControlFlow("else");
methodBuilder.addCode("resultList.add(null);\n");
methodBuilder.endControlFlow();
methodBuilder.endControlFlow("while (_cursor.moveToNext())");
methodBuilder.endControlFlow();
methodBuilder.addCode("return resultList;\n");
methodBuilder.endControlFlow();
// @formatter:on
}
Aggregations