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 
16 #ifndef PKG_DB_HELPER_H
17 #define PKG_DB_HELPER_H
18 
19 #include "data_ability_predicates.h"
20 #include "rdb_errno.h"
21 #include "rdb_helper.h"
22 #include "rdb_open_callback.h"
23 #include "rdb_predicates.h"
24 #include "rdb_store.h"
25 #include "result_set.h"
26 #include "pkg_database.h"
27 #include "value_object.h"
28 #include "pkg_tables.h"
29 #include <unordered_set>
30 
31 namespace OHOS {
32 namespace ExternalDeviceManager {
33 class PkgDbHelper {
34 public:
35     static std::shared_ptr<PkgDbHelper> GetInstance();
36 
37     /* update (user, device, app) record */
38     int32_t QueryAllDriverInfos(std::vector<std::string> &driverInfos);
39     int32_t QueryPkgInfos(std::vector<PkgInfoTable> &pkgInfos,
40         bool isByDriverUid = false, const std::string &driverUid = "");
41     /* add or update (user, device, app) record */
42     int32_t AddOrUpdateRightRecord(
43         const std::string &bundleName, const std::string &bundleAbility, const std::string &driverInfo);
44     std::string QueryBundleInfoNames(const std::string &driverInfo);
45     int32_t QueryAllBundleAbilityNames(const std::string &bundleName, std::vector<std::string> &bundleAbilityNames);
46     /* delete (user, device, app) record */
47     int32_t DeleteRightRecord(const std::string &bundleName);
48 
49     int32_t CheckIfNeedUpdateEx(
50         bool &isUpdate, const std::string &bundleName);
51     int32_t QueryAllSize(std::vector<std::string> &allBundleAbility);
52     int32_t AddOrUpdatePkgInfo(const std::vector<PkgInfoTable> &pkgInfos, const std::string &bundleName = "");
53 
54 private:
55     PkgDbHelper();
56     DISALLOW_COPY_AND_MOVE(PkgDbHelper);
57     int32_t AddOrUpdateRightRecordEx(bool isUpdate, const std::string &bundleName,
58         const std::string &bundleAbility, const std::string &driverInfo);
59     int32_t QueryAndGetResultColumnValues(const OHOS::NativeRdb::RdbPredicates &rdbPredicates,
60         const std::vector<std::string> &columns, const std::string &columnName, std::vector<std::string> &columnValues);
61     int32_t DeleteAndNoOtherOperation(const std::string &whereClause, const std::vector<std::string> &whereArgs);
62 
63     static std::shared_ptr<PkgDbHelper> instance_;
64     std::mutex databaseMutex_;
65     std::shared_ptr<PkgDataBase> rightDatabase_;
66 };
67 } // namespace USB
68 } // namespace OHOS
69 
70 #endif
71