package com.maxiee.heartbeat.database;

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

import com.maxiee.heartbeat.database.tables.EventImageRelationTable;
import com.maxiee.heartbeat.database.tables.EventLabelRelationTable;
import com.maxiee.heartbeat.database.tables.EventThoughtRelationTable;
import com.maxiee.heartbeat.database.tables.EventsTable;
import com.maxiee.heartbeat.database.tables.ImageTable;
import com.maxiee.heartbeat.database.tables.LabelsTable;
import com.maxiee.heartbeat.database.tables.ThoughtsTable;

/**
 * Created by maxiee on 15-6-11.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private final static String DB_NAME = "heartbeat";
    private final static int DB_VER = 2;

    private static DatabaseHelper instance;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VER);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(EventsTable.CREATE);
        db.execSQL(LabelsTable.CREATE);
        db.execSQL(EventLabelRelationTable.CREATE);
        db.execSQL(ThoughtsTable.CREATE);
        db.execSQL(EventThoughtRelationTable.CREATE);
        db.execSQL(ImageTable.CREATE);
        db.execSQL(EventImageRelationTable.CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1) {
            db.execSQL(ImageTable.CREATE);
            db.execSQL(EventImageRelationTable.CREATE);
        }
    }

    public static synchronized DatabaseHelper instance(Context context) {
        if (instance == null) {
            instance = new DatabaseHelper(context);
        }

        return instance;
    }
}

@@ -162,16 +162,72 @@
 elper;%0A%0A
+import com.maxiee.heartbeat.database.tables.CrashTable;%0A
 import c
@@ -279,32 +279,32 @@
 eRelationTable;%0A
-
 import com.maxie
@@ -655,16 +655,66 @@
 tsTable;
+%0Aimport com.maxiee.heartbeat.support.CrashHandler;
 %0A%0A/**%0A *
@@ -901,9 +901,9 @@
 R = 
-2
+3
 ;%0A%0A 
@@ -1420,24 +1420,63 @@
 le.CREATE);%0A
+        db.execSQL(CrashTable.CREATE);%0A
     %7D%0A%0A    @
@@ -1629,32 +1629,32 @@
 eTable.CREATE);%0A
-
             db.e
@@ -1685,32 +1685,113 @@
 nTable.CREATE);%0A
+        %7D else if (oldVersion == 2) %7B%0A            db.execSQL(CrashTable.CREATE);%0A
         %7D%0A    %7D%0A

update db version to 3.