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 SQLITE_DB_H
17 #define SQLITE_DB_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 
23 #include "rdb_open_callback.h"
24 #include "rdb_store.h"
25 #include "result_set.h"
26 
27 #include "idatabase.h"
28 
29 namespace OHOS {
30 namespace UpdateEngine {
31 class SqliteDb : public IDataBase {
32 public:
33     SqliteDb() = default;
34     virtual ~SqliteDb() = default;
35     bool DeleteDbStore() final;
36     bool Insert(const std::string &tableName, const std::vector<NativeRdb::ValuesBucket> &values) final;
37     bool Delete(int32_t &deletedRows, const NativeRdb::RdbPredicates &predicates) final;
38     std::shared_ptr<NativeRdb::ResultSet> Query(
39         const NativeRdb::RdbPredicates &predicates, const std::vector<std::string> &columns) final;
40     bool Update(
41         int &changedRows, const NativeRdb::ValuesBucket &values, const NativeRdb::RdbPredicates &predicates) final;
42 
43 protected:
44     virtual std::string GetDbName() = 0;
45     virtual int32_t GetDbVersion() = 0;
46     virtual std::string GetDbStoreDir() = 0;
47     virtual void InitDbStoreDir() = 0;
48     virtual NativeRdb::RdbOpenCallback &GetDbOpenCallback() = 0;
49 
50 private:
51     std::shared_ptr<NativeRdb::RdbStore> GetDbStore();
52     std::shared_ptr<NativeRdb::RdbStore> CreateDbStore();
53 
54 private:
55     std::shared_ptr<NativeRdb::RdbStore> dbStore_ = nullptr;
56     std::mutex dbMutex_;
57     std::mutex writeMutex_;
58 };
59 } // namespace UpdateEngine
60 } // namespace OHOS
61 #endif // SQLITE_DB_H