1 /* 2 * Copyright (C) 2022 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_RDBSTORE_H 17 #define OHOS_MEDIALIBRARY_RDBSTORE_H 18 19 #include "medialibrary_unistore.h" 20 #include "medialibrary_sync_operation.h" 21 #include "timer.h" 22 #include "value_object.h" 23 #include <memory> 24 25 namespace OHOS { 26 namespace Media { 27 #ifdef DISTRIBUTED 28 class MediaLibraryRdbStoreObserver; 29 #endif 30 31 class MediaLibraryDataCallBack; 32 33 class MediaLibraryRdbStore final : public MediaLibraryUnistore { 34 public: 35 explicit MediaLibraryRdbStore(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 36 virtual ~MediaLibraryRdbStore(); 37 38 virtual int32_t Init() override; 39 virtual void Stop() override; 40 41 virtual int32_t Insert(MediaLibraryCommand &cmd, int64_t &rowId) override; 42 virtual int32_t Delete(MediaLibraryCommand &cmd, int32_t &deletedRows) override; 43 virtual int32_t Update(MediaLibraryCommand &cmd, int32_t &changedRows) override; 44 std::shared_ptr<NativeRdb::ResultSet> Query(MediaLibraryCommand &cmd, 45 const std::vector<std::string> &columns) override; 46 47 int32_t ExecuteSql(const std::string &sql) override; 48 std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql, 49 const std::vector<std::string> &selectionArgs = std::vector<std::string>()) override; 50 51 int32_t BeginTransaction(); 52 int32_t RollBack(); 53 int32_t Commit(); 54 55 std::shared_ptr<NativeRdb::RdbStore> GetRaw() const; 56 57 static void BuildValuesSql(const NativeRdb::ValuesBucket &values, std::vector<NativeRdb::ValueObject> &bindArgs, 58 std::string &sql); 59 static void BuildQuerySql(const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns, 60 std::vector<NativeRdb::ValueObject> &bindArgs, std::string &sql); 61 static int32_t ExecuteForLastInsertedRowId(const std::string &sql, 62 const std::vector<NativeRdb::ValueObject> &bindArgs); 63 static std::shared_ptr<NativeRdb::ResultSet> Query(const NativeRdb::AbsRdbPredicates &predicates, 64 const std::vector<std::string> &columns); 65 static int32_t Delete(const NativeRdb::AbsRdbPredicates &predicates); 66 static int32_t Update(const NativeRdb::ValuesBucket &values, const NativeRdb::AbsRdbPredicates &predicates); 67 static int32_t DeleteFromDisk(const NativeRdb::AbsRdbPredicates &predicates); 68 static void ReplacePredicatesUriToId(NativeRdb::AbsRdbPredicates &predicates); 69 static void UpdateAPI10Tables(); 70 static std::shared_ptr<NativeRdb::ResultSet> GetIndexOfUri(const NativeRdb::AbsRdbPredicates &predicates, 71 const std::vector<std::string> &columns, const std::string &id); 72 static int32_t GetInt(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, const std::string &column); 73 static std::string GetString(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, const std::string &column); 74 75 private: 76 static const std::string CloudSyncTriggerFunc(const std::vector<std::string> &args); 77 static const std::string IsCallerSelfFunc(const std::vector<std::string> &args); 78 static constexpr int RDB_TRANSACTION_WAIT_MS = 1000; 79 std::mutex transactionMutex_; 80 std::condition_variable transactionCV_; 81 std::atomic<bool> isInTransaction_; 82 static std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 83 #ifdef DISTRIBUTED 84 std::shared_ptr<MediaLibraryRdbStoreObserver> rdbStoreObs_; 85 #endif 86 std::string bundleName_{BUNDLE_NAME}; 87 NativeRdb::RdbStoreConfig config_{""}; 88 }; 89 90 class MediaLibraryDataCallBack : public NativeRdb::RdbOpenCallback { 91 public: 92 struct DirValuesBucket { 93 int32_t directoryType; 94 std::string dirValues; 95 std::string typeValues; 96 std::string extensionValues; 97 }; 98 99 struct SmartAlbumValuesBucket { 100 int32_t albumId; 101 std::string albumName; 102 int32_t albumType; 103 }; 104 105 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 106 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 107 108 private: 109 int32_t PrepareDir(NativeRdb::RdbStore &store); 110 int32_t PrepareSmartAlbum(NativeRdb::RdbStore &store); 111 112 int32_t InsertDirValues(const DirValuesBucket &dirValuesBucket, NativeRdb::RdbStore &store); 113 int32_t InsertSmartAlbumValues(const SmartAlbumValuesBucket &smartAlbum, NativeRdb::RdbStore &store); 114 }; 115 116 #ifdef DISTRIBUTED 117 class MediaLibraryRdbStoreObserver : public NativeRdb::RdbStore::RdbStoreObserver { 118 public: 119 explicit MediaLibraryRdbStoreObserver(const std::string &bundleName); 120 virtual ~MediaLibraryRdbStoreObserver(); 121 void OnChange(const std::vector<std::string> &devices) override; 122 123 private: 124 void NotifyDeviceChange(); 125 static constexpr int NOTIFY_TIME_INTERVAL = 10000; 126 std::unique_ptr<OHOS::Utils::Timer> timer_; 127 uint32_t timerId_{0}; 128 std::string bundleName_; 129 bool isNotifyDeviceChange_; 130 }; 131 #endif 132 133 /** 134 * This class is used for database transaction creation, commit, and rollback 135 * The usage of class is as follows: 136 * 1. initialize TransactionOperations object 137 * (for example: TranscationOperations opt) 138 * 2. After init opt, you need call Start() function to start transaction 139 * int32_t err = opt.Start(); 140 * if err != E_OK, transaction init failed 141 * 3. If you need to commit transaction, then use 142 * int32_t err = opt.Finish(); 143 * if err != E_OK, transation commit failed and auto rollback 144 * 4. If TransactionOperations is destructed without successfully finish, it will be auto rollback 145 */ 146 class TransactionOperations { 147 public: 148 TransactionOperations(); 149 ~TransactionOperations(); 150 int32_t Start(); 151 void Finish(); 152 private: 153 int32_t BeginTransaction(); 154 int32_t TransactionCommit(); 155 int32_t TransactionRollback(); 156 157 std::shared_ptr<MediaLibraryRdbStore> rdbStore_; 158 bool isStart = false; 159 bool isFinish = false; 160 std::mutex mutex_; 161 }; 162 } // namespace Media 163 } // namespace OHOS 164 165 #endif // OHOS_MEDIALIBRARY_RDBSTORE_OPERATIONS_H 166