Search in sources :

Example 11 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom43To44_containsCorrectData.

@Test
public void migrationFrom43To44_containsCorrectData() throws IOException {
    // First version that uses Room, so can use migration test helper
    SupportSQLiteDatabase db = migrationTestHelper.createDatabase(TEST_DB_NAME, SgRoomDatabase.VERSION_43_ROOM);
    RoomDatabaseTestHelper.insertShow(SHOW, db);
    RoomDatabaseTestHelper.insertSeason(SEASON, db);
    RoomDatabaseTestHelper.insertEpisode(EPISODE, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), db);
    db.close();
    assertTestData_series_seasons_episodes(getMigratedDatabase(SgRoomDatabase.VERSION_44_RECREATE_SERIES_EPISODES));
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Test(org.junit.Test)

Example 12 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom45To46_containsCorrectData.

@Test
public void migrationFrom45To46_containsCorrectData() throws IOException {
    SupportSQLiteDatabase db = migrationTestHelper.createDatabase(TEST_DB_NAME, SgRoomDatabase.VERSION_45_RECREATE_SEASONS);
    RoomDatabaseTestHelper.insertShow(SHOW, db);
    RoomDatabaseTestHelper.insertSeason(SEASON, db);
    RoomDatabaseTestHelper.insertEpisode(EPISODE, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), db);
    db.close();
    db = getMigratedDatabase(SgRoomDatabase.VERSION_46_SERIES_SLUG);
    assertTestData_series_seasons_episodes(db);
    queryAndAssert(db, "SELECT series_slug FROM series", seriesQuery -> assertThat(seriesQuery.isNull(0)).isTrue());
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Test(org.junit.Test)

Example 13 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom42To43_containsCorrectData.

@Test
public void migrationFrom42To43_containsCorrectData() throws IOException {
    // Create the database with the initial version 42 schema and insert test data
    insertTestDataSqlite();
    // Re-open the database with version 43 and
    // provide MIGRATION_42_43 as the migration process.
    SupportSQLiteDatabase database = migrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 43, false, /* adding FTS table ourselves */
    MIGRATION_42_43);
    assertTestData_series_seasons_episodes(database);
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Test(org.junit.Test)

Example 14 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class SeriesGuideDatabase method rebuildFtsTable.

// Upgrading from versions older than 34 is no longer supported. Keeping upgrade code for reference.
// /**
// * See {@link #DBVER_34_TRAKT_V2}.
// */
// private static void upgradeToThirtyFour(SQLiteDatabase db) {
// // add new columns
// db.beginTransaction();
// try {
// // shows
// if (isTableColumnMissing(db, Tables.SHOWS, Shows.RELEASE_TIMEZONE)) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN "
// + Shows.RELEASE_TIMEZONE + " TEXT;");
// }
// if (isTableColumnMissing(db, Tables.SHOWS, Shows.RATING_VOTES)) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN "
// + Shows.RATING_VOTES + " INTEGER;");
// }
// if (isTableColumnMissing(db, Tables.SHOWS, Shows.RATING_USER)) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN "
// + Shows.RATING_USER + " INTEGER;");
// }
// 
// // episodes
// if (isTableColumnMissing(db, Tables.EPISODES, Episodes.RATING_VOTES)) {
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN "
// + Episodes.RATING_VOTES + " INTEGER;");
// }
// if (isTableColumnMissing(db, Tables.EPISODES, Episodes.RATING_USER)) {
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN "
// + Episodes.RATING_USER + " INTEGER;");
// }
// 
// // movies
// if (isTableColumnMissing(db, Tables.MOVIES, Movies.RATING_USER)) {
// db.execSQL("ALTER TABLE " + Tables.MOVIES + " ADD COLUMN "
// + Movies.RATING_USER + " INTEGER;");
// }
// 
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// }
// 
// // migrate existing data to new formats
// Cursor query = db.query(Tables.SHOWS,
// new String[]{Shows._ID, Shows.RELEASE_TIME, Shows.RELEASE_WEEKDAY}, null, null,
// null, null, null);
// 
// // create calendar, set to custom time zone
// Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-08:00"));
// ContentValues values = new ContentValues();
// 
// db.beginTransaction();
// try {
// while (query.moveToNext()) {
// // time changed from ms to encoded local time
// long timeOld = query.getLong(1);
// int timeNew;
// if (timeOld != -1) {
// calendar.setTimeInMillis(timeOld);
// int hour = calendar.get(Calendar.HOUR_OF_DAY);
// int minute = calendar.get(Calendar.MINUTE);
// timeNew = hour * 100 + minute;
// } else {
// timeNew = -1;
// }
// values.put(Shows.RELEASE_TIME, timeNew);
// 
// // week day changed from string to int
// String weekDayOld = query.getString(2);
// int weekDayNew = TimeTools.parseShowReleaseWeekDay(weekDayOld);
// values.put(Shows.RELEASE_WEEKDAY, weekDayNew);
// 
// db.update(Tables.SHOWS, values, Shows._ID + "=" + query.getInt(0), null);
// }
// 
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// query.close();
// }
// }
// 
// /**
// * Add shows and movies title column without articles.
// */
// private static void upgradeToThirtyThree(SQLiteDatabase db) {
// /*
// Add new columns. Added existence checks as 14.0.3 update botched upgrade process.
// */
// if (isTableColumnMissing(db, Tables.SHOWS, Shows.TITLE_NOARTICLE)) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + Shows.TITLE_NOARTICLE
// + " TEXT;");
// }
// if (isTableColumnMissing(db, Tables.MOVIES, Movies.TITLE_NOARTICLE)) {
// db.execSQL("ALTER TABLE " + Tables.MOVIES + " ADD COLUMN " + Movies.TITLE_NOARTICLE
// + " TEXT;");
// }
// 
// // shows
// Cursor shows = db.query(Tables.SHOWS, new String[]{Shows._ID, Shows.TITLE}, null, null,
// null, null, null);
// ContentValues newTitleValues = new ContentValues();
// if (shows != null) {
// db.beginTransaction();
// try {
// while (shows.moveToNext()) {
// // put overwrites previous value
// newTitleValues.put(Shows.TITLE_NOARTICLE,
// DBUtils.trimLeadingArticle(shows.getString(1)));
// db.update(Tables.SHOWS, newTitleValues, Shows._ID + "=" + shows.getInt(0),
// null);
// }
// 
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// }
// shows.close();
// }
// 
// newTitleValues.clear();
// 
// // movies
// Cursor movies = db.query(Tables.MOVIES, new String[]{Movies._ID, Movies.TITLE}, null,
// null, null, null, null);
// if (movies != null) {
// db.beginTransaction();
// try {
// while (movies.moveToNext()) {
// // put overwrites previous value
// newTitleValues.put(Movies.TITLE_NOARTICLE,
// DBUtils.trimLeadingArticle(movies.getString(1)));
// db.update(Tables.MOVIES, newTitleValues, Movies._ID + "=" + movies.getInt(0),
// null);
// }
// 
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// }
// movies.close();
// }
// }
// 
// /**
// * Add movies table.
// */
// private static void upgradeToThirtyTwo(SQLiteDatabase db) {
// if (!isTableExisting(db, Tables.MOVIES)) {
// db.execSQL(CREATE_MOVIES_TABLE);
// }
// }
// 
// // Must be watched and have an airdate
// private static final String LATEST_SELECTION = Episodes.WATCHED + "=1 AND "
// + Episodes.FIRSTAIREDMS + "!=-1 AND " + Shows.REF_SHOW_ID + "=?";
// 
// // Latest aired first (ensures we get specials), if equal sort by season,
// // then number
// private static final String LATEST_ORDER = Episodes.FIRSTAIREDMS + " DESC,"
// + Episodes.SEASON + " DESC,"
// + Episodes.NUMBER + " DESC";
// 
// /**
// * Add {@link Shows} column to store the last watched episode id for better prediction of next
// * episode.
// */
// private static void upgradeToThirtyOne(SQLiteDatabase db) {
// if (isTableColumnMissing(db, Tables.SHOWS, Shows.LASTWATCHEDID)) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + Shows.LASTWATCHEDID
// + " INTEGER DEFAULT 0;");
// }
// 
// // pre populate with latest watched episode ids
// ContentValues values = new ContentValues();
// final Cursor shows = db.query(Tables.SHOWS, new String[]{
// Shows._ID,
// }, null, null, null, null, null);
// if (shows != null) {
// db.beginTransaction();
// try {
// while (shows.moveToNext()) {
// final String showId = shows.getString(0);
// final Cursor highestWatchedEpisode = db.query(Tables.EPISODES, new String[]{
// Episodes._ID
// }, LATEST_SELECTION, new String[]{
// showId
// }, null, null, LATEST_ORDER);
// 
// if (highestWatchedEpisode != null) {
// if (highestWatchedEpisode.moveToFirst()) {
// values.put(Shows.LASTWATCHEDID, highestWatchedEpisode.getInt(0));
// db.update(Tables.SHOWS, values, Shows._ID + "=?", new String[]{
// showId
// });
// values.clear();
// }
// 
// highestWatchedEpisode.close();
// }
// }
// 
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// }
// 
// shows.close();
// }
// }
// 
// /**
// * Add {@link Episodes} column to store absolute episode number.
// */
// private static void upgradeToThirty(SQLiteDatabase db) {
// if (isTableColumnMissing(db, Tables.EPISODES, Episodes.ABSOLUTE_NUMBER)) {
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN "
// + Episodes.ABSOLUTE_NUMBER + " INTEGER;");
// }
// }
// 
// /**
// * Add tables to store lists and list items.
// */
// private static void upgradeToTwentyEight(SQLiteDatabase db) {
// db.execSQL(CREATE_LISTS_TABLE);
// 
// db.execSQL(CREATE_LIST_ITEMS_TABLE);
// }
// 
// /**
// * Add {@link Episodes} columns for storing its IMDb id and last time of edit on theTVDB.com.
// * Add {@link Shows} column for storing last time of edit as well.
// */
// private static void upgradeToTwentySeven(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.LASTEDIT
// + " INTEGER DEFAULT 0;");
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN " + EpisodesColumns.IMDBID
// + " TEXT DEFAULT '';");
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN " + EpisodesColumns.LAST_EDITED
// + " INTEGER DEFAULT 0;");
// }
// 
// /**
// * Add a {@link Episodes} column for storing whether an episode was collected in digital or
// * physical form.
// */
// private static void upgradeToTwentySix(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN " + EpisodesColumns.COLLECTED
// + " INTEGER DEFAULT 0;");
// }
// 
// /**
// * Add a {@link Shows} column for storing the next air date in ms as integer data type rather
// * than as text.
// */
// private static void upgradeToTwentyFive(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.NEXTAIRDATEMS
// + " INTEGER DEFAULT 0;");
// }
// 
// /**
// * Adds a column to the {@link Tables#EPISODES} table to store the airdate and possibly time in
// * milliseconds.
// */
// private static void upgradeToTwentyFour(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.EPISODES + " ADD COLUMN " + EpisodesColumns.FIRSTAIREDMS
// + " INTEGER DEFAULT -1;");
// 
// // populate the new column from existing data
// final Cursor shows = db.query(Tables.SHOWS, new String[]{
// Shows._ID
// }, null, null, null, null, null);
// 
// while (shows.moveToNext()) {
// final String showId = shows.getString(0);
// 
// //noinspection deprecation
// final Cursor episodes = db.query(Tables.EPISODES, new String[]{
// Episodes._ID, Episodes.FIRSTAIRED
// }, Shows.REF_SHOW_ID + "=?", new String[]{
// showId
// }, null, null, null);
// 
// db.beginTransaction();
// try {
// ContentValues values = new ContentValues();
// ZoneId defaultShowTimeZone = TimeTools.getDateTimeZone(null);
// LocalTime defaultShowReleaseTime = TimeTools.getShowReleaseTime(-1);
// String deviceTimeZone = TimeZone.getDefault().getID();
// while (episodes.moveToNext()) {
// String firstAired = episodes.getString(1);
// long episodeAirtime = TimeTools.parseEpisodeReleaseDate(null,
// defaultShowTimeZone, firstAired, defaultShowReleaseTime, null, null,
// deviceTimeZone);
// 
// values.put(Episodes.FIRSTAIREDMS, episodeAirtime);
// db.update(Tables.EPISODES, values, Episodes._ID + "=?", new String[]{
// episodes.getString(0)
// });
// values.clear();
// }
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// }
// 
// episodes.close();
// }
// 
// shows.close();
// }
// 
// /**
// * Adds a column to the {@link Tables#SHOWS} table similar to the favorite boolean, but to allow
// * hiding shows.
// */
// private static void upgradeToTwentyThree(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.HIDDEN
// + " INTEGER DEFAULT 0;");
// }
// 
// /**
// * Add a column to store the last time a show has been updated to allow for more precise control
// * over which shows should get updated. This is in conjunction with a 7 day limit when a show
// * will get updated regardless if it has been marked as updated or not.
// */
// private static void upgradeToTwentyTwo(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.LASTUPDATED
// + " INTEGER DEFAULT 0;");
// }
// 
// private static void upgradeToTwentyOne(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.RELEASE_COUNTRY
// + " TEXT DEFAULT '';");
// }
// 
// private static void upgradeToTwenty(SQLiteDatabase db) {
// db.execSQL(
// "ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.HEXAGON_MERGE_COMPLETE
// + " INTEGER DEFAULT 1;");
// }
// 
// /**
// * In version 19 the season integer column totalcount was added.
// */
// private static void upgradeToNineteen(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SEASONS + " ADD COLUMN " + SeasonsColumns.TOTALCOUNT
// + " INTEGER DEFAULT 0;");
// }
// 
// /**
// * In version 18 the series text column nextairdatetext was added.
// */
// private static void upgradeToEighteen(SQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.NEXTAIRDATETEXT
// + " TEXT DEFAULT '';");
// 
// // convert status text to 0/1 integer
// final Cursor shows = db.query(Tables.SHOWS, new String[]{
// Shows._ID, Shows.STATUS
// }, null, null, null, null, null);
// final ContentValues values = new ContentValues();
// String status;
// 
// db.beginTransaction();
// try {
// while (shows.moveToNext()) {
// status = shows.getString(1);
// if (status.length() == 10) {
// status = "1";
// } else if (status.length() == 5) {
// status = "0";
// } else {
// status = "";
// }
// values.put(Shows.STATUS, status);
// db.update(Tables.SHOWS, values, Shows._ID + "=?", new String[]{
// shows.getString(0)
// });
// values.clear();
// }
// db.setTransactionSuccessful();
// } finally {
// db.endTransaction();
// }
// 
// shows.close();
// }
// 
// /**
// * In version 17 the series boolean column favorite was added.
// */
// private static void upgradeToSeventeen(@NonNull SupportSQLiteDatabase db) {
// db.execSQL("ALTER TABLE " + Tables.SHOWS + " ADD COLUMN " + ShowsColumns.FAVORITE
// + " INTEGER DEFAULT 0;");
// }
/**
 * Drops the current {@link Tables#EPISODES_SEARCH} table and re-creates it with current data
 * from {@link Tables#SG_EPISODE}.
 */
public static void rebuildFtsTable(Context context) {
    Timber.d("Renewing FTS table");
    SupportSQLiteDatabase db = SgRoomDatabase.getInstance(context).getOpenHelper().getWritableDatabase();
    if (!recreateFtsTable(db)) {
        return;
    }
    rebuildFtsTableJellyBean(db);
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase)

Example 15 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class SeriesGuideProvider method insertInTransaction.

/**
 * @param bulkInsert It seems to happen on occasion that TVDB has duplicate episodes, also
 * backup files may contain duplicates. Handle them by making the last insert win (ON CONFLICT
 * REPLACE) for bulk inserts.
 */
private Uri insertInTransaction(SgRoomDatabase room, Uri uri, ContentValues values, boolean bulkInsert) {
    if (LOGV) {
        Timber.v("insert(uri=%s, values=%s)", uri, values.toString());
    }
    Uri notifyUri = null;
    final SupportSQLiteDatabase db = room.getOpenHelper().getWritableDatabase();
    final int match = sUriMatcher.match(uri);
    switch(match) {
        case SHOWS:
            {
                long id = tryInsert(db, Tables.SHOWS, CONFLICT_NONE, values);
                if (id < 0) {
                    break;
                }
                notifyUri = Shows.buildShowUri(values.getAsString(Shows._ID));
                break;
            }
        case SEASONS:
            {
                long id;
                if (bulkInsert) {
                    id = tryInsert(db, Tables.SEASONS, CONFLICT_REPLACE, values);
                } else {
                    id = tryInsert(db, Tables.SEASONS, CONFLICT_NONE, values);
                }
                if (id < 0) {
                    break;
                }
                notifyUri = Seasons.buildSeasonUri(values.getAsString(Seasons._ID));
                break;
            }
        case EPISODES:
            {
                long id;
                if (bulkInsert) {
                    id = tryInsert(db, Tables.EPISODES, CONFLICT_REPLACE, values);
                } else {
                    id = tryInsert(db, Tables.EPISODES, CONFLICT_NONE, values);
                }
                if (id < 0) {
                    break;
                }
                notifyUri = Episodes.buildEpisodeUri(values.getAsString(Episodes._ID));
                break;
            }
        case LISTS:
            {
                long id = tryInsert(db, Tables.LISTS, CONFLICT_REPLACE, values);
                if (id < 0) {
                    break;
                }
                notifyUri = Lists.buildListUri(values.getAsString(Lists.LIST_ID));
                break;
            }
        case LIST_ITEMS:
            {
                long id;
                id = tryInsert(db, Tables.LIST_ITEMS, CONFLICT_REPLACE, values);
                if (id < 0) {
                    break;
                }
                notifyUri = ListItems.buildListItemUri(values.getAsString(ListItems.LIST_ITEM_ID));
                break;
            }
        case MOVIES:
            {
                long id = tryInsert(db, Tables.MOVIES, CONFLICT_REPLACE, values);
                if (id < 0) {
                    break;
                }
                notifyUri = Movies.buildMovieUri(values.getAsInteger(Movies.TMDB_ID));
                break;
            }
        case ACTIVITY:
            {
                long id = tryInsert(db, Tables.ACTIVITY, CONFLICT_REPLACE, values);
                if (id < 0) {
                    break;
                }
                notifyUri = Activity.buildActivityUri(values.getAsString(Activity.EPISODE_TVDB_OR_TMDB_ID));
                break;
            }
        case JOBS:
            {
                long id = tryInsert(db, Tables.JOBS, CONFLICT_REPLACE, values);
                if (id < 0) {
                    break;
                }
                notifyUri = Jobs.buildJobUri(id);
                break;
            }
        default:
            {
                throw new IllegalArgumentException("Unknown uri: " + uri);
            }
    }
    return notifyUri;
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Uri(android.net.Uri)

Aggregations

SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)24 Test (org.junit.Test)16 Cursor (android.database.Cursor)5 ContentValues (android.content.ContentValues)4 SupportSQLiteOpenHelper (androidx.sqlite.db.SupportSQLiteOpenHelper)3 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteException (android.database.sqlite.SQLiteException)1 Uri (android.net.Uri)1 SupportSQLiteQuery (androidx.sqlite.db.SupportSQLiteQuery)1 FrameworkSQLiteOpenHelperFactory (androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory)1 SqlDriver (app.cash.sqldelight.db.SqlDriver)1 AndroidSqliteDriver (app.cash.sqldelight.driver.android.AndroidSqliteDriver)1 CloudEntry (com.amaze.filemanager.database.models.explorer.CloudEntry)1 TestEpisode (com.battlelancer.seriesguide.provider.RoomDatabaseTestHelper.TestEpisode)1 MovieDetails (com.battlelancer.seriesguide.ui.movies.MovieDetails)1 SelectionBuilder (com.battlelancer.seriesguide.util.SelectionBuilder)1