3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 /*@file: IntPresentationModelBase.cpp
20 *@brief: Defines the database/storage functions.
26 #include "IntPresentationModelBase.h"
29 const wchar_t* BOOKMARK_URL_TABLE = L"Bookmarks";
30 //const wchar_t* BOOKMARK_FOLDER_TABLE = L"BookmarkFolder";
31 const wchar_t* HISTORY_DATA_TABLE = L"History";
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Utility;
37 using namespace Tizen::Io;
38 using namespace Tizen::Locales;
39 using namespace Tizen::System;
41 const int PresentationModelBase::MAX_DB_QUERY_SIZE = 21474836;
42 const int PresentationModelBase::QUERY_TYPE_LENGTH = 6;
44 PresentationModelBase::PresentationModelBase(void)
48 __pDbStatement = null;
49 __isBeginTransaction = false;
50 DB_FILE_PATH = L"/opt/usr/dbspace/.browser-history.db";
53 PresentationModelBase::~PresentationModelBase(void)
59 PresentationModelBase::Initialize(void)
69 PresentationModelBase::UnInitialize(void)
73 r = PresentationModelBase::CloseDb();
79 PresentationModelBase::OpenDb(void)
83 if (__pDataBase != null)
85 if (__pDbEnum != null)
90 if (__pDbStatement != null)
92 delete __pDbStatement;
93 __pDbStatement = null;
97 __pDataBase = new(std::nothrow) Database();
98 if (null == __pDataBase)
100 return E_OUT_OF_MEMORY;
102 String dbName = DB_FILE_PATH;
104 r = __pDataBase->Construct(dbName, true);
107 AppLogDebug("DB construction failed:%s", GetErrorMessage(r));
114 __isBeginTransaction = false;
115 if (File::IsFileExist(dbName))
117 r = InitializeDBTables();
129 PresentationModelBase::CloseDb(void)
131 if (__pDbEnum != null)
136 if (__pDbStatement != null)
138 delete __pDbStatement;
139 __pDbStatement = null;
141 if (__pDataBase != null)
147 __isBeginTransaction = false;
153 PresentationModelBase::CreateDBTables(BrowserDbTableTypes tableType)
155 result r = E_FAILURE;
157 int resultCount = -1;
159 if (DB_FILE_PATH.CompareTo(L"/opt/usr/dbspace/.internet_bookmark.db") == 0)
163 case DB_TYPE_BOOKMARKURLS:
165 query.Append(L"CREATE TABLE IF NOT EXISTS ");
166 query.Append(BOOKMARK_URL_TABLE);
167 //query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, URL TEXT, PARENT_BOOKMARK_ID INTEGER DEFAULT -1, CREATED_TIME DATETIME, MODIFIED_TIME DATETIME, ICON_PATH TEXT, FAVICON_ID INTEGER)");
168 query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TYPE INTEGER, PARENT INTEGER DEFAULT -1, ADDRESS TEXT, TITLE TEXT, CREATIONDATE DATETIME, SEQUENCE INTEGER, UPDATEDATE DATETIME, VISITDATE DATETIME, EDITABLE INTEGER, ACCESSACCOUNT INTEGER, FAVICON BLOB, FAVICON_LENGTH INTEGER, FAVICON_W INTEGER, FAVICON_H INTEGER, CREATED_DATE DATETIME, ACCOUNT_NAME TEXT, ACCOUNT_TYPE TEXT, THUMBNAIL BLOB, THUMBNAIL_LENGTH INTEGER, THUMBNAIL_W INTEGER, THUMBNAIL_H INTEGER, VERSION INTEGER, SYNC TEXT, TAG1 TEXT, TAG2 TEXT, TAG3 TEXT, TAG4 TEXT)");
172 case DB_TYPE_FAVICON:
174 query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
179 AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
184 else if(DB_FILE_PATH.CompareTo(L"/opt/usr/dbspace/.browser-history.db") == 0)
188 case DB_TYPE_HISTORYDATA:
190 query.Append(L"CREATE TABLE IF NOT EXISTS ");
191 query.Append(HISTORY_DATA_TABLE);
192 //query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, URL TEXT, VISITED_TIME DATETIME, ICON_PATH TEXT, FAVICON_ID INTEGER, THUMBNAIL_PATH TEXT)");
193 query.Append(L"(ID INTEGER PRIMARY KEY AUTOINCREMENT, ADDRESS TEXT, TITLE TEXT, COUNTER INTEGER, VISITDATE DATETIME, FAVICON BLOB, FAVICON_LENGTH INTEGER, FAVICON_W INTEGER, FAVICON_H INTEGER, SNAPSHOT BLOB, SNAPSHOT_STRIDE INTEGER, SNAPSHOT_W INTEGER, SNAPSHOT_H INTEGER)");
196 case DB_TYPE_FAVICON:
198 query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
203 AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
209 r = PresentationModelBase::ExecuteQuery(query, resultCount);
210 TryCatch(!IsFailed(r),,"PresentationModelBase::CreateDBTables query failed %s",GetErrorMessage(r));
216 PresentationModelBase::InitializeDBTables(void)
218 result r = E_FAILURE;
219 AppLog("DB_FILE_PATH :%ls", String(DB_FILE_PATH).GetPointer());
220 for (register int nDbTblIndex = DB_TYPE_INVALID + 1; nDbTblIndex < DB_TYPE_MAX; nDbTblIndex++)
222 r = CreateDBTables((BrowserDbTableTypes) nDbTblIndex);
223 TryCatch(!IsFailed(r),,"PresentationModelBase::InitializeDBTables failed %s",GetErrorMessage(r));
230 PresentationModelBase::DropDBTables(void)
232 result r = E_FAILURE;
234 int resultCount = -1;
236 for (register int tableIndex = DB_TYPE_INVALID + 1; tableIndex < DB_TYPE_MAX; tableIndex++)
240 case DB_TYPE_BOOKMARKURLS:
243 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS Bookmark");
246 /*case DB_TYPE_BOOKMARKFOLDERS:
249 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS BookmarkFolder");
253 case DB_TYPE_HISTORYDATA:
256 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS HistoryData");
260 case DB_TYPE_FAVICON:
263 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS FaviconData");
269 AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableIndex);
275 r = PresentationModelBase::ExecuteQuery(query, resultCount);
276 TryCatch( !IsFailed(r),,"PresentationModelBase::DropDBTables query failed %s",GetErrorMessage(r));
283 PresentationModelBase::ExecuteQuery(const String& formatQuery, int& count)
285 AppLogDebug("rahul Query: %ls", formatQuery.GetPointer());
287 result r = E_FAILURE;
290 if (__pDataBase == null)
292 return E_INIT_FAILED;
295 if (__pDbEnum != null)
301 if (__pDbStatement != null)
303 delete __pDbStatement;
304 __pDbStatement = null;
308 #if defined(FBK_DB_QUERY_LOG)
311 file.Construct(L"/Home/Query.txt", "a+", true);
312 file.Write(formatQuery);
313 file.Write(String("\r\n"));
317 r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
320 if (queryType == "SELECT")
322 __pDbEnum = __pDataBase->QueryN(formatQuery);
326 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
331 if (__pDbEnum != null)
333 while (__pDbEnum->MoveNext() == E_SUCCESS)
343 if (__isBeginTransaction == false &&
344 (queryType == L"INSERT" || queryType == L"UPDATE" || queryType == L"DELETE"))
346 r = __pDataBase->BeginTransaction();
349 AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
353 __isBeginTransaction = true;
356 r = __pDataBase->ExecuteSql(formatQuery, false);
359 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
370 PresentationModelBase::ExecuteBookmarkQuery(const Tizen::Base::String& formatQuery, int& count, BookmarkData& bookmark)
372 AppLogDebug("PresentationModelBase::ExecuteBookmarkQuery Query: %ls", formatQuery.GetPointer());
374 result r = E_FAILURE;
379 String bookmarkTitle = bookmark.GetBookmarkTitle();
380 bookmarkTitle.Replace(L"'", L"''");
381 String bookmarkUrl = bookmark.GetUrl();
382 bookmarkUrl.Replace(L"'", L"''");
384 PresentationModelBase::GetCurrentDateTime(dt);
386 if (__pDataBase == null)
388 return E_INIT_FAILED;
391 if (__pDbEnum != null)
397 if (__pDbStatement != null)
399 delete __pDbStatement;
400 __pDbStatement = null;
404 #if defined(FBK_DB_QUERY_LOG)
407 file.Construct(L"/Home/Query.txt", "a+", true);
408 file.Write(formatQuery);
409 file.Write(String("\r\n"));
413 r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
416 if (queryType == "SELECT")
418 __pDbEnum = __pDataBase->QueryN(formatQuery);
422 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
427 if (__pDbEnum != null)
429 while (__pDbEnum->MoveNext() == E_SUCCESS)
438 if (__isBeginTransaction == false && (queryType == L"INSERT" ))
440 r = __pDataBase->BeginTransaction();
443 AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
447 __isBeginTransaction = true;
448 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
449 __pDbStatement->BindInt(0,bookmark.GetBookmarkType());
450 __pDbStatement->BindString(1,bookmark.GetParentId());
451 __pDbStatement->BindString(2,bookmarkUrl);
452 __pDbStatement->BindString(3,bookmarkTitle);
453 __pDbStatement->BindString(4,dt.ToString());
454 __pDbStatement->BindString(5,dt.ToString());
456 if(bookmark.GetFavIconBuffer() != null)
458 __pDbStatement->BindBlob(6,*(bookmark.GetFavIconBuffer()));
464 __pDbStatement->BindBlob(6,buff);
466 __pDbStatement->BindInt(7,bookmark.GetFavIconWidth());
467 __pDbStatement->BindInt(8,bookmark.GetFavIconHeight());
469 __pDataBase->ExecuteStatementN(*__pDbStatement);
471 if (IsFailed(GetLastResult()))
473 AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
478 AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
481 else if (__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
483 r = __pDataBase->ExecuteSql(formatQuery, false);
486 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
499 PresentationModelBase::ExecuteHistoryQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
502 AppLogDebug("Query: %ls", formatQuery.GetPointer());
504 result r = E_FAILURE;
509 String historyTitle = history.GetHistoryTitle();
510 historyTitle.Replace(L"'", L"''");
512 String historyUrl = history.GetHistoryUrl();
513 historyUrl.Replace(L"'", L"''");
515 GetCurrentDateTime(date);
517 if (__pDataBase == null)
519 return E_INIT_FAILED;
522 if (__pDbEnum != null)
528 if (__pDbStatement != null)
530 delete __pDbStatement;
531 __pDbStatement = null;
534 r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
537 if (queryType == "SELECT")
539 __pDbEnum = __pDataBase->QueryN(formatQuery);
543 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
548 if (__pDbEnum != null)
550 while (__pDbEnum->MoveNext() == E_SUCCESS)
560 if (__isBeginTransaction == false && (queryType == L"INSERT"))
562 r = __pDataBase->BeginTransaction();
565 AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
569 __isBeginTransaction = true;
570 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
571 if(GetLastResult() != E_SUCCESS)
573 AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
574 return GetLastResult();
576 __pDbStatement->BindString(0,historyUrl);
577 __pDbStatement->BindString(1,historyTitle);
578 __pDbStatement->BindDateTime(2, date);
579 if(history.GetFavIconBuffer() != null)
581 __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
587 __pDbStatement->BindBlob(3,buff);
589 __pDbStatement->BindInt(4,history.GetFavIconWidth());
590 __pDbStatement->BindInt(5,history.GetFavIconHeight());
592 __pDataBase->ExecuteStatementN(*__pDbStatement);
594 if (IsFailed(GetLastResult()))
596 AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
601 AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
604 else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
606 r = __pDataBase->ExecuteSql(formatQuery, false);
609 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
621 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
623 result r = E_FAILURE;
625 if (null == __pDataBase || null == __pDbEnum)
627 if (__pDbEnum == null)
629 AppLogException("PresentationModelBase : pdbenum is null");
631 if (__pDataBase == null)
633 AppLogException("PresentationModelBase : __pDataBase is null");
635 return E_OUT_OF_MEMORY;
637 r = __pDbEnum->MoveNext();
640 nextRowpresent = false;
643 nextRowpresent = true;
648 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
650 if (null == __pDataBase || null == __pDbEnum)
654 isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
660 PresentationModelBase::GetColumn(int index, String& stringValue)
662 result r = E_FAILURE;
663 DbColumnType columnType;
665 if (null == __pDataBase || null == __pDbEnum)
667 return E_OUT_OF_MEMORY;
670 columnType = __pDbEnum->GetColumnType(index);
672 //if column type is null then assign empty string and return
673 if (columnType == DB_COLUMNTYPE_NULL)
679 r = __pDbEnum->GetStringAt(index, stringValue);
682 AppLogDebug("GetStringAt: Failed");
689 PresentationModelBase::GetColumn(int index, int& intValue)
691 result r = E_FAILURE;
692 DbColumnType columnType;
694 if (null == __pDataBase || null == __pDbEnum)
696 return E_OUT_OF_MEMORY;
699 columnType = __pDbEnum->GetColumnType(index);
701 //if column type is null then assign empty string and return
702 if (columnType == DB_COLUMNTYPE_NULL)
708 r = __pDbEnum->GetIntAt(index, intValue);
709 TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
716 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
718 result r = E_FAILURE;
721 if (null == __pDataBase || null == __pDbEnum)
723 AppLogDebug("__pDataBase or __pDbEnum is null");
724 return E_OUT_OF_MEMORY;
727 nType = __pDbEnum->GetColumnType(index);
728 //if column type is null then assign empty string and return
729 if (nType == DB_COLUMNTYPE_NULL)
734 r = __pDbEnum->GetDateTimeAt(index, dateValue);
737 AppLogDebug("GetDateTimeAt: Failed");
745 PresentationModelBase::GetColumn(int index, double& doubleValue)
747 result r = E_FAILURE;
748 DbColumnType columnType;
750 if (null == __pDataBase || null == __pDbEnum)
752 return E_OUT_OF_MEMORY;
755 columnType = __pDbEnum->GetColumnType(index);
757 //if column type is null then assign empty string and return
758 if (columnType == DB_COLUMNTYPE_NULL)
764 r = __pDbEnum->GetDoubleAt(index, doubleValue);
767 AppLogDebug("GetDateTimeAt: Failed");
775 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
777 result r = E_FAILURE;
778 DbColumnType columnType;
780 if (null == __pDataBase || null == __pDbEnum)
782 return E_OUT_OF_MEMORY;
785 columnType = __pDbEnum->GetColumnType(index);
787 //if column type is null then assign empty string and return
788 if (columnType == DB_COLUMNTYPE_NULL)
792 favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
794 r = __pDbEnum->GetBlobAt(index, favIconBuffer);
795 AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
800 AppLogDebug("GetDateTimeAt: Failed");
808 PresentationModelBase::RollbackDb()
810 result r = E_FAILURE;
812 if (__pDataBase != null && __isBeginTransaction == true)
814 r = __pDataBase->RollbackTransaction();
819 __isBeginTransaction = false;
825 PresentationModelBase::CommitDb()
828 result r = E_FAILURE;
830 if (__pDataBase != null && __isBeginTransaction == true)
832 r = __pDataBase->CommitTransaction();
833 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
835 __isBeginTransaction = false;
843 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
846 result r = E_FAILURE;
848 bool nextRowpresent = false;
850 query.Append(L"SELECT last_insert_rowid() FROM ");
851 query.Append(tableName);
853 r = PresentationModelBase::ExecuteQuery(query, count);
854 TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
856 r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
858 if (nextRowpresent == true)
860 r = PresentationModelBase::GetColumn(0, rowId);
863 AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
871 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
874 LocaleManager localManager;
875 result r = E_FAILURE;
878 r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
883 localManager.Construct();
884 tz = localManager.GetSystemTimeZone();
885 dt = tz.UtcTimeToWallTime(dtNow);