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"Bookmark";
30 const wchar_t* BOOKMARK_FOLDER_TABLE = L"BookmarkFolder";
31 const wchar_t* HISTORY_DATA_TABLE = L"HistoryData";
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 Database* PresentationModelBase::__pDataBase = null;
42 DbEnumerator* PresentationModelBase::__pDbEnum = null;
43 DbStatement* PresentationModelBase::__pDbStatement = null;
44 bool PresentationModelBase::__isBeginTransaction = false;
46 String PresentationModelBase::DB_FILE_PATH = L"/data/bookmark.db" ;
47 const int PresentationModelBase::MAX_DB_QUERY_SIZE = 21474836;
48 const int PresentationModelBase::QUERY_TYPE_LENGTH = 6;
50 PresentationModelBase::PresentationModelBase(void)
55 PresentationModelBase::~PresentationModelBase(void)
61 PresentationModelBase::Initialize(void)
65 r = PresentationModelBase::OpenDb();
71 PresentationModelBase::UnInitialize(void)
75 r = PresentationModelBase::CloseDb();
81 PresentationModelBase::OpenDb(void)
85 if (__pDataBase != null)
87 if (__pDbEnum != null)
92 if (__pDbStatement != null)
94 delete __pDbStatement;
95 __pDbStatement = null;
99 __pDataBase = new(std::nothrow) Database();
100 if (null == __pDataBase)
102 return E_OUT_OF_MEMORY;
104 String dbName = App::GetInstance()->GetAppRootPath() + DB_FILE_PATH;
106 r = __pDataBase->Construct(dbName, true);
109 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;
161 case DB_TYPE_BOOKMARKURLS:
163 query.Append(L"CREATE TABLE IF NOT EXISTS ");
164 query.Append(BOOKMARK_URL_TABLE);
165 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 case DB_TYPE_BOOKMARKFOLDERS:
170 query.Append(L"CREATE TABLE IF NOT EXISTS ");
171 query.Append(BOOKMARK_FOLDER_TABLE);
172 query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, CREATED_TIME DATETIME, MODIFIED_TIME DATETIME)");
176 case DB_TYPE_HISTORYDATA:
178 query.Append(L"CREATE TABLE IF NOT EXISTS ");
179 query.Append(HISTORY_DATA_TABLE);
180 query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, URL TEXT, VISITED_TIME DATETIME, ICON_PATH TEXT, FAVICON_ID INTEGER, THUMBNAIL_PATH TEXT)");
184 case DB_TYPE_FAVICON:
186 query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
192 AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
198 r = PresentationModelBase::ExecuteQuery(query, resultCount);
199 TryCatch(!IsFailed(r),,"PresentationModelBase::CreateDBTables query failed %s",GetErrorMessage(r));
205 PresentationModelBase::InitializeDBTables(void)
207 result r = E_FAILURE;
209 for (register int nDbTblIndex = DB_TYPE_INVALID + 1; nDbTblIndex < DB_TYPE_MAX; nDbTblIndex++)
211 r = CreateDBTables((BrowserDbTableTypes) nDbTblIndex);
212 TryCatch(!IsFailed(r),,"PresentationModelBase::InitializeDBTables failed %s",GetErrorMessage(r));
219 PresentationModelBase::DropDBTables(void)
221 result r = E_FAILURE;
223 int resultCount = -1;
225 for (register int tableIndex = DB_TYPE_INVALID + 1; tableIndex < DB_TYPE_MAX; tableIndex++)
229 case DB_TYPE_BOOKMARKURLS:
232 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS Bookmark");
235 case DB_TYPE_BOOKMARKFOLDERS:
238 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS BookmarkFolder");
242 case DB_TYPE_HISTORYDATA:
245 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS HistoryData");
249 case DB_TYPE_FAVICON:
252 query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS FaviconData");
258 AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableIndex);
264 r = PresentationModelBase::ExecuteQuery(query, resultCount);
265 TryCatch( !IsFailed(r),,"PresentationModelBase::DropDBTables query failed %s",GetErrorMessage(r));
272 PresentationModelBase::ExecuteQuery(const String& formatQuery, int& count)
274 AppLogDebug("Query: %ls", formatQuery.GetPointer());
276 result r = E_FAILURE;
279 if (__pDataBase == null)
281 return E_INIT_FAILED;
284 if (__pDbEnum != null)
290 if (__pDbStatement != null)
292 delete __pDbStatement;
293 __pDbStatement = null;
297 #if defined(FBK_DB_QUERY_LOG)
300 file.Construct(L"/Home/Query.txt", "a+", true);
301 file.Write(formatQuery);
302 file.Write(String("\r\n"));
306 r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
309 if (queryType == "SELECT")
311 __pDbEnum = __pDataBase->QueryN(formatQuery);
315 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
320 if (__pDbEnum != null)
322 while (__pDbEnum->MoveNext() == E_SUCCESS)
332 if (__isBeginTransaction == false &&
333 (queryType == L"INSERT" || queryType == L"UPDATE" || queryType == L"DELETE"))
335 r = __pDataBase->BeginTransaction();
338 AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
342 __isBeginTransaction = true;
345 r = __pDataBase->ExecuteSql(formatQuery, false);
348 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
359 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
361 result r = E_FAILURE;
363 if (null == __pDataBase || null == __pDbEnum)
365 if (__pDbEnum == null)
367 AppLogException("PresentationModelBase : pdbenum is null");
369 if (__pDataBase == null)
371 AppLogException("PresentationModelBase : __pDataBase is null");
373 return E_OUT_OF_MEMORY;
375 r = __pDbEnum->MoveNext();
378 nextRowpresent = false;
381 nextRowpresent = true;
386 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
388 if (null == __pDataBase || null == __pDbEnum)
392 isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
398 PresentationModelBase::GetColumn(int index, String& stringValue)
400 result r = E_FAILURE;
401 DbColumnType columnType;
403 if (null == __pDataBase || null == __pDbEnum)
405 return E_OUT_OF_MEMORY;
408 columnType = __pDbEnum->GetColumnType(index);
410 //if column type is null then assign empty string and return
411 if (columnType == DB_COLUMNTYPE_NULL)
417 r = __pDbEnum->GetStringAt(index, stringValue);
420 AppLogDebug("GetStringAt: Failed");
427 PresentationModelBase::GetColumn(int index, int& intValue)
429 result r = E_FAILURE;
430 DbColumnType columnType;
432 if (null == __pDataBase || null == __pDbEnum)
434 return E_OUT_OF_MEMORY;
437 columnType = __pDbEnum->GetColumnType(index);
439 //if column type is null then assign empty string and return
440 if (columnType == DB_COLUMNTYPE_NULL)
446 r = __pDbEnum->GetIntAt(index, intValue);
447 TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
454 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
456 result r = E_FAILURE;
459 if (null == __pDataBase || null == __pDbEnum)
461 AppLogDebug("__pDataBase or __pDbEnum is null");
462 return E_OUT_OF_MEMORY;
465 nType = __pDbEnum->GetColumnType(index);
466 //if column type is null then assign empty string and return
467 if (nType == DB_COLUMNTYPE_NULL)
472 r = __pDbEnum->GetDateTimeAt(index, dateValue);
475 AppLogDebug("GetDateTimeAt: Failed");
483 PresentationModelBase::GetColumn(int index, double& doubleValue)
485 result r = E_FAILURE;
486 DbColumnType columnType;
488 if (null == __pDataBase || null == __pDbEnum)
490 return E_OUT_OF_MEMORY;
493 columnType = __pDbEnum->GetColumnType(index);
495 //if column type is null then assign empty string and return
496 if (columnType == DB_COLUMNTYPE_NULL)
502 r = __pDbEnum->GetDoubleAt(index, doubleValue);
505 AppLogDebug("GetDateTimeAt: Failed");
513 PresentationModelBase::RollbackDb()
515 result r = E_FAILURE;
517 if (__pDataBase != null && __isBeginTransaction == true)
519 r = __pDataBase->RollbackTransaction();
524 __isBeginTransaction = false;
530 PresentationModelBase::CommitDb()
533 result r = E_FAILURE;
535 if (__pDataBase != null && __isBeginTransaction == true)
537 r = __pDataBase->CommitTransaction();
538 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
540 __isBeginTransaction = false;
548 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
551 result r = E_FAILURE;
553 bool nextRowpresent = false;
555 query.Append(L"SELECT last_insert_rowid() FROM ");
556 query.Append(tableName);
558 r = PresentationModelBase::ExecuteQuery(query, count);
559 TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
561 r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
563 if (nextRowpresent == true)
565 r = PresentationModelBase::GetColumn(0, rowId);
568 AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
576 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
579 LocaleManager localManager;
580 result r = E_FAILURE;
583 r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
588 localManager.Construct();
589 tz = localManager.GetSystemTimeZone();
590 dt = tz.UtcTimeToWallTime(dtNow);