From 35748fc1f32e1f39cd94576172403d3431354de7 Mon Sep 17 00:00:00 2001 From: len Date: Fri, 18 Mar 2016 23:23:56 +0100 Subject: [PATCH] Raw queries in Kotlin --- .../data/database/DatabaseHelper.java | 4 +- .../tachiyomi/data/database/DbOpenHelper.java | 47 ------------------- .../tachiyomi/data/database/DbOpenHelper.kt | 43 +++++++++++++++++ .../tachiyomi/data/database/RawQueries.kt | 40 ++++++++++++++++ .../resolvers/LibraryMangaGetResolver.java | 33 ------------- .../resolvers/MangaChapterGetResolver.java | 18 ------- 6 files changed, 85 insertions(+), 100 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/database/RawQueries.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java index 0be54b2d2..6de7b6689 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java @@ -75,7 +75,7 @@ public class DatabaseHelper { return db.get() .listOfObjects(Manga.class) .withQuery(RawQuery.builder() - .query(LibraryMangaGetResolver.QUERY) + .query(RawQueriesKt.getLibraryQuery()) .observesTables(MangaTable.TABLE, ChapterTable.TABLE, MangaCategoryTable.TABLE) .build()) .withGetResolver(LibraryMangaGetResolver.INSTANCE) @@ -168,7 +168,7 @@ public class DatabaseHelper { return db.get() .listOfObjects(MangaChapter.class) .withQuery(RawQuery.builder() - .query(MangaChapterGetResolver.getRecentChaptersQuery(date)) + .query(RawQueriesKt.getRecentsQuery(date)) .observesTables(ChapterTable.TABLE) .build()) .withGetResolver(MangaChapterGetResolver.INSTANCE) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.java deleted file mode 100644 index 3fefdd98c..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.java +++ /dev/null @@ -1,47 +0,0 @@ -package eu.kanade.tachiyomi.data.database; - -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.support.annotation.NonNull; - -import eu.kanade.tachiyomi.data.database.tables.CategoryTable; -import eu.kanade.tachiyomi.data.database.tables.ChapterTable; -import eu.kanade.tachiyomi.data.database.tables.MangaCategoryTable; -import eu.kanade.tachiyomi.data.database.tables.MangaSyncTable; -import eu.kanade.tachiyomi.data.database.tables.MangaTable; - -public class DbOpenHelper extends SQLiteOpenHelper { - - public static final String DATABASE_NAME = "tachiyomi.db"; - public static final int DATABASE_VERSION = 1; - - public DbOpenHelper(@NonNull Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } - - @Override - public void onCreate(@NonNull SQLiteDatabase db) { - db.execSQL(MangaTable.getCreateTableQuery()); - db.execSQL(ChapterTable.getCreateTableQuery()); - db.execSQL(MangaSyncTable.getCreateTableQuery()); - db.execSQL(CategoryTable.getCreateTableQuery()); - db.execSQL(MangaCategoryTable.getCreateTableQuery()); - - // DB indexes - db.execSQL(MangaTable.getCreateUrlIndexQuery()); - db.execSQL(MangaTable.getCreateFavoriteIndexQuery()); - db.execSQL(ChapterTable.getCreateMangaIdIndexQuery()); - } - - @Override - public void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion) { - - } - - @Override - public void onConfigure(@NonNull SQLiteDatabase db) { - db.setForeignKeyConstraintsEnabled(true); - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.kt new file mode 100644 index 000000000..35b993cc4 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.kt @@ -0,0 +1,43 @@ +package eu.kanade.tachiyomi.data.database + +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import eu.kanade.tachiyomi.data.database.tables.* + +class DbOpenHelper(context: Context) : SQLiteOpenHelper(context, DbOpenHelper.DATABASE_NAME, null, DbOpenHelper.DATABASE_VERSION) { + + companion object { + /** + * Name of the database file. + */ + const val DATABASE_NAME = "tachiyomi.db" + + /** + * Version of the database. + */ + const val DATABASE_VERSION = 1 + } + + override fun onCreate(db: SQLiteDatabase) = with(db) { + execSQL(MangaTable.getCreateTableQuery()) + execSQL(ChapterTable.getCreateTableQuery()) + execSQL(MangaSyncTable.getCreateTableQuery()) + execSQL(CategoryTable.getCreateTableQuery()) + execSQL(MangaCategoryTable.getCreateTableQuery()) + + // DB indexes + execSQL(MangaTable.getCreateUrlIndexQuery()) + execSQL(MangaTable.getCreateFavoriteIndexQuery()) + execSQL(ChapterTable.getCreateMangaIdIndexQuery()) + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + + } + + override fun onConfigure(db: SQLiteDatabase) { + db.setForeignKeyConstraintsEnabled(true) + } + +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/RawQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/RawQueries.kt new file mode 100644 index 000000000..0e4342418 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/RawQueries.kt @@ -0,0 +1,40 @@ +package eu.kanade.tachiyomi.data.database + +import java.util.* +import eu.kanade.tachiyomi.data.database.tables.ChapterTable as Chapter +import eu.kanade.tachiyomi.data.database.tables.MangaCategoryTable as MangaCategory +import eu.kanade.tachiyomi.data.database.tables.MangaTable as Manga + +/** + * Query to get the manga from the library, with their categories and unread count. + */ +val libraryQuery = + "SELECT M.*, COALESCE(MC.${MangaCategory.COLUMN_CATEGORY_ID}, 0) AS ${Manga.COLUMN_CATEGORY} " + + "FROM (" + + "SELECT ${Manga.TABLE}.*, COALESCE(C.unread, 0) AS ${Manga.COLUMN_UNREAD} " + + "FROM ${Manga.TABLE} " + + "LEFT JOIN (" + + "SELECT ${Chapter.COLUMN_MANGA_ID}, COUNT(*) AS unread " + + "FROM ${Chapter.TABLE} " + + "WHERE ${Chapter.COLUMN_READ} = 0 " + + "GROUP BY ${Chapter.COLUMN_MANGA_ID}" + + ") AS C " + + "ON ${Manga.COLUMN_ID} = C.${Chapter.COLUMN_MANGA_ID} " + + "WHERE ${Manga.COLUMN_FAVORITE} = 1 " + + "GROUP BY ${Manga.COLUMN_ID} " + + "ORDER BY ${Manga.COLUMN_TITLE}" + + ") AS M " + + "LEFT JOIN (" + + "SELECT * FROM ${MangaCategory.TABLE}) AS MC " + + "ON MC.${MangaCategory.COLUMN_MANGA_ID} = M.${Manga.COLUMN_ID}" + +/** + * Query to get the recent chapters of manga from the library up to a date. + * + * @param date the delimiting date. + */ +fun getRecentsQuery(date: Date): String = + "SELECT * FROM ${Manga.TABLE} JOIN ${Chapter.TABLE} " + + "ON ${Manga.TABLE}.${Manga.COLUMN_ID} = ${Chapter.TABLE}.${Chapter.COLUMN_MANGA_ID} " + + "WHERE ${Manga.COLUMN_FAVORITE} = 1 AND ${Chapter.COLUMN_DATE_UPLOAD} > ${date.time} " + + "ORDER BY ${Chapter.COLUMN_DATE_UPLOAD} DESC" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.java index af815de1c..219fcdc58 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.java @@ -5,45 +5,12 @@ import android.support.annotation.NonNull; import eu.kanade.tachiyomi.data.database.models.Manga; import eu.kanade.tachiyomi.data.database.models.MangaStorIOSQLiteGetResolver; -import eu.kanade.tachiyomi.data.database.tables.ChapterTable; -import eu.kanade.tachiyomi.data.database.tables.MangaCategoryTable; import eu.kanade.tachiyomi.data.database.tables.MangaTable; public class LibraryMangaGetResolver extends MangaStorIOSQLiteGetResolver { public static final LibraryMangaGetResolver INSTANCE = new LibraryMangaGetResolver(); - public static final String QUERY = String.format( - "SELECT M.*, COALESCE(MC.%10$s, 0) AS %12$s " + - "FROM (" + - "SELECT %1$s.*, COALESCE(C.unread, 0) AS %6$s " + - "FROM %1$s " + - "LEFT JOIN (" + - "SELECT %5$s, COUNT(*) AS unread " + - "FROM %2$s " + - "WHERE %7$s = 0 " + - "GROUP BY %5$s" + - ") AS C " + - "ON %4$s = C.%5$s " + - "WHERE %8$s = 1 " + - "GROUP BY %4$s " + - "ORDER BY %9$s" + - ") AS M " + - "LEFT JOIN (SELECT * FROM %3$s) AS MC ON MC.%11$s = M.%4$s", - MangaTable.TABLE, - ChapterTable.TABLE, - MangaCategoryTable.TABLE, - MangaTable.COLUMN_ID, - ChapterTable.COLUMN_MANGA_ID, - MangaTable.COLUMN_UNREAD, - ChapterTable.COLUMN_READ, - MangaTable.COLUMN_FAVORITE, - MangaTable.COLUMN_TITLE, - MangaCategoryTable.COLUMN_CATEGORY_ID, - MangaCategoryTable.COLUMN_MANGA_ID, - MangaTable.COLUMN_CATEGORY - ); - @Override @NonNull public Manga mapFromCursor(@NonNull Cursor cursor) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java index 2313f6bd3..35fbdc62d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java @@ -5,34 +5,16 @@ import android.support.annotation.NonNull; import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver; -import java.util.Date; - import eu.kanade.tachiyomi.data.database.models.Chapter; import eu.kanade.tachiyomi.data.database.models.ChapterStorIOSQLiteGetResolver; import eu.kanade.tachiyomi.data.database.models.Manga; import eu.kanade.tachiyomi.data.database.models.MangaChapter; import eu.kanade.tachiyomi.data.database.models.MangaStorIOSQLiteGetResolver; -import eu.kanade.tachiyomi.data.database.tables.ChapterTable; -import eu.kanade.tachiyomi.data.database.tables.MangaTable; public class MangaChapterGetResolver extends DefaultGetResolver { public static final MangaChapterGetResolver INSTANCE = new MangaChapterGetResolver(); - public static final String QUERY = String.format( - "SELECT * FROM %1$s JOIN %2$s on %1$s.%3$s = %2$s.%4$s", - MangaTable.TABLE, - ChapterTable.TABLE, - MangaTable.COLUMN_ID, - ChapterTable.COLUMN_MANGA_ID); - - public static String getRecentChaptersQuery(Date date) { - return QUERY + String.format(" WHERE %1$s = 1 AND %2$s > %3$d ORDER BY %2$s DESC", - MangaTable.COLUMN_FAVORITE, - ChapterTable.COLUMN_DATE_UPLOAD, - date.getTime()); - } - @NonNull private final MangaStorIOSQLiteGetResolver mangaGetResolver;