• 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 = std::function<void(const std::string &device)>;
31 class RelationalStoreConnection : public IConnection, public virtual RefObject {
32 public:
33     struct SyncInfo {
34         const std::vector<std::string> &devices;
35         SyncMode mode = SYNC_MODE_PUSH_PULL;
36         const SyncStatusCallback &onComplete;
37         const Query &query;
38         bool wait = true;
39     };
40 
41     RelationalStoreConnection();
42 
43     explicit RelationalStoreConnection(IRelationalStore *store);
44 
45     virtual ~RelationalStoreConnection() = default;
46 
47     DISABLE_COPY_ASSIGN_MOVE(RelationalStoreConnection);
48 
49     // Close and release the connection.
50     virtual int Close() = 0;
51     virtual int TriggerAutoSync() = 0;
52     virtual int SyncToDevice(SyncInfo &info) = 0;
53     virtual std::string GetIdentifier() = 0;
54     virtual int CreateDistributedTable(const std::string &tableName) = 0;
55     virtual int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier) = 0;
56 
57     virtual int RemoveDeviceData(const std::string &device) = 0;
58     virtual int RemoveDeviceData(const std::string &device, const std::string &tableName) = 0;
59     virtual void RegisterObserverAction(const RelationalObserverAction &action) = 0;
60     virtual int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
61         std::shared_ptr<ResultSet> &result) = 0;
62     virtual int RemoveDeviceData() = 0;
63 protected:
64     // Get the stashed 'RelationalDB_ pointer' without ref.
65     template<typename DerivedDBType>
GetDB()66     DerivedDBType *GetDB() const
67     {
68         return static_cast<DerivedDBType *>(store_);
69     }
70 
71     virtual int Pragma(int cmd, void *parameter);
72     IRelationalStore *store_ = nullptr;
73     std::atomic<bool> isExclusive_;
74 };
75 } // namespace DistributedDB
76 #endif
77 #endif // RELATIONAL_STORE_CONNECTION_H