• 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 #ifndef NATIVE_RDB_TRANSACTION_IMPL_H
16 #define NATIVE_RDB_TRANSACTION_IMPL_H
17 
18 #include <memory>
19 #include <mutex>
20 #include <vector>
21 
22 #include "connection.h"
23 #include "transaction.h"
24 
25 namespace OHOS::NativeRdb {
26 class RdbStore;
27 class TransactionImpl : public Transaction {
28 public:
29     TransactionImpl(std::shared_ptr<Connection> connection, const std::string &name);
30     ~TransactionImpl() override;
31 
32     int32_t Commit() override;
33     int32_t Rollback() override;
34     int32_t Close() override;
35 
36     std::pair<int32_t, int64_t> Insert(const std::string &table, const Row &row, Resolution resolution) override;
37     std::pair<int32_t, int64_t> BatchInsert(const std::string &table, const Rows &rows) override;
38     std::pair<int32_t, int64_t> BatchInsert(const std::string &table, const RefRows &rows) override;
39     std::pair<int32_t, int64_t> BatchInsertWithConflictResolution(
40         const std::string &table, const RefRows &rows, Resolution resolution) override;
41     std::pair<int, int> Update(const std::string &table, const Row &row, const std::string &where, const Values &args,
42         Resolution resolution) override;
43     std::pair<int32_t, int32_t> Update(
44         const Row &row, const AbsRdbPredicates &predicates, Resolution resolution) override;
45     std::pair<int32_t, int32_t> Delete(
46         const std::string &table, const std::string &whereClause, const Values &args) override;
47     std::pair<int32_t, int32_t> Delete(const AbsRdbPredicates &predicates) override;
48     std::shared_ptr<ResultSet> QueryByStep(const std::string &sql, const Values &args, bool preCount) override;
49     std::shared_ptr<ResultSet> QueryByStep(const AbsRdbPredicates &predicates, const Fields &columns,
50         bool preCount) override;
51     std::pair<int32_t, ValueObject> Execute(const std::string &sql, const Values &args) override;
52 
53     static std::pair<int32_t, std::shared_ptr<Transaction>> Create(
54         int32_t type, std::shared_ptr<Connection> connection, const std::string &name);
55 
56 private:
57     static std::string GetBeginSql(int32_t type);
58     int32_t Begin(int32_t type);
59     int32_t CloseInner(bool connRecycle = true);
60     std::shared_ptr<RdbStore> GetStore();
61     void AddResultSet(std::weak_ptr<ResultSet> resultSet);
62 
63     std::string name_;
64     std::recursive_mutex mutex_;
65     std::shared_ptr<RdbStore> store_;
66     std::shared_ptr<Connection> connection_;
67     std::vector<std::weak_ptr<ResultSet>> resultSets_;
68 
69     static const int32_t regCreator_;
70     static constexpr char COMMIT_SQL[] = "COMMIT;";
71     static constexpr char ROLLBACK_SQL[] = "ROLLBACK;";
72     static constexpr const char *BEGIN_SQLS[] = { "BEGIN DEFERRED;", "BEGIN IMMEDIATE;", "BEGIN EXCLUSIVE;" };
73 };
74 } // namespace OHOS::NativeRdb
75 #endif
76