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 29 namespace DistributedDB { 30 class StoreObserver; 31 } 32 namespace OHOS::NativeRdb { 33 class RdbStoreConfig; 34 class Statement; 35 class Connection { 36 public: 37 using Info = DistributedRdb::RdbDebugInfo; 38 using SConn = std::shared_ptr<Connection>; 39 using Stmt = std::shared_ptr<Statement>; 40 using Notifier = std::function<void(const DistributedRdb::RdbChangedData &rdbChangedData)>; 41 using Creator = std::pair<int32_t, SConn> (*)(const RdbStoreConfig &config, bool isWriter); 42 using Repairer = int32_t (*)(const RdbStoreConfig &config); 43 using Deleter = int32_t (*)(const RdbStoreConfig &config); 44 using Collector = std::map<std::string, Info> (*)(const RdbStoreConfig &config); 45 using Restorer = int32_t (*)(const RdbStoreConfig &config, const std::string &srcPath, const std::string &destPath); 46 static std::pair<int32_t, SConn> Create(const RdbStoreConfig &config, bool isWriter); 47 static int32_t Repair(const RdbStoreConfig &config); 48 static int32_t Delete(const RdbStoreConfig &config); 49 static int32_t Restore(const RdbStoreConfig &config, const std::string &srcPath, const std::string &destPath); 50 static std::map<std::string, Info> Collect(const RdbStoreConfig &config); 51 static int32_t RegisterCreator(int32_t dbType, Creator creator); 52 static int32_t RegisterRepairer(int32_t dbType, Repairer repairer); 53 static int32_t RegisterDeleter(int32_t dbType, Deleter deleter); 54 static int32_t RegisterCollector(int32_t dbType, Collector collector); 55 static int32_t RegisterRestorer(int32_t dbType, Restorer restorer); 56 57 int32_t SetId(int32_t id); 58 int32_t GetId() const; 59 void SetIsRecyclable(bool recyclable); 60 bool IsRecyclable() const; 61 virtual ~Connection() = default; 62 virtual int32_t VerifyAndRegisterHook(const RdbStoreConfig &config) = 0; 63 virtual std::pair<int32_t, Stmt> CreateStatement(const std::string &sql, SConn conn) = 0; 64 virtual int32_t GetDBType() const = 0; 65 virtual bool IsWriter() const = 0; 66 virtual int32_t ReSetKey(const RdbStoreConfig &config) = 0; 67 virtual int32_t TryCheckPoint(bool timeout) = 0; 68 virtual int32_t LimitWalSize() = 0; 69 virtual int32_t ConfigLocale(const std::string &localeStr) = 0; 70 virtual int32_t CleanDirtyData(const std::string &table, uint64_t cursor) = 0; 71 virtual int32_t SubscribeTableChanges(const Notifier ¬ifier) = 0; 72 virtual int32_t GetMaxVariable() const = 0; 73 virtual int32_t GetJournalMode() = 0; 74 virtual int32_t ClearCache() = 0; 75 virtual int32_t Subscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) = 0; 76 virtual int32_t Unsubscribe(const std::shared_ptr<DistributedDB::StoreObserver> &observer) = 0; 77 virtual int32_t Backup(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, bool isAsync, 78 SlaveStatus &slaveStatus) = 0; 79 virtual int32_t Restore( 80 const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey, SlaveStatus &slaveStatus) = 0; 81 virtual ExchangeStrategy GenerateExchangeStrategy(const SlaveStatus &status) = 0; 82 83 private: 84 int32_t id_ = 0; 85 bool isRecyclable_ = true; 86 }; 87 } // namespace OHOS::NativeRdb 88 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_INCLUDE_CONNECTION_H 89