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 #ifndef PKG_DATABASE_H
16 #define PKG_DATABASE_H
17 
18 #include <pthread.h>
19 
20 #include "data_ability_predicates.h"
21 #include "rdb_errno.h"
22 #include "rdb_helper.h"
23 #include "rdb_open_callback.h"
24 #include "rdb_predicates.h"
25 #include "rdb_store.h"
26 #include "result_set.h"
27 #include "value_object.h"
28 
29 namespace OHOS {
30 namespace ExternalDeviceManager {
31 static std::string PKG_DB_PATH = "/data/service/el1/public/pkg_service/";
32 
33 constexpr const char *PKG_DB_NAME = "pkg.db";
34 constexpr const char *PKG_TABLE_NAME = "pkgInfoTable";
35 constexpr int32_t DATABASE_OPEN_VERSION = 1;
36 constexpr int32_t DATABASE_NEW_VERSION = 2;
37 
38 constexpr const char *CREATE_PKG_TABLE = "CREATE TABLE IF NOT EXISTS [pkgInfoTable]("
39                                                "[id] INTEGER PRIMARY KEY AUTOINCREMENT, "
40                                                "[driverUid] TEXT,"
41                                                "[userId] INTEGER,"
42                                                "[appIndex] INTEGER,"
43                                                "[bundleAbility] TEXT,"
44                                                "[bundleName] TEXT,"
45                                                "[driverName] TEXT,"
46                                                "[driverInfo] TEXT );";
47 
48 class PkgDataBase {
49 public:
50     static std::shared_ptr<PkgDataBase> GetInstance();
51     int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues);
52     int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values,
53         const OHOS::NativeRdb::RdbPredicates &predicates);
54     int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const std::string &whereClause,
55         const std::vector<std::string> &whereArgs);
56     int32_t Delete(const OHOS::NativeRdb::RdbPredicates &rdbPredicates);
57     int32_t Delete(int32_t &changedRows, const std::string &whereClause, const std::vector<std::string> &whereArgs);
58     std::shared_ptr<OHOS::NativeRdb::ResultSet> Query(
59         const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns);
60     int32_t BeginTransaction();
61     int32_t Commit();
62     int32_t RollBack();
63     bool InitDB();
64 
65 private:
66     PkgDataBase();
67     DISALLOW_COPY_AND_MOVE(PkgDataBase);
68     static std::shared_ptr<PkgDataBase> instance_;
69     std::shared_ptr<OHOS::NativeRdb::RdbStore> store_;
70 };
71 
72 class PkgDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback {
73 public:
74     int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override;
75     int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
76     int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override;
77 };
78 } // namespace USB
79 } // namespace OHOS
80 
81 #endif
82