Search in sources :

Example 1 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class InMemoryStorageAdapter method query.

@Override
public void query(@NonNull String modelName, @NonNull QueryOptions options, @NonNull Consumer<Iterator<? extends Model>> onSuccess, @NonNull Consumer<DataStoreException> onError) {
    final List<Model> result = new ArrayList<>();
    final QueryPredicate predicate = options.getQueryPredicate();
    for (Model item : items) {
        if (modelName.equals(item.getClass().getSimpleName()) && predicate.evaluate(item)) {
            // TODO, add tests for new query method.
            result.add(item);
        }
    }
    onSuccess.accept(result.iterator());
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) ArrayList(java.util.ArrayList)

Example 2 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class InMemoryStorageAdapter method query.

// (T) item *is* checked, via isAssignableFrom().
@SuppressWarnings("unchecked")
@Override
public <T extends Model> void query(@NonNull final Class<T> itemClass, @NonNull final QueryOptions options, @NonNull final Consumer<Iterator<T>> onSuccess, @NonNull final Consumer<DataStoreException> onError) {
    final List<T> result = new ArrayList<>();
    final QueryPredicate predicate = options.getQueryPredicate();
    for (Model item : items) {
        if (itemClass.isAssignableFrom(item.getClass()) && predicate.evaluate(item)) {
            result.add((T) item);
        }
    }
    onSuccess.accept(result.iterator());
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ArrayList(java.util.ArrayList) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model)

Example 3 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLCommandProcessorTest method executeExistsReturnsFalseWhenItemDoesntExist.

/**
 * Create a BlogOwner, but don't insert it.  Then verify that executeExists returns false.
 * @throws AmplifyException on failure to create ModelSchema from class.
 */
@Test
public void executeExistsReturnsFalseWhenItemDoesntExist() throws AmplifyException {
    // Create a BlogOwner, but don't insert it
    ModelSchema blogOwnerSchema = ModelSchema.fromModelClass(BlogOwner.class);
    BlogOwner abigailMcGregor = BlogOwner.builder().name("Abigail McGregor").build();
    QueryPredicate predicate = BlogOwner.ID.eq(abigailMcGregor.getId());
    SqlCommand existsCommand = sqlCommandFactory.existsFor(blogOwnerSchema, predicate);
    assertFalse(sqlCommandProcessor.executeExists(existsCommand));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 4 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLPredicateTest method testContainsForStringField.

/**
 * Test contains in the context of a String field.
 * @throws DataStoreException Not thrown.
 */
@Test
public void testContainsForStringField() throws DataStoreException {
    QueryPredicate predicate = Where.matches(Blog.NAME.contains("something")).getQueryPredicate();
    SQLPredicate sqlPredicate = new SQLPredicate(predicate);
    validateSQLExpressionForContains(sqlPredicate, "name");
}
Also used : SQLPredicate(com.amplifyframework.datastore.storage.sqlite.adapter.SQLPredicate) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Test(org.junit.Test)

Example 5 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLPredicateTest method testNotContainsForStringField.

/**
 * Test notContains in the context of a String field.
 * @throws DataStoreException Not thrown.
 */
@Test
public void testNotContainsForStringField() throws DataStoreException {
    QueryPredicate predicate = Where.matches(Blog.NAME.notContains("something")).getQueryPredicate();
    SQLPredicate sqlPredicate = new SQLPredicate(predicate);
    validateSQLExpressionForNotContains(sqlPredicate, "name");
}
Also used : SQLPredicate(com.amplifyframework.datastore.storage.sqlite.adapter.SQLPredicate) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Test(org.junit.Test)

Aggregations

QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)31 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)11 ModelSchema (com.amplifyframework.core.model.ModelSchema)9 DataStoreException (com.amplifyframework.datastore.DataStoreException)9 HashSet (java.util.HashSet)8 List (java.util.List)7 NonNull (androidx.annotation.NonNull)6 SQLPredicate (com.amplifyframework.datastore.storage.sqlite.adapter.SQLPredicate)6 Consumer (com.amplifyframework.core.Consumer)5 Cancelable (com.amplifyframework.core.async.Cancelable)5 Model (com.amplifyframework.core.model.Model)5 SerializedModel (com.amplifyframework.core.model.SerializedModel)5 SQLiteTable (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)5 Action (com.amplifyframework.core.Action)4 TimeUnit (java.util.concurrent.TimeUnit)4 AmplifyException (com.amplifyframework.AmplifyException)3 Amplify (com.amplifyframework.core.Amplify)3 ModelProvider (com.amplifyframework.core.model.ModelProvider)3