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 DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
454 pFormatter->ApplyPattern(L"yyyy-mm-dd hh:mm:ss");
456 pFormatter->Format(dt, dateStr);
457 AppLog("DateStr: %ls", dateStr.GetPointer());
458 __pDbStatement->BindString(4,dateStr);
459 __pDbStatement->BindString(5,dateStr);
461 if(bookmark.GetFavIconBuffer() != null)
463 __pDbStatement->BindBlob(6,*(bookmark.GetFavIconBuffer()));
465 __pDbStatement->BindInt(7,bookmark.GetFavIconWidth());
466 __pDbStatement->BindInt(8,bookmark.GetFavIconHeight());
468 __pDataBase->ExecuteStatementN(*__pDbStatement);
470 if (IsFailed(GetLastResult()))
472 AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
477 AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
480 else if (__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
482 r = __pDataBase->ExecuteSql(formatQuery, false);
485 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
498 PresentationModelBase::ExecuteHistoryQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
501 AppLogDebug("Query: %ls", formatQuery.GetPointer());
503 result r = E_FAILURE;
508 String historyTitle = history.GetHistoryTitle();
509 historyTitle.Replace(L"'", L"''");
511 String historyUrl = history.GetHistoryUrl();
512 historyUrl.Replace(L"'", L"''");
514 GetCurrentDateTime(date);
516 if (__pDataBase == null)
518 return E_INIT_FAILED;
521 if (__pDbEnum != null)
527 if (__pDbStatement != null)
529 delete __pDbStatement;
530 __pDbStatement = null;
533 r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
536 if (queryType == "SELECT")
538 __pDbEnum = __pDataBase->QueryN(formatQuery);
542 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
547 if (__pDbEnum != null)
549 while (__pDbEnum->MoveNext() == E_SUCCESS)
559 if (__isBeginTransaction == false && (queryType == L"INSERT"))
561 r = __pDataBase->BeginTransaction();
564 AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
568 __isBeginTransaction = true;
569 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
570 if(GetLastResult() != E_SUCCESS)
572 AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
573 return GetLastResult();
575 __pDbStatement->BindString(0,historyUrl);
576 __pDbStatement->BindString(1,historyTitle);
577 DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
578 pFormatter->ApplyPattern(L"YYYY-MM-dd hh:mm:ss");
580 pFormatter->Format(date, dateStr);
581 AppLog("DateStr: %ls", dateStr.GetPointer());
582 __pDbStatement->BindString(2, dateStr);
583 if(history.GetFavIconBuffer() != null)
585 __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
587 __pDbStatement->BindInt(4,history.GetFavIconWidth());
588 __pDbStatement->BindInt(5,history.GetFavIconHeight());
590 __pDataBase->ExecuteStatementN(*__pDbStatement);
592 if (IsFailed(GetLastResult()))
594 AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
599 AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
602 else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
604 r = __pDataBase->ExecuteSql(formatQuery, false);
607 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
619 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
621 result r = E_FAILURE;
623 if (null == __pDataBase || null == __pDbEnum)
625 if (__pDbEnum == null)
627 AppLogException("PresentationModelBase : pdbenum is null");
629 if (__pDataBase == null)
631 AppLogException("PresentationModelBase : __pDataBase is null");
633 return E_OUT_OF_MEMORY;
635 r = __pDbEnum->MoveNext();
638 nextRowpresent = false;
641 nextRowpresent = true;
646 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
648 if (null == __pDataBase || null == __pDbEnum)
652 isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
658 PresentationModelBase::GetColumn(int index, String& stringValue)
660 result r = E_FAILURE;
661 DbColumnType columnType;
663 if (null == __pDataBase || null == __pDbEnum)
665 return E_OUT_OF_MEMORY;
668 columnType = __pDbEnum->GetColumnType(index);
670 //if column type is null then assign empty string and return
671 if (columnType == DB_COLUMNTYPE_NULL)
677 r = __pDbEnum->GetStringAt(index, stringValue);
680 AppLogDebug("GetStringAt: Failed");
687 PresentationModelBase::GetColumn(int index, int& intValue)
689 result r = E_FAILURE;
690 DbColumnType columnType;
692 if (null == __pDataBase || null == __pDbEnum)
694 return E_OUT_OF_MEMORY;
697 columnType = __pDbEnum->GetColumnType(index);
699 //if column type is null then assign empty string and return
700 if (columnType == DB_COLUMNTYPE_NULL)
706 r = __pDbEnum->GetIntAt(index, intValue);
707 TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
714 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
716 result r = E_FAILURE;
719 if (null == __pDataBase || null == __pDbEnum)
721 AppLogDebug("__pDataBase or __pDbEnum is null");
722 return E_OUT_OF_MEMORY;
725 nType = __pDbEnum->GetColumnType(index);
726 //if column type is null then assign empty string and return
727 if (nType == DB_COLUMNTYPE_NULL)
732 r = __pDbEnum->GetDateTimeAt(index, dateValue);
735 AppLogDebug("GetDateTimeAt: Failed");
748 String dbColoumn = "";
749 __pDbEnum->GetStringAt(index, dbColoumn);
750 dbColoumn.IndexOf("-", 0, currIndex);
753 dbColoumn.SubString(0, currIndex ,temp);
754 Integer::Parse(temp, 10, year);
757 dbColoumn.IndexOf("-", currIndex + 1, nextIndex);
760 dbColoumn.SubString(currIndex + 1, nextIndex - currIndex - 1 ,temp);
761 Integer::Parse(temp, 10, month);
763 currIndex = nextIndex;
765 dbColoumn.IndexOf(" ", currIndex + 1, nextIndex);
768 dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
769 Integer::Parse(temp, 10, day);
771 currIndex = nextIndex;
773 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
776 dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
777 Integer::Parse(temp, 10, hour);
779 currIndex = nextIndex;
781 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
784 dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
785 Integer::Parse(temp, 10, min);
787 currIndex = nextIndex;
789 dbColoumn.SubString(currIndex + 1, temp);
790 Integer::Parse(temp, 10, sec);
792 dateValue.SetValue(year, month, day, hour, min, sec);
800 PresentationModelBase::GetColumn(int index, double& doubleValue)
802 result r = E_FAILURE;
803 DbColumnType columnType;
805 if (null == __pDataBase || null == __pDbEnum)
807 return E_OUT_OF_MEMORY;
810 columnType = __pDbEnum->GetColumnType(index);
812 //if column type is null then assign empty string and return
813 if (columnType == DB_COLUMNTYPE_NULL)
819 r = __pDbEnum->GetDoubleAt(index, doubleValue);
822 AppLogDebug("GetDateTimeAt: Failed");
830 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
832 result r = E_FAILURE;
833 DbColumnType columnType;
835 if (null == __pDataBase || null == __pDbEnum)
837 return E_OUT_OF_MEMORY;
840 columnType = __pDbEnum->GetColumnType(index);
842 //if column type is null then assign empty string and return
843 if (columnType == DB_COLUMNTYPE_NULL)
847 favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
849 r = __pDbEnum->GetBlobAt(index, favIconBuffer);
850 AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
855 AppLogDebug("GetDateTimeAt: Failed");
863 PresentationModelBase::RollbackDb()
865 result r = E_FAILURE;
867 if (__pDataBase != null && __isBeginTransaction == true)
869 r = __pDataBase->RollbackTransaction();
874 __isBeginTransaction = false;
880 PresentationModelBase::CommitDb()
883 result r = E_FAILURE;
885 if (__pDataBase != null && __isBeginTransaction == true)
887 r = __pDataBase->CommitTransaction();
888 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
890 __isBeginTransaction = false;
898 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
901 result r = E_FAILURE;
903 bool nextRowpresent = false;
905 query.Append(L"SELECT last_insert_rowid() FROM ");
906 query.Append(tableName);
908 r = PresentationModelBase::ExecuteQuery(query, count);
909 TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
911 r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
913 if (nextRowpresent == true)
915 r = PresentationModelBase::GetColumn(0, rowId);
918 AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
926 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
929 LocaleManager localManager;
930 result r = E_FAILURE;
933 r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
938 localManager.Construct();
939 tz = localManager.GetSystemTimeZone();
940 dt = tz.UtcTimeToWallTime(dtNow);