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 #include "relational_store_impl_resultsetproxy.h"
21 #include "relational_store_utils.h"
22 #include "rdb_store_config.h"
23 #include "ffi_remote_data.h"
24 #include "napi_base_context.h"
25 #include "rdb_store.h"
26 #include "rdb_types.h"
27 #include "rdb_common.h"
28 #include "napi_rdb_js_utils.h"
29 #include "cj_lambda.h"
30 
31 namespace OHOS {
32 namespace Relational {
33     class RdbStoreObserverImpl : public OHOS::DistributedRdb::RdbStoreObserver {
34     public:
35         RdbStoreObserverImpl(std::function<void()> *callback, const std::function<void()>& callbackRef);
36         ~RdbStoreObserverImpl() override = default;
37         void OnChange() override;
OnChange(const std::vector<std::string> & devices)38         void OnChange(const std::vector<std::string> &devices) override
39         {
40             return;
41         };
OnChange(const DistributedRdb::Origin & origin,const PrimaryFields & fields,ChangeInfo && changeInfo)42         void OnChange(const DistributedRdb::Origin &origin, const PrimaryFields &fields,
43             ChangeInfo &&changeInfo) override
44         {
45             OnChange(origin.id);
46         };
47         std::function<void()> *GetCallBack();
48     private:
49         std::function<void()> *m_callback;
50         std::function<void()> m_callbackRef;
51     };
52 
53     class RdbStoreImpl : public OHOS::FFI::FFIData {
54     public:
GetRuntimeType()55         OHOS::FFI::RuntimeType* GetRuntimeType() override
56         {
57             return GetClassType();
58         }
59 
60         explicit RdbStoreImpl(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore);
61 
62         std::shared_ptr<NativeRdb::ResultSet> Query(RdbPredicatesImpl &predicates, char** column, int64_t columnSize);
63         std::shared_ptr<NativeRdb::ResultSet> RemoteQuery(char* device, RdbPredicatesImpl &predicates, char** column,
64             int64_t columnSize);
65         int Delete(RdbPredicatesImpl &predicates, int32_t *errCode);
66         void SetDistributedTables(char** tables, int64_t tablesSize);
67         void SetDistributedTables(char** tables, int64_t tablesSize, int32_t type);
68         void SetDistributedTables(char** tables, int64_t tablesSize, int32_t type,
69             DistributedRdb::DistributedConfig &distributedConfig);
70         int32_t Commit();
71         int32_t RollBack();
72         int32_t BeginTransaction();
73         int32_t Backup(const char* destName);
74         int32_t Restore(const char* srcName);
75         char* ObtainDistributedTableName(const char* device, const char* table);
76         int32_t Emit(const char* event);
77         int64_t Insert(const char* table, ValuesBucket valuesBucket, int32_t conflict, int32_t *errCode);
78         int32_t BatchInsert(int64_t &insertNum, const char* tableName, ValuesBucket* valuesBuckets, int64_t valuesSize);
79         void ExecuteSql(const char* sql, int32_t *errCode);
80         int32_t CleanDirtyData(const char* tableName, uint64_t cursor);
81         CArrSyncResult Sync(int32_t mode, RdbPredicatesImpl &predicates);
82         int32_t Update(ValuesBucket valuesBucket, RdbPredicatesImpl &predicates,
83             NativeRdb::ConflictResolution conflictResolution, int32_t *errCode);
84         std::shared_ptr<NativeRdb::ResultSet> QuerySql(const char *sql, ValueType *bindArgs, int64_t size);
85         void ExecuteSql(const char* sql, ValueType* bindArgs, int64_t bindArgsSize, int32_t *errCode);
86         int32_t RegisterObserver(const char *event, bool interProcess, std::function<void()> *callback,
87             const std::function<void()>& callbackRef);
88         int32_t RegisteredObserver(DistributedRdb::SubscribeOption option, std::map<std::string,
89             std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers,
90         std::function<void()> *callback, const std::function<void()>& callbackRef);
91         bool HasRegisteredObserver(std::function<void()> *callback,
92             std::list<std::shared_ptr<RdbStoreObserverImpl>> &observers);
93         int32_t UnRegisterObserver(const char *event, bool interProcess, std::function<void()> *callback);
94         int32_t UnRegisterAllObserver(const char *event, bool interProcess);
95         int32_t UnRegisteredObserver(DistributedRdb::SubscribeOption option, std::map<std::string,
96             std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers,
97         std::function<void()> *callback);
98         int32_t UnRegisteredAllObserver(DistributedRdb::SubscribeOption option, std::map<std::string,
99             std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers);
100 
101         std::vector<OHOS::NativeRdb::ValueObject> bindArgs;
102         std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore_;
103         std::vector<uint8_t> newKey;
104         std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> localObservers_;
105         std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> localSharedObservers_;
106     private:
107         friend class OHOS::FFI::RuntimeType;
108         friend class OHOS::FFI::TypeBase;
109         static OHOS::FFI::RuntimeType* GetClassType();
110     };
111 
112     int64_t GetRdbStore(OHOS::AbilityRuntime::Context* context, StoreConfig config,
113         int32_t *errCode);
114 
115     void DeleteRdbStore(OHOS::AbilityRuntime::Context* context, const char* name,
116         int32_t *errCode);
117 
118     void DeleteRdbStoreConfig(OHOS::AbilityRuntime::Context* context, StoreConfig config,
119         int32_t *errCode);
120 }
121 }
122 
123 #endif
124 
125