1 /*
2  * Copyright (c) 2023 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 DATASHARE_TEMPLATE_H
17 #define DATASHARE_TEMPLATE_H
18 
19 #include <string>
20 #include <variant>
21 #include <vector>
22 #include "iremote_object.h"
23 
24 namespace OHOS {
25 namespace DataShare {
26 /**
27  *  Specifies the predicates structure of the template.
28  */
29 struct PredicateTemplateNode {
30     PredicateTemplateNode() = default;
PredicateTemplateNodePredicateTemplateNode31     PredicateTemplateNode(const std::string &key, const std::string &selectSql) : key_(key), selectSql_(selectSql) {}
32     /** Specifies the key of the sql. */
33     std::string key_;
34     /** Specifies the sql of the template. */
35     std::string selectSql_;
36 };
37 
38 /**
39  *  Specifies the template structure in subscribe.
40  */
41 struct Template {
42     Template() = default;
TemplateTemplate43     Template(const std::vector<PredicateTemplateNode> &predicates,
44         const std::string &scheduler) : predicates_(predicates), scheduler_(scheduler) {}
45     /** Specifies the predicates of the template. {@link #PredicateTemplateNode} */
46     std::vector<PredicateTemplateNode> predicates_;
47     /** Specifies the scheduler sql of the template. */
48     std::string scheduler_;
49 };
50 
51 /**
52  * Specifies the {@link Template} id structure, template is marked by the template id.
53  */
54 struct TemplateId {
55     /** Specifies the id of subscriber. */
56     int64_t subscriberId_;
57     /** Specifies the bundleName of template owner. */
58     std::string bundleName_;
59     bool operator==(const TemplateId &tplId) const
60     {
61         return subscriberId_ == tplId.subscriberId_ && bundleName_ == tplId.bundleName_;
62     }
63     bool operator!=(const TemplateId &tplId) const
64     {
65         return !(tplId == *this);
66     }
67     bool operator<(const TemplateId &tplId) const
68     {
69         if (subscriberId_ != tplId.subscriberId_) {
70             return subscriberId_ < tplId.subscriberId_;
71         }
72         return bundleName_ < tplId.bundleName_;
73     }
74 };
75 
76 /**
77  * Manages create datashare helper options.
78  */
79 struct CreateOptions {
80     /** Specifies whether the {@link DataShareHelper} in proxy mode. */
81     bool isProxy_ = true;
82     /** Specifies the System token. */
83     sptr<IRemoteObject> token_;
84     /** Specifies whether use options to create DataShareHelper. */
85     bool enabled_ = false;
86 };
87 
88 struct AshmemNode {
89     sptr<Ashmem> ashmem;
90     bool isManaged;
91 };
92 
93 /**
94  * Specifies the published item structure.
95  */
96 struct PublishedDataItem {
97     using DataType = std::variant<std::vector<uint8_t>, std::string>;
98     /** The key of the published data. */
99     std::string key_;
100     /** The subscriber id */
101     int64_t subscriberId_;
102     /** The published data. If the data is large, use Ashmem. Do not access, only for ipc */
103     std::variant<AshmemNode, std::string> value_;
PublishedDataItemPublishedDataItem104     PublishedDataItem(){};
105     PublishedDataItem(const PublishedDataItem &) = delete;
106     PublishedDataItem &operator=(const PublishedDataItem &) = delete;
107     virtual ~PublishedDataItem();
108     PublishedDataItem(const std::string &key, int64_t subscriberId, DataType value);
109     PublishedDataItem(PublishedDataItem &&item);
110     PublishedDataItem &operator=(PublishedDataItem &&item);
111     bool IsAshmem() const;
112     bool IsString() const;
113     sptr<Ashmem> MoveOutAshmem();
114     void SetAshmem(sptr<Ashmem> ashmem, bool isManaged = false);
115     void Set(DataType &value);
116     DataType GetData() const;
117 
118 private:
119     void Clear();
120 };
121 
122 /** Specifies the published data structure. */
123 struct Data {
124     /** Indicates the published data. {@link PublishedDataItem} */
125     std::vector<PublishedDataItem> datas_;
126     /** Indicates the version of data to publish, larger is newer. */
127     int version_ = 0;
128 };
129 
130 /**
131  * Specifies the change node structure of published data in callback.
132  */
133 struct PublishedDataChangeNode {
134     /** Specifies the bundleName of the callback. */
135     std::string ownerBundleName_;
136     /** Specifies the datas of the callback. */
137     std::vector<PublishedDataItem> datas_;
138 };
139 
140 /**
141  * Specifies the change node structure of rdb store data in callback.
142  */
143 struct RdbChangeNode {
144     /** Specifies the uri of the callback. */
145     std::string uri_;
146     /** Specifies the templateId of the callback. */
147     TemplateId templateId_;
148     /** Specifies the datas of the callback. */
149     std::vector<std::string> data_;
150 };
151 
152 /**
153  * Specifies the operation result structure.
154  */
155 struct OperationResult {
156     OperationResult() = default;
OperationResultOperationResult157     OperationResult(const std::string &key, int errCode) : key_(key), errCode_(errCode) {}
158     /**  Specifies the key of the operation result. */
159     std::string key_;
160     /** Specifies the operation result error code. */
161     int errCode_;
162 };
163 }  // namespace DataShare
164 }  // namespace OHOS
165 #endif //DATASHARE_TEMPLATE_H
166