1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define LOG_TAG "RdbLocalDbObserver"
17 #include "rdb_local_db_observer.h"
18 
19 #include "raw_data_parser.h"
20 #include "value_object.h"
21 
22 namespace OHOS {
23 namespace NativeRdb {
24 using namespace OHOS::DistributedRdb;
25 
OnChange(DistributedDB::StoreObserver::StoreChangedInfo && data)26 void RdbStoreLocalDbObserver::OnChange(DistributedDB::StoreObserver::StoreChangedInfo &&data)
27 {
28     RdbStoreObserver::ChangeInfo changeInfo;
29     for (const auto &dataInfo : data) {
30         Convert(dataInfo, changeInfo, RdbStoreObserver::ChangeType::CHG_TYPE_INSERT);
31         Convert(dataInfo, changeInfo, RdbStoreObserver::ChangeType::CHG_TYPE_UPDATE);
32         Convert(dataInfo, changeInfo, RdbStoreObserver::ChangeType::CHG_TYPE_DELETE);
33     }
34     Origin origin;
35     RdbStoreObserver::PrimaryFields fields;
36     observer_->OnChange(origin, fields, std::move(changeInfo));
37 }
38 
Convert(const DistributedDB::ChangedData & dataInfo,RdbStoreObserver::ChangeInfo & changeInfo,RdbStoreObserver::ChangeType changeType)39 void RdbStoreLocalDbObserver::Convert(const DistributedDB::ChangedData &dataInfo,
40     RdbStoreObserver::ChangeInfo &changeInfo, RdbStoreObserver::ChangeType changeType)
41 {
42     if (changeType < RdbStoreObserver::ChangeType::CHG_TYPE_INSERT ||
43         changeType >= RdbStoreObserver::ChangeType::CHG_TYPE_BUTT) {
44         return;
45     }
46     changeInfo.try_emplace(dataInfo.tableName);
47     for (const auto &primary : dataInfo.primaryData[changeType]) {
48         RdbStoreObserver::PrimaryKey primaryKey;
49         RawDataParser::Convert(std::move(primary[0]), primaryKey);
50         changeInfo[dataInfo.tableName][changeType].push_back(std::move(primaryKey));
51     }
52 }
53 
54 } // namespace NativeRdb
55 } // namespace OHOS