123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package eu.kanade.mangafeed.data.managers;
- import com.pushtorefresh.storio.sqlite.StorIOSQLite;
- import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult;
- import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults;
- import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
- import com.pushtorefresh.storio.sqlite.operations.put.PutResults;
- import com.pushtorefresh.storio.sqlite.queries.Query;
- import com.pushtorefresh.storio.sqlite.queries.RawQuery;
- import java.util.List;
- import eu.kanade.mangafeed.data.models.Manga;
- import eu.kanade.mangafeed.data.tables.ChaptersTable;
- import eu.kanade.mangafeed.data.tables.MangasTable;
- import rx.Observable;
- public class MangaManagerImpl extends BaseManager implements MangaManager {
- public MangaManagerImpl(StorIOSQLite db) {
- super(db);
- }
- private final String mangasWithUnreadQuery = String.format(
- "SELECT %1$s.*, COUNT(C.%4$s) AS %5$s FROM %1$s LEFT JOIN " +
- "(SELECT %4$s FROM %2$s WHERE %6$s = 0) AS C ON %3$s = C.%4$s " +
- "GROUP BY %3$s",
- MangasTable.TABLE,
- ChaptersTable.TABLE,
- MangasTable.TABLE + "." + MangasTable.COLUMN_ID,
- ChaptersTable.COLUMN_MANGA_ID,
- MangasTable.COLUMN_UNREAD,
- ChaptersTable.COLUMN_READ
- );
- public Observable<List<Manga>> getMangas() {
- return db.get()
- .listOfObjects(Manga.class)
- .withQuery(Query.builder()
- .table(MangasTable.TABLE)
- .build())
- .prepare()
- .createObservable();
- }
- public Observable<List<Manga>> getMangasWithUnread() {
- return db.get()
- .listOfObjects(Manga.class)
- .withQuery(RawQuery.builder()
- .query(mangasWithUnreadQuery)
- .observesTables(MangasTable.TABLE, ChaptersTable.TABLE)
- .build())
- .prepare()
- .createObservable();
- }
- public Observable<List<Manga>> getManga(String url) {
- return db.get()
- .listOfObjects(Manga.class)
- .withQuery(Query.builder()
- .table(MangasTable.TABLE)
- .where(MangasTable.COLUMN_URL + "=?")
- .whereArgs(url)
- .build())
- .prepare()
- .createObservable();
- }
- public Observable<List<Manga>> getManga(int id) {
- return null;
- }
- @Override
- public Manga getMangaBlock(String url) {
- List<Manga> result = db.get()
- .listOfObjects(Manga.class)
- .withQuery(Query.builder()
- .table(MangasTable.TABLE)
- .where(MangasTable.COLUMN_URL + "=?")
- .whereArgs(url)
- .build())
- .prepare()
- .executeAsBlocking();
- if (result.isEmpty())
- return null;
- return result.get(0);
- }
- public Observable<PutResult> insertManga(Manga manga) {
- return db.put()
- .object(manga)
- .prepare()
- .createObservable();
- }
- public Observable<PutResults<Manga>> insertMangas(List<Manga> mangas) {
- return db.put()
- .objects(mangas)
- .prepare()
- .createObservable();
- }
- public PutResult insertMangaBlock(Manga manga) {
- return db.put()
- .object(manga)
- .prepare()
- .executeAsBlocking();
- }
- public Observable<DeleteResult> deleteManga(Manga manga) {
- return db.delete()
- .object(manga)
- .prepare()
- .createObservable();
- }
- public Observable<DeleteResults<Manga>> deleteMangas(List<Manga> mangas) {
- return db.delete()
- .objects(mangas)
- .prepare()
- .createObservable();
- }
- }
|