Search in sources :

Example 26 with ModelSchema

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

the class SQLiteStorageAdapter method query.

/**
 * {@inheritDoc}
 */
@Override
public void query(@NonNull String modelName, @NonNull QueryOptions options, @NonNull Consumer<Iterator<? extends Model>> onSuccess, @NonNull Consumer<DataStoreException> onError) {
    Objects.requireNonNull(modelName);
    Objects.requireNonNull(options);
    Objects.requireNonNull(onSuccess);
    Objects.requireNonNull(onError);
    threadPool.submit(() -> {
        final ModelSchema modelSchema = schemaRegistry.getModelSchemaForModelClass(modelName);
        try (Cursor cursor = sqlCommandProcessor.rawQuery(sqlCommandFactory.queryFor(modelSchema, options))) {
            LOG.debug("Querying item for: " + modelName);
            final List<Model> models = new ArrayList<>();
            final SQLiteModelFieldTypeConverter converter = new SQLiteModelFieldTypeConverter(modelSchema, schemaRegistry, gson);
            if (cursor == null) {
                onError.accept(new DataStoreException("Error in getting a cursor to the table for class: " + modelName, AmplifyException.TODO_RECOVERY_SUGGESTION));
                return;
            }
            if (cursor.moveToFirst()) {
                do {
                    final Map<String, Object> data = converter.buildMapForModel(cursor);
                    final SerializedModel model = createSerializedModel(modelSchema, data);
                    models.add(model);
                } while (cursor.moveToNext());
            }
            onSuccess.accept(models.iterator());
        } catch (Exception exception) {
            onError.accept(new DataStoreException("Error in querying the model.", exception, "See attached exception for details."));
        }
    });
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) DataStoreException(com.amplifyframework.datastore.DataStoreException) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SerializedModel(com.amplifyframework.core.model.SerializedModel) AmplifyException(com.amplifyframework.AmplifyException) DataStoreException(com.amplifyframework.datastore.DataStoreException)

Example 27 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema 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 28 with ModelSchema

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

the class SQLiteStorageAdapter method delete.

/**
 * {@inheritDoc}
 */
@Override
public <T extends Model> void delete(@NonNull T item, @NonNull StorageItemChange.Initiator initiator, @NonNull QueryPredicate predicate, @NonNull Consumer<StorageItemChange<T>> onSuccess, @NonNull Consumer<DataStoreException> onError) {
    Objects.requireNonNull(item);
    Objects.requireNonNull(initiator);
    Objects.requireNonNull(predicate);
    Objects.requireNonNull(onSuccess);
    Objects.requireNonNull(onError);
    threadPool.submit(() -> {
        try {
            final String modelName = item.getModelName();
            final ModelSchema modelSchema = schemaRegistry.getModelSchemaForModelClass(modelName);
            // Check if data being deleted exists; "Succeed" deletion in that case.
            if (!sqlQueryProcessor.modelExists(item, QueryPredicates.all())) {
                LOG.verbose(modelName + " model with id = " + item.getId() + " does not exist.");
                // Pass back item change instance without publishing it.
                onSuccess.accept(StorageItemChange.<T>builder().item(item).patchItem(SerializedModel.create(item, modelSchema)).modelSchema(modelSchema).type(StorageItemChange.Type.DELETE).predicate(predicate).initiator(initiator).build());
                return;
            }
            // Check if existing data meets the condition, only if a condition other than all() was provided.
            if (!QueryPredicates.all().equals(predicate) && !sqlQueryProcessor.modelExists(item, predicate)) {
                throw new DataStoreException("Deletion failed because condition did not match existing model instance.", "The deletion will continue to fail until the model instance is updated.");
            }
            // identify items affected by cascading delete before deleting them
            List<Model> cascadedModels = sqliteModelTree.descendantsOf(Collections.singleton(item));
            // execute local deletion
            writeData(item, StorageItemChange.Type.DELETE);
            // publish cascaded deletions
            for (Model cascadedModel : cascadedModels) {
                ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(cascadedModel.getModelName());
                itemChangeSubject.onNext(StorageItemChange.builder().item(cascadedModel).patchItem(SerializedModel.create(cascadedModel, schema)).modelSchema(schema).type(StorageItemChange.Type.DELETE).predicate(QueryPredicates.all()).initiator(initiator).build());
            }
            // publish successful deletion of top-level item
            StorageItemChange<T> change = StorageItemChange.<T>builder().item(item).patchItem(SerializedModel.create(item, modelSchema)).modelSchema(modelSchema).type(StorageItemChange.Type.DELETE).predicate(predicate).initiator(initiator).build();
            itemChangeSubject.onNext(change);
            onSuccess.accept(change);
        } catch (DataStoreException dataStoreException) {
            onError.accept(dataStoreException);
        } catch (Exception someOtherTypeOfException) {
            DataStoreException dataStoreException = new DataStoreException("Error in deleting the model.", someOtherTypeOfException, "See attached exception for details.");
            onError.accept(dataStoreException);
        }
    });
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) DataStoreException(com.amplifyframework.datastore.DataStoreException) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) AmplifyException(com.amplifyframework.AmplifyException) DataStoreException(com.amplifyframework.datastore.DataStoreException)

Example 29 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema 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 30 with ModelSchema

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

the class SQLiteStorageAdapter method save.

/**
 * {@inheritDoc}
 */
@Override
public <T extends Model> void save(@NonNull T item, @NonNull StorageItemChange.Initiator initiator, @NonNull QueryPredicate predicate, @NonNull Consumer<StorageItemChange<T>> onSuccess, @NonNull Consumer<DataStoreException> onError) {
    Objects.requireNonNull(item);
    Objects.requireNonNull(initiator);
    Objects.requireNonNull(predicate);
    Objects.requireNonNull(onSuccess);
    Objects.requireNonNull(onError);
    threadPool.submit(() -> {
        try {
            final ModelSchema modelSchema = schemaRegistry.getModelSchemaForModelClass(item.getModelName());
            final StorageItemChange.Type writeType;
            SerializedModel patchItem = null;
            if (sqlQueryProcessor.modelExists(item, QueryPredicates.all())) {
                // if data exists already, then UPDATE the row
                writeType = StorageItemChange.Type.UPDATE;
                // Check if existing data meets the condition, only if a condition other than all() was provided.
                if (!QueryPredicates.all().equals(predicate) && !sqlQueryProcessor.modelExists(item, predicate)) {
                    throw new DataStoreException("Save failed because condition did not match existing model instance.", "The save will continue to fail until the model instance is updated.");
                }
                if (initiator == StorageItemChange.Initiator.DATA_STORE_API) {
                    // When saving items via the DataStore API, compute a SerializedModel of the changed model.
                    // This is not necessary when save
                    // is initiated by the sync engine, so skip it for optimization to avoid the extra SQL query.
                    patchItem = SerializedModel.create(item, modelSchema);
                }
            } else if (!QueryPredicates.all().equals(predicate)) {
                // insert not permitted with a condition
                throw new DataStoreException("Conditional update must be performed against an already existing data. " + "Insertion is not permitted while using a predicate.", "Please save without specifying a predicate.");
            } else {
                // if data doesn't exist yet, then INSERT a new row
                writeType = StorageItemChange.Type.CREATE;
            }
            // execute local save
            writeData(item, writeType);
            // publish successful save
            StorageItemChange<T> change = StorageItemChange.<T>builder().item(item).patchItem(patchItem != null ? patchItem : SerializedModel.create(item, modelSchema)).modelSchema(modelSchema).type(writeType).predicate(predicate).initiator(initiator).build();
            itemChangeSubject.onNext(change);
            onSuccess.accept(change);
        } catch (DataStoreException dataStoreException) {
            onError.accept(dataStoreException);
        } catch (Exception someOtherTypeOfException) {
            String modelToString = item.getModelName() + "[id=" + item.getId() + "]";
            DataStoreException dataStoreException = new DataStoreException("Error in saving the model: " + modelToString, someOtherTypeOfException, "See attached exception for details.");
            onError.accept(dataStoreException);
        }
    });
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) DataStoreException(com.amplifyframework.datastore.DataStoreException) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) SerializedModel(com.amplifyframework.core.model.SerializedModel) AmplifyException(com.amplifyframework.AmplifyException) DataStoreException(com.amplifyframework.datastore.DataStoreException)

Aggregations

ModelSchema (com.amplifyframework.core.model.ModelSchema)109 Test (org.junit.Test)69 SerializedModel (com.amplifyframework.core.model.SerializedModel)34 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)30 Model (com.amplifyframework.core.model.Model)28 DataStoreException (com.amplifyframework.datastore.DataStoreException)26 HashMap (java.util.HashMap)23 ArrayList (java.util.ArrayList)22 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)21 AmplifyException (com.amplifyframework.AmplifyException)19 Consumer (com.amplifyframework.core.Consumer)19 List (java.util.List)17 NonNull (androidx.annotation.NonNull)14 Cancelable (com.amplifyframework.core.async.Cancelable)14 TimeUnit (java.util.concurrent.TimeUnit)14 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)13 Action (com.amplifyframework.core.Action)12 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)12 Collections (java.util.Collections)12 ModelProvider (com.amplifyframework.core.model.ModelProvider)11