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 <memory> 20 #include <mutex> 21 22 #include "medialibrary_async_worker.h" 23 #include "medialibrary_unistore.h" 24 #include "timer.h" 25 #include "value_object.h" 26 #include "medialibrary_rdb_transaction.h" 27 28 namespace OHOS { 29 namespace Media { 30 #define EXPORT __attribute__ ((visibility ("default"))) 31 #ifdef DISTRIBUTED 32 class MediaLibraryRdbStoreObserver; 33 #endif 34 35 class MediaLibraryDataCallBack; 36 37 class MediaLibraryRdbStore final : public MediaLibraryUnistore { 38 public: 39 EXPORT explicit MediaLibraryRdbStore(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 40 EXPORT virtual ~MediaLibraryRdbStore(); 41 42 EXPORT virtual int32_t Init() override; 43 EXPORT int32_t Init(const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback); 44 EXPORT virtual void Stop() override; 45 EXPORT static bool CheckRdbStore(); 46 EXPORT virtual int32_t Insert(MediaLibraryCommand &cmd, int64_t &rowId) override; 47 EXPORT virtual int32_t BatchInsert(MediaLibraryCommand &cmd, int64_t& outInsertNum, 48 const std::vector<NativeRdb::ValuesBucket>& values) override; 49 EXPORT virtual int32_t Delete(MediaLibraryCommand &cmd, int32_t &deletedRows) override; 50 EXPORT virtual int32_t Update(MediaLibraryCommand &cmd, int32_t &changedRows) override; 51 EXPORT std::shared_ptr<NativeRdb::ResultSet> Query(MediaLibraryCommand &cmd, 52 const std::vector<std::string> &columns) override; 53 EXPORT static std::shared_ptr<NativeRdb::ResultSet> Query(const NativeRdb::AbsRdbPredicates &predicates, 54 const std::vector<std::string> &columns); 55 56 EXPORT int32_t ExecuteSql(const std::string &sql) override; 57 EXPORT std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql, 58 const std::vector<std::string> &selectionArgs = std::vector<std::string>()) override; 59 60 EXPORT static int32_t BatchInsert(int64_t &outRowId, const std::string &table, 61 const std::vector<NativeRdb::ValuesBucket> &values); 62 EXPORT static void BuildValuesSql(const NativeRdb::ValuesBucket &values, 63 std::vector<NativeRdb::ValueObject> &bindArgs, std::string &sql); 64 EXPORT static void BuildQuerySql(const NativeRdb::AbsRdbPredicates &predicates, 65 const std::vector<std::string> &columns, std::vector<NativeRdb::ValueObject> &bindArgs, std::string &sql); 66 EXPORT static int32_t ExecuteForLastInsertedRowId(const std::string &sql, 67 const std::vector<NativeRdb::ValueObject> &bindArgs); 68 EXPORT static std::shared_ptr<NativeRdb::ResultSet> QueryWithFilter(const NativeRdb::AbsRdbPredicates &predicates, 69 const std::vector<std::string> &columns); 70 EXPORT static int32_t Delete(const NativeRdb::AbsRdbPredicates &predicates); 71 EXPORT static std::shared_ptr<NativeRdb::ResultSet> StepQueryWithoutCheck( 72 const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns); 73 EXPORT static int32_t UpdateWithDateTime(NativeRdb::ValuesBucket &values, 74 const NativeRdb::AbsRdbPredicates &predicates); 75 static void ReplacePredicatesUriToId(NativeRdb::AbsRdbPredicates &predicates); 76 static std::shared_ptr<NativeRdb::ResultSet> GetIndexOfUri(const NativeRdb::AbsRdbPredicates &predicates, 77 const std::vector<std::string> &columns, const std::string &id); 78 static std::shared_ptr<NativeRdb::ResultSet> GetIndexOfUriForPhotos(const NativeRdb::AbsRdbPredicates &predicates, 79 const std::vector<std::string> &columns, const std::string &id); 80 static int32_t GetInt(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, const std::string &column); 81 static std::string GetString(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, const std::string &column); 82 EXPORT static bool ResetAnalysisTables(); 83 EXPORT static bool ResetSearchTables(); 84 EXPORT static int32_t UpdateLastVisitTime(const std::string &id); 85 EXPORT static bool HasColumnInTable(NativeRdb::RdbStore &store, const std::string &columnName, 86 const std::string &tableName); 87 static void AddColumnIfNotExists(NativeRdb::RdbStore &store, const std::string &columnName, 88 const std::string &columnType, const std::string &tableName); 89 EXPORT static int32_t QueryPragma(const std::string &key, int64_t &value); 90 EXPORT static void SetOldVersion(int32_t oldVersion); 91 EXPORT static int32_t GetOldVersion(); 92 EXPORT static void CreateBurstIndex(const std::shared_ptr<MediaLibraryRdbStore> store); 93 EXPORT static void UpdateBurstDirty(const std::shared_ptr<MediaLibraryRdbStore> store); 94 EXPORT static void UpdateReadyOnThumbnailUpgrade(const std::shared_ptr<MediaLibraryRdbStore> store); 95 EXPORT static void UpdateDateTakenToMillionSecond(const std::shared_ptr<MediaLibraryRdbStore> store); 96 EXPORT static void UpdateDateTakenIndex(const std::shared_ptr<MediaLibraryRdbStore> store); 97 EXPORT static void UpdateDateTakenAndDetalTime(const std::shared_ptr<MediaLibraryRdbStore> store); 98 EXPORT static void ClearAudios(const std::shared_ptr<MediaLibraryRdbStore> store); 99 EXPORT static void UpdateIndexForCover(const std::shared_ptr<MediaLibraryRdbStore> store); 100 EXPORT static void UpdateLcdStatusNotUploaded(const std::shared_ptr<MediaLibraryRdbStore> store); 101 EXPORT static void AddReadyCountIndex(const std::shared_ptr<MediaLibraryRdbStore> store); 102 EXPORT static void RevertFixDateAddedIndex(const std::shared_ptr<MediaLibraryRdbStore> store); 103 EXPORT static int32_t ReconstructMediaLibraryStorageFormat(const std::shared_ptr<MediaLibraryRdbStore> store); 104 EXPORT int Update(int &changedRows, const std::string &table, const NativeRdb::ValuesBucket &row, 105 const std::string &whereClause, const std::vector<std::string> &args); 106 EXPORT std::string ObtainDistributedTableName(const std::string &device, const std::string &table, int &errCode); 107 EXPORT int Backup(const std::string &databasePath, const std::vector<uint8_t> &encryptKey = {}); 108 EXPORT int Sync(const DistributedRdb::SyncOption &option, const NativeRdb::AbsRdbPredicates &predicate, 109 const DistributedRdb::AsyncBrief &async); 110 EXPORT std::shared_ptr<NativeRdb::ResultSet> QueryByStep(const std::string &sql, 111 const std::vector<NativeRdb::ValueObject> &args = {}); 112 EXPORT std::shared_ptr<NativeRdb::ResultSet> QueryByStep(const NativeRdb::AbsRdbPredicates &predicates, 113 const std::vector<std::string> &columns = {}); 114 EXPORT int Update(int &changedRows, const NativeRdb::ValuesBucket &row, 115 const NativeRdb::AbsRdbPredicates &predicates); 116 EXPORT int Insert(int64_t &outRowId, const std::string &table, const NativeRdb::ValuesBucket &row); 117 EXPORT int Delete(int &deletedRows, const std::string &table, const std::string &whereClause, 118 const std::vector<std::string> &args); 119 EXPORT int Delete(int &deletedRows, const NativeRdb::AbsRdbPredicates &predicates); 120 EXPORT std::shared_ptr<NativeRdb::AbsSharedResultSet> QuerySql(const std::string &sql, 121 const std::vector<NativeRdb::ValueObject> &args); 122 EXPORT int InterruptBackup(); 123 EXPORT bool IsSlaveDiffFromMaster() const; 124 EXPORT int Restore(const std::string &backupPath, const std::vector<uint8_t> &newKey = {}); 125 static int32_t DoDeleteFromPredicates(const NativeRdb::AbsRdbPredicates &predicates, int32_t &deletedRows); 126 int32_t DataCallBackOnCreate(); 127 static void WalCheckPoint(); ExecuteSql(std::string & sql,const std::vector<NativeRdb::ValueObject> & args)128 EXPORT int32_t ExecuteSql(std::string &sql, const std::vector<NativeRdb::ValueObject> &args) 129 { 130 return MediaLibraryRdbStore::GetRaw()->ExecuteSql(sql, args); 131 } 132 EXPORT int ExecuteForChangedRowCount(int64_t &outValue, const std::string &sql, 133 const std::vector<NativeRdb::ValueObject> &args = {}); 134 static void UpdateMediaTypeAndThumbnailReadyIdx(const std::shared_ptr<MediaLibraryRdbStore> rdbStore); 135 136 private: 137 EXPORT static std::shared_ptr<NativeRdb::RdbStore> GetRaw(); 138 EXPORT static const std::string CloudSyncTriggerFunc(const std::vector<std::string> &args); 139 EXPORT static const std::string IsCallerSelfFunc(const std::vector<std::string> &args); 140 friend class TransactionOperations; 141 static std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 142 EXPORT static const std::string PhotoAlbumNotifyFunc(const std::vector<std::string>& args); 143 static std::mutex reconstructLock_; 144 static std::mutex walCheckPointMutex_; 145 #ifdef DISTRIBUTED 146 std::shared_ptr<MediaLibraryRdbStoreObserver> rdbStoreObs_; 147 #endif 148 std::string bundleName_ {BUNDLE_NAME}; 149 NativeRdb::RdbStoreConfig config_ {""}; 150 }; 151 152 class CompensateAlbumIdData : public AsyncTaskData { 153 public: CompensateAlbumIdData(const std::shared_ptr<MediaLibraryRdbStore> store,std::mutex & lock)154 CompensateAlbumIdData(const std::shared_ptr<MediaLibraryRdbStore> store, std::mutex &lock) 155 : upgradeStore_(store), lock_(lock){}; 156 virtual ~CompensateAlbumIdData() override = default; 157 std::shared_ptr<MediaLibraryRdbStore> upgradeStore_; 158 std::mutex &lock_; 159 }; 160 161 class MediaLibraryDataCallBack : public NativeRdb::RdbOpenCallback { 162 public: 163 struct DirValuesBucket { 164 int32_t directoryType; 165 std::string dirValues; 166 std::string typeValues; 167 std::string extensionValues; 168 }; 169 170 struct SmartAlbumValuesBucket { 171 int32_t albumId; 172 std::string albumName; 173 int32_t albumType; 174 }; 175 176 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 177 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 178 179 private: 180 int32_t PrepareDir(NativeRdb::RdbStore &store); 181 int32_t PrepareSmartAlbum(NativeRdb::RdbStore &store); 182 183 int32_t InsertDirValues(const DirValuesBucket &dirValuesBucket, NativeRdb::RdbStore &store); 184 int32_t InsertSmartAlbumValues(const SmartAlbumValuesBucket &smartAlbum, NativeRdb::RdbStore &store); 185 }; 186 187 class DeleteFilesTask : public AsyncTaskData { 188 public: DeleteFilesTask(const std::vector<std::string> & ids,const std::vector<std::string> & paths,const std::vector<std::string> & notifyUris,const std::vector<std::string> & dateTakens,const std::vector<int32_t> & subTypes,const std::string & table,int32_t deleteRows,std::string bundleName)189 DeleteFilesTask(const std::vector<std::string> &ids, const std::vector<std::string> &paths, 190 const std::vector<std::string> ¬ifyUris, const std::vector<std::string> &dateTakens, 191 const std::vector<int32_t> &subTypes, const std::string &table, int32_t deleteRows, std::string bundleName) 192 : ids_(ids), paths_(paths), notifyUris_(notifyUris), dateTakens_(dateTakens), subTypes_(subTypes), 193 table_(table), deleteRows_(deleteRows), bundleName_(bundleName) {} 194 virtual ~DeleteFilesTask() override = default; 195 196 std::vector<std::string> ids_; 197 std::vector<std::string> paths_; 198 std::vector<std::string> notifyUris_; 199 std::vector<std::string> dateTakens_; 200 std::vector<int32_t> subTypes_; 201 std::string table_; 202 int32_t deleteRows_; 203 std::string bundleName_; 204 }; 205 206 #ifdef DISTRIBUTED 207 class MediaLibraryRdbStoreObserver : public NativeRdb::RdbStore::RdbStoreObserver { 208 public: 209 explicit MediaLibraryRdbStoreObserver(const std::string &bundleName); 210 virtual ~MediaLibraryRdbStoreObserver(); 211 void OnChange(const std::vector<std::string> &devices) override; 212 213 private: 214 void NotifyDeviceChange(); 215 static constexpr int NOTIFY_TIME_INTERVAL = 10000; 216 std::unique_ptr<OHOS::Utils::Timer> timer_; 217 uint32_t timerId_ {0}; 218 std::string bundleName_; 219 bool isNotifyDeviceChange_; 220 }; 221 #endif 222 } // namespace Media 223 } // namespace OHOS 224 225 #endif // OHOS_MEDIALIBRARY_RDBSTORE_OPERATIONS_H 226