1 /* 2 * Copyright (c) 2021 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 DOWNLOAD_DATABASE_H 17 #define DOWNLOAD_DATABASE_H 18 19 #include <pthread.h> 20 21 #include "data_ability_predicates.h" 22 #include "rdb_errno.h" 23 #include "rdb_helper.h" 24 #include "rdb_open_callback.h" 25 #include "rdb_predicates.h" 26 #include "rdb_store.h" 27 #include "result_set.h" 28 #include "value_object.h" 29 30 namespace OHOS::Request::Download { 31 constexpr const char *DB_NAME = "download.db"; 32 constexpr const char *TABLE_NAME = "downloadInfo"; 33 constexpr int DATABASE_OPEN_VERSION = 1; 34 constexpr int DATABASE_NEW_VERSION = 2; 35 36 constexpr const char *CREATE_DOWNLOAD = 37 "CREATE TABLE IF NOT EXISTS [downloadInfo](" 38 "[taskid] INTEGER PRIMARY KEY AUTOINCREMENT, " 39 "[url] TEXT, " 40 "[description] TEXT, " 41 "[title] TEXT, " 42 "[filePath] TEXT, " 43 "[header] TEXT," 44 "[metered] INTEGER, " 45 "[roaming] INTEGER, " 46 "[network] INTEGER, " 47 "[status] INTEGER, " 48 "[reason] INTEGER, " 49 "[size] INTEGER, " 50 "[mime] TEXT )"; 51 52 class DownloadDataBase { 53 public: 54 static std::shared_ptr<DownloadDataBase> GetInstance(); 55 static std::shared_ptr<OHOS::NativeRdb::RdbStore> store_; 56 int64_t Insert(OHOS::NativeRdb::ValuesBucket insertValues); 57 int Update(OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &rdbPredicates); 58 int Delete(OHOS::NativeRdb::RdbPredicates &rdbPredicates); 59 std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> Query( 60 OHOS::NativeRdb::RdbPredicates &rdbPredicates, std::vector<std::string> columns); 61 int BeginTransaction(); 62 int Commit(); 63 int RollBack(); 64 65 private: 66 DownloadDataBase(); 67 DownloadDataBase(const DownloadDataBase &); 68 const DownloadDataBase &operator=(const DownloadDataBase &); 69 70 private: 71 static std::shared_ptr<DownloadDataBase> instance_; 72 }; 73 74 class SqliteOpenHelperDownloadCallback : public OHOS::NativeRdb::RdbOpenCallback { 75 public: 76 int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; 77 int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override; 78 int OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override; 79 }; 80 } // namespace OHOS::Request::Download 81 #endif // DOWNLOAD_DATABASE_H