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 "iconnection.h" 24 #include "macro_utils.h" 25 #include "ref_object.h" 26 #include "relational_store_delegate.h" 27 28 namespace DistributedDB { 29 class IRelationalStore; 30 using RelationalObserverAction = 31 std::function<void(const std::string &device, ChangedData &&changedData, bool isChangedData)>; 32 class RelationalStoreConnection : public IConnection, public virtual RefObject { 33 public: 34 struct SyncInfo { 35 const std::vector<std::string> &devices; 36 SyncMode mode = SYNC_MODE_PUSH_PULL; 37 const SyncStatusCallback onComplete = nullptr; 38 const Query &query; 39 bool wait = true; 40 }; 41 42 RelationalStoreConnection(); 43 44 explicit RelationalStoreConnection(IRelationalStore *store); 45 46 virtual ~RelationalStoreConnection() = default; 47 48 DISABLE_COPY_ASSIGN_MOVE(RelationalStoreConnection); 49 50 // Close and release the connection. 51 virtual int Close() = 0; 52 virtual int SyncToDevice(SyncInfo &info) = 0; 53 virtual int32_t GetCloudSyncTaskCount() = 0; 54 virtual std::string GetIdentifier() = 0; 55 virtual int CreateDistributedTable(const std::string &tableName, TableSyncType syncType) = 0; 56 virtual int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) = 0; 57 58 virtual int RemoveDeviceData() = 0; 59 virtual int RemoveDeviceData(const std::string &device) = 0; 60 virtual int RemoveDeviceData(const std::string &device, const std::string &tableName) = 0; 61 virtual int DoClean(ClearMode mode) = 0; 62 virtual void RegisterObserverAction(const RelationalObserverAction &action) = 0; 63 virtual int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 64 std::shared_ptr<ResultSet> &result) = 0; 65 virtual int SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb) = 0; 66 virtual int SetCloudDbSchema(const DataBaseSchema &schema) = 0; 67 virtual int SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader) = 0; 68 69 virtual int Sync(const std::vector<std::string> &devices, SyncMode mode, const Query &query, 70 const SyncProcessCallback &onProcess, int64_t waitTime) = 0; 71 72 virtual int GetStoreInfo(std::string &userId, std::string &appId, std::string &storeId) = 0; 73 74 protected: 75 // Get the stashed 'RelationalDB_ pointer' without ref. 76 template<typename DerivedDBType> GetDB()77 DerivedDBType *GetDB() const 78 { 79 return static_cast<DerivedDBType *>(store_); 80 } 81 82 virtual int Pragma(int cmd, void *parameter); 83 IRelationalStore *store_ = nullptr; 84 std::atomic<bool> isExclusive_; 85 }; 86 } // namespace DistributedDB 87 #endif 88 #endif // RELATIONAL_STORE_CONNECTION_H