Skip to content

Commit

Permalink
更新数据库设计
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Jan 9, 2024
1 parent aa1da0a commit 7c5a352
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AccountCacher {
public static int TYPE_DEV = 1;

static boolean hasAdaptScopedStorage = true;
static String dbName = "";
static String appName = "";
/**
* 是否存储正式环境账号,默认false,可以设置为true
*/
Expand All @@ -49,11 +49,11 @@ public static void configHostType(int dev, int test, int release) {
/**
* 非必须
*
* @param dbName 可以为空. 为空则存储于默认数据库
* @param appName 可以为空. 为空则存储于默认数据库
* @param hasAdaptScopedStorage 是否已经适配Android11的分区存储
*/
public static void init(@Nullable String dbName, boolean hasAdaptScopedStorage) {
AccountCacher.dbName = dbName;
public static void init(@Nullable String appName, boolean hasAdaptScopedStorage) {
AccountCacher.appName = appName;
AccountCacher.hasAdaptScopedStorage = hasAdaptScopedStorage;
}

Expand Down Expand Up @@ -222,13 +222,16 @@ public void onGranted(List<String> permissions, boolean all) {
@Override
public void run() {
List<DebugAccount> list = MyDbUtil.getDaoSession().getDebugAccountDao()
.queryBuilder().where(DebugAccountDao.Properties.Account.eq(account)
.queryBuilder().where(
DebugAccountDao.Properties.AppName.eq(AccountCacher.appName),
DebugAccountDao.Properties.Account.eq(account)
, DebugAccountDao.Properties.CountryCode.eq(countryCode)
, DebugAccountDao.Properties.HostType.eq(currentHostType)).list();
if (list == null || list.isEmpty()) {
DebugAccount debugAccount = new DebugAccount();
debugAccount.account = account;
debugAccount.pw = pw;
debugAccount.appName = AccountCacher.appName;
debugAccount.updateTime = System.currentTimeMillis();
debugAccount.position = 0;
debugAccount.countryCode = countryCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class DebugAccount {

public String countryCode;

public String appName;

public String account;
public String pw;
public int hostType;
Expand All @@ -31,6 +33,7 @@ public class DebugAccount {
public String toString() {
return "DebugAccount{" +
"id=" + id +
", appName=" + appName +
", updateTime=" + updateTime +
", countryCode='" + countryCode + '\'' +
", account='" + account + '\'' +
Expand All @@ -41,18 +44,21 @@ public String toString() {
'}';
}

@Generated(hash = 142474538)
@Generated(hash = 261427384)
public DebugAccount(Long id, long updateTime, String countryCode,
String account, String pw, int hostType, int usedNum, int position) {
String appName, String account, String pw, int hostType, int usedNum,
int position) {
this.id = id;
this.updateTime = updateTime;
this.countryCode = countryCode;
this.appName = appName;
this.account = account;
this.pw = pw;
this.hostType = hostType;
this.usedNum = usedNum;
this.position = position;
}

@Generated(hash = 65329523)
public DebugAccount() {
}
Expand Down Expand Up @@ -105,6 +111,14 @@ public void setPosition(int position) {
this.position = position;
}

public String getAppName() {
return this.appName;
}

public void setAppName(String appName) {
this.appName = appName;
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public File getDatabasePath(String name) {
else{//如果存在
//获取sd卡路径
String dbDir=android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
dbDir += "/.yuv/databases";//数据库所在目录
dbDir += "/.yuv2/databases";//数据库所在目录
String dbPath = dbDir+"/"+name;//数据库路径
//判断目录是否存在,不存在则创建该目录
File dirFile = new File(dbDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.greenrobot.greendao.database.Database;

import java.io.File;
import java.util.List;

public class MyDbUtil {
Expand All @@ -21,7 +20,7 @@ private static void initGreenDao(Application context) {
//指定数据库存储路径
Context context2 = new MyDBContext(context);
//升级自动迁移数据的工具
DaoMaster.OpenHelper helper = new MySQLiteUpgradeOpenHelper(context2, AccountCacher.dbName+"testaccount3.db");
DaoMaster.OpenHelper helper = new MySQLiteUpgradeOpenHelper(context2, "test_account.db");
Database db = helper.getWritableDb();
//不再加密.以规避sqlitesipher在6.0以下版本的c层崩溃问题
DaoMaster daoMaster = new DaoMaster(db);
Expand Down Expand Up @@ -66,7 +65,10 @@ public static DaoSession getDaoSession() {
}

static List<DebugAccount> getAll(int hostType,String countCode){
return getDaoSession().getDebugAccountDao().queryBuilder().where(DebugAccountDao.Properties.HostType.eq(hostType)
return getDaoSession().getDebugAccountDao().queryBuilder()
.where(
DebugAccountDao.Properties.AppName.eq(AccountCacher.appName),
DebugAccountDao.Properties.HostType.eq(hostType)
,DebugAccountDao.Properties.CountryCode.eq(countCode))
.orderDesc(DebugAccountDao.Properties.UsedNum).list();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public static class Properties {
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
public final static Property UpdateTime = new Property(1, long.class, "updateTime", false, "UPDATE_TIME");
public final static Property CountryCode = new Property(2, String.class, "countryCode", false, "COUNTRY_CODE");
public final static Property Account = new Property(3, String.class, "account", false, "ACCOUNT");
public final static Property Pw = new Property(4, String.class, "pw", false, "PW");
public final static Property HostType = new Property(5, int.class, "hostType", false, "HOST_TYPE");
public final static Property UsedNum = new Property(6, int.class, "usedNum", false, "USED_NUM");
public final static Property Position = new Property(7, int.class, "position", false, "POSITION");
public final static Property AppName = new Property(3, String.class, "appName", false, "APP_NAME");
public final static Property Account = new Property(4, String.class, "account", false, "ACCOUNT");
public final static Property Pw = new Property(5, String.class, "pw", false, "PW");
public final static Property HostType = new Property(6, int.class, "hostType", false, "HOST_TYPE");
public final static Property UsedNum = new Property(7, int.class, "usedNum", false, "USED_NUM");
public final static Property Position = new Property(8, int.class, "position", false, "POSITION");
}


Expand All @@ -50,11 +51,12 @@ public static void createTable(Database db, boolean ifNotExists) {
"\"_id\" INTEGER PRIMARY KEY ," + // 0: id
"\"UPDATE_TIME\" INTEGER NOT NULL ," + // 1: updateTime
"\"COUNTRY_CODE\" TEXT," + // 2: countryCode
"\"ACCOUNT\" TEXT," + // 3: account
"\"PW\" TEXT," + // 4: pw
"\"HOST_TYPE\" INTEGER NOT NULL ," + // 5: hostType
"\"USED_NUM\" INTEGER NOT NULL ," + // 6: usedNum
"\"POSITION\" INTEGER NOT NULL );"); // 7: position
"\"APP_NAME\" TEXT," + // 3: appName
"\"ACCOUNT\" TEXT," + // 4: account
"\"PW\" TEXT," + // 5: pw
"\"HOST_TYPE\" INTEGER NOT NULL ," + // 6: hostType
"\"USED_NUM\" INTEGER NOT NULL ," + // 7: usedNum
"\"POSITION\" INTEGER NOT NULL );"); // 8: position
}

/** Drops the underlying database table. */
Expand All @@ -78,18 +80,23 @@ protected final void bindValues(DatabaseStatement stmt, DebugAccount entity) {
stmt.bindString(3, countryCode);
}

String appName = entity.getAppName();
if (appName != null) {
stmt.bindString(4, appName);
}

String account = entity.getAccount();
if (account != null) {
stmt.bindString(4, account);
stmt.bindString(5, account);
}

String pw = entity.getPw();
if (pw != null) {
stmt.bindString(5, pw);
stmt.bindString(6, pw);
}
stmt.bindLong(6, entity.getHostType());
stmt.bindLong(7, entity.getUsedNum());
stmt.bindLong(8, entity.getPosition());
stmt.bindLong(7, entity.getHostType());
stmt.bindLong(8, entity.getUsedNum());
stmt.bindLong(9, entity.getPosition());
}

@Override
Expand All @@ -107,18 +114,23 @@ protected final void bindValues(SQLiteStatement stmt, DebugAccount entity) {
stmt.bindString(3, countryCode);
}

String appName = entity.getAppName();
if (appName != null) {
stmt.bindString(4, appName);
}

String account = entity.getAccount();
if (account != null) {
stmt.bindString(4, account);
stmt.bindString(5, account);
}

String pw = entity.getPw();
if (pw != null) {
stmt.bindString(5, pw);
stmt.bindString(6, pw);
}
stmt.bindLong(6, entity.getHostType());
stmt.bindLong(7, entity.getUsedNum());
stmt.bindLong(8, entity.getPosition());
stmt.bindLong(7, entity.getHostType());
stmt.bindLong(8, entity.getUsedNum());
stmt.bindLong(9, entity.getPosition());
}

@Override
Expand All @@ -132,11 +144,12 @@ public DebugAccount readEntity(Cursor cursor, int offset) {
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
cursor.getLong(offset + 1), // updateTime
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // countryCode
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // account
cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // pw
cursor.getInt(offset + 5), // hostType
cursor.getInt(offset + 6), // usedNum
cursor.getInt(offset + 7) // position
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // appName
cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // account
cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // pw
cursor.getInt(offset + 6), // hostType
cursor.getInt(offset + 7), // usedNum
cursor.getInt(offset + 8) // position
);
return entity;
}
Expand All @@ -146,11 +159,12 @@ public void readEntity(Cursor cursor, DebugAccount entity, int offset) {
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
entity.setUpdateTime(cursor.getLong(offset + 1));
entity.setCountryCode(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
entity.setAccount(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
entity.setPw(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
entity.setHostType(cursor.getInt(offset + 5));
entity.setUsedNum(cursor.getInt(offset + 6));
entity.setPosition(cursor.getInt(offset + 7));
entity.setAppName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
entity.setAccount(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
entity.setPw(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
entity.setHostType(cursor.getInt(offset + 6));
entity.setUsedNum(cursor.getInt(offset + 7));
entity.setPosition(cursor.getInt(offset + 8));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class BaseApp extends MultiDexApplication {
@Override
public void onCreate() {
super.onCreate();
AccountCacher.init("",true);
AccountCacher.init("app1",true);
AccountCacher.storeReleaseAccount = true;
AccountCacher.configHostType(1,3,0);
}
Expand Down

0 comments on commit 7c5a352

Please sign in to comment.