package basic.db.listeners;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.support.ConnectionSource;
import com.wasu.module.db.DBManage;

import java.sql.SQLException;

import basic.db.model.DBProgramFavorite;

/**
 * Created by fanwang on 9/12/16.
 */
public class SaveFavoriteTimeListener extends AbsWasuDbListener {
    //the sql this listener will be executed when onUpgrade
    private static final String s_SQL = "ALTER TABLE `ProgramFavorite` ADD COLUMN savefavoritetime LONG DEFAULT 0;";

    public boolean onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource,
                             int oldVersion, int newVersion) {

        if (oldVersion == 1) {
            return false;
        }
        if (oldVersion < DATABASE_VERSION) {
            try {
                //Dao<DBProgramFavorite, Integer> favoriteDao = DBModule.getInstance().getDao(DBProgramFavorite.class);
                //favoriteDao.executeRaw(s_SQL);

                DBManage.executeRaw(DBProgramFavorite.class, s_SQL);
                return true;
            } catch (SQLException e) {
                Log.e("DBUpgrade", "upgrade fail cause by:" + e.toString());
                e.printStackTrace();
                return false;
            }
        }
        return true;

    }

}
