Search in sources :

Example 1 with QueryOptions

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

the class SQLiteStorageAdapter method delete.

/**
 * {@inheritDoc}
 */
@Override
public <T extends Model> void delete(@NonNull Class<T> itemClass, @NonNull StorageItemChange.Initiator initiator, @NonNull QueryPredicate predicate, @NonNull Action onSuccess, @NonNull Consumer<DataStoreException> onError) {
    Objects.requireNonNull(itemClass);
    Objects.requireNonNull(initiator);
    Objects.requireNonNull(predicate);
    Objects.requireNonNull(onSuccess);
    Objects.requireNonNull(onError);
    threadPool.submit(() -> {
        final ModelSchema modelSchema = schemaRegistry.getModelSchemaForModelClass(itemClass);
        QueryOptions options = Where.matches(predicate);
        try (Cursor cursor = sqlCommandProcessor.rawQuery(sqlCommandFactory.queryFor(modelSchema, options))) {
            final SQLiteTable sqliteTable = SQLiteTable.fromSchema(modelSchema);
            final String primaryKeyName = sqliteTable.getPrimaryKey().getAliasedName();
            // identify items that meet the predicate
            List<T> items = new ArrayList<>();
            if (cursor != null && cursor.moveToFirst()) {
                int index = cursor.getColumnIndexOrThrow(primaryKeyName);
                do {
                    String id = cursor.getString(index);
                    String dummyJson = gson.toJson(Collections.singletonMap("id", id));
                    T dummyItem = gson.fromJson(dummyJson, itemClass);
                    items.add(dummyItem);
                } while (cursor.moveToNext());
            }
            // identify every model to delete as a result of this operation
            List<Model> modelsToDelete = new ArrayList<>(items);
            List<Model> cascadedModels = sqliteModelTree.descendantsOf(items);
            modelsToDelete.addAll(cascadedModels);
            // execute local deletions
            sqlCommandProcessor.execute(sqlCommandFactory.deleteFor(modelSchema, predicate));
            // publish every deletion
            for (Model model : modelsToDelete) {
                ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(model.getModelName());
                itemChangeSubject.onNext(StorageItemChange.builder().item(model).patchItem(SerializedModel.create(model, schema)).modelSchema(schema).type(StorageItemChange.Type.DELETE).predicate(QueryPredicates.all()).initiator(initiator).build());
            }
            onSuccess.call();
        } catch (DataStoreException dataStoreException) {
            onError.accept(dataStoreException);
        } catch (Exception someOtherTypeOfException) {
            DataStoreException dataStoreException = new DataStoreException("Error in deleting models.", someOtherTypeOfException, "See attached exception for details.");
            onError.accept(dataStoreException);
        }
    });
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) ArrayList(java.util.ArrayList) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable) Cursor(android.database.Cursor) QueryOptions(com.amplifyframework.core.model.query.QueryOptions) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) AmplifyException(com.amplifyframework.AmplifyException) DataStoreException(com.amplifyframework.datastore.DataStoreException) ModelSchema(com.amplifyframework.core.model.ModelSchema) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model)

Aggregations

Cursor (android.database.Cursor)1 AmplifyException (com.amplifyframework.AmplifyException)1 Model (com.amplifyframework.core.model.Model)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1 SerializedModel (com.amplifyframework.core.model.SerializedModel)1 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)1 QueryOptions (com.amplifyframework.core.model.query.QueryOptions)1 DataStoreException (com.amplifyframework.datastore.DataStoreException)1 SQLiteTable (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)1 ArrayList (java.util.ArrayList)1