• 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 
16 #ifndef FOUNDATION_APPEXECFWK_OHOS_DATA_ABILITY_OPERATION_H
17 #define FOUNDATION_APPEXECFWK_OHOS_DATA_ABILITY_OPERATION_H
18 
19 #include <map>
20 #include <memory>
21 #include "data_ability_operation_builder.h"
22 #include "uri.h"
23 #include "parcel.h"
24 
25 using Uri = OHOS::Uri;
26 
27 namespace OHOS {
28 namespace NativeRdb {
29 class DataAbilityPredicates;
30 class ValuesBucket;
31 }
32 namespace AppExecFwk {
33 class DataAbilityOperationBuilder;
34 class DataAbilityOperation final : public Parcelable, public std::enable_shared_from_this<DataAbilityOperation> {
35 public:
36     ~DataAbilityOperation();
37 
38     DataAbilityOperation(
39         const std::shared_ptr<DataAbilityOperation> &dataAbilityOperation, const std::shared_ptr<Uri> &withUri);
40     DataAbilityOperation(Parcel &in);
41     DataAbilityOperation(const std::shared_ptr<DataAbilityOperationBuilder> &builder);
42     DataAbilityOperation();
43     /**
44      * @brief Creates an operation for inserting data.
45      * @param uri Indicates the path of data to operate.
46      * @return Returns an insert DataAbilityOperationBuilder object.
47      */
48     static std::shared_ptr<DataAbilityOperationBuilder> NewInsertBuilder(const std::shared_ptr<Uri> &uri);
49     /**
50      * @brief Creates an operation for updating data.
51      * @param uri Indicates the path of data to operate.
52      * @return Returns an update DataAbilityOperationBuilder object.
53      */
54     static std::shared_ptr<DataAbilityOperationBuilder> NewUpdateBuilder(const std::shared_ptr<Uri> &uri);
55     /**
56      * @brief Creates an operation for deleting data.
57      * @param uri Indicates the path of data to operate.
58      * @return Returns an delete DataAbilityOperationBuilder object.
59      */
60     static std::shared_ptr<DataAbilityOperationBuilder> NewDeleteBuilder(const std::shared_ptr<Uri> &uri);
61     /**
62      * @brief Creates an operation for asserting data.
63      * @param uri Indicates the path of data to operate.
64      * @return Returns an assert DataAbilityOperationBuilder object.
65      */
66     static std::shared_ptr<DataAbilityOperationBuilder> NewAssertBuilder(const std::shared_ptr<Uri> &uri);
67     /**
68      * @brief Obtains the value of the type attribute included in this DataAbilityOperation.
69      * @return Returns the type included in this DataAbilityOperation.
70      */
71     int GetType() const;
72     /**
73      * @brief Obtains the value of the uri attribute included in this DataAbilityOperation.
74      * @return Returns the uri included in this DataAbilityOperation.
75      */
76     std::shared_ptr<Uri> GetUri() const;
77     /**
78      * @brief Obtains the value of the valuesBucket attribute included in this DataAbilityOperation.
79      * @return Returns the valuesBucket included in this DataAbilityOperation.
80      */
81     std::shared_ptr<NativeRdb::ValuesBucket> GetValuesBucket() const;
82     /**
83      * @brief Obtains the value of the expectedCount attribute included in this DataAbilityOperation.
84      * @return Returns the expectedCount included in this DataAbilityOperation.
85      */
86     int GetExpectedCount() const;
87     /**
88      * @brief Obtains the value of the dataAbilityPredicates attribute included in this DataAbilityOperation.
89      * @return Returns the dataAbilityPredicates included in this DataAbilityOperation.
90      */
91     std::shared_ptr<NativeRdb::DataAbilityPredicates> GetDataAbilityPredicates() const;
92     /**
93      * @brief Obtains the value of the valuesBucketReferences attribute included in this DataAbilityOperation.
94      * @return Returns the valuesBucketReferences included in this DataAbilityOperation.
95      */
96     std::shared_ptr<NativeRdb::ValuesBucket> GetValuesBucketReferences() const;
97     /**
98      * @brief Obtains the value of the dataAbilityPredicatesBackReferences attribute included in this
99      * DataAbilityOperation.
100      * @return Returns the dataAbilityPredicatesBackReferences included in this DataAbilityOperation.
101      */
102     std::map<int, int> GetDataAbilityPredicatesBackReferences() const;
103     /**
104      * @brief Checks whether an insert operation is created.
105      * @return Returns true if it is an insert operation; returns false otherwise.
106      */
107     bool IsInsertOperation() const;
108     /**
109      * @brief Checks whether an delete operation is created.
110      * @return Returns true if it is an delete operation; returns false otherwise.
111      */
112     bool IsDeleteOperation() const;
113     /**
114      * @brief Checks whether an update operation is created.
115      * @return Returns true if it is an update operation; returns false otherwise.
116      */
117     bool IsUpdateOperation() const;
118     /**
119      * @brief Checks whether an assert operation is created.
120      * @return Returns true if it is an assert operation; returns false otherwise.
121      */
122     bool IsAssertOperation() const;
123     /**
124      * @brief Checks whether an operation can be interrupted.
125      * @return Returns true if the operation can be interrupted; returns false otherwise.
126      */
127     bool IsInterruptionAllowed() const;
128 
129     bool operator==(const DataAbilityOperation &other) const;
130     DataAbilityOperation &operator=(const DataAbilityOperation &other);
131     bool Marshalling(Parcel &out) const;
132     static DataAbilityOperation *Unmarshalling(Parcel &in);
133 
134     /**
135      * @brief Creates a DataAbilityOperation instance based on the given Parcel object
136      * @param in Indicates the Parcel object.
137      * @return Returns the DataAbilityOperation object.
138      */
139     static std::shared_ptr<DataAbilityOperation> CreateFromParcel(Parcel &in);
140 
141 public:
142     static constexpr int TYPE_INSERT = 1;
143     static constexpr int TYPE_UPDATE = 2;
144     static constexpr int TYPE_DELETE = 3;
145     static constexpr int TYPE_ASSERT = 4;
146 
147 private:
148     void PutMap(Parcel &in);
149     bool ReadFromParcel(Parcel &in);
150 
151 private:
152     // no object in parcel
153     static constexpr int VALUE_NULL = 0;
154     // object exist in parcel
155     static constexpr int VALUE_OBJECT = 1;
156     static constexpr int REFERENCE_THRESHOLD = 3 * 1024 * 1024;
157     int type_ = -1;
158     int expectedCount_ = 0;
159     bool interrupted_ = false;
160     std::shared_ptr<Uri> uri_ = nullptr;
161     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucket_ = nullptr;
162     std::shared_ptr<NativeRdb::DataAbilityPredicates> dataAbilityPredicates_ = nullptr;
163     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucketReferences_ = nullptr;
164     std::map<int, int> dataAbilityPredicatesBackReferences_;
165 };
166 }  // namespace AppExecFwk
167 }  // namespace OHOS
168 #endif  // FOUNDATION_APPEXECFWK_OHOS_DATA_ABILITY_OPERATION_H