package com.satsumasoftware.timetable.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public final class TimetableDbHelper extends SQLiteOpenHelper {

    private static TimetableDbHelper sInstance;

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Timetable.db";

    public static synchronized TimetableDbHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new TimetableDbHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    private TimetableDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(AssignmentsSchema.SQL_CREATE);
        db.execSQL(ClassDetailsSchema.SQL_CREATE);
        db.execSQL(ClassesSchema.SQL_CREATE);
        db.execSQL(ClassTimesSchema.SQL_CREATE);
        db.execSQL(ExamsSchema.SQL_CREATE);
        db.execSQL(SubjectsSchema.SQL_CREATE);
        db.execSQL(TermsSchema.SQL_CREATE);
        db.execSQL(TimetablesSchema.SQL_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(AssignmentsSchema.SQL_DELETE);
        db.execSQL(ClassDetailsSchema.SQL_DELETE);
        db.execSQL(ClassesSchema.SQL_DELETE);
        db.execSQL(ClassTimesSchema.SQL_DELETE);
        db.execSQL(ExamsSchema.SQL_DELETE);
        db.execSQL(SubjectsSchema.SQL_DELETE);
        db.execSQL(TermsSchema.SQL_DELETE);
        db.execSQL(TimetablesSchema.SQL_DELETE);
        onCreate(db);
    }

}

@@ -163,16 +163,102 @@
 nHelper;
+%0Aimport android.util.Log;%0A%0Aimport com.satsumasoftware.timetable.db.util.SchemaUtilsKt;
 %0A%0Apublic
@@ -417,9 +417,9 @@
 N = 
-1
+2
 ;%0A  
@@ -1388,58 +1388,148 @@
     
-db.execSQL(AssignmentsSchema.SQL_DELETE
+Log.i(%22TimetableDbHelper%22, %22onUpgrade() called with oldVersion %22 + oldVersion +%0A                %22 and new version %22 + newVersion
 );%0A
+%0A
         
 db.e
@@ -1528,351 +1528,803 @@
     
-db.execSQL(ClassDetailsSchema.SQL_DELETE);%0A        db.execSQL(ClassesSchema.SQL_DELETE);%0A        db.execSQL(ClassTimesSchema.SQL_DELETE);%0A        db.execSQL(ExamsSchema.SQL_DELETE);%0A        db.execSQL(SubjectsSchema.SQL_DELETE);%0A        db.execSQL(TermsSchema.SQL_DELETE);%0A        db.execSQL(TimetablesSchema.SQL_DELETE);%0A        onCreate(db);
+/*%0A         * Note that there are no breaks in the switch statement below (except for the last case).%0A         *%0A         * This means, for example, if installed version is 2 onUpgrade will get called with%0A         * oldVersion as 2. The switch case will execute cases 2, 3, 4, etc as the break%0A         * statements are missing in all the older cases.%0A         */%0A%0A        switch (oldVersion) %7B%0A            case 1:%0A                db.execSQL(%22ALTER TABLE %22 + SubjectsSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        SubjectsSchema.COL_ABBREVIATION + SchemaUtilsKt.TEXT_TYPE + %22 DEFAULT ''%22);%0A                break;%0A%0A            default:%0A                throw new IllegalArgumentException(%22onUpgrade() called with unknown oldVersion %22%0A                        + oldVersion);%0A        %7D
 %0A   

Modify database to add subject abbreviation column