• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, Origin origin)>;
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 std::string GetIdentifier() = 0;
54     virtual int CreateDistributedTable(const std::string &tableName, TableSyncType syncType) = 0;
55     virtual int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier) = 0;
56 
57     virtual int RemoveDeviceData() = 0;
58     virtual int RemoveDeviceData(const std::string &device) = 0;
59     virtual int RemoveDeviceData(const std::string &device, const std::string &tableName) = 0;
60     virtual int RegisterObserverAction(const StoreObserver *observer, const RelationalObserverAction &action) = 0;
61     virtual int UnRegisterObserverAction(const StoreObserver *observer) = 0;
62     virtual int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
63         std::shared_ptr<ResultSet> &result) = 0;
64 
65     virtual int GetStoreInfo(std::string &userId, std::string &appId, std::string &storeId) = 0;
66 
67     virtual int SetTrackerTable(const TrackerSchema &schema) = 0;
68     virtual int ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records) = 0;
69     virtual int CleanTrackerData(const std::string &tableName, int64_t cursor) = 0;
70 
71     virtual int SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty) = 0;
72 
73     virtual int Pragma(PragmaCmd cmd, PragmaData &pragmaData) = 0;
74 
75     virtual int UpsertData(RecordStatus status, const std::string &tableName, const std::vector<VBucket> &records) = 0;
76 
77     virtual int GetDownloadingAssetsCount(int32_t &count) = 0;
78 
79     virtual int SetDistributedDbSchema(const DistributedSchema &schema, bool isForceUpgrade) = 0;
80 
81     virtual int SetTableMode(DistributedTableMode tableMode) = 0;
82 
83 #ifdef USE_DISTRIBUTEDDB_CLOUD
84     virtual int32_t GetCloudSyncTaskCount() = 0;
85 
86     virtual int DoClean(ClearMode mode) = 0;
87 
88     virtual int ClearCloudWatermark(const std::set<std::string> &tableNames) = 0;
89 
90     virtual int SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb) = 0;
91 
92     virtual int PrepareAndSetCloudDbSchema(const DataBaseSchema &schema) = 0;
93 
94     virtual int SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader) = 0;
95 
96     virtual int SetCloudSyncConfig(const CloudSyncConfig &config) = 0;
97 
98     virtual int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess, uint64_t taskId) = 0;
99 
100     virtual SyncProcess GetCloudTaskStatus(uint64_t taskId) = 0;
101 #endif
102     virtual int OperateDataStatus(uint32_t dataOperator) = 0;
103 
104     virtual int32_t GetDeviceSyncTaskCount() = 0;
105 protected:
106     // Get the stashed 'RelationalDB_ pointer' without ref.
107     template<typename DerivedDBType>
GetDB()108     DerivedDBType *GetDB() const
109     {
110         return static_cast<DerivedDBType *>(store_);
111     }
112 
113     virtual int Pragma(int cmd, void *parameter);
114     IRelationalStore *store_ = nullptr;
115     std::atomic<bool> isExclusive_;
116 };
117 } // namespace DistributedDB
118 #endif
119 #endif // RELATIONAL_STORE_CONNECTION_H