Search in sources :

Example 1 with ContentUriPlaceHolder

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);
}
Also used : ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) ArrayList(java.util.ArrayList)

Example 2 with ContentUriPlaceHolder

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);
    }
}
Also used : ContentUriChecker(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker) ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) ArrayList(java.util.ArrayList) List(java.util.List) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 3 with ContentUriPlaceHolder

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("?"));
    }
}
Also used : ContentUriChecker(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker) ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) UriPlaceHolderReplacerListener(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker.UriPlaceHolderReplacerListener) ContentProviderURIParamsExtractor(com.abubusoft.kripton.android.sqlite.ContentProviderURIParamsExtractor) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 4 with ContentUriPlaceHolder

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"));
    }
}
Also used : ContentUriChecker(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker) ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) ArrayList(java.util.ArrayList) List(java.util.List) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 5 with ContentUriPlaceHolder

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ConflictAlgorithmType(com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)

Aggregations

ContentUriPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder)12 BaseProcessorTest (base.BaseProcessorTest)7 ContentUriChecker (com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 List (java.util.List)5 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)4 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)3 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)3 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 LinkedHashSet (java.util.LinkedHashSet)3 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)2 JQLPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder)2 UriPlaceHolderReplacerListener (com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker.UriPlaceHolderReplacerListener)2 TypeName (com.squareup.javapoet.TypeName)2 BindSqlDynamicWhere (com.abubusoft.kripton.android.annotation.BindSqlDynamicWhere)1 ConflictAlgorithmType (com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType)1 ContentProviderURIParamsExtractor (com.abubusoft.kripton.android.sqlite.ContentProviderURIParamsExtractor)1 One (com.abubusoft.kripton.common.One)1