249 lines
7.2 KiB
Plaintext
249 lines
7.2 KiB
Plaintext
import relationalStore from '@ohos.data.relationalStore'
|
|
import common from '@ohos.app.ability.common'
|
|
import { BusinessError } from '@ohos.base'
|
|
import { DbTag } from '../config'
|
|
|
|
export interface ColumnInfo {
|
|
name: string,
|
|
columnName: string,
|
|
type: ColumnType,
|
|
}
|
|
|
|
export enum ColumnType {
|
|
LONG,
|
|
STRING
|
|
}
|
|
|
|
class DbUtils {
|
|
rdbStore: relationalStore.RdbStore | undefined = undefined
|
|
|
|
// 初始化 无需记录日志
|
|
init(context: common.UIAbilityContext) {
|
|
let config: relationalStore.StoreConfig = {
|
|
name: 'car.db',
|
|
securityLevel: relationalStore.SecurityLevel.S1
|
|
}
|
|
return new Promise<void>((resolve, reject) => {
|
|
relationalStore.getRdbStore(context, config)
|
|
.then(rdbStore => {
|
|
this.rdbStore = rdbStore
|
|
console.log(DbTag, "db rdbStore init success")
|
|
resolve()
|
|
})
|
|
.catch((err: BusinessError) => {
|
|
console.error(DbTag, `db rdbStore init fail reason:${err}`);
|
|
reject(err)
|
|
})
|
|
|
|
})
|
|
}
|
|
|
|
// 创建表
|
|
createTable(createSql: string): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.executeSql(createSql)
|
|
.then(() => {
|
|
console.log(DbTag, "sql createTable success")
|
|
resolve()
|
|
})
|
|
.catch((err: BusinessError) => {
|
|
console.error(DbTag, `sql createTable fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
})
|
|
} else {
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 请空表
|
|
clearTable(tableName: string): Promise<void> {
|
|
let sql = `DELETE FROM ${tableName}`
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.executeSql(sql).then(() => {
|
|
console.log(DbTag, "sql clearTable success", sql)
|
|
resolve()
|
|
})
|
|
} else {
|
|
console.error(DbTag, "sql clearTable fail", sql)
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 执行sql 无法记录
|
|
executeSql(sql: string): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.executeSql(sql).then(() => {
|
|
console.log(DbTag, "sql executeSql success", sql)
|
|
resolve("")
|
|
}).catch((err: BusinessError) => {
|
|
console.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
})
|
|
} else {
|
|
console.error(DbTag, "sql executeSql fail", sql)
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 插入数据
|
|
insertData(tableName: string, obj: ESObject): Promise<number> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.insert(tableName, obj, (err, res) => {
|
|
if (err) {
|
|
console.error(DbTag, `sql insertData fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
} else {
|
|
console.log(DbTag, `sql insertData success res:${res}`);
|
|
resolve(res)
|
|
}
|
|
})
|
|
} else {
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 根据表名删除表
|
|
deleteByName(tableName: string): Promise<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
const SQL = `DELETE FROM ${tableName};`
|
|
this.executeSql(SQL).then(res => {
|
|
console.log(DbTag, "删除表成功!表名:", tableName)
|
|
resolve(true)
|
|
}).catch((err: BusinessError) => {
|
|
console.error(DbTag, "删除表失败!表名:", tableName, "错误信息:", JSON.stringify(err))
|
|
reject(false)
|
|
})
|
|
})
|
|
}
|
|
|
|
// 插入表数据
|
|
// insertData(tableName: string, obj: object, columns: ColumnInfo[]): Promise<number> {
|
|
// return new Promise((resolve, reject) => {
|
|
// if(this.)
|
|
// })
|
|
//
|
|
// }
|
|
|
|
// 查找
|
|
queryCount<T extends string | number | boolean>(sql: string): Promise<T> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore.querySql(sql, []).then((res: relationalStore.ResultSet) => {
|
|
console.log(DbTag, "sql query", JSON.stringify(res))
|
|
if (res.rowCount <= 0) {
|
|
resolve(0 as T)
|
|
} else {
|
|
res.goToNextRow()
|
|
resolve(res.getLong(0) as T)
|
|
}
|
|
}).catch((err: BusinessError) => {
|
|
console.error(DbTag, `sql queryCount fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
})
|
|
} else {
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 查找数据
|
|
queryList<T>(predicates: relationalStore.RdbPredicates, columns: ColumnInfo[]): Promise<T[]> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.query(predicates, columns.map(info => info.columnName), (err, result) => {
|
|
if (err) {
|
|
console.error(DbTag, `sql queryForList fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
} else {
|
|
console.log(DbTag, `sql queryForList success rows: ${result.rowCount.toString()}`)
|
|
resolve(this.parseResultSet(result, columns))
|
|
}
|
|
})
|
|
} else {
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 获取表列表数据
|
|
queryListBySql<T>(sql: string, columns: ColumnInfo[]): Promise<T[]> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.querySql(sql, [], (err, result) => {
|
|
if (err) {
|
|
console.error(DbTag, `sql queryForListBySql fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
} else {
|
|
resolve(this.parseResultSet(result, columns))
|
|
}
|
|
})
|
|
} else {
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
|
|
//
|
|
parseResultSet<T>(result: relationalStore.ResultSet, columns: ColumnInfo[]): T[] {
|
|
// 1.声明最终返回的结果
|
|
let arr: T[] = [];
|
|
// 2.判断是否有结果
|
|
if (result.rowCount <= 0) {
|
|
return arr;
|
|
}
|
|
// 3.处理结果
|
|
while (!result.isAtLastRow) {
|
|
// 3.1.去下一行
|
|
result.goToNextRow();
|
|
// 3.2.解析这行数据,转为对象
|
|
let obj: object = new Object();
|
|
columns.forEach(info => {
|
|
let val: string | number;
|
|
switch (info.type) {
|
|
case ColumnType.LONG:
|
|
val = result.getLong(result.getColumnIndex(info.columnName));
|
|
break;
|
|
case ColumnType.STRING:
|
|
val = result.getString(result.getColumnIndex(info.columnName));
|
|
break;
|
|
}
|
|
obj[info.name] = val;
|
|
});
|
|
// 3.3.将对象填入结果数组
|
|
arr.push(obj as ESObject as T); // 将 obj 断言为 T 类型
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
// 删除表数据
|
|
delete(predicates: relationalStore.RdbPredicates): Promise<number> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.rdbStore) {
|
|
this.rdbStore?.delete(predicates)
|
|
.then(rows => {
|
|
console.log(DbTag, `DbUtil delete success rows: ${rows.toString()}`)
|
|
resolve(rows)
|
|
})
|
|
.catch((err: BusinessError) => {
|
|
console.error(DbTag, `DbUtil delete fail err:${JSON.stringify(err)}`);
|
|
reject(err)
|
|
})
|
|
} else {
|
|
reject('rdbStore is null')
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
let DB: DbUtils = new DbUtils()
|
|
|
|
export default DB
|