• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "InsertStrategy"
16 
17 #include "insert_strategy.h"
18 
19 #include "db_delegate.h"
20 #include "general/load_config_common_strategy.h"
21 #include "general/load_config_data_info_strategy.h"
22 #include "general/load_config_from_bundle_info_strategy.h"
23 #include "general/permission_strategy.h"
24 #include "general/process_single_app_user_cross_strategy.h"
25 #include "log_print.h"
26 #include "utils/anonymous.h"
27 
28 namespace OHOS::DataShare {
Execute(std::shared_ptr<Context> context,const DataShareValuesBucket & valuesBucket)29 int64_t InsertStrategy::Execute(std::shared_ptr<Context> context, const DataShareValuesBucket &valuesBucket)
30 {
31     auto &preProcess = GetStrategy();
32     if (preProcess.IsEmpty()) {
33         ZLOGE("get strategy fail, maybe memory not enough");
34         return -1;
35     }
36     if (!preProcess(context)) {
37         ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
38         return -1;
39     }
40     auto delegate = DBDelegate::Create(context->calledSourceDir, context->version);
41     if (delegate == nullptr) {
42         ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str());
43         return -1;
44     }
45     return delegate->Insert(context->calledTableName, valuesBucket);
46 }
47 
GetStrategy()48 SeqStrategy &InsertStrategy::GetStrategy()
49 {
50     std::lock_guard<decltype(mutex_)> lock(mutex_);
51     if (!strategies_.IsEmpty()) {
52         return strategies_;
53     }
54     std::initializer_list<Strategy *> list = {
55         new (std::nothrow)LoadConfigCommonStrategy(),
56         new (std::nothrow)LoadConfigFromBundleInfoStrategy(),
57         new (std::nothrow)PermissionStrategy(),
58         new (std::nothrow)LoadConfigDataInfoStrategy(),
59         new (std::nothrow)ProcessSingleAppUserCrossStrategy()
60     };
61     auto ret = strategies_.Init(list);
62     if (!ret) {
63         std::for_each(list.begin(), list.end(), [](Strategy *item) {
64             delete item;
65         });
66         return strategies_;
67     }
68     return strategies_;
69 }
70 } // namespace OHOS::DataShare