1 /*
2  * Copyright (c) 2021 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_LOCAL_KV_DB_H
17 #define SQLITE_LOCAL_KV_DB_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include <mutex>
21 #include <string>
22 
23 #include "local_kvdb.h"
24 #include "sqlite_local_storage_executor.h"
25 #include "sqlite_storage_engine.h"
26 
27 namespace DistributedDB {
28 class SQLiteLocalKvDB final : public LocalKvDB {
29 public:
30     SQLiteLocalKvDB();
31     ~SQLiteLocalKvDB() override;
32 
33     // Delete the copy and assign constructors
34     DISABLE_COPY_ASSIGN_MOVE(SQLiteLocalKvDB);
35 
36     // Save the option and uri for sqlite
37     int Open(const KvDBProperties &kvDBProp) override;
38 
39     // Create a connection object.
40     GenericKvDBConnection *NewConnection(int &errCode) override;
41 
42     // Invoked automatically when connection count is zero
43     void Close() override;
44 
45     int Rekey(const CipherPassword &passwd) override;
46 
47     int Export(const std::string &filePath, const CipherPassword &passwd) override;
48 
49     int Import(const std::string &filePath, const CipherPassword &passwd) override;
50 
51     int RunExportLogic(CipherType type, const CipherPassword &passwd, const std::string &newDbName);
52 
53     int RunRekeyLogic(CipherType type, const CipherPassword &passwd);
54 
55     SQLiteLocalStorageExecutor *GetHandle(bool isWrite, int &errCode,
56         OperatePerm perm = OperatePerm::NORMAL_PERM) const;
57 
58     void ReleaseHandle(SQLiteLocalStorageExecutor *&handle) const;
59 
60     int GetVersion(const KvDBProperties &kvDBProp, int &version, bool &isDbExisted) const;
61 
62     int SetVersion(const KvDBProperties &kvDBProp, int version);
63 
64     const KvDBProperties &GetDbProperties() const;
65 
66     KvDBProperties &GetDbPropertyForUpdate();
67 
68     static int BackupCurrentDatabase(const KvDBProperties &properties, const std::string &dir);
69 
70     static int ImportDatabase(const KvDBProperties &properties, const std::string &dir, const CipherPassword &passwd);
71 
72     int RemoveKvDB(const KvDBProperties &properties) override;
73 
74     int GetKvDBSize(const KvDBProperties &properties, uint64_t &size) const override;
75 
76     int InitDatabaseContext(const KvDBProperties &kvDBProp);
77 
78     void EnableAutonomicUpgrade() override;
79 
80 private:
81     int InitStorageEngine(const KvDBProperties &kvDBProp);
82 
83     void InitDataBaseOption(const KvDBProperties &kvDBProp, OpenDbProperties &option) const;
84 
85     int CheckVersionAndUpgradeIfNeed(const OpenDbProperties &openProp);
86 
87     DECLARE_OBJECT_TAG(SQLiteLocalKvDB);
88 
89     bool isAutonomicUpgradeEnable_ = false;
90     SQLiteStorageEngine *storageEngine_;
91 };
92 } // namespace DistributedDB
93 #endif // OMIT_MULTI_VER
94 #endif  // SQLITE_LOCAL_KV_DB_H
95