package com.hikapro.backpack.model.dao;

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

/**
 * Created by tariel on 20/04/16.
 */
public class DbHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "backpack";

    public static final int DATABASE_VERSION = 1;


    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.beginTransaction();
        try {
            updateDatabase(db, 0, DATABASE_VERSION);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        updateDatabase(db, oldVersion, newVersion);
    }

    private void updateDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL(Db.ItemsTable.CREATE);
        db.execSQL(Db.CategoriesTable.CREATE);
        db.execSQL(Db.SetsTable.CREATE);
        db.execSQL(Db.LogTable.CREATE);
        db.execSQL(Db.SetItemsTable.CREATE);
        db.execSQL(Db.SharingPicsTable.CREATE);

        if (oldVersion < 2) {
            // place the logic here
        }
    }
}
