• 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 NATIVE_RDB_TRANSACTION_H
17 #define NATIVE_RDB_TRANSACTION_H
18 
19 #include <functional>
20 #include <tuple>
21 #include <utility>
22 #include <vector>
23 
24 #include "abs_rdb_predicates.h"
25 #include "rdb_common.h"
26 #include "rdb_errno.h"
27 #include "rdb_types.h"
28 #include "rdb_visibility.h"
29 #include "result_set.h"
30 #include "values_bucket.h"
31 #include "values_buckets.h"
32 
33 namespace OHOS::NativeRdb {
34 class Connection;
35 class API_EXPORT Transaction {
36 public:
37     /**
38      * @brief Use Fields replace std::vector<std::string> columns.
39      */
40     using Fields = std::vector<std::string>;
41 
42     /**
43      * @brief Use Values replace std::vector<ValueObject> args.
44      */
45     using Values = std::vector<ValueObject>;
46 
47     /**
48      * @brief Use Row replace ValuesBucket.
49      */
50     using Row = ValuesBucket;
51 
52     /**
53      * @brief Use Rows replace std::vector<Row>.
54      */
55     using Rows = std::vector<Row>;
56 
57     /**
58      * @brief Use Rows replace std::vector<Row>.
59      */
60     using RefRows = ValuesBuckets;
61 
62     /**
63      * @brief Use Resolution replace ConflictResolution.
64      */
65     using Resolution = ConflictResolution;
66 
67     static constexpr Resolution NO_ACTION = ConflictResolution::ON_CONFLICT_NONE;
68 
69     enum TransactionType : int32_t {
70         DEFERRED,
71         IMMEDIATE,
72         EXCLUSIVE,
73         TRANS_BUTT,
74     };
75 
76     using Creator = std::function<std::pair<int32_t, std::shared_ptr<Transaction>>(
77         int32_t type, std::shared_ptr<Connection> connection, const std::string &)>;
78 
79     static std::pair<int32_t, std::shared_ptr<Transaction>> Create(
80         int32_t type, std::shared_ptr<Connection> connection, const std::string &path);
81     static int32_t RegisterCreator(Creator creator);
82 
83     virtual ~Transaction() = default;
84 
85     virtual int32_t Commit() = 0;
86     virtual int32_t Rollback() = 0;
87     virtual int32_t Close() = 0;
88 
89     /**
90      * @brief Inserts a row of data into the target table.
91      *
92      * @param table Indicates the target table.
93      * @param row Indicates the row of data {@link ValuesBucket} to be inserted into the table.
94      * @param resolution Indicates the {@link ConflictResolution} to insert data into the table.
95      */
96     virtual std::pair<int32_t, int64_t> Insert(
97         const std::string &table, const Row &row, Resolution resolution = NO_ACTION) = 0;
98 
99     /**
100      * @brief Inserts a batch of data into the target table.
101      *
102      * @param table Indicates the target table.
103      * @param rows Indicates the rows of data {@link Rows} to be inserted into the table.
104      */
105     virtual std::pair<int32_t, int64_t> BatchInsert(const std::string &table, const Rows &rows) = 0;
106 
107     /**
108      * @brief Inserts a batch of data into the target table.
109      *
110      * @param table Indicates the target table.
111      * @param rows Indicates the rows of data {@link RefRows} to be inserted into the table.
112      */
113     virtual std::pair<int32_t, int64_t> BatchInsert(const std::string &table, const RefRows &rows) = 0;
114 
115     /**
116      * @brief Inserts a batch of data into the target table.
117      *
118      * @param table Indicates the target table.
119      * @param rows Indicates the rows of data {@link RefRows} to be inserted into the table.
120      * @param resolution Indicates the {@link ConflictResolution} to insert data into the table.
121      */
122     virtual std::pair<int32_t, int64_t> BatchInsert(
123         const std::string &table, const RefRows &rows, Resolution resolution);
124 
125     /**
126      * @brief Inserts a batch of data into the target table.
127      *
128      * @param table Indicates the target table.
129      * @param rows Indicates the rows of data {@link ValuesBucket} to be inserted into the table.
130      * @param returningFields Indicates the returning fields.
131      * @param resolution Indicates the {@link ConflictResolution} to insert data into the table.
132      * @return Return the inserted result. Contains error codes, affected rows,
133      * and returningField values for inserting data
134      * @warning 1. When using returningField, it is not recommended to use the ON_CONFLICT_FAIL strategy. This will
135      * result in returned results that do not match expectations. 2.When the number of affected rows exceeds 1024,
136      * only the first 1024 returningFields will be returned
137      */
138     virtual std::pair<int32_t, Results> BatchInsert(const std::string &table, const RefRows &rows,
139         const std::vector<std::string> &returningFields, Resolution resolution = NO_ACTION);
140     /**
141      * @brief Updates data in the database based on specified conditions.
142      *
143      * @param table Indicates the target table.
144      * @param row Indicates the row of data to be updated in the database.
145      * The key-value pairs are associated with column names of the database table.
146      * @param whereClause Indicates the where clause.
147      * @param args Indicates the where arguments.
148      */
149     virtual std::pair<int, int> Update(const std::string &table, const Row &row, const std::string &where = "",
150         const Values &args = {}, Resolution resolution = NO_ACTION);
151 
152     /**
153      * @brief Updates data in the database based on a a specified instance object of AbsRdbPredicates.
154      *
155      * @param values Indicates the row of data to be updated in the database.
156      * The key-value pairs are associated with column names of the database table.
157      * @param predicates Indicates the specified update condition by the instance object of {@link AbsRdbPredicates}.
158      */
159     virtual std::pair<int32_t, int32_t> Update(
160         const Row &row, const AbsRdbPredicates &predicates, Resolution resolution = NO_ACTION);
161 
162     /**
163      * @brief Updates data in the database based on a a specified instance object of AbsRdbPredicates.
164      *
165      * @param row Indicates the row of data to be updated in the database.
166      * The key-value pairs are associated with column names of the database table.
167      * @param predicates Indicates the specified update condition by the instance object of {@link AbsRdbPredicates}.
168      * @param returningFields Indicates the returning fields.
169      * @param resolution Indicates the {@link ConflictResolution} to insert data into the table.
170      * @return Return the updated result. Contains error code, number of affected rows,
171      * and value of returningField after update
172      * @warning 1. When using returningField, it is not recommended to use the ON_CONFLICT_FAIL strategy. This will
173      * result in returned results that do not match expectations. 2.When the number of affected rows exceeds 1024,
174      * only the first 1024 returningFields will be returned
175      */
176     virtual std::pair<int32_t, Results> Update(const Row &row, const AbsRdbPredicates &predicates,
177         const std::vector<std::string> &returningFields, Resolution resolution = NO_ACTION);
178 
179     /**
180      * @brief Deletes data from the database based on specified conditions.
181      *
182      * @param table Indicates the target table.
183      * @param whereClause Indicates the where clause.
184      * @param args Indicates the where arguments.
185      */
186     virtual std::pair<int32_t, int32_t> Delete(
187         const std::string &table, const std::string &whereClause = "", const Values &args = {});
188 
189     /**
190      * @brief Deletes data from the database based on a specified instance object of AbsRdbPredicates.
191      *
192      * @param predicates Indicates the specified update condition by the instance object of {@link AbsRdbPredicates}.
193      */
194     virtual std::pair<int32_t, int32_t> Delete(const AbsRdbPredicates &predicates);
195 
196     /**
197     * @brief Deletes data from the database based on a specified instance object of AbsRdbPredicates.
198     *
199     * @param predicates Indicates the specified update condition by the instance object of {@link AbsRdbPredicates}.
200     * @param returningFields Indicates the returning fields.
201     * @return Return the deleted result. Contains error code, number of affected rows,
202     * and value of returningField before delete
203     * @warning When the number of affected rows exceeds 1024, only the first 1024 returningFields will be returned.
204     */
205     virtual std::pair<int32_t, Results> Delete(
206         const AbsRdbPredicates &predicates, const std::vector<std::string> &returningFields);
207 
208     /**
209      * @brief Queries data in the database based on SQL statement.
210      *
211      * @param sql Indicates the SQL statement to execute.
212      * @param args Indicates the selection arguments.
213      * @param preCount Indicates whether to calculate the count during query.
214      */
215     virtual std::shared_ptr<ResultSet> QueryByStep(const std::string &sql, const Values &args = {},
216         bool preCount = true) = 0;
217 
218     /**
219      * @brief Queries data in the database based on specified conditions.
220      *
221      * @param predicates Indicates the specified query condition by the instance object of {@link AbsRdbPredicates}.
222      * @param columns Indicates the columns to query. If the value is empty array, the query applies to all columns.
223      * @param preCount Indicates whether to calculate the count during query.
224      */
225     virtual std::shared_ptr<ResultSet> QueryByStep(const AbsRdbPredicates &predicates, const Fields &columns = {},
226         bool preCount = true) = 0;
227 
228     /**
229      * @brief Executes an SQL statement that contains specified parameters and
230      *        get two values of type int and ValueObject.
231      *
232      * @param sql Indicates the SQL statement to execute.
233      * @param args Indicates the {@link ValueObject} values of the parameters in the SQL statement.
234      */
235     virtual std::pair<int32_t, ValueObject> Execute(const std::string &sql, const Values &args = {});
236 
237     /**
238      * @brief Executes an SQL statement that contains specified parameters and
239      *        get two values of type int and ValueObject.
240      *
241      * @param sql Indicates the SQL statement to execute.
242      * @param args Indicates the {@link ValueObject} values of the parameters in the SQL statement.
243      * @warning When the number of affected rows exceeds 1024, only the first 1024 returningFields will be returned.
244      */
245     virtual std::pair<int32_t, Results> ExecuteExt(const std::string &sql, const Values &args = {});
246 
247 private:
248     static inline Creator creator_;
249 };
250 } // namespace OHOS::NativeRdb
251 #endif
252