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