119 lines
3.8 KiB
Java
119 lines
3.8 KiB
Java
package com.gh.gamecenter.db;
|
|
|
|
import android.content.Context;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.support.v4.util.ArrayMap;
|
|
|
|
import com.gh.common.util.Utils;
|
|
import com.gh.gamecenter.db.info.AppRunTimeInfo;
|
|
import com.gh.gamecenter.db.info.CommentInfo;
|
|
import com.gh.gamecenter.db.info.ConcernInfo;
|
|
import com.gh.gamecenter.db.info.DataCollectionInfo;
|
|
import com.gh.gamecenter.db.info.FilterInfo;
|
|
import com.gh.gamecenter.db.info.GameInfo;
|
|
import com.gh.gamecenter.db.info.LibaoInfo;
|
|
import com.gh.gamecenter.db.info.PackageInfo;
|
|
import com.gh.gamecenter.db.info.SearchHistoryInfo;
|
|
import com.gh.gamecenter.db.info.VoteInfo;
|
|
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
|
|
import com.j256.ormlite.dao.Dao;
|
|
import com.j256.ormlite.support.ConnectionSource;
|
|
import com.j256.ormlite.table.TableUtils;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|
|
|
private static final String DATABASE_NAME = "gh_assist.db";
|
|
private static final int DATABASE_VERSION = 5;
|
|
|
|
private static DatabaseHelper instance;
|
|
private ArrayMap<String, Dao> daos = new ArrayMap<>();
|
|
|
|
private DatabaseHelper(Context context) {
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
}
|
|
|
|
/**
|
|
* 单例获取该Helper
|
|
*
|
|
* @param context
|
|
* @return
|
|
*/
|
|
public static synchronized DatabaseHelper getHelper(Context context) {
|
|
context = context.getApplicationContext();
|
|
if (instance == null) {
|
|
synchronized (DatabaseHelper.class) {
|
|
if (instance == null) {
|
|
instance = new DatabaseHelper(context);
|
|
}
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
|
|
try {
|
|
Utils.log("DatabaseHelper onCreate");
|
|
TableUtils.createTable(connectionSource, ConcernInfo.class);
|
|
TableUtils.createTable(connectionSource, SearchHistoryInfo.class);
|
|
TableUtils.createTable(connectionSource, GameInfo.class);
|
|
TableUtils.createTable(connectionSource, DataCollectionInfo.class);
|
|
TableUtils.createTable(connectionSource, VoteInfo.class);
|
|
TableUtils.createTable(connectionSource, CommentInfo.class);
|
|
TableUtils.createTable(connectionSource, LibaoInfo.class);
|
|
TableUtils.createTable(connectionSource, AppRunTimeInfo.class);
|
|
TableUtils.createTable(connectionSource, PackageInfo.class);
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
|
|
try {
|
|
Utils.log("DatabaseHelper onUpgrade");
|
|
TableUtils.dropTable(connectionSource, ConcernInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, SearchHistoryInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, GameInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, FilterInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, DataCollectionInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, VoteInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, CommentInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, LibaoInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, AppRunTimeInfo.class, true);
|
|
TableUtils.dropTable(connectionSource, PackageInfo.class, true);
|
|
onCreate(database, connectionSource);
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public synchronized Dao getDao(Class clazz) throws SQLException {
|
|
Dao dao = null;
|
|
String className = clazz.getSimpleName();
|
|
|
|
if (daos.containsKey(className)) {
|
|
dao = daos.get(className);
|
|
}
|
|
if (dao == null) {
|
|
dao = super.getDao(clazz);
|
|
daos.put(className, dao);
|
|
}
|
|
return dao;
|
|
}
|
|
|
|
/**
|
|
* 释放资源
|
|
*/
|
|
@Override
|
|
public void close() {
|
|
super.close();
|
|
for (String key : daos.keySet()) {
|
|
Dao dao = daos.get(key);
|
|
dao = null;
|
|
}
|
|
}
|
|
}
|