🎨 feat(sqlite): 添加 GameAccount 表用于存储游戏账号数据

This commit is contained in:
BTMuli
2023-05-06 01:40:07 +08:00
parent 5074ce2953
commit 7189b65f3c
4 changed files with 112 additions and 31 deletions

View File

@@ -2,7 +2,7 @@
* @file utils TGSql.ts
* @description 数据库sql语句
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.4
* @since Alpha v0.2.0
*/
import { app } from "@tauri-apps/api";
@@ -28,6 +28,33 @@ function initAppTable (): string[] {
return sqlRes;
}
/**
* @description 初始化游戏账号数据表
* @since Alpha v0.2.0
* @see BTMuli.User.Game.Account
* @returns {string[]} sql
*/
function initGameAccountTable (): string[] {
const sqlRes = [];
// 创建游戏账号数据表
sqlRes.push(`
CREATE TABLE IF NOT EXISTS GameAccount
(
gameBiz TEXT PRIMARY KEY,
gameUid TEXT DEFAULT NULL,
isChosen BOOLEAN DEFAULT 0,
isOfficial BOOLEAN DEFAULT 0,
level INTEGER DEFAULT 0,
nickname TEXT DEFAULT NULL,
region TEXT DEFAULT NULL,
regionName TEXT DEFAULT NULL,
updated TEXT DEFAULT NULL,
PRIMARY KEY (gameBiz, gameUid)
);
`);
return sqlRes;
}
/**
* @description 初始化成就系列数据表
* @since Alpha v0.1.4
@@ -126,14 +153,16 @@ function initNameCardTable (): string[] {
`);
return sqlRes;
}
/**
* @description 初始化数据库表
* @since Alpha v0.1.4
* @since Alpha v0.2.0
* @returns {string[]} sql
*/
export function initSQLiteTable (): string[] {
const sqlRes = [];
sqlRes.push(...initAppTable());
sqlRes.push(...initGameAccountTable());
sqlRes.push(...initAchievementSeriesTable());
sqlRes.push(...initAchievementTable());
sqlRes.push(...initNameCardTable());

View File

@@ -30,6 +30,7 @@ class TGSqlite {
"AppData",
"Achievements",
"AchievementSeries",
"GameAccount",
"NameCard",
];
@@ -68,18 +69,33 @@ class TGSqlite {
}
/**
* @description 获取应用数据某一项
* @description 封装-根据 table keys 获取数据
* @memberof TGSqlite
* @since Alpha v0.2.0
* @param {string} key
* @returns {Promise<string>}
* @param {string} table 表名
* @param {string} keyName 键名
* @param {string} keyValue 键值
* @returns {Promise<unknown[]>} 数据
*/
public async getAppDataItem (key: string): Promise<string> {
public async getDataByKey (table: string, keyName: string, keyValue: string): Promise<unknown[]> {
const db = await Database.load(this.dbPath);
const sql = `SELECT value FROM AppData WHERE key='${key}';`;
const res: Array<{ value: string }> = await db.select(sql);
const sql = `SELECT * FROM ${table} WHERE ${keyName}='${keyValue}';`;
const res: unknown[] = await db.select(sql);
await db.close();
return res;
}
/**
* @description 封装-保存数据
* @memberof TGSqlite
* @since Alpha v0.2.0
* @param {string} sql sql语句
* @returns {Promise<void>}
*/
public async saveData (sql: string): Promise<void> {
const db = await Database.load(this.dbPath);
await db.execute(sql);
await db.close();
return res[0].value;
}
/**
@@ -99,20 +115,6 @@ class TGSqlite {
await db.close();
}
/**
* @description 获取 cookie
* @memberof TGSqlite
* @since Alpha v0.2.0
* @returns {Promise<string>}
*/
public async getCookie (): Promise<string> {
const db = await Database.load(this.dbPath);
const sql = "SELECT value FROM AppData WHERE key='cookie';";
const res: Array<{ value: string }> = await db.select(sql);
await db.close();
return res[0].value;
}
/**
* @description 保存 appData
* @memberof TGSqlite