Raw queries in Kotlin
This commit is contained in:
parent
a122d817e8
commit
35748fc1f3
@ -75,7 +75,7 @@ public class DatabaseHelper {
|
|||||||
return db.get()
|
return db.get()
|
||||||
.listOfObjects(Manga.class)
|
.listOfObjects(Manga.class)
|
||||||
.withQuery(RawQuery.builder()
|
.withQuery(RawQuery.builder()
|
||||||
.query(LibraryMangaGetResolver.QUERY)
|
.query(RawQueriesKt.getLibraryQuery())
|
||||||
.observesTables(MangaTable.TABLE, ChapterTable.TABLE, MangaCategoryTable.TABLE)
|
.observesTables(MangaTable.TABLE, ChapterTable.TABLE, MangaCategoryTable.TABLE)
|
||||||
.build())
|
.build())
|
||||||
.withGetResolver(LibraryMangaGetResolver.INSTANCE)
|
.withGetResolver(LibraryMangaGetResolver.INSTANCE)
|
||||||
@ -168,7 +168,7 @@ public class DatabaseHelper {
|
|||||||
return db.get()
|
return db.get()
|
||||||
.listOfObjects(MangaChapter.class)
|
.listOfObjects(MangaChapter.class)
|
||||||
.withQuery(RawQuery.builder()
|
.withQuery(RawQuery.builder()
|
||||||
.query(MangaChapterGetResolver.getRecentChaptersQuery(date))
|
.query(RawQueriesKt.getRecentsQuery(date))
|
||||||
.observesTables(ChapterTable.TABLE)
|
.observesTables(ChapterTable.TABLE)
|
||||||
.build())
|
.build())
|
||||||
.withGetResolver(MangaChapterGetResolver.INSTANCE)
|
.withGetResolver(MangaChapterGetResolver.INSTANCE)
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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"
|
@ -5,45 +5,12 @@ import android.support.annotation.NonNull;
|
|||||||
|
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga;
|
import eu.kanade.tachiyomi.data.database.models.Manga;
|
||||||
import eu.kanade.tachiyomi.data.database.models.MangaStorIOSQLiteGetResolver;
|
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;
|
import eu.kanade.tachiyomi.data.database.tables.MangaTable;
|
||||||
|
|
||||||
public class LibraryMangaGetResolver extends MangaStorIOSQLiteGetResolver {
|
public class LibraryMangaGetResolver extends MangaStorIOSQLiteGetResolver {
|
||||||
|
|
||||||
public static final LibraryMangaGetResolver INSTANCE = new LibraryMangaGetResolver();
|
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
|
@Override
|
||||||
@NonNull
|
@NonNull
|
||||||
public Manga mapFromCursor(@NonNull Cursor cursor) {
|
public Manga mapFromCursor(@NonNull Cursor cursor) {
|
||||||
|
@ -5,34 +5,16 @@ import android.support.annotation.NonNull;
|
|||||||
|
|
||||||
import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver;
|
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.Chapter;
|
||||||
import eu.kanade.tachiyomi.data.database.models.ChapterStorIOSQLiteGetResolver;
|
import eu.kanade.tachiyomi.data.database.models.ChapterStorIOSQLiteGetResolver;
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga;
|
import eu.kanade.tachiyomi.data.database.models.Manga;
|
||||||
import eu.kanade.tachiyomi.data.database.models.MangaChapter;
|
import eu.kanade.tachiyomi.data.database.models.MangaChapter;
|
||||||
import eu.kanade.tachiyomi.data.database.models.MangaStorIOSQLiteGetResolver;
|
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<MangaChapter> {
|
public class MangaChapterGetResolver extends DefaultGetResolver<MangaChapter> {
|
||||||
|
|
||||||
public static final MangaChapterGetResolver INSTANCE = new MangaChapterGetResolver();
|
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
|
@NonNull
|
||||||
private final MangaStorIOSQLiteGetResolver mangaGetResolver;
|
private final MangaStorIOSQLiteGetResolver mangaGetResolver;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user