2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file download-manager-history-db.cpp
19 * @author Jungki Kwak (jungki.kwak@samsung.com)
20 * @brief Manager for a download history DB
24 #include "download-manager-common.h"
25 #include "download-manager-history-db.h"
27 #define FINALIZE_ON_ERROR( stmt ) { \
28 DP_LOG("SQL error: %d", ret);\
29 if (sqlite3_finalize(stmt) != SQLITE_OK)\
30 DP_LOGE("sqlite3_finalize is failed.");\
35 sqlite3 *DownloadHistoryDB::historyDb = NULL;
37 DownloadHistoryDB::DownloadHistoryDB()
41 DownloadHistoryDB::~DownloadHistoryDB()
45 bool DownloadHistoryDB::open()
53 ret = db_util_open(DBDATADIR"/"HISTORYDB, &historyDb,
54 DB_UTIL_REGISTER_HOOK_METHOD);
56 if (ret != SQLITE_OK) {
58 db_util_close(historyDb);
66 void DownloadHistoryDB::close()
70 db_util_close(historyDb);
75 /* FIXME : Hitory entry limitation ?? */
76 bool DownloadHistoryDB::addToHistoryDB(Item *item)
79 sqlite3_stmt *stmt = NULL;
84 DP_LOGE("Item is NULL");
88 if (item->historyId() < 0) {
89 DP_LOGE("Cannot add to DB. Because historyId is invaild");
94 DP_LOGE("historyDB is NULL");
98 const string statement = "insert into history (historyid, downloadtype,\
99 contenttype, state, err, name, path, url, cookie, date) \
100 values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
102 ret = sqlite3_prepare_v2(historyDb, statement.c_str(), -1, &stmt, NULL);
104 if (ret != SQLITE_OK)
105 FINALIZE_ON_ERROR(stmt);
107 if (sqlite3_bind_int(stmt, 1, item->historyId()) != SQLITE_OK)
108 DP_LOGE("sqlite3_bind_int is failed.");
109 if (sqlite3_bind_int(stmt, 2, item->downloadType()) != SQLITE_OK)
110 DP_LOGE("sqlite3_bind_int is failed.");
111 if (sqlite3_bind_int(stmt, 3, item->contentType()) != SQLITE_OK)
112 DP_LOGE("sqlite3_bind_int is failed.");
113 if (sqlite3_bind_int(stmt, 4, item->state()) != SQLITE_OK)
114 DP_LOGE("sqlite3_bind_int is failed.");
115 if (sqlite3_bind_int(stmt, 5, item->errorCode()) != SQLITE_OK)
116 DP_LOGE("sqlite3_bind_int is failed.");
117 if (sqlite3_bind_text(stmt, 6, item->title().c_str(), -1, NULL) != SQLITE_OK)
118 DP_LOGE("sqlite3_bind_text is failed.");
119 if (sqlite3_bind_text(
120 stmt, 7, item->registeredFilePath().c_str(), -1, NULL) != SQLITE_OK)
121 DP_LOGE("sqlite3_bind_text is failed.");
122 if (sqlite3_bind_text(stmt, 8, item->url().c_str(), -1, NULL) != SQLITE_OK)
123 DP_LOGE("sqlite3_bind_text is failed.");
124 if (sqlite3_bind_text(stmt, 9, item->cookie().c_str(), -1, NULL) != SQLITE_OK)
125 DP_LOGE("sqlite3_bind_text is failed.");
126 if (sqlite3_bind_double(stmt, 10, item->finishedTime()) != SQLITE_OK)
127 DP_LOGE("sqlite3_bind_double is failed.");
128 ret = sqlite3_step(stmt);
130 DP_LOGD("SQL return: %s", (ret == SQLITE_ROW || ret == SQLITE_OK)?"Success":"Fail");
134 return ret == SQLITE_DONE;
137 bool DownloadHistoryDB::getCountOfHistory(int *count)
140 sqlite3_stmt *stmt = NULL;
145 DP_LOGE("historyDB is NULL");
148 ret = sqlite3_prepare_v2(historyDb, "select COUNT(*) from history", -1, &stmt, NULL);
149 if (ret != SQLITE_OK)
150 FINALIZE_ON_ERROR(stmt);
152 ret = sqlite3_step(stmt);
153 DP_LOGD("SQL return: %s", (ret == SQLITE_ROW || ret == SQLITE_OK)?"Success":"Fail");
154 if (ret == SQLITE_ROW) {
155 *count = sqlite3_column_int(stmt,0);
156 DP_LOGD("count[%d]",*count);
158 DP_LOGE("SQL query error");
162 if (sqlite3_finalize(stmt) != SQLITE_OK)
163 DP_LOGE("sqlite3_finalize is failed.");
168 bool DownloadHistoryDB::createRemainedItemsFromHistoryDB(int limit, int offset)
171 stringstream limitStr;
172 stringstream offsetStr;
173 sqlite3_stmt *stmt = NULL;
179 DP_LOGE("historyDB is NULL");
185 tmp.append("select historyid, downloadtype, contenttype, state, err, ");
186 tmp.append("name, path, url, cookie, date from history order by ");
187 tmp.append("date DESC limit ");
188 tmp.append(limitStr.str());
189 tmp.append(" offset ");
190 tmp.append(offsetStr.str());
191 ret = sqlite3_prepare_v2(historyDb, tmp.c_str(), -1, &stmt, NULL);
192 if (ret != SQLITE_OK)
193 FINALIZE_ON_ERROR(stmt);
196 ret = sqlite3_step(stmt);
197 if (ret == SQLITE_ROW) {
198 const char *tempStr = NULL;
202 Item *item = Item::createHistoryItem();
204 DP_LOGE("Fail to create item");
207 item->setHistoryId(sqlite3_column_int(stmt,0));
208 item->setDownloadType((DL_TYPE::TYPE)sqlite3_column_int(stmt,1));
209 item->setContentType(sqlite3_column_int(stmt,2));
210 item->setState((ITEM::STATE)sqlite3_column_int(stmt,3));
211 item->setErrorCode((ERROR::CODE)sqlite3_column_int(stmt,4));
212 tempStr = (const char *)(sqlite3_column_text(stmt,5));
218 tempStr = (const char *)(sqlite3_column_text(stmt,6));
223 item->setRegisteredFilePath(arg);
224 tempStr = (const char *)(sqlite3_column_text(stmt,7));
229 tempStr = (const char *)(sqlite3_column_text(stmt,8));
234 item->setFinishedTime(sqlite3_column_double(stmt,9));
235 item->attachHistoryItem();
236 item->setRetryData(url, cookie);
240 DP_LOGD("SQL error: %d", ret);
242 if (sqlite3_finalize(stmt) != SQLITE_OK)
243 DP_LOGE("sqlite3_finalize is failed.");
247 if (ret == SQLITE_DONE || ret == SQLITE_ROW)
253 bool DownloadHistoryDB::createItemsFromHistoryDB()
256 sqlite3_stmt *stmt = NULL;
258 stringstream limitStr;
263 DP_LOGE("historyDB is NULL");
266 limitStr << LOAD_HISTORY_COUNT;
267 tmp.append("select historyid, downloadtype, contenttype, state, err, ");
268 tmp.append("name, path, url, cookie, date from history order by ");
269 tmp.append("date DESC limit ");
270 tmp.append(limitStr.str());
271 ret = sqlite3_prepare_v2(historyDb, tmp.c_str(), -1, &stmt, NULL);
272 if (ret != SQLITE_OK)
273 FINALIZE_ON_ERROR(stmt);
276 ret = sqlite3_step(stmt);
277 if (ret == SQLITE_ROW) {
278 const char *tempStr = NULL;
279 string arg = string();
280 string url = string();
281 string cookie = string();
282 Item *item = Item::createHistoryItem();
284 DP_LOGE("Fail to create item");
287 item->setHistoryId(sqlite3_column_int(stmt,0));
288 item->setDownloadType((DL_TYPE::TYPE)sqlite3_column_int(stmt,1));
289 item->setContentType(sqlite3_column_int(stmt,2));
290 item->setState((ITEM::STATE)sqlite3_column_int(stmt,3));
291 item->setErrorCode((ERROR::CODE)sqlite3_column_int(stmt,4));
292 tempStr = (const char *)(sqlite3_column_text(stmt,5));
297 tempStr = (const char *)(sqlite3_column_text(stmt,6));
300 item->setRegisteredFilePath(arg);
301 tempStr = (const char *)(sqlite3_column_text(stmt,7));
304 tempStr = (const char *)(sqlite3_column_text(stmt,8));
307 item->setFinishedTime(sqlite3_column_double(stmt,9));
308 item->attachHistoryItem();
309 item->setRetryData(url, cookie);
313 DP_LOGD("SQL error: %d", ret);
315 if (sqlite3_finalize(stmt) != SQLITE_OK)
316 DP_LOGE("sqlite3_finalize is failed.");
319 if (ret == SQLITE_DONE || ret == SQLITE_ROW)
325 bool DownloadHistoryDB::deleteItem(unsigned int historyId)
328 sqlite3_stmt *stmt = NULL;
333 DP_LOGE("historyDB is NULL");
337 ret = sqlite3_prepare_v2(historyDb, "delete from history where historyid=?",
340 if (ret != SQLITE_OK)
341 FINALIZE_ON_ERROR(stmt);
342 if (sqlite3_bind_int(stmt, 1, historyId) != SQLITE_OK)
343 DP_LOGE("sqlite3_bind_int is failed.");
344 ret = sqlite3_step(stmt);
345 if (ret != SQLITE_OK && ret != SQLITE_DONE)
346 FINALIZE_ON_ERROR(stmt);
348 if (sqlite3_finalize(stmt) != SQLITE_OK)
349 DP_LOGE("sqlite3_finalize is failed.");
354 bool DownloadHistoryDB::deleteMultipleItem(queue <unsigned int> &q)
357 unsigned int historyId = -1;
358 sqlite3_stmt *stmt = NULL;
364 DP_LOGE("historyDB is NULL");
367 ret = sqlite3_exec(historyDb, "PRAGMA synchronous=OFF;\
368 PRAGMA count_changes=OFF; PRAGMA temp_store=memory;",
369 NULL, NULL, &errmsg);
370 if (SQLITE_OK != ret) {
371 sqlite3_free(errmsg);
376 DP_LOGD("queue size[%d]",q.size());
378 ret = sqlite3_prepare_v2(historyDb, "delete from history where historyid=?",
380 if (ret != SQLITE_OK)
381 FINALIZE_ON_ERROR(stmt);
382 historyId = q.front();
384 if (sqlite3_bind_int(stmt, 1, historyId) != SQLITE_OK)
385 DP_LOGE("sqlite3_bind_int is failed.");
386 ret = sqlite3_step(stmt);
387 if (ret != SQLITE_OK && ret != SQLITE_DONE)
388 FINALIZE_ON_ERROR(stmt);
391 if (sqlite3_finalize(stmt) != SQLITE_OK)
392 DP_LOGE("sqlite3_finalize is failed.");
397 bool DownloadHistoryDB::clearData(void)
400 sqlite3_stmt *stmt = NULL;
405 DP_LOGE("historyDB is NULL");
409 ret = sqlite3_prepare_v2(historyDb, "delete from history", -1, &stmt, NULL);
410 if (ret != SQLITE_OK)
411 FINALIZE_ON_ERROR(stmt);
413 ret = sqlite3_step(stmt);
414 if (ret != SQLITE_DONE)
415 FINALIZE_ON_ERROR(stmt);
417 if (sqlite3_finalize(stmt) != SQLITE_OK)
418 DP_LOGE("sqlite3_finalize is failed.");