| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-25 14:27:06 +08:00
										 |  |  |   // 初始化 无需记录日志 | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.log(DbTag, "db rdbStore init success") | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           resolve() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.error(DbTag, `db rdbStore init fail reason:${err}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           reject(err) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   创建表 | 
					
						
							|  |  |  |   createTable(createSql: string): Promise<void> { | 
					
						
							|  |  |  |     return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |       if (this.rdbStore) { | 
					
						
							|  |  |  |         this.rdbStore?.executeSql(createSql) | 
					
						
							|  |  |  |           .then(() => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.log(DbTag, "sql createTable success") | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             resolve() | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |           .catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.error(DbTag, `sql createTable fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             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(() => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.log(DbTag, "sql clearTable success", sql) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           resolve() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |         console.error(DbTag, "sql clearTable fail", sql) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         reject('rdbStore is null') | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-25 14:27:06 +08:00
										 |  |  |   // 执行sql 无法记录 | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |   executeSql(sql: string): Promise<string> { | 
					
						
							|  |  |  |     return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |       if (this.rdbStore) { | 
					
						
							|  |  |  |         this.rdbStore?.executeSql(sql).then(() => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.log(DbTag, "sql executeSql success", sql) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           resolve("") | 
					
						
							|  |  |  |         }).catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           reject(err) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |         console.error(DbTag, "sql executeSql fail", sql) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         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) { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.error(DbTag, `sql insertData fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             reject(err) | 
					
						
							|  |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.log(DbTag, `sql insertData success res:${res}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             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 => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |         console.log(DbTag, "删除表成功!表名:", tableName) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         resolve(true) | 
					
						
							|  |  |  |       }).catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |         console.error(DbTag, "删除表失败!表名:", tableName, "错误信息:", JSON.stringify(err)) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         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) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.log(DbTag, "sql query", JSON.stringify(res)) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           if (res.rowCount <= 0) { | 
					
						
							|  |  |  |             resolve(0 as T) | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             res.goToNextRow() | 
					
						
							|  |  |  |             resolve(res.getLong(0) as T) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }).catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |           console.error(DbTag, `sql queryCount fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |           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) { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.error(DbTag, `sql queryForList fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             reject(err) | 
					
						
							|  |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.log(DbTag, `sql queryForList success rows: ${result.rowCount.toString()}`) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.error(DbTag, `sql queryForListBySql fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             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.将对象填入结果数组 | 
					
						
							| 
									
										
										
										
											2025-04-15 17:36:14 +08:00
										 |  |  |       arr.push(obj as ESObject as T); // 将 obj 断言为 T 类型 | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return arr; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   删除表数据 | 
					
						
							|  |  |  |   delete(predicates: relationalStore.RdbPredicates): Promise<number> { | 
					
						
							|  |  |  |     return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |       if (this.rdbStore) { | 
					
						
							|  |  |  |         this.rdbStore?.delete(predicates) | 
					
						
							|  |  |  |           .then(rows => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.log(DbTag, `DbUtil delete success rows: ${rows.toString()}`) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             resolve(rows) | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |           .catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-06-25 14:10:40 +08:00
										 |  |  |             console.error(DbTag, `DbUtil delete fail err:${JSON.stringify(err)}`); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |             reject(err) | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         reject('rdbStore is null') | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let DB: DbUtils = new DbUtils() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default DB |