Skip to content

Commit

Permalink
Add language column to episodes table if it does not exist (jamienico…
Browse files Browse the repository at this point in the history
…l#23)

Add language column to episodes table if it does not exist. Closes jamienicol#22.
  • Loading branch information
red-coracle authored May 12, 2020
1 parent a021aac commit 3374796
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class DatabaseOpenHelper extends SQLiteOpenHelper
{
private static final String name = "episodes.db";
private static final int version = 7;
private static final int version = 8;

private static final String TAG = "DatabaseOpenHelper";

Expand Down
113 changes: 58 additions & 55 deletions app/src/main/java/com/redcoracle/episodes/db/EpisodesTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,72 @@

package com.redcoracle.episodes.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.util.Log;

public class EpisodesTable
{
private static final String TAG = "EpisodesTable";
import java.util.Arrays;

static final String TABLE_NAME = "episodes";
public class EpisodesTable {
private static final String TAG = "EpisodesTable";

public static final String COLUMN_ID = BaseColumns._ID;
public static final String COLUMN_TVDB_ID = "tvdb_id";
public static final String COLUMN_SHOW_ID = "show_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_LANGUAGE = "language";
public static final String COLUMN_OVERVIEW = "overview";
public static final String COLUMN_EPISODE_NUMBER = "episode_number";
public static final String COLUMN_SEASON_NUMBER = "season_number";
public static final String COLUMN_FIRST_AIRED = "first_aired";
public static final String COLUMN_WATCHED = "watched";
static final String TABLE_NAME = "episodes";

public static void onCreate(SQLiteDatabase db) {
String create =
String.format("CREATE TABLE %s (" +
" %s INTEGER PRIMARY KEY," +
" %s INTEGER UNIQUE NOT NULL," +
" %s INTEGER NOT NULL," +
" %s VARCHAR(200) NOT NULL," +
" %s TEXT," +
" %s TEXT," +
" %s INTEGER," +
" %s INTEGER," +
" %s DATE," +
" %s BOOLEAN" +
");",
TABLE_NAME,
COLUMN_ID,
COLUMN_TVDB_ID,
COLUMN_SHOW_ID,
COLUMN_NAME,
COLUMN_LANGUAGE,
COLUMN_OVERVIEW,
COLUMN_EPISODE_NUMBER,
COLUMN_SEASON_NUMBER,
COLUMN_FIRST_AIRED,
COLUMN_WATCHED);
public static final String COLUMN_ID = BaseColumns._ID;
public static final String COLUMN_TVDB_ID = "tvdb_id";
public static final String COLUMN_SHOW_ID = "show_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_LANGUAGE = "language";
public static final String COLUMN_OVERVIEW = "overview";
public static final String COLUMN_EPISODE_NUMBER = "episode_number";
public static final String COLUMN_SEASON_NUMBER = "season_number";
public static final String COLUMN_FIRST_AIRED = "first_aired";
public static final String COLUMN_WATCHED = "watched";

Log.d(TAG, String.format("creating episodes table: %s", create));
public static void onCreate(SQLiteDatabase db) {
String create = String.format("CREATE TABLE %s (" +
"%s INTEGER PRIMARY KEY," +
"%s INTEGER UNIQUE NOT NULL," +
"%s INTEGER NOT NULL," +
"%s VARCHAR(200) NOT NULL," +
"%s TEXT," +
"%s TEXT," +
"%s INTEGER," +
"%s INTEGER," +
"%s DATE," +
"%s BOOLEAN" +
");",
TABLE_NAME,
COLUMN_ID,
COLUMN_TVDB_ID,
COLUMN_SHOW_ID,
COLUMN_NAME,
COLUMN_LANGUAGE,
COLUMN_OVERVIEW,
COLUMN_EPISODE_NUMBER,
COLUMN_SEASON_NUMBER,
COLUMN_FIRST_AIRED,
COLUMN_WATCHED);

db.execSQL(create);
}
Log.d(TAG, String.format("creating episodes table: %s", create));

static void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion) {
switch (oldVersion) {
case 5:
// Add language column
Log.d(TAG, "upgrading episodes table: adding language column");
db.execSQL(String.format("ALTER TABLE %s ADD COLUMN %s TEXT",
TABLE_NAME,
COLUMN_LANGUAGE));
// fall through
}
}
db.execSQL(create);
}

static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 5:
case 6:
case 7:
// Add language column
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
String[] columns = cursor.getColumnNames();
if (!Arrays.asList(columns).contains(COLUMN_LANGUAGE)) {
Log.d(TAG, "upgrading episodes table: adding language column");
db.execSQL(String.format("ALTER TABLE %s ADD COLUMN %s TEXT", TABLE_NAME, COLUMN_LANGUAGE));
}
cursor.close();
}
}
}

0 comments on commit 3374796

Please sign in to comment.