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, bool preCount) override; 47 std::shared_ptr<ResultSet> QueryByStep(const AbsRdbPredicates &predicates, const Fields &columns, 48 bool preCount) override; 49 std::pair<int32_t, ValueObject> Execute(const std::string &sql, const Values &args) override; 50 51 static std::pair<int32_t, std::shared_ptr<Transaction>> Create( 52 int32_t type, std::shared_ptr<Connection> connection, const std::string &name); 53 54 private: 55 static std::string GetBeginSql(int32_t type); 56 int32_t Begin(int32_t type); 57 int32_t CloseInner(); 58 std::shared_ptr<RdbStore> GetStore(); 59 void AddResultSet(std::weak_ptr<ResultSet> resultSet); 60 61 std::string name_; 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 } 73 #endif 74