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());
}
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));
}
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;
}
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));
}
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);
}
Aggregations