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 
135 private:
136     EXPORT static std::shared_ptr<NativeRdb::RdbStore> GetRaw();
137     EXPORT static const std::string CloudSyncTriggerFunc(const std::vector<std::string> &args);
138     EXPORT static const std::string IsCallerSelfFunc(const std::vector<std::string> &args);
139     friend class TransactionOperations;
140     static std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
141     EXPORT static const std::string PhotoAlbumNotifyFunc(const std::vector<std::string>& args);
142     static std::mutex reconstructLock_;
143     static std::mutex walCheckPointMutex_;
144 #ifdef DISTRIBUTED
145     std::shared_ptr<MediaLibraryRdbStoreObserver> rdbStoreObs_;
146 #endif
147     std::string bundleName_ {BUNDLE_NAME};
148     NativeRdb::RdbStoreConfig config_ {""};
149 };
150 
151 class CompensateAlbumIdData : public AsyncTaskData {
152 public:
CompensateAlbumIdData(const std::shared_ptr<MediaLibraryRdbStore> store,std::mutex & lock)153     CompensateAlbumIdData(const std::shared_ptr<MediaLibraryRdbStore> store, std::mutex &lock)
154         : upgradeStore_(store), lock_(lock){};
155     virtual ~CompensateAlbumIdData() override = default;
156     std::shared_ptr<MediaLibraryRdbStore> upgradeStore_;
157     std::mutex &lock_;
158 };
159 
160 class MediaLibraryDataCallBack : public NativeRdb::RdbOpenCallback {
161 public:
162     struct DirValuesBucket {
163         int32_t directoryType;
164         std::string dirValues;
165         std::string typeValues;
166         std::string extensionValues;
167     };
168 
169     struct SmartAlbumValuesBucket {
170         int32_t albumId;
171         std::string albumName;
172         int32_t albumType;
173     };
174 
175     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
176     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
177 
178 private:
179     int32_t PrepareDir(NativeRdb::RdbStore &store);
180     int32_t PrepareSmartAlbum(NativeRdb::RdbStore &store);
181 
182     int32_t InsertDirValues(const DirValuesBucket &dirValuesBucket, NativeRdb::RdbStore &store);
183     int32_t InsertSmartAlbumValues(const SmartAlbumValuesBucket &smartAlbum, NativeRdb::RdbStore &store);
184 };
185 
186 class DeleteFilesTask : public AsyncTaskData {
187 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)188     DeleteFilesTask(const std::vector<std::string> &ids, const std::vector<std::string> &paths,
189         const std::vector<std::string> &notifyUris, const std::vector<std::string> &dateTakens,
190         const std::vector<int32_t> &subTypes, const std::string &table, int32_t deleteRows, std::string bundleName)
191         : ids_(ids), paths_(paths), notifyUris_(notifyUris), dateTakens_(dateTakens), subTypes_(subTypes),
192         table_(table), deleteRows_(deleteRows), bundleName_(bundleName) {}
193     virtual ~DeleteFilesTask() override = default;
194 
195     std::vector<std::string> ids_;
196     std::vector<std::string> paths_;
197     std::vector<std::string> notifyUris_;
198     std::vector<std::string> dateTakens_;
199     std::vector<int32_t> subTypes_;
200     std::string table_;
201     int32_t deleteRows_;
202     std::string bundleName_;
203 };
204 
205 #ifdef DISTRIBUTED
206 class MediaLibraryRdbStoreObserver : public NativeRdb::RdbStore::RdbStoreObserver {
207 public:
208     explicit MediaLibraryRdbStoreObserver(const std::string &bundleName);
209     virtual ~MediaLibraryRdbStoreObserver();
210     void OnChange(const std::vector<std::string> &devices) override;
211 
212 private:
213     void NotifyDeviceChange();
214     static constexpr int NOTIFY_TIME_INTERVAL = 10000;
215     std::unique_ptr<OHOS::Utils::Timer> timer_;
216     uint32_t timerId_ {0};
217     std::string bundleName_;
218     bool isNotifyDeviceChange_;
219 };
220 #endif
221 } // namespace Media
222 } // namespace OHOS
223 
224 #endif // OHOS_MEDIALIBRARY_RDBSTORE_OPERATIONS_H
225