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, true,
44 context->isEncryptDb, context->secretMetaKey);
45 if (delegate == nullptr) {
46 ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str());
47 return nullptr;
48 }
49 return delegate->Query(context->calledTableName, predicates, columns, errCode);
50 }
51
GetStrategy()52 SeqStrategy &QueryStrategy::GetStrategy()
53 {
54 std::lock_guard<decltype(mutex_)> lock(mutex_);
55 if (!strategies_.IsEmpty()) {
56 return strategies_;
57 }
58 std::initializer_list<Strategy *> list = {
59 new (std::nothrow)LoadConfigCommonStrategy(),
60 new (std::nothrow)LoadConfigFromBundleInfoStrategy(),
61 new (std::nothrow)PermissionStrategy(),
62 new (std::nothrow)LoadConfigDataInfoStrategy(),
63 new (std::nothrow)ProcessSingleAppUserCrossStrategy()
64 };
65 auto ret = strategies_.Init(list);
66 if (!ret) {
67 std::for_each(list.begin(), list.end(), [](Strategy *item) {
68 delete item;
69 });
70 return strategies_;
71 }
72 return strategies_;
73 }
74 } // namespace OHOS::DataShare