• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #ifndef RELATIONAL_STORE_IMPL_RDBSTORE_FFI_H
17 #define RELATIONAL_STORE_IMPL_RDBSTORE_FFI_H
18 
19 #include <list>
20 
21 #include "cj_lambda.h"
22 #include "ffi_remote_data.h"
23 #include "napi_base_context.h"
24 #include "napi_rdb_js_utils.h"
25 #include "rdb_common.h"
26 #include "rdb_store.h"
27 #include "rdb_store_config.h"
28 #include "rdb_types.h"
29 #include "relational_store_impl_resultsetproxy.h"
30 #include "relational_store_utils.h"
31 
32 namespace OHOS {
33 namespace Relational {
34 class RdbStoreObserverImpl : public DistributedRdb::RdbStoreObserver {
35 public:
36     enum FuncType : int32_t { NoParam = 0, ParamArrStr, ParamChangeInfo };
37     RdbStoreObserverImpl(std::function<void()> *callback, const std::function<void()> &callbackRef);
38     RdbStoreObserverImpl(int64_t id, FuncType type, int32_t mode = DistributedRdb::REMOTE);
39     ~RdbStoreObserverImpl() override = default;
OnChange()40     void OnChange() override
41     {
42         m_callbackRef();
43     };
OnChange(const std::vector<std::string> & devices)44     void OnChange(const std::vector<std::string> &devices) override
45     {
46         carrStrFunc(devices);
47     };
OnChange(const DistributedRdb::Origin & origin,const PrimaryFields & fields,DistributedRdb::RdbStoreObserver::ChangeInfo && changeInfo)48     void OnChange(const DistributedRdb::Origin &origin, const PrimaryFields &fields,
49         DistributedRdb::RdbStoreObserver::ChangeInfo &&changeInfo) override
50     {
51         if (mode_ != DistributedRdb::CLOUD_DETAIL && mode_ != DistributedRdb::LOCAL_DETAIL) {
52             RdbStoreObserver::OnChange(origin, fields, std::move(changeInfo));
53             return;
54         }
55         changeInfoFunc(origin, fields, std::move(changeInfo));
56     };
57 
GetCallBackId()58     int64_t GetCallBackId()
59     {
60         return callbackId;
61     };
62     std::function<void()> *GetCallBack();
63 
64 private:
65     std::function<void()> *m_callback = nullptr;
66     std::function<void()> m_callbackRef = nullptr;
67     int32_t mode_ = DistributedRdb::REMOTE;
68     int64_t callbackId = 0;
69     FuncType funcType = NoParam;
70     std::function<void()> func = nullptr;
71     std::function<void(const std::vector<std::string> &devices)> carrStrFunc = nullptr;
72     std::function<void(const DistributedRdb::Origin &origin, const PrimaryFields &fields,
73         DistributedRdb::RdbStoreObserver::ChangeInfo &&changeInfo)>
74         changeInfoFunc = nullptr;
75 };
76 
77 class SyncObserverImpl : public DistributedRdb::DetailProgressObserver {
78 public:
79     SyncObserverImpl(int64_t id);
80     ~SyncObserverImpl() override = default;
ProgressNotification(const DistributedRdb::Details & details)81     void ProgressNotification(const DistributedRdb::Details &details) override
82     {
83         func(details);
84     };
85 
GetCallBackId()86     int64_t GetCallBackId()
87     {
88         return callbackId;
89     };
90 
91 private:
92     int64_t callbackId;
93     std::function<void(const DistributedRdb::Details &details)> func;
94 };
95 
96 class RdbStoreImpl : public OHOS::FFI::FFIData {
97 public:
GetRuntimeType()98     OHOS::FFI::RuntimeType *GetRuntimeType() override
99     {
100         return GetClassType();
101     }
102 
103     explicit RdbStoreImpl(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore);
104 
105     std::shared_ptr<NativeRdb::ResultSet> Query(RdbPredicatesImpl &predicates, char **column, int64_t columnSize);
106     std::shared_ptr<NativeRdb::ResultSet> RemoteQuery(
107         char *device, RdbPredicatesImpl &predicates, char **column, int64_t columnSize);
108     int Delete(RdbPredicatesImpl &predicates, int32_t *errCode);
109     int32_t SetDistributedTables(char **tables, int64_t tablesSize);
110     int32_t SetDistributedTables(char **tables, int64_t tablesSize, int32_t type);
111     int32_t SetDistributedTables(
112         char **tables, int64_t tablesSize, int32_t type, DistributedRdb::DistributedConfig &distributedConfig);
113     int32_t Commit();
114     int32_t RollBack();
115     int32_t BeginTransaction();
116     int32_t Backup(const char *destName);
117     int32_t Restore(const char *srcName);
118     char *ObtainDistributedTableName(const char *device, const char *table);
119     int32_t Emit(const char *event);
120     int64_t Insert(const char *table, ValuesBucket valuesBucket, int32_t conflict, int32_t *errCode);
121     int32_t BatchInsert(int64_t &insertNum, const char *tableName, ValuesBucket *valuesBuckets, int64_t valuesSize);
122     void ExecuteSql(const char *sql, int32_t *errCode);
123     int32_t CleanDirtyData(const char *tableName, uint64_t cursor);
124     CArrSyncResult Sync(int32_t mode, RdbPredicatesImpl &predicates);
125     int32_t Update(ValuesBucket valuesBucket, RdbPredicatesImpl &predicates,
126         NativeRdb::ConflictResolution conflictResolution, int32_t *errCode);
127     std::shared_ptr<NativeRdb::ResultSet> QuerySql(const char *sql, ValueType *bindArgs, int64_t size);
128     void ExecuteSql(const char *sql, ValueType *bindArgs, int64_t bindArgsSize, int32_t *errCode);
129     int32_t RegisterObserver(const char *event, bool interProcess, std::function<void()> *callback,
130         const std::function<void()> &callbackRef);
131     int32_t RegisteredObserver(DistributedRdb::SubscribeOption option,
132         std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers,
133         std::function<void()> *callback, const std::function<void()> &callbackRef);
134     int32_t RegisterObserverArrStr(int32_t subscribeType, int64_t callbackId);
135     int32_t RegisterObserverChangeInfo(int32_t subscribeType, int64_t callbackId);
136     int32_t RegisterObserverProgressDetails(int64_t callbackId);
137     bool HasRegisteredObserver(
138         std::function<void()> *callback, std::list<std::shared_ptr<RdbStoreObserverImpl>> &observers);
139     int32_t UnRegisterObserver(const char *event, bool interProcess, std::function<void()> *callback);
140     int32_t UnRegisterAllObserver(const char *event, bool interProcess);
141     int32_t UnRegisteredObserver(DistributedRdb::SubscribeOption option,
142         std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers,
143         std::function<void()> *callback);
144     int32_t UnRegisteredAllObserver(DistributedRdb::SubscribeOption option,
145         std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers);
146     int32_t UnRegisterObserverArrStrChangeInfo(int32_t subscribeType, int64_t callbackId);
147     int32_t UnRegisterObserverArrStrChangeInfoAll(int32_t subscribeType);
148     int32_t UnRegisterObserverProgressDetails(int64_t callbackId);
149     int32_t UnRegisterObserverProgressDetailsAll();
150     int32_t CloudSync(int32_t mode, CArrStr tables, int64_t callbackId);
151     int32_t GetVersion(int32_t &errCode);
152     void SetVersion(int32_t value, int32_t &errCode);
153     ModifyTime GetModifyTime(char *cTable, char *cColumnName, CArrPRIKeyType &cPrimaryKeys, int32_t &errCode);
154 
155 private:
156     friend class OHOS::FFI::RuntimeType;
157     friend class OHOS::FFI::TypeBase;
158     static OHOS::FFI::RuntimeType *GetClassType();
159     std::vector<OHOS::NativeRdb::ValueObject> bindArgs;
160     std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore_;
161     std::vector<uint8_t> newKey;
162     std::list<std::shared_ptr<RdbStoreObserverImpl>> observers_[DistributedRdb::SUBSCRIBE_MODE_MAX];
163     std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> localObservers_;
164     std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> localSharedObservers_;
165     std::list<std::shared_ptr<SyncObserverImpl>> syncObservers_;
166 };
167 
168 int64_t GetRdbStore(OHOS::AbilityRuntime::Context *context, StoreConfig config, int32_t *errCode);
169 
170 void DeleteRdbStore(OHOS::AbilityRuntime::Context *context, const char *name, int32_t *errCode);
171 
172 void DeleteRdbStoreConfig(OHOS::AbilityRuntime::Context *context, StoreConfig config, int32_t *errCode);
173 } // namespace Relational
174 } // namespace OHOS
175 
176 #endif
177