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 <mutex> 19 #include <memory> 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<int, int> Update(const std::string &table, const Row &row, const std::string &where, 40 const Values &args, Resolution resolution) override; 41 std::pair<int32_t, int32_t> Update(const Row &row, const AbsRdbPredicates &predicates, 42 Resolution resolution) override; 43 std::pair<int32_t, int32_t> Delete(const std::string &table, const std::string &whereClause, 44 const Values &args) override; 45 std::pair<int32_t, int32_t> Delete(const AbsRdbPredicates &predicates) override; 46 std::shared_ptr<ResultSet> QueryByStep(const std::string &sql, const Values &args) override; 47 std::shared_ptr<ResultSet> QueryByStep(const AbsRdbPredicates &predicates, const Fields &columns) override; 48 std::pair<int32_t, ValueObject> Execute(const std::string &sql, const Values &args) override; 49 50 static std::pair<int32_t, std::shared_ptr<Transaction>> Create( 51 int32_t type, std::shared_ptr<Connection> connection, const std::string &name); 52 53 private: 54 static std::string GetBeginSql(int32_t type); 55 int32_t Begin(int32_t type); 56 int32_t CloseInner(); 57 std::shared_ptr<RdbStore> GetStore(); 58 void AddResultSet(std::weak_ptr<ResultSet> resultSet); 59 60 std::string name_; 61 std::recursive_mutex mutex_; 62 std::shared_ptr<RdbStore> store_; 63 std::shared_ptr<Connection> connection_; 64 std::vector<std::weak_ptr<ResultSet>> resultSets_; 65 66 static const int32_t regCreator_; 67 static constexpr char COMMIT_SQL[] = "COMMIT;"; 68 static constexpr char ROLLBACK_SQL[] = "ROLLBACK;"; 69 static constexpr const char *BEGIN_SQLS[] = { "BEGIN DEFERRED;", "BEGIN IMMEDIATE;", "BEGIN EXCLUSIVE;" }; 70 }; 71 } 72 #endif 73