101 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-01-05 11:11:15 +08:00
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import data_rdb from '@ohos.data.rdb';
import Rdb from '../Rdb';
import AccountData from '../../bean/AccountData';
//import CommonConstants from '../../constants/CommonConstants';
export default class AccountTable {
2024-06-27 20:53:36 +08:00
// private accountTable = new Rdb(CommonConstants.ACCOUNT_TABLE.tableName, CommonConstants.ACCOUNT_TABLE.sqlCreate,
// CommonConstants.ACCOUNT_TABLE.columns);
2024-01-05 11:11:15 +08:00
private commonConstants
private accountTable
2024-06-27 20:53:36 +08:00
// private CommonConstants
constructor(callback: Function = () => {},Constants,context?) {
2024-01-05 11:11:15 +08:00
this.accountTable= new Rdb(Constants.ACCOUNT_TABLE.tableName, Constants.ACCOUNT_TABLE.sqlCreate,
2024-06-27 20:53:36 +08:00
Constants.ACCOUNT_TABLE.columns)
// this.CommonConstants=Constants
2024-01-05 11:11:15 +08:00
this.commonConstants=Constants
2024-06-27 20:53:36 +08:00
this.accountTable.getRdbStore(callback,context);
2024-01-05 11:11:15 +08:00
}
2024-06-27 20:53:36 +08:00
getRdbStore(callback: Function = () => {},context?) {
this.accountTable.getRdbStore(callback,context);
2024-01-05 11:11:15 +08:00
}
//删除全部数据
2024-06-27 20:53:36 +08:00
deleteTableData(callback: Function,name,context?) {
this.accountTable.deleteTableData(callback,name,context);
2024-01-05 11:11:15 +08:00
}
insertData(account, callback: Function) {
const valueBucket = generateBucket(this.commonConstants,account);
this.accountTable.insertData(valueBucket, callback);
}
sqlOperate(str,name,callback: Function) {
// const valueBucket = generateBucket(this.commonConstants,account);
this.accountTable.sqlOperate(str,name,callback);
}
deleteData(account, callback: Function) {
let predicates = new data_rdb.RdbPredicates(this.commonConstants.ACCOUNT_TABLE.tableName)
2024-01-31 14:35:16 +08:00
const key=this.commonConstants.ACCOUNT_TABLE.columns[0];
2024-01-05 11:11:15 +08:00
predicates.equalTo(key, account[key]);
this.accountTable.deleteData(predicates, callback);
}
updateData(account, callback: Function) {
const valueBucket = generateBucket(this.commonConstants,account);
let predicates = new data_rdb.RdbPredicates(this.commonConstants.ACCOUNT_TABLE.tableName);
2024-01-31 14:35:16 +08:00
const key=this.commonConstants.ACCOUNT_TABLE.columns[0]
2024-01-05 11:11:15 +08:00
predicates.equalTo(key, account[key]);
this.accountTable.updateData(predicates, valueBucket, callback);
}
query(id: string, callback: Function, isAll: boolean = true) {
let predicates = new data_rdb.RdbPredicates(this.commonConstants.ACCOUNT_TABLE.tableName);
if (!isAll) {
2024-01-31 14:35:16 +08:00
const key=this.commonConstants.ACCOUNT_TABLE.columns[0]
2024-01-05 11:11:15 +08:00
predicates.equalTo(key, id);
}
const that=this
this.accountTable.query(predicates, function (resultSet) {
let count = resultSet.rowCount;
if (count === 0 || typeof count === 'string') {
console.log(`${that.commonConstants.TABLE_TAG}` + 'Query no results!');
callback([]);
} else {
resultSet.goToFirstRow();
const result = [];
for (let i = 0; i < count; i++) {
let tmp={}
that.commonConstants.ACCOUNT_TABLE.columns.map(res=>{
tmp[res]= resultSet.getString(resultSet.getColumnIndex(res))
})
result[i] = tmp;
resultSet.goToNextRow();
}
callback(result);
}
});
}
}
function generateBucket(CommonConstants,account) {
let obj = {};
CommonConstants.ACCOUNT_TABLE.columns.forEach((item) => {
if (item != 'id') {
obj[item] = account[item];
}
});
return obj;
}