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