use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.
the class TestUriChecker method checkList.
protected void checkList(List<ContentUriPlaceHolder> actual, ContentUriPlaceHolder... input) {
List<ContentUriPlaceHolder> aspected = new ArrayList<>();
for (ContentUriPlaceHolder item : input) {
aspected.add(item);
}
checkCollectionExactly(actual, aspected);
}
use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.
the class TestUriChecker method testAuthorityWithVariableInPathError.
@Test(expected = KriptonProcessorException.class)
public void testAuthorityWithVariableInPathError() {
String input = "content://androi.authority/test/${ input0 }/";
ContentUriChecker checker = ContentUriChecker.getInstance();
// check bind parameters
{
List<ContentUriPlaceHolder> aspected = new ArrayList<>();
aspected.add(new ContentUriPlaceHolder(1, "input0"));
aspected.add(new ContentUriPlaceHolder(2, "input1"));
List<ContentUriPlaceHolder> actual = checker.extract(input);
checkCollectionExactly(actual, aspected);
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.
the class TestUriChecker method testExtractor.
@Test
public void testExtractor() throws Throwable {
String input = "content://androi.authority/master/${ master }/detail/${detail}/subdetail/${subdetail}";
log(input);
ContentUriChecker checker = ContentUriChecker.getInstance();
Map<String, ContentUriPlaceHolder> parameters = checker.extractAsMap(input);
String actual = checker.replace(input, new UriPlaceHolderReplacerListener() {
@Override
public String onParameterName(int pathSegmentIndex, String name) {
log("segment : %s, name: %s", pathSegmentIndex, name);
return "?";
}
});
String expected = "content://androi.authority/master/?/detail/?/subdetail/?";
assertEquals(actual, expected);
log(expected);
// log(""+expected.split("/").length);
ContentProviderURIParamsExtractor extractor = new ContentProviderURIParamsExtractor(expected, input.split("/").length);
for (ContentUriPlaceHolder item : parameters.values()) {
assertTrue(extractor.getPathSegment(item.pathSegmentIndex).equals("?"));
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.
the class TestUriChecker method testExtractParametersFromPath.
@Test
public void testExtractParametersFromPath() {
String input = "test/${ input }/ test /${ detail.id}";
ContentUriChecker checker = ContentUriChecker.getInstance();
{
List<ContentUriPlaceHolder> result = checker.extractFromPath(input);
for (ContentUriPlaceHolder item : result) {
log(item.toString());
}
checkList(result, new ContentUriPlaceHolder(1, "input"), new ContentUriPlaceHolder(3, "detail.id"));
}
{
List<ContentUriPlaceHolder> result = checker.extractFromPath(input);
for (ContentUriPlaceHolder item : result) {
log(item.toString());
}
checkList(result, new ContentUriPlaceHolder(1, "input"), new ContentUriPlaceHolder(3, "detail.id"));
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.
the class SqlInsertBuilder method generateInsertForContentProvider.
/**
* <p>
* Generate insert used in content provider class.
* </p>
*
* @param methodBuilder
* @param method
* @param insertResultType
*/
private static void generateInsertForContentProvider(TypeSpec.Builder classBuilder, final SQLiteModelMethod method, InsertType insertResultType) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
final SQLiteEntity entity = daoDefinition.getEntity();
final Set<String> columns = new LinkedHashSet<>();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.contentProviderMethodName);
ParameterSpec parameterSpec;
parameterSpec = ParameterSpec.builder(Uri.class, "uri").build();
methodBuilder.addParameter(parameterSpec);
parameterSpec = ParameterSpec.builder(ContentValues.class, "contentValues").build();
methodBuilder.addParameter(parameterSpec);
methodBuilder.returns(Long.TYPE);
SqlBuilderHelper.generateLogForContentProviderBeginning(method, methodBuilder);
// just detect which columns are admitted
JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = entity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
columns.add(tempProperty.columnName);
return tempProperty.columnName;
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
AssertKripton.fail("Inconsistent state");
return null;
}
});
// generate columnCheckSet
SqlBuilderHelper.generateColumnCheckSet(classBuilder, method, columns);
// retrieve content values
methodBuilder.addStatement("$T _contentValues=contentValuesForContentProvider(contentValues)", KriptonContentValues.class);
// generate column check
SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "_contentValues.values().keySet()", true, null);
methodBuilder.addCode("\n");
// every controls was done in constructor of SQLiteModelMethod
for (ContentUriPlaceHolder variable : method.contentProviderUriVariables) {
SQLProperty entityProperty = entity.get(variable.value);
if (entityProperty != null) {
methodBuilder.addCode("// Add parameter $L at path segment $L\n", variable.value, variable.pathSegmentIndex);
TypeName entityPropertyType = entityProperty.getPropertyType().getTypeName();
if (TypeUtility.isString(entityPropertyType)) {
methodBuilder.addStatement("contentValues.put($S, uri.getPathSegments().get($L))", entityProperty.columnName, variable.pathSegmentIndex);
} else {
methodBuilder.addStatement("contentValues.put($S, Long.valueOf(uri.getPathSegments().get($L)))", entityProperty.columnName, variable.pathSegmentIndex);
}
}
}
// generate log for inser operation
SqlBuilderHelper.generateLogForContentValuesContentProvider(method, methodBuilder);
ConflictAlgorithmType conflictAlgorithmType = InsertBeanHelper.getConflictAlgorithmType(method);
String conflictString1 = "";
String conflictString2 = "";
if (conflictAlgorithmType != ConflictAlgorithmType.NONE) {
conflictString1 = "WithOnConflict";
conflictString2 = ", " + conflictAlgorithmType.getConflictAlgorithm();
methodBuilder.addCode("// conflict algorithm $L\n", method.jql.conflictAlgorithmType);
}
methodBuilder.addComment("insert operation");
methodBuilder.addStatement("long result = database().insert$L($S, null, _contentValues.values()$L)", conflictString1, daoDefinition.getEntity().getTableName(), conflictString2);
if (method.getParent().getParent().generateRx) {
GenericSQLHelper.generateSubjectNext(methodBuilder, SubjectType.INSERT);
}
// support for livedata
if (daoDefinition.hasLiveData()) {
methodBuilder.addComment("support for livedata");
methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result>0?1:0)");
}
methodBuilder.addStatement("return result");
// javadoc
// 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 contentValues content values\n");
methodBuilder.addJavadoc("@return new row's id\n");
classBuilder.addMethod(methodBuilder.build());
}
Aggregations