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 &notifier) = 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 int RegisterObserverAction(const StoreObserver *observer, const RelationalObserverAction &action) = 0;
63     virtual int UnRegisterObserverAction(const StoreObserver *observer) = 0;
64     virtual int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
65         std::shared_ptr<ResultSet> &result) = 0;
66     virtual int SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb) = 0;
67     virtual int PrepareAndSetCloudDbSchema(const DataBaseSchema &schema) = 0;
68     virtual int SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader) = 0;
69 
70     virtual int GetStoreInfo(std::string &userId, std::string &appId, std::string &storeId) = 0;
71 
72     virtual int SetTrackerTable(const TrackerSchema &schema) = 0;
73     virtual int ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records) = 0;
74     virtual int CleanTrackerData(const std::string &tableName, int64_t cursor) = 0;
75 
76     virtual int SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty) = 0;
77 
78     virtual int Pragma(PragmaCmd cmd, PragmaData &pragmaData) = 0;
79 
80     virtual int UpsertData(RecordStatus status, const std::string &tableName, const std::vector<VBucket> &records) = 0;
81 
82     virtual int SetCloudSyncConfig(const CloudSyncConfig &config) = 0;
83 
84     virtual int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess, uint64_t taskId) = 0;
85 
86     virtual SyncProcess GetCloudTaskStatus(uint64_t taskId) = 0;
87 protected:
88     // Get the stashed 'RelationalDB_ pointer' without ref.
89     template<typename DerivedDBType>
GetDB()90     DerivedDBType *GetDB() const
91     {
92         return static_cast<DerivedDBType *>(store_);
93     }
94 
95     virtual int Pragma(int cmd, void *parameter);
96     IRelationalStore *store_ = nullptr;
97     std::atomic<bool> isExclusive_;
98 };
99 } // namespace DistributedDB
100 #endif
101 #endif // RELATIONAL_STORE_CONNECTION_H