• 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 &path);
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, Results> BatchInsert(const std::string &table, const RefRows &rows,
40         const std::vector<std::string> &returningFields, Resolution resolution) override;
41     std::pair<int32_t, Results> Update(const Row &row, const AbsRdbPredicates &predicates,
42         const std::vector<std::string> &returningFields, Resolution resolution) override;
43     std::pair<int32_t, Results> Delete(
44         const AbsRdbPredicates &predicates, const std::vector<std::string> &returningFields) override;
45     std::shared_ptr<ResultSet> QueryByStep(const std::string &sql, const Values &args, bool preCount) override;
46     std::shared_ptr<ResultSet> QueryByStep(const AbsRdbPredicates &predicates, const Fields &columns,
47         bool preCount) override;
48     std::pair<int32_t, ValueObject> Execute(const std::string &sql, const Values &args) override;
49     std::pair<int32_t, Results> ExecuteExt(const std::string &sql, const Values &args) override;
50     static std::pair<int32_t, std::shared_ptr<Transaction>> Create(
51         int32_t type, std::shared_ptr<Connection> connection, const std::string &path);
52 
53 private:
54     static std::string GetBeginSql(int32_t type);
55     int32_t Begin(int32_t type);
56     int32_t CloseInner(bool connRecycle = true);
57     std::shared_ptr<RdbStore> GetStore();
58     void AddResultSet(std::weak_ptr<ResultSet> resultSet);
59 
60     std::string path_;
61     uint32_t seqId_ = 0;
62     std::recursive_mutex mutex_;
63     std::shared_ptr<RdbStore> store_;
64     std::shared_ptr<Connection> connection_;
65     std::vector<std::weak_ptr<ResultSet>> resultSets_;
66 
67     static const int32_t regCreator_;
68     static constexpr char COMMIT_SQL[] = "COMMIT;";
69     static constexpr char ROLLBACK_SQL[] = "ROLLBACK;";
70     static constexpr const char *BEGIN_SQLS[] = { "BEGIN DEFERRED;", "BEGIN IMMEDIATE;", "BEGIN EXCLUSIVE;" };
71 };
72 } // namespace OHOS::NativeRdb
73 #endif
74