use of com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable 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.datastore.storage.sqlite.adapter.SQLiteTable 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.datastore.storage.sqlite.adapter.SQLiteTable 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);
}
});
}
use of com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable 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.datastore.storage.sqlite.adapter.SQLiteTable in project amplify-android by aws-amplify.
the class SQLiteCommandFactory method createTableFor.
@NonNull
@Override
public SqlCommand createTableFor(@NonNull ModelSchema modelSchema) {
final SQLiteTable table = SQLiteTable.fromSchema(modelSchema);
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("CREATE TABLE IF NOT EXISTS").append(SqlKeyword.DELIMITER).append(Wrap.inBackticks(table.getName())).append(SqlKeyword.DELIMITER);
if (Empty.check(table.getColumns())) {
return new SqlCommand(table.getName(), stringBuilder.toString());
}
stringBuilder.append("(").append(parseColumns(table));
if (!table.getForeignKeys().isEmpty()) {
stringBuilder.append(",").append(SqlKeyword.DELIMITER).append(parseForeignKeys(table));
}
stringBuilder.append(");");
final String createSqlStatement = stringBuilder.toString();
return new SqlCommand(table.getName(), createSqlStatement);
}
Aggregations