Search in sources :

Example 6 with QueryPredicate

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

the class SQLPredicateTest method testMatchAllPredicate.

/**
 * Test that MATCH ALL predicate is correctly parsed to an
 * expression that is always true.
 * @throws DataStoreException if parsing fails
 */
@Test
public void testMatchAllPredicate() throws DataStoreException {
    QueryPredicate predicate = QueryPredicates.all();
    SQLPredicate sqlPredicate = new SQLPredicate(predicate);
    assertEquals("1 = 1", sqlPredicate.toString());
    assertTrue(sqlPredicate.getBindings().isEmpty());
}
Also used : SQLPredicate(com.amplifyframework.datastore.storage.sqlite.adapter.SQLPredicate) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Test(org.junit.Test)

Example 7 with QueryPredicate

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

the class SQLiteStorageAdapter method modelExists.

private boolean modelExists(Model model, QueryPredicate predicate) throws DataStoreException {
    final String modelName = model.getModelName();
    final ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(modelName);
    final SQLiteTable table = SQLiteTable.fromSchema(schema);
    final String tableName = table.getName();
    final String primaryKeyName = table.getPrimaryKey().getName();
    final QueryPredicate matchId = QueryField.field(tableName, primaryKeyName).eq(model.getId());
    final QueryPredicate condition = predicate.and(matchId);
    return sqlCommandProcessor.executeExists(sqlCommandFactory.existsFor(schema, condition));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)

Example 8 with QueryPredicate

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

the class SQLiteStorageAdapter method query.

/**
 * Helper method to synchronously query for a single model instance.  Used before any save initiated by
 * DATASTORE_API in order to determine which fields have changed.
 * @param model a Model that we want to query for the same type and id in SQLite.
 * @return the Model instance from SQLite, if it exists, otherwise null.
 */
private Model query(Model model) {
    final String modelName = model.getModelName();
    final ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(modelName);
    final SQLiteTable table = SQLiteTable.fromSchema(schema);
    final String primaryKeyName = table.getPrimaryKey().getName();
    final QueryPredicate matchId = QueryField.field(modelName, primaryKeyName).eq(model.getId());
    Iterator<? extends Model> result = Single.<Iterator<? extends Model>>create(emitter -> {
        if (model instanceof SerializedModel) {
            query(model.getModelName(), Where.matches(matchId), emitter::onSuccess, emitter::onError);
        } else {
            query(model.getClass(), Where.matches(matchId), emitter::onSuccess, emitter::onError);
        }
    }).blockingGet();
    return result.hasNext() ? result.next() : null;
}
Also used : ObjectsCompat(androidx.core.util.ObjectsCompat) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) ModelProvider(com.amplifyframework.core.model.ModelProvider) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Gson(com.google.gson.Gson) Map(java.util.Map) PublishSubject(io.reactivex.rxjava3.subjects.PublishSubject) QueryOptions(com.amplifyframework.core.model.query.QueryOptions) CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) SerializedModel(com.amplifyframework.core.model.SerializedModel) Immutable(com.amplifyframework.util.Immutable) Set(java.util.Set) Executors(java.util.concurrent.Executors) Logger(com.amplifyframework.logging.Logger) Objects(java.util.Objects) DataStoreException(com.amplifyframework.datastore.DataStoreException) LocalStorageAdapter(com.amplifyframework.datastore.storage.LocalStorageAdapter) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Context(android.content.Context) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) QueryField(com.amplifyframework.core.model.query.predicate.QueryField) Single(io.reactivex.rxjava3.core.Single) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) SystemModelsProviderFactory(com.amplifyframework.datastore.model.SystemModelsProviderFactory) HashMap(java.util.HashMap) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) ModelField(com.amplifyframework.core.model.ModelField) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) GsonFactory(com.amplifyframework.util.GsonFactory) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) ModelSchema(com.amplifyframework.core.model.ModelSchema) Subject(io.reactivex.rxjava3.subjects.Subject) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ExecutorService(java.util.concurrent.ExecutorService) Cursor(android.database.Cursor) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) Amplify(com.amplifyframework.core.Amplify) CompoundModelProvider(com.amplifyframework.datastore.model.CompoundModelProvider) Iterator(java.util.Iterator) Model(com.amplifyframework.core.model.Model) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) Where(com.amplifyframework.core.model.query.Where) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) TimeUnit(java.util.concurrent.TimeUnit) ModelMigrations(com.amplifyframework.datastore.storage.sqlite.migrations.ModelMigrations) VisibleForTesting(androidx.annotation.VisibleForTesting) Collections(java.util.Collections) ModelSchema(com.amplifyframework.core.model.ModelSchema) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable) SerializedModel(com.amplifyframework.core.model.SerializedModel)

Example 9 with QueryPredicate

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

the class SqlQueryProcessor method modelExists.

boolean modelExists(Model model, QueryPredicate predicate) throws DataStoreException {
    final String modelName = model.getModelName();
    final ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelClass(modelName);
    final SQLiteTable table = SQLiteTable.fromSchema(schema);
    final String tableName = table.getName();
    final String primaryKeyName = table.getPrimaryKey().getName();
    final QueryPredicate matchId = QueryField.field(tableName, primaryKeyName).eq(model.getId());
    final QueryPredicate condition = predicate.and(matchId);
    return sqlCommandProcessor.executeExists(sqlCommandFactory.existsFor(schema, condition));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)

Example 10 with QueryPredicate

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

the class AppSyncRequestFactoryTest method validatePredicateGroupForSyncExpressionIsNotWrappedWithAnd.

/**
 * If a QueryPredicateGroup is provided, it should be parsed as is, and not be wrapped with another AND group.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validatePredicateGroupForSyncExpressionIsNotWrappedWithAnd() throws AmplifyException, JSONException {
    String id = "426f8e8d-ea0f-4839-a73f-6a2a38565ba1";
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    QueryPredicate predicate = BlogOwner.ID.eq(id).and(Blog.NAME.eq("Spaghetti"));
    final GraphQLRequest<Iterable<Post>> request = AppSyncRequestFactory.buildSyncRequest(schema, null, null, predicate);
    JSONAssert.assertEquals(Resources.readAsString("base-sync-request-with-predicate-group.txt"), request.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) 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