Search in sources :

Example 6 with ContentUriPlaceHolder

use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.

the class TestUriChecker method testReplaceAuthorityWithVariable.

@Test
public void testReplaceAuthorityWithVariable() {
    String input = "content://androi.authority/test/${field0}/${field1}";
    String expected = "content://androi.authority/test/#/*";
    log(input);
    ContentUriChecker checker = ContentUriChecker.getInstance();
    // verify sql
    checker.verify(input);
    // check bind parameters
    {
        String actual = checker.replace(input, new UriPlaceHolderReplacerListener() {

            @Override
            public String onParameterName(int pathSegmentIndex, String name) {
                log("segment :" + pathSegmentIndex);
                if (name.endsWith("0")) {
                    return "#";
                }
                ;
                return "*";
            }
        });
        assertEquals(actual, expected);
        {
            List<ContentUriPlaceHolder> aspectedHolders = new ArrayList<>();
            aspectedHolders.add(new ContentUriPlaceHolder(1, "field0"));
            aspectedHolders.add(new ContentUriPlaceHolder(2, "field1"));
            List<ContentUriPlaceHolder> actualHolders = checker.extract(input);
            checkCollectionExactly(aspectedHolders, actualHolders);
        }
    }
}
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) ArrayList(java.util.ArrayList) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 7 with ContentUriPlaceHolder

use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.

the class TestUriChecker method testExtractParameters.

@Test
public void testExtractParameters() {
    String input = "content://androi.authority/test/${ input }/ test /${ detail.id}";
    ContentUriChecker checker = ContentUriChecker.getInstance();
    {
        List<ContentUriPlaceHolder> result = checker.extract(input);
        for (ContentUriPlaceHolder item : result) {
            log(item.toString());
        }
        checkList(result, new ContentUriPlaceHolder(1, "input"), new ContentUriPlaceHolder(3, "detail.id"));
    }
    {
        List<ContentUriPlaceHolder> result = checker.extract(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 8 with ContentUriPlaceHolder

use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.

the class TestUriChecker method testAuthorityWithVariableInPath.

@Test
public void testAuthorityWithVariableInPath() {
    String input = "content://androi.authority/test/${ input1 }/${input2   }";
    ContentUriChecker checker = ContentUriChecker.getInstance();
    // check bind parameters
    {
        List<ContentUriPlaceHolder> aspected = new ArrayList<>();
        aspected.add(new ContentUriPlaceHolder(1, "input1"));
        aspected.add(new ContentUriPlaceHolder(2, "input2"));
        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 9 with ContentUriPlaceHolder

use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder in project kripton by xcesco.

the class TestUriChecker method testAuthorityWithVariableInPathError2.

@Test(expected = AssertionError.class)
public void testAuthorityWithVariableInPathError2() {
    String input = "content://androi.authority/test/#";
    ContentUriChecker checker = ContentUriChecker.getInstance();
    // verify sql
    checker.verify(input);
    // 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 10 with ContentUriPlaceHolder

use of com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder 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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) 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