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 #ifndef RELATIONAL_STORE_CONNECTION_H 16 #define RELATIONAL_STORE_CONNECTION_H 17 #ifdef RELATIONAL_STORE 18 19 #include <atomic> 20 #include <string> 21 22 #include "db_types.h" 23 #include "macro_utils.h" 24 #include "ref_object.h" 25 #include "relational_store_delegate.h" 26 27 namespace DistributedDB { 28 class IRelationalStore; 29 using RelationalObserverAction = std::function<void(const std::string &device)>; 30 class RelationalStoreConnection : public virtual RefObject { 31 public: 32 struct SyncInfo { 33 const std::vector<std::string> &devices; 34 SyncMode mode = SYNC_MODE_PUSH_PULL; 35 const SyncStatusCallback &onComplete; 36 const Query &query; 37 bool wait = true; 38 }; 39 40 RelationalStoreConnection(); 41 42 explicit RelationalStoreConnection(IRelationalStore *store); 43 44 virtual ~RelationalStoreConnection() = default; 45 46 DISABLE_COPY_ASSIGN_MOVE(RelationalStoreConnection); 47 48 // Close and release the connection. 49 virtual int Close() = 0; 50 virtual int TriggerAutoSync() = 0; 51 virtual int SyncToDevice(SyncInfo &info) = 0; 52 virtual std::string GetIdentifier() = 0; 53 virtual int CreateDistributedTable(const std::string &tableName) = 0; 54 virtual int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) = 0; 55 56 virtual int RemoveDeviceData(const std::string &device) = 0; 57 virtual int RemoveDeviceData(const std::string &device, const std::string &tableName) = 0; 58 virtual void RegisterObserverAction(const RelationalObserverAction &action) = 0; 59 60 protected: 61 // Get the stashed 'RelationalDB_ pointer' without ref. 62 template<typename DerivedDBType> GetDB()63 DerivedDBType *GetDB() const 64 { 65 return static_cast<DerivedDBType *>(store_); 66 } 67 68 virtual int Pragma(int cmd, void *parameter); 69 IRelationalStore *store_ = nullptr; 70 std::atomic<bool> isExclusive_; 71 }; 72 } // namespace DistributedDB 73 #endif 74 #endif // RELATIONAL_STORE_CONNECTION_H