1 /* 2 * Copyright (C) 2023 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 16 #ifndef OHOS_MEDIALIBRARY_RDB_TRANSACTION_H 17 #define OHOS_MEDIALIBRARY_RDB_TRANSACTION_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <cstdint> 22 #include <mutex> 23 #include "medialibrary_async_worker.h" 24 #include "medialibrary_unistore.h" 25 #include "timer.h" 26 #include "value_object.h" 27 #include "transaction.h" 28 29 namespace OHOS::Media { 30 constexpr int32_t MAX_TRY_TIMES = 30; 31 constexpr int32_t MAX_BUSY_TRY_TIMES = 2; 32 constexpr int32_t TRANSACTION_WAIT_INTERVAL = 50; // in milliseconds. 33 34 #define EXPORT __attribute__ ((visibility ("default"))) 35 /** 36 * This class is used for database transaction creation, commit, and rollback 37 * The usage of class is as follows: 38 * 1. initialize TransactionOperations object with a rdb instance 39 * (for example: TranscationOperations opt(rdb)) 40 * 2. After init opt, you need call Start() function to start transaction 41 * int32_t err = opt.Start(); 42 * if err != E_OK, transaction init failed 43 * 3. If you need to commit transaction, then use 44 * int32_t err = opt.Finish(); 45 * if err != E_OK, transaction commit failed and auto rollback 46 * 4. If TransactionOperations is destructed without successfully finish, it will be auto rollback 47 */ 48 class TransactionOperations { 49 public: 50 EXPORT TransactionOperations(); 51 EXPORT ~TransactionOperations(); 52 EXPORT int32_t Start(std::string funcName, bool isBackup = false); 53 EXPORT int32_t Finish(); 54 EXPORT int32_t TryTrans(std::function<int(void)> &func, std::string funcName, bool isBackup); 55 EXPORT int32_t RetryTrans(std::function<int(void)> &func, std::string funcName, bool isBackup = false); 56 EXPORT int32_t Rollback(); 57 EXPORT void SetBackupRdbStore(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore); 58 59 EXPORT int32_t ExecuteSql(const std::string &sql, const std::vector<NativeRdb::ValueObject> &args = {}); 60 EXPORT int32_t Execute(const std::string &sql, const std::vector<NativeRdb::ValueObject> &args = {}); 61 EXPORT int32_t ExecuteForLastInsertedRowId(const std::string &sql, 62 const std::vector<NativeRdb::ValueObject> &bindArgs); 63 EXPORT int32_t Insert(MediaLibraryCommand &cmd, int64_t &rowId); 64 EXPORT int32_t Insert(int64_t &rowId, const std::string tableName, const NativeRdb::ValuesBucket &values); 65 EXPORT int32_t BatchInsert(int64_t &outRowId, const std::string &table, 66 const std::vector<NativeRdb::ValuesBucket> &values); 67 EXPORT int32_t BatchInsert(MediaLibraryCommand &cmd, int64_t &outInsertNum, 68 const std::vector<NativeRdb::ValuesBucket> &values); 69 EXPORT int32_t Update(NativeRdb::ValuesBucket &values, const NativeRdb::AbsRdbPredicates &predicates); 70 EXPORT int32_t Update(int32_t &changedRows, NativeRdb::ValuesBucket &values, 71 const NativeRdb::AbsRdbPredicates &predicates); 72 EXPORT int32_t Update(MediaLibraryCommand &cmd, int32_t &changedRows); 73 EXPORT int32_t Delete(MediaLibraryCommand &cmd, int32_t &deletedRows); 74 75 private: 76 int32_t TransactionCommit(); 77 78 std::shared_ptr<OHOS::NativeRdb::Transaction> transaction_ = nullptr; 79 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore_; 80 std::shared_ptr<OHOS::NativeRdb::RdbStore> backupRdbStore_; 81 std::string funcName_ = ""; 82 bool isSkipCloudSync_ = false; 83 }; 84 } // namespace OHOS::Media 85 86 #endif // OHOS_MEDIALIBRARY_RDB_TRANSACTION_H 87