use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class DefaultDeleteResolver method performDelete.
/**
* {@inheritDoc}
*/
@NonNull
@Override
public DeleteResult performDelete(@NonNull StorIOSQLite storIOSQLite, @NonNull T object) {
final DeleteQuery deleteQuery = mapToDeleteQuery(object);
final int numberOfRowsDeleted = storIOSQLite.lowLevel().delete(deleteQuery);
return DeleteResult.newInstance(numberOfRowsDeleted, deleteQuery.table());
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class PreparedDeleteObject method executeAsBlocking.
/**
* Executes Delete Operation immediately in current thread.
* <p>
* Notice: This is blocking I/O operation that should not be executed on the Main Thread,
* it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
* So please, call this method on some background thread. See {@link WorkerThread}.
*
* @return non-null result of Delete Operation.
*/
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResult executeAsBlocking() {
try {
final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
final DeleteResolver<T> deleteResolver;
if (explicitDeleteResolver != null) {
deleteResolver = explicitDeleteResolver;
} else {
final SQLiteTypeMapping<T> typeMapping = lowLevel.typeMapping((Class<T>) object.getClass());
if (typeMapping == null) {
throw new IllegalStateException("Object does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + ", " + "db was not affected by this operation, please add type mapping for this type");
}
deleteResolver = typeMapping.deleteResolver();
}
final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object);
if (deleteResult.numberOfRowsDeleted() > 0) {
lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
}
return deleteResult;
} catch (Exception exception) {
throw new StorIOException("Error has occurred during Delete operation. object = " + object, exception);
}
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class PreparedExecuteSQL method executeAsBlocking.
/**
* Executes SQL Operation immediately in current thread.
* <p>
* Notice: This is blocking I/O operation that should not be executed on the Main Thread,
* it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
* So please, call this method on some background thread. See {@link WorkerThread}.
*
* @return just a new instance of {@link Object}, actually Execute SQL should return {@code void},
* but we can not return instance of {@link Void} so we just return {@link Object}
* and you don't have to deal with {@code null}.
*/
@WorkerThread
@NonNull
@Override
public Object executeAsBlocking() {
try {
final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
lowLevel.executeSQL(rawQuery);
final Set<String> affectedTables = rawQuery.affectsTables();
if (affectedTables.size() > 0) {
lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
}
return new Object();
} catch (Exception exception) {
throw new StorIOException("Error has occurred during ExecuteSQL operation. query = " + rawQuery, exception);
}
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class PreparedGetCursor method asRxObservable.
/**
* Creates "Hot" {@link Observable} which will be subscribed to changes of tables from query
* and will emit result each time change occurs.
* <p>
* First result will be emitted immediately after subscription,
* other emissions will occur only if changes of tables from query will occur during lifetime of
* the {@link Observable}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>Operates on {@link StorIOSQLite#defaultScheduler()} if not {@code null}.</dd>
* </dl>
* <p>
* Please don't forget to unsubscribe from this {@link Observable} because
* it's "Hot" and endless.
*
* @return non-null {@link Observable} which will emit non-null
* list with mapped results and will be subscribed to changes of tables from query.
*/
@NonNull
@CheckResult
@Override
public Observable<Cursor> asRxObservable() {
throwExceptionIfRxJavaIsNotAvailable("asRxObservable()");
final Set<String> tables;
if (query != null) {
tables = new HashSet<String>(1);
tables.add(query.table());
} else if (rawQuery != null) {
tables = rawQuery.observesTables();
} else {
throw new StorIOException("Please specify query");
}
final Observable<Cursor> observable;
if (!tables.isEmpty()) {
observable = storIOSQLite.observeChangesInTables(// each change triggers executeAsBlocking
tables).map(MapSomethingToExecuteAsBlocking.newInstance(this)).startWith(// start stream with first query result
Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))).onBackpressureLatest();
} else {
observable = Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this));
}
return RxJavaUtils.subscribeOn(storIOSQLite, observable);
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class PreparedGetListOfObjects method executeAsBlocking.
/**
* Executes Get Operation immediately in current thread.
* <p>
* Notice: This is blocking I/O operation that should not be executed on the Main Thread,
* it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
* So please, call this method on some background thread. See {@link WorkerThread}.
*
* @return non-null, immutable {@link List} with mapped results, list can be empty.
*/
@WorkerThread
@SuppressWarnings({ "TryFinallyCanBeTryWithResources", "unchecked" })
// Min SDK :( unchecked for empty list
@NonNull
@Override
public List<T> executeAsBlocking() {
try {
final GetResolver<T> getResolver;
if (explicitGetResolver != null) {
getResolver = explicitGetResolver;
} else {
final SQLiteTypeMapping<T> typeMapping = storIOSQLite.lowLevel().typeMapping(type);
if (typeMapping == null) {
throw new IllegalStateException("This type does not have type mapping: " + "type = " + type + "," + "db was not touched by this operation, please add type mapping for this type");
}
getResolver = typeMapping.getResolver();
}
final Cursor cursor;
if (query != null) {
cursor = getResolver.performGet(storIOSQLite, query);
} else if (rawQuery != null) {
cursor = getResolver.performGet(storIOSQLite, rawQuery);
} else {
throw new IllegalStateException("Please specify query");
}
try {
final int count = cursor.getCount();
if (count == 0) {
// it's immutable
return EMPTY_LIST;
}
final List<T> list = new ArrayList<T>(count);
while (cursor.moveToNext()) {
list.add(getResolver.mapFromCursor(cursor));
}
return unmodifiableList(list);
} finally {
cursor.close();
}
} catch (Exception exception) {
throw new StorIOException("Error has occurred during Get operation. query = " + (query != null ? query : rawQuery), exception);
}
}
Aggregations