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_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H
17 #define OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H
18 #include <cstdint>
19 #include <functional>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <utility>
24 
25 #include "rdb_common.h"
26 #include "rdb_types.h"
27 #include "statement.h"
28 namespace OHOS::NativeRdb {
29 class RdbStoreConfig;
30 class Statement;
31 class Connection {
32 public:
33     using Info = DistributedRdb::RdbDebugInfo;
34     using SConn = std::shared_ptr<Connection>;
35     using Stmt = std::shared_ptr<Statement>;
36     using Notifier = std::function<void(const std::set<std::string> &tables)>;
37     using Creator = std::pair<int32_t, SConn> (*)(const RdbStoreConfig &config, bool isWriter);
38     using Repairer = int32_t (*)(const RdbStoreConfig &config);
39     using Deleter = int32_t (*)(const RdbStoreConfig &config);
40     using Collector = std::map<std::string, Info> (*)(const RdbStoreConfig &config);
41     static std::pair<int32_t, SConn> Create(const RdbStoreConfig &config, bool isWriter);
42     static int32_t Repair(const RdbStoreConfig &config);
43     static int32_t Delete(const RdbStoreConfig &config);
44     static std::map<std::string, Info> Collect(const RdbStoreConfig &config);
45     static int32_t RegisterCreator(int32_t dbType, Creator creator);
46     static int32_t RegisterRepairer(int32_t dbType, Repairer repairer);
47     static int32_t RegisterDeleter(int32_t dbType, Deleter deleter);
48     static int32_t RegisterCollector(int32_t dbType, Collector collector);
49 
50     int32_t SetId(int32_t id);
51     int32_t GetId() const;
52     virtual ~Connection() = default;
53     virtual int32_t OnInitialize() = 0;
54     virtual std::pair<int32_t, Stmt> CreateStatement(const std::string &sql, SConn conn) = 0;
55     virtual int32_t GetDBType() const = 0;
56     virtual bool IsWriter() const = 0;
57     virtual int32_t ReSetKey(const RdbStoreConfig &config) = 0;
58     virtual int32_t TryCheckPoint(bool timeout) = 0;
59     virtual int32_t LimitWalSize() = 0;
60     virtual int32_t ConfigLocale(const std::string &localeStr) = 0;
61     virtual int32_t CleanDirtyData(const std::string &table, uint64_t cursor) = 0;
62     virtual int32_t SubscribeTableChanges(const Notifier &notifier) = 0;
63     virtual int32_t GetMaxVariable() const = 0;
64     virtual int32_t GetJournalMode() = 0;
65     virtual int32_t ClearCache() = 0;
66     virtual int32_t Subscribe(const std::string &event,
67         const std::shared_ptr<DistributedRdb::RdbStoreObserver> &observer) = 0;
68     virtual int32_t Unsubscribe(const std::string &event,
69         const std::shared_ptr<DistributedRdb::RdbStoreObserver> &observer) = 0;
70     virtual int32_t Backup(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey,
71         bool isAsync, SlaveStatus &slaveStatus) = 0;
72     virtual int32_t Restore(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey,
73         SlaveStatus &slaveStatus) = 0;
74     virtual ExchangeStrategy GenerateExchangeStrategy(const SlaveStatus &status) = 0;
75 
76 private:
77     int32_t id_ = 0;
78 };
79 } // namespace OHOS::NativeRdb
80 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H
81