• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "download_database.h"
17 
18 #include "constant.h"
19 #include "db_path.h"
20 #include "log.h"
21 
22 namespace OHOS::Request::Download {
23 std::shared_ptr<DownloadDataBase> DownloadDataBase::instance_ = nullptr;
24 std::shared_ptr<OHOS::NativeRdb::RdbStore> DownloadDataBase::store_ = nullptr;
25 static std::string g_databaseName;
26 
DownloadDataBase()27 DownloadDataBase::DownloadDataBase()
28 {
29     g_databaseName = DBPath::RDB_PATH + DB_NAME;
30     DOWNLOAD_HILOGI("DownloadDataBase g_databaseName :%{public}s", g_databaseName.c_str());
31     int errCode = OHOS::NativeRdb::E_OK;
32     OHOS::NativeRdb::RdbStoreConfig config(g_databaseName);
33     SqliteOpenHelperDownloadCallback sqliteOpenHelperCallback;
34     store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode);
35     if (errCode != OHOS::NativeRdb::E_OK) {
36         DOWNLOAD_HILOGE("DownloadDataBase errCode :%{public}d", errCode);
37     } else {
38         DOWNLOAD_HILOGI("DownloadDataBase errCode :%{public}d", errCode);
39     }
40 }
41 
GetInstance()42 std::shared_ptr<DownloadDataBase> DownloadDataBase::GetInstance()
43 {
44     if (instance_ == nullptr) {
45         instance_.reset(new DownloadDataBase());
46         return instance_;
47     }
48     return instance_;
49 }
50 
BeginTransaction()51 int DownloadDataBase::BeginTransaction()
52 {
53     if (store_ == nullptr) {
54         DOWNLOAD_HILOGE("DownloadDataBase BeginTransaction store_ is nullptr");
55         return RDB_OBJECT_EMPTY;
56     }
57     int ret = store_->BeginTransaction();
58     if (ret != OHOS::NativeRdb::E_OK) {
59         DOWNLOAD_HILOGE("DownloadDataBase BeginTransaction fail :%{public}d", ret);
60     }
61     return ret;
62 }
63 
Commit()64 int DownloadDataBase::Commit()
65 {
66     if (store_ == nullptr) {
67         DOWNLOAD_HILOGE(" DownloadDataBase Commit store_ is nullptr");
68         return RDB_OBJECT_EMPTY;
69     }
70     int ret = store_->Commit();
71     if (ret != OHOS::NativeRdb::E_OK) {
72         DOWNLOAD_HILOGE(" DownloadDataBase Commit fail :%{public}d", ret);
73     }
74     return ret;
75 }
76 
RollBack()77 int DownloadDataBase::RollBack()
78 {
79     if (store_ == nullptr) {
80         DOWNLOAD_HILOGE(" DownloadDataBase RollBack store_ is nullptr");
81         return RDB_OBJECT_EMPTY;
82     }
83     int ret = store_->RollBack();
84     if (ret != OHOS::NativeRdb::E_OK) {
85         DOWNLOAD_HILOGE(" DownloadDataBase RollBack fail :%{public}d", ret);
86     }
87     return ret;
88 }
89 
90 /**
91  * @brief Insert operation
92  *
93  * @param insertValues Conditions for update operation
94  *
95  * @return Insert operation results
96  */
Insert(OHOS::NativeRdb::ValuesBucket insertValues)97 int64_t DownloadDataBase::Insert(OHOS::NativeRdb::ValuesBucket insertValues)
98 {
99     int64_t outRowId = RDB_EXECUTE_FAIL;
100     if (store_ == nullptr) {
101         DOWNLOAD_HILOGE("DownloadDataBase Insert store_ is  nullptr");
102         return RDB_OBJECT_EMPTY;
103     }
104 
105     int ret = store_->Insert(outRowId, TABLE_NAME, insertValues);
106     DOWNLOAD_HILOGI("DownloadDataBase Insert id = %{public}lld ", outRowId);
107     if (ret != OHOS::NativeRdb::E_OK) {
108         DOWNLOAD_HILOGE("DownloadDataBase Insert ret :%{public}d", ret);
109         return RDB_EXECUTE_FAIL;
110     }
111     return outRowId;
112 }
113 
114 /**
115  * @brief Update operation
116  *
117  * @param values Conditions for update operation
118  * @param predicates Conditions for update operation
119  *
120  * @return Update operation results
121  */
Update(OHOS::NativeRdb::ValuesBucket values,OHOS::NativeRdb::RdbPredicates & predicates)122 int DownloadDataBase::Update(OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &predicates)
123 {
124     if (store_ == nullptr) {
125         DOWNLOAD_HILOGE("DownloadDataBase Update store_ is nullptr");
126         return RDB_OBJECT_EMPTY;
127     }
128 
129     int changeRow;
130     int ret = store_->Update(changeRow, values, predicates);
131     if (ret != OHOS::NativeRdb::E_OK) {
132         DOWNLOAD_HILOGE("DownloadDataBase Update ret :%{public}d", ret);
133         return RDB_EXECUTE_FAIL;
134     }
135     return ret;
136 }
137 
138 /**
139  * @brief Delete operation
140  *
141  * @param predicates Conditions for delete operation
142  *
143  * @return Delete operation results
144  */
Delete(OHOS::NativeRdb::RdbPredicates & predicates)145 int DownloadDataBase::Delete(OHOS::NativeRdb::RdbPredicates &predicates)
146 {
147     if (store_ == nullptr) {
148         DOWNLOAD_HILOGE("DownloadDataBase Delete store_ is  nullptr");
149         return RDB_OBJECT_EMPTY;
150     }
151     int deleteRow;
152     int ret = store_->Delete(deleteRow, predicates);
153     if (ret != OHOS::NativeRdb::E_OK) {
154         DOWNLOAD_HILOGE("DownloadDataBase Delete ret :%{public}d", ret);
155         return RDB_EXECUTE_FAIL;
156     }
157     return ret;
158 }
159 
160 /**
161  * @brief Query operation
162  *
163  * @param predicates Conditions for query operation
164  * @param columns Conditions for query operation
165  *
166  * @return Query database results
167  */
Query(OHOS::NativeRdb::RdbPredicates & predicates,std::vector<std::string> columns)168 std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> DownloadDataBase::Query(
169     OHOS::NativeRdb::RdbPredicates &predicates, std::vector<std::string> columns)
170 {
171     if (store_ == nullptr) {
172         DOWNLOAD_HILOGE("DownloadDataBase Delete store_ is  nullptr");
173         return nullptr;
174     }
175     std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> result = store_->Query(predicates, columns);
176     return result;
177 }
178 
OnCreate(OHOS::NativeRdb::RdbStore & store)179 int SqliteOpenHelperDownloadCallback::OnCreate(OHOS::NativeRdb::RdbStore &store)
180 {
181     if (store.ExecuteSql(CREATE_DOWNLOAD) != OHOS::NativeRdb::E_OK) {
182         DOWNLOAD_HILOGE("SqliteOpenHelperDownloadCallback create table error");
183     }
184     return OHOS::NativeRdb::E_OK;
185 }
186 
OnUpgrade(OHOS::NativeRdb::RdbStore & store,int oldVersion,int newVersion)187 int SqliteOpenHelperDownloadCallback::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int oldVersion, int newVersion)
188 {
189     return OHOS::NativeRdb::E_OK;
190 }
191 
OnDowngrade(OHOS::NativeRdb::RdbStore & store,int oldVersion,int newVersion)192 int SqliteOpenHelperDownloadCallback::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int oldVersion, int newVersion)
193 {
194     return OHOS::NativeRdb::E_OK;
195 }
196 } // namespace OHOS::Request::Download