1 /*
2  * Copyright (c) 2021-2022 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 OHOS_ABILITY_RUNTIME_DATA_ABILITY_OPERATION_BUILDER_H
17 #define OHOS_ABILITY_RUNTIME_DATA_ABILITY_OPERATION_BUILDER_H
18 
19 #include <map>
20 #include <memory>
21 #include "data_ability_operation.h"
22 #include "uri.h"
23 #include "parcel.h"
24 
25 using Uri = OHOS::Uri;
26 namespace OHOS {
27 namespace NativeRdb {
28 class DataAbilityPredicates;
29 class ValuesBucket;
30 }
31 namespace AppExecFwk {
32 class DataAbilityOperation;
33 class DataAbilityOperationBuilder final : public std::enable_shared_from_this<DataAbilityOperationBuilder> {
34     friend class DataAbilityOperation;
35 
36 public:
37     DataAbilityOperationBuilder(const int type, const std::shared_ptr<Uri> &uri);
38     ~DataAbilityOperationBuilder();
39     /**
40      * @brief Creates a DataAbilityOperation object.
41      * @return Returns the DataAbilityOperation object.
42      */
43     std::shared_ptr<DataAbilityOperation> Build();
44     /**
45      * @brief Sets the data records to be inserted or updated.
46      * @param values Indicates the data values to be set.
47      * @return Returns a DataAbilityOperationBuilder object containing the given values parameter.
48      */
49     std::shared_ptr<DataAbilityOperationBuilder> WithValuesBucket(std::shared_ptr<NativeRdb::ValuesBucket> &values);
50     /**
51      * @brief Sets filter criteria used for deleting updating or assert query data.
52      * @param predicates Indicates the filter criteria to set. If this parameter is null, all data records will be
53      * operated by default.
54      * @return Returns an object containing the given filter criteria.
55      */
56     std::shared_ptr<DataAbilityOperationBuilder> WithPredicates(
57         std::shared_ptr<NativeRdb::DataAbilityPredicates> &predicates);
58     /**
59      * @brief Sets the expected number of rows to update ,delete or assert query.
60      * @param count Indicates the expected number of rows to update or delete.
61      * @return Returns a DataAbilityOperationBuilder object containing the given count parameter.
62      */
63     std::shared_ptr<DataAbilityOperationBuilder> WithExpectedCount(int count);
64     /**
65      * @brief Adds a back reference to be used as a filter criterion in withPredicates(DataAbilityPredicates).
66      * @param requestArgIndex Indicates the index referencing the predicate parameter whose value is to be replaced.
67      * @param previousResult Indicates the index referencing the historical DataAbilityResult used to replace the value
68      * of the specified predicate parameter.
69      * @return Returns a DataAbilityOperationBuilder object containing the given requestArgIndex and previousResult
70      * parameters.
71      */
72     std::shared_ptr<DataAbilityOperationBuilder> WithPredicatesBackReference(int requestArgIndex, int previousResult);
73     /**
74      * @brief Adds a back reference to be used in withValuesBucket(ValuesBucket).
75      * @param backReferences Indicates the ValuesBucket object containing a set of key-value pairs. In each pair, the
76      * key specifies the value to be updated and the value specifies the index referencing the DataAbilityResult used to
77      * replace the specified value. This parameter cannot be null.
78      * @return Returns a DataAbilityOperationBuilder object containing the given backReferences parameter.
79      */
80     std::shared_ptr<DataAbilityOperationBuilder> WithValueBackReferences(
81         std::shared_ptr<NativeRdb::ValuesBucket> &backReferences);
82     /**
83      * @brief Sets an interrupt flag bit for a batch operation, which can be insert, update, delete, or assert.
84      * @param interrupted Specifies whether a batch operation can be interrupted. The value true indicates that the
85      * operation can be interrupted, and false indicates the opposite.
86      * @return Returns a DataAbilityOperationBuilder object containing the given interrupted parameter.
87      */
88     std::shared_ptr<DataAbilityOperationBuilder> WithInterruptionAllowed(bool interrupted);
89 
90 private:
91     int type_;
92     int expectedCount_;
93     bool interrupted_;
94     std::shared_ptr<Uri> uri_;
95     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucket_;
96     std::shared_ptr<NativeRdb::DataAbilityPredicates> dataAbilityPredicates_;
97     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucketReferences_;
98     std::map<int, int> dataAbilityPredicatesBackReferences_;
99 };
100 }  // namespace AppExecFwk
101 }  // namespace OHOS
102 #endif  // OHOS_ABILITY_RUNTIME_DATA_ABILITY_OPERATION_BUILDER_H
103