fix: 优化数据库方面操作打印信息
This commit is contained in:
parent
014c21ad81
commit
9da3b0f856
@ -50,3 +50,6 @@ export const JudgeTag = '[Judge]';
|
||||
|
||||
//初始化表
|
||||
export const InitTableTag = '[InitTable]';
|
||||
|
||||
//数据库操作
|
||||
export const DbOperationTag = '[DbOperation]';
|
||||
@ -2,6 +2,7 @@ import relationalStore from '@ohos.data.relationalStore'
|
||||
import common from '@ohos.app.ability.common'
|
||||
import { BusinessError } from '@ohos.base'
|
||||
import { DbTag } from '../config'
|
||||
import { dConsole } from './LogWorker'
|
||||
|
||||
export interface ColumnInfo {
|
||||
name: string,
|
||||
@ -27,11 +28,11 @@ class DbUtils {
|
||||
relationalStore.getRdbStore(context, config)
|
||||
.then(rdbStore => {
|
||||
this.rdbStore = rdbStore
|
||||
console.log(DbTag, "db rdbStore init success")
|
||||
dConsole.log(DbTag, "db rdbStore init success")
|
||||
resolve()
|
||||
})
|
||||
.catch((err: BusinessError) => {
|
||||
console.error(DbTag, `db rdbStore init fail reason:${err}`);
|
||||
dConsole.error(DbTag, `db rdbStore init fail reason:${err}`);
|
||||
reject(err)
|
||||
})
|
||||
|
||||
@ -44,11 +45,11 @@ class DbUtils {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore?.executeSql(createSql)
|
||||
.then(() => {
|
||||
console.log(DbTag, "sql createTable success")
|
||||
dConsole.log(DbTag, "sql createTable success")
|
||||
resolve()
|
||||
})
|
||||
.catch((err: BusinessError) => {
|
||||
console.error(DbTag, `sql createTable fail err:${JSON.stringify(err)}`);
|
||||
dConsole.error(DbTag, `sql createTable fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
})
|
||||
} else {
|
||||
@ -63,11 +64,11 @@ class DbUtils {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore?.executeSql(sql).then(() => {
|
||||
console.log(DbTag, "sql clearTable success", sql)
|
||||
dConsole.log(DbTag, "sql clearTable success", sql)
|
||||
resolve()
|
||||
})
|
||||
} else {
|
||||
console.error(DbTag, "sql clearTable fail", sql)
|
||||
dConsole.error(DbTag, "sql clearTable fail", sql)
|
||||
reject('rdbStore is null')
|
||||
}
|
||||
})
|
||||
@ -78,14 +79,14 @@ class DbUtils {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore?.executeSql(sql).then(() => {
|
||||
console.log(DbTag, "sql executeSql success", sql)
|
||||
dConsole.log(DbTag, "sql executeSql success", sql)
|
||||
resolve("")
|
||||
}).catch((err: BusinessError) => {
|
||||
console.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`);
|
||||
dConsole.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
})
|
||||
} else {
|
||||
console.error(DbTag, "sql executeSql fail", sql)
|
||||
dConsole.error(DbTag, "sql executeSql fail", sql)
|
||||
reject('rdbStore is null')
|
||||
}
|
||||
})
|
||||
@ -97,10 +98,10 @@ class DbUtils {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore?.insert(tableName, obj, (err, res) => {
|
||||
if (err) {
|
||||
console.error(DbTag, `sql insertData fail err:${JSON.stringify(err)}`);
|
||||
dConsole.error(DbTag, `sql insertData fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
} else {
|
||||
console.log(DbTag, `sql insertData success res:${res}`);
|
||||
dConsole.log(DbTag, `sql insertData success res:${res}`);
|
||||
resolve(res)
|
||||
}
|
||||
})
|
||||
@ -115,10 +116,10 @@ class DbUtils {
|
||||
return new Promise((resolve, reject) => {
|
||||
const SQL = `DELETE FROM ${tableName};`
|
||||
this.executeSql(SQL).then(res => {
|
||||
console.log(DbTag, "删除表成功!表名:", tableName)
|
||||
dConsole.log(DbTag, "删除表成功!表名:", tableName)
|
||||
resolve(true)
|
||||
}).catch((err: BusinessError) => {
|
||||
console.error(DbTag, "删除表失败!表名:", tableName, "错误信息:", JSON.stringify(err))
|
||||
dConsole.error(DbTag, "删除表失败!表名:", tableName, "错误信息:", JSON.stringify(err))
|
||||
reject(false)
|
||||
})
|
||||
})
|
||||
@ -137,7 +138,7 @@ class DbUtils {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore.querySql(sql, []).then((res: relationalStore.ResultSet) => {
|
||||
console.log(DbTag, "sql query", JSON.stringify(res))
|
||||
dConsole.log(DbTag, "sql query", JSON.stringify(res))
|
||||
if (res.rowCount <= 0) {
|
||||
resolve(0 as T)
|
||||
} else {
|
||||
@ -145,7 +146,7 @@ class DbUtils {
|
||||
resolve(res.getLong(0) as T)
|
||||
}
|
||||
}).catch((err: BusinessError) => {
|
||||
console.error(DbTag, `sql queryCount fail err:${JSON.stringify(err)}`);
|
||||
dConsole.error(DbTag, `sql queryCount fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
})
|
||||
} else {
|
||||
@ -160,10 +161,10 @@ class DbUtils {
|
||||
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)}`);
|
||||
dConsole.error(DbTag, `sql queryForList fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
} else {
|
||||
console.log(DbTag, `sql queryForList success rows: ${result.rowCount.toString()}`)
|
||||
dConsole.log(DbTag, `sql queryForList success rows: ${result.rowCount.toString()}`)
|
||||
resolve(this.parseResultSet(result, columns))
|
||||
}
|
||||
})
|
||||
@ -179,7 +180,7 @@ class DbUtils {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore?.querySql(sql, [], (err, result) => {
|
||||
if (err) {
|
||||
console.error(DbTag, `sql queryForListBySql fail err:${JSON.stringify(err)}`);
|
||||
dConsole.error(DbTag, `sql queryForListBySql fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
} else {
|
||||
resolve(this.parseResultSet(result, columns))
|
||||
@ -229,11 +230,11 @@ class DbUtils {
|
||||
if (this.rdbStore) {
|
||||
this.rdbStore?.delete(predicates)
|
||||
.then(rows => {
|
||||
console.log(DbTag, `DbUtil delete success rows: ${rows.toString()}`)
|
||||
dConsole.log(DbTag, `DbUtil delete success rows: ${rows.toString()}`)
|
||||
resolve(rows)
|
||||
})
|
||||
.catch((err: BusinessError) => {
|
||||
console.error(DbTag, `DbUtil delete fail err:${JSON.stringify(err)}`);
|
||||
dConsole.error(DbTag, `DbUtil delete fail err:${JSON.stringify(err)}`);
|
||||
reject(err)
|
||||
})
|
||||
} else {
|
||||
|
||||
@ -5,19 +5,19 @@ import { BusinessError } from '@ohos.base';
|
||||
import Prompt from '@system.prompt';
|
||||
import { initialization } from '../../api';
|
||||
|
||||
import { GlobalConfig, InitTableTag } from '../../config';
|
||||
import { DbOperationTag, GlobalConfig, InitTableTag } from '../../config';
|
||||
import FileUtils from '../FileUtils';
|
||||
import { dConsole } from '../LogWorker';
|
||||
|
||||
// 建表操作
|
||||
export async function InitTable() {
|
||||
ParameterPlatform.forEach(item => {
|
||||
console.log("表名", item.sqlCreate)
|
||||
dConsole.log(DbOperationTag, "表名", item.sqlCreate)
|
||||
if (item) {
|
||||
DB.executeSql(item.sqlCreate).then(() => {
|
||||
console.log("表创建成功", item.tableName)
|
||||
dConsole.log(DbOperationTag, "表创建成功", item.tableName)
|
||||
}).catch((err: BusinessError) => {
|
||||
console.log("表创建失败", item.tableName, err)
|
||||
dConsole.error(DbOperationTag, "表创建失败", item.tableName, err)
|
||||
})
|
||||
}
|
||||
})
|
||||
@ -40,12 +40,12 @@ export function SqlInsertTable(tableName: string, data: Array<RecordType>, delFl
|
||||
}).join(",")
|
||||
let INSERT_SQL = "INSERT INTO " + tableName
|
||||
+ " (" + columns + ") VALUES " + values
|
||||
console.log('表插入语句,', INSERT_SQL, 'tableName', tableName)
|
||||
dConsole.log(DbOperationTag, '表插入语句,', INSERT_SQL, 'tableName', tableName)
|
||||
DB.executeSql(INSERT_SQL).then(() => {
|
||||
console.log('插入表成功', 'tableName', tableName)
|
||||
dConsole.log(DbOperationTag, '插入表成功', 'tableName', tableName)
|
||||
resolve(true)
|
||||
}).catch((err: BusinessError) => {
|
||||
console.log('插入表失败,', JSON.stringify(err), 'tableName', tableName)
|
||||
dConsole.error(DbOperationTag, '插入表失败,', JSON.stringify(err), 'tableName', tableName)
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
@ -67,6 +67,7 @@ export async function GetSyncData<T>(tableName: string): Promise<T[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const platform = ParameterPlatform.get(tableName);
|
||||
if (!platform || !platform.columns) {
|
||||
dConsole.error(DbOperationTag, `Invalid tableName or columns not found for table: ${tableName}`);
|
||||
throw new Error(`Invalid tableName or columns not found for table: ${tableName}`);
|
||||
}
|
||||
const columns: ColumnInfo[] = platform.columns.map((res: string) => {
|
||||
@ -77,8 +78,10 @@ export async function GetSyncData<T>(tableName: string): Promise<T[]> {
|
||||
} as ColumnInfo;
|
||||
});
|
||||
DB.queryListBySql<T>(`select * from ${tableName}`, columns).then((res: T[]) => {
|
||||
dConsole.log(DbOperationTag, `获取表 ${tableName} 数据成功`, res);
|
||||
resolve(res);
|
||||
}).catch((err: BusinessError) => {
|
||||
dConsole.error(DbOperationTag, `获取表 ${tableName} 数据失败`, JSON.stringify(err));
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user