1 /*
2  * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_RDB_RDB_DATA_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_RDB_RDB_DATA_MANAGER_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <utility>
22 
23 #include "rdb_helper.h"
24 #include "rdb_open_callback.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 namespace {
29 constexpr static const char *ABILITY_RDB_NAME = "/ability_manager_service.db";
30 constexpr static const char *ABILITY_RDB_PATH = "/data/service/el1/public/database/ability_manager_service";
31 constexpr static int32_t ABILITY_RDB_VERSION = 1;
32 } // namespace
33 
34 struct AmsRdbConfig {
35     std::string dbPath{ ABILITY_RDB_PATH };
36     std::string dbName{ ABILITY_RDB_NAME };
37     std::string tableName;
38     std::string journalMode;
39     std::string syncMode;
40     int32_t version{ ABILITY_RDB_VERSION };
41 };
42 
43 class RdbDataManager final {
44 public:
RdbDataManager(const AmsRdbConfig & rdbConfig)45     RdbDataManager(const AmsRdbConfig &rdbConfig) : amsRdbConfig_(rdbConfig) {}
~RdbDataManager()46     ~RdbDataManager() {}
47 
48     int32_t Init(NativeRdb::RdbOpenCallback &rdbCallback);
49 
50     int32_t InsertData(const NativeRdb::ValuesBucket &valuesBucket);
51     int32_t BatchInsert(int64_t &outInsertNum, const std::vector<NativeRdb::ValuesBucket> &valuesBuckets);
52     int32_t UpdateData(
53         const NativeRdb::ValuesBucket &valuesBucket, const NativeRdb::AbsRdbPredicates &absRdbPredicates);
54     int32_t DeleteData(const NativeRdb::AbsRdbPredicates &absRdbPredicates);
55     std::shared_ptr<NativeRdb::AbsSharedResultSet> QueryData(const NativeRdb::AbsRdbPredicates &absRdbPredicates);
56     void ClearCache();
57 
58 private:
59     std::mutex rdbMutex_;
60     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
61     AmsRdbConfig amsRdbConfig_;
62 };
63 } // namespace AbilityRuntime
64 } // namespace OHOS
65 #endif // OHOS_ABILITY_RUNTIME_RDB_RDB_DATA_MANAGER_H