/* * 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 { // private accountTable = new Rdb(CommonConstants.ACCOUNT_TABLE.tableName, CommonConstants.ACCOUNT_TABLE.sqlCreate, // CommonConstants.ACCOUNT_TABLE.columns); private commonConstants private accountTable // private CommonConstants constructor(callback: Function = () => {},Constants,context?) { this.accountTable= new Rdb(Constants.ACCOUNT_TABLE.tableName, Constants.ACCOUNT_TABLE.sqlCreate, Constants.ACCOUNT_TABLE.columns) // this.CommonConstants=Constants this.commonConstants=Constants this.accountTable.getRdbStore(callback,context); } getRdbStore(callback: Function = () => {},context?) { this.accountTable.getRdbStore(callback,context); } //删除全部数据 deleteTableData(callback: Function,name,context?) { this.accountTable.deleteTableData(callback,name,context); } 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) const key=this.commonConstants.ACCOUNT_TABLE.columns[0]; 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); const key=this.commonConstants.ACCOUNT_TABLE.columns[0] 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) { const key=this.commonConstants.ACCOUNT_TABLE.columns[0] 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; }