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 int32_t UpdateWithDateTime(NativeRdb::ValuesBucket &values, 72 const NativeRdb::AbsRdbPredicates &predicates); 73 static void ReplacePredicatesUriToId(NativeRdb::AbsRdbPredicates &predicates); 74 static std::shared_ptr<NativeRdb::ResultSet> GetIndexOfUri(const NativeRdb::AbsRdbPredicates &predicates, 75 const std::vector<std::string> &columns, const std::string &id); 76 static std::shared_ptr<NativeRdb::ResultSet> GetIndexOfUriForPhotos(const NativeRdb::AbsRdbPredicates &predicates, 77 const std::vector<std::string> &columns, const std::string &id); 78 static int32_t GetInt(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, const std::string &column); 79 static std::string GetString(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, const std::string &column); 80 EXPORT static bool ResetAnalysisTables(); 81 EXPORT static bool ResetSearchTables(); 82 EXPORT static int32_t UpdateLastVisitTime(const std::string &id); 83 EXPORT static bool HasColumnInTable(NativeRdb::RdbStore &store, const std::string &columnName, 84 const std::string &tableName); 85 static void AddColumnIfNotExists(NativeRdb::RdbStore &store, const std::string &columnName, 86 const std::string &columnType, const std::string &tableName); 87 EXPORT static int32_t QueryPragma(const std::string &key, int64_t &value); 88 EXPORT static void SetOldVersion(int32_t oldVersion); 89 EXPORT static int32_t GetOldVersion(); 90 EXPORT static void CreateBurstIndex(const std::shared_ptr<MediaLibraryRdbStore> store); 91 EXPORT static void UpdateBurstDirty(const std::shared_ptr<MediaLibraryRdbStore> store); 92 EXPORT static void UpdateReadyOnThumbnailUpgrade(const std::shared_ptr<MediaLibraryRdbStore> store); 93 EXPORT static void UpdateDateTakenToMillionSecond(const std::shared_ptr<MediaLibraryRdbStore> store); 94 EXPORT static void UpdateDateTakenIndex(const std::shared_ptr<MediaLibraryRdbStore> store); 95 EXPORT static void ClearAudios(const std::shared_ptr<MediaLibraryRdbStore> store); 96 EXPORT static void UpdateIndexForCover(const std::shared_ptr<MediaLibraryRdbStore> store); 97 EXPORT static int32_t ReconstructMediaLibraryStorageFormat(const std::shared_ptr<MediaLibraryRdbStore> store); 98 EXPORT int Update(int &changedRows, const std::string &table, const NativeRdb::ValuesBucket &row, 99 const std::string &whereClause, const std::vector<std::string> &args); 100 EXPORT std::string ObtainDistributedTableName(const std::string &device, const std::string &table, int &errCode); 101 EXPORT int Backup(const std::string &databasePath, const std::vector<uint8_t> &encryptKey = {}); 102 EXPORT int Sync(const DistributedRdb::SyncOption &option, const NativeRdb::AbsRdbPredicates &predicate, 103 const DistributedRdb::AsyncBrief &async); 104 EXPORT std::shared_ptr<NativeRdb::ResultSet> QueryByStep(const std::string &sql, 105 const std::vector<NativeRdb::ValueObject> &args = {}); 106 EXPORT std::shared_ptr<NativeRdb::ResultSet> QueryByStep(const NativeRdb::AbsRdbPredicates &predicates, 107 const std::vector<std::string> &columns = {}); 108 EXPORT int Update(int &changedRows, const NativeRdb::ValuesBucket &row, 109 const NativeRdb::AbsRdbPredicates &predicates); 110 EXPORT int Insert(int64_t &outRowId, const std::string &table, const NativeRdb::ValuesBucket &row); 111 EXPORT int Delete(int &deletedRows, const std::string &table, const std::string &whereClause, 112 const std::vector<std::string> &args); 113 EXPORT int Delete(int &deletedRows, const NativeRdb::AbsRdbPredicates &predicates); 114 EXPORT std::shared_ptr<NativeRdb::AbsSharedResultSet> QuerySql(const std::string &sql, 115 const std::vector<NativeRdb::ValueObject> &args); 116 EXPORT int InterruptBackup(); 117 EXPORT bool IsSlaveDiffFromMaster() const; 118 EXPORT int Restore(const std::string &backupPath, const std::vector<uint8_t> &newKey = {}); 119 static int32_t DoDeleteFromPredicates(const NativeRdb::AbsRdbPredicates &predicates, int32_t &deletedRows); 120 int32_t DataCallBackOnCreate(); 121 122 private: 123 EXPORT static std::shared_ptr<NativeRdb::RdbStore> GetRaw(); 124 EXPORT static const std::string CloudSyncTriggerFunc(const std::vector<std::string> &args); 125 EXPORT static const std::string IsCallerSelfFunc(const std::vector<std::string> &args); 126 friend class TransactionOperations; 127 static std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 128 static std::mutex reconstructLock_; 129 static std::mutex rdbStoreMutex_; 130 #ifdef DISTRIBUTED 131 std::shared_ptr<MediaLibraryRdbStoreObserver> rdbStoreObs_; 132 #endif 133 std::string bundleName_ {BUNDLE_NAME}; 134 NativeRdb::RdbStoreConfig config_ {""}; 135 }; 136 137 class CompensateAlbumIdData : public AsyncTaskData { 138 public: CompensateAlbumIdData(const std::shared_ptr<MediaLibraryRdbStore> store,std::mutex & lock)139 CompensateAlbumIdData(const std::shared_ptr<MediaLibraryRdbStore> store, std::mutex &lock) 140 : upgradeStore_(store), lock_(lock){}; 141 virtual ~CompensateAlbumIdData() override = default; 142 std::shared_ptr<MediaLibraryRdbStore> upgradeStore_; 143 std::mutex &lock_; 144 }; 145 146 class MediaLibraryDataCallBack : public NativeRdb::RdbOpenCallback { 147 public: 148 struct DirValuesBucket { 149 int32_t directoryType; 150 std::string dirValues; 151 std::string typeValues; 152 std::string extensionValues; 153 }; 154 155 struct SmartAlbumValuesBucket { 156 int32_t albumId; 157 std::string albumName; 158 int32_t albumType; 159 }; 160 161 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 162 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 163 164 private: 165 int32_t PrepareDir(NativeRdb::RdbStore &store); 166 int32_t PrepareSmartAlbum(NativeRdb::RdbStore &store); 167 168 int32_t InsertDirValues(const DirValuesBucket &dirValuesBucket, NativeRdb::RdbStore &store); 169 int32_t InsertSmartAlbumValues(const SmartAlbumValuesBucket &smartAlbum, NativeRdb::RdbStore &store); 170 }; 171 172 class DeleteFilesTask : public AsyncTaskData { 173 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)174 DeleteFilesTask(const std::vector<std::string> &ids, const std::vector<std::string> &paths, 175 const std::vector<std::string> ¬ifyUris, const std::vector<std::string> &dateTakens, 176 const std::vector<int32_t> &subTypes, const std::string &table, int32_t deleteRows, std::string bundleName) 177 : ids_(ids), paths_(paths), notifyUris_(notifyUris), dateTakens_(dateTakens), subTypes_(subTypes), 178 table_(table), deleteRows_(deleteRows), bundleName_(bundleName) {} 179 virtual ~DeleteFilesTask() override = default; 180 181 std::vector<std::string> ids_; 182 std::vector<std::string> paths_; 183 std::vector<std::string> notifyUris_; 184 std::vector<std::string> dateTakens_; 185 std::vector<int32_t> subTypes_; 186 std::string table_; 187 int32_t deleteRows_; 188 std::string bundleName_; 189 }; 190 191 #ifdef DISTRIBUTED 192 class MediaLibraryRdbStoreObserver : public NativeRdb::RdbStore::RdbStoreObserver { 193 public: 194 explicit MediaLibraryRdbStoreObserver(const std::string &bundleName); 195 virtual ~MediaLibraryRdbStoreObserver(); 196 void OnChange(const std::vector<std::string> &devices) override; 197 198 private: 199 void NotifyDeviceChange(); 200 static constexpr int NOTIFY_TIME_INTERVAL = 10000; 201 std::unique_ptr<OHOS::Utils::Timer> timer_; 202 uint32_t timerId_ {0}; 203 std::string bundleName_; 204 bool isNotifyDeviceChange_; 205 }; 206 #endif 207 } // namespace Media 208 } // namespace OHOS 209 210 #endif // OHOS_MEDIALIBRARY_RDBSTORE_OPERATIONS_H 211