/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef NATIVE_RDB_TRANSACTION_IMPL_H #define NATIVE_RDB_TRANSACTION_IMPL_H #include #include #include #include "connection.h" #include "transaction.h" namespace OHOS::NativeRdb { class RdbStore; class TransactionImpl : public Transaction { public: TransactionImpl(std::shared_ptr connection, const std::string &name); ~TransactionImpl() override; int32_t Commit() override; int32_t Rollback() override; int32_t Close() override; std::pair Insert(const std::string &table, const Row &row, Resolution resolution) override; std::pair BatchInsert(const std::string &table, const Rows &rows) override; std::pair BatchInsert(const std::string &table, const RefRows &rows) override; std::pair Update(const std::string &table, const Row &row, const std::string &where, const Values &args, Resolution resolution) override; std::pair Update(const Row &row, const AbsRdbPredicates &predicates, Resolution resolution) override; std::pair Delete(const std::string &table, const std::string &whereClause, const Values &args) override; std::pair Delete(const AbsRdbPredicates &predicates) override; std::shared_ptr QueryByStep(const std::string &sql, const Values &args) override; std::shared_ptr QueryByStep(const AbsRdbPredicates &predicates, const Fields &columns) override; std::pair Execute(const std::string &sql, const Values &args) override; static std::pair> Create( int32_t type, std::shared_ptr connection, const std::string &name); private: static std::string GetBeginSql(int32_t type); int32_t Begin(int32_t type); int32_t CloseInner(); std::shared_ptr GetStore(); void AddResultSet(std::weak_ptr resultSet); std::string name_; std::recursive_mutex mutex_; std::shared_ptr store_; std::shared_ptr connection_; std::vector> resultSets_; static const int32_t regCreator_; static constexpr char COMMIT_SQL[] = "COMMIT;"; static constexpr char ROLLBACK_SQL[] = "ROLLBACK;"; static constexpr const char *BEGIN_SQLS[] = { "BEGIN DEFERRED;", "BEGIN IMMEDIATE;", "BEGIN EXCLUSIVE;" }; }; } #endif