boiler plate : flora site fixed
[apps/osp/Internet.git] / src / IntPresentationModelBase.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17
18 //!Internet
19 /*@file:        IntPresentationModelBase.cpp
20  *@brief:       Defines the database/storage functions.
21  */
22
23 #include <FBase.h>
24 #include <FLocales.h>
25
26 #include "IntPresentationModelBase.h"
27 #include "IntTypes.h"
28
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";
32
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;
40
41 const int PresentationModelBase::MAX_DB_QUERY_SIZE = 21474836;
42 const int PresentationModelBase::QUERY_TYPE_LENGTH = 6;
43
44 PresentationModelBase::PresentationModelBase(void)
45 {
46         __pDataBase = null;
47         __pDbEnum = null;
48         __pDbStatement = null;
49         __isBeginTransaction = false;
50         DB_FILE_PATH = L"/opt/usr/dbspace/.browser-history.db";
51 }
52
53 PresentationModelBase::~PresentationModelBase(void)
54 {
55
56 }
57
58 result
59 PresentationModelBase::Initialize(void)
60 {
61         result r = E_FAILURE;
62
63         r = OpenDb();
64
65         return r;
66 }
67
68 result
69 PresentationModelBase::UnInitialize(void)
70 {
71         result r = E_FAILURE;
72
73         r = PresentationModelBase::CloseDb();
74
75         return r;
76 }
77
78 result
79 PresentationModelBase::OpenDb(void)
80 {
81         result r = E_FAILURE;
82
83         if (__pDataBase != null)
84         {
85                 if (__pDbEnum != null)
86                 {
87                         delete __pDbEnum;
88                         __pDbEnum = null;
89                 }
90                 if (__pDbStatement != null)
91                 {
92                         delete __pDbStatement;
93                         __pDbStatement = null;
94                 }
95                 return r;
96         }
97         __pDataBase = new(std::nothrow) Database();
98         if (null == __pDataBase)
99         {
100                 return E_OUT_OF_MEMORY;
101         }
102         String dbName =  DB_FILE_PATH;
103
104         r = __pDataBase->Construct(dbName, true);
105         if (IsFailed(r))
106         {
107                 AppLogDebug("DB construction failed:%s", GetErrorMessage(r));
108                 delete __pDataBase;
109                 __pDataBase = null;
110                 return r;
111         }
112
113
114         __isBeginTransaction = false;
115         if (File::IsFileExist(dbName))
116         {
117                 r = InitializeDBTables();
118                 if (IsFailed(r))
119                 {
120                         delete __pDataBase;
121                         __pDataBase = null;
122                         return r;
123                 }
124         }
125         return r;
126 }
127
128 result
129 PresentationModelBase::CloseDb(void)
130 {
131         if (__pDbEnum != null)
132         {
133                 delete __pDbEnum;
134                 __pDbEnum = null;
135         }
136         if (__pDbStatement != null)
137         {
138                 delete __pDbStatement;
139                 __pDbStatement = null;
140         }
141         if (__pDataBase != null)
142         {
143                 delete __pDataBase;
144                 __pDataBase = null;
145         }
146
147         __isBeginTransaction = false;
148
149         return E_SUCCESS;
150 }
151
152 result
153 PresentationModelBase::CreateDBTables(BrowserDbTableTypes tableType)
154 {
155         result r = E_FAILURE;
156         String query;
157         int resultCount = -1;
158
159         if (DB_FILE_PATH.CompareTo(L"/opt/usr/dbspace/.internet_bookmark.db") == 0)
160         {
161                 switch (tableType)
162                 {
163                 case DB_TYPE_BOOKMARKURLS:
164                 {
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)");
169
170                         break;
171                 }
172                 case DB_TYPE_FAVICON:
173                 {
174                         query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
175                         break;
176                 }
177                 default:
178                 {
179                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
180                         return E_SUCCESS;
181                 }
182                 }
183         }
184         else if(DB_FILE_PATH.CompareTo(L"/opt/usr/dbspace/.browser-history.db") == 0)
185         {
186                 switch (tableType)
187                 {
188                 case DB_TYPE_HISTORYDATA:
189                 {
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)");
194                         break;
195                 }
196                 case DB_TYPE_FAVICON:
197                 {
198                         query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
199                         break;
200                 }
201                 default:
202                 {
203                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
204                         return E_SUCCESS;
205                 }
206                 }
207
208         }
209         r = PresentationModelBase::ExecuteQuery(query, resultCount);
210         TryCatch(!IsFailed(r),,"PresentationModelBase::CreateDBTables query failed %s",GetErrorMessage(r));
211
212         CATCH: return r;
213 }
214
215 result
216 PresentationModelBase::InitializeDBTables(void)
217 {
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++)
221         {
222                 r = CreateDBTables((BrowserDbTableTypes) nDbTblIndex);
223                 TryCatch(!IsFailed(r),,"PresentationModelBase::InitializeDBTables failed %s",GetErrorMessage(r));
224         }
225
226         CATCH: return r;
227 }
228
229 result
230 PresentationModelBase::DropDBTables(void)
231 {
232         result r = E_FAILURE;
233         String query;
234         int resultCount = -1;
235
236         for (register int tableIndex = DB_TYPE_INVALID + 1; tableIndex < DB_TYPE_MAX; tableIndex++)
237         {
238                 switch (tableIndex)
239                 {
240                 case DB_TYPE_BOOKMARKURLS:
241                 {
242                         query.Clear();
243                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS Bookmark");
244                         break;
245                 }
246                 /*case DB_TYPE_BOOKMARKFOLDERS:
247                 {
248                         query.Clear();
249                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS BookmarkFolder");
250                         break;
251                 }*/
252
253                 case DB_TYPE_HISTORYDATA:
254                 {
255                         query.Clear();
256                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS HistoryData");
257                         break;
258                 }
259
260                 case DB_TYPE_FAVICON:
261                 {
262                         query.Clear();
263                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS FaviconData");
264                         break;
265                 }
266
267                 default:
268                 {
269                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableIndex);
270                         break;
271                 }
272
273                 }
274
275                 r = PresentationModelBase::ExecuteQuery(query, resultCount);
276                 TryCatch( !IsFailed(r),,"PresentationModelBase::DropDBTables query failed %s",GetErrorMessage(r));
277         }
278
279         CATCH: return r;
280 }
281
282 result
283 PresentationModelBase::ExecuteQuery(const String& formatQuery, int& count)
284 {
285         AppLogDebug("rahul Query: %ls", formatQuery.GetPointer());
286
287         result r = E_FAILURE;
288         String queryType;
289
290         if (__pDataBase == null)
291         {
292                 return E_INIT_FAILED;
293         }
294
295         if (__pDbEnum != null)
296         {
297                 delete __pDbEnum;
298                 __pDbEnum = null;
299         }
300
301         if (__pDbStatement != null)
302         {
303                 delete __pDbStatement;
304                 __pDbStatement = null;
305         }
306
307
308 #if defined(FBK_DB_QUERY_LOG)
309         {
310                 File file;
311                 file.Construct(L"/Home/Query.txt", "a+", true);
312                 file.Write(formatQuery);
313                 file.Write(String("\r\n"));
314         }
315 #endif
316
317         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
318         if (r == E_SUCCESS)
319         {
320                 if (queryType == "SELECT")
321                 {
322                         __pDbEnum = __pDataBase->QueryN(formatQuery);
323                         r = GetLastResult();
324                         if (IsFailed(r))
325                         {
326                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
327                                 return r;
328                         }
329                         count = 0;
330
331                         if (__pDbEnum != null)
332                         {
333                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
334                                 {
335                                         count++;
336                                 }
337                                 __pDbEnum->Reset();
338                         }
339
340                 }
341                 else
342                 {
343                         if (__isBeginTransaction == false &&
344                                         (queryType == L"INSERT" || queryType == L"UPDATE" || queryType == L"DELETE"))
345                         {
346                                 r = __pDataBase->BeginTransaction();
347                                 if (IsFailed(r))
348                                 {
349                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
350                                         return r;
351                                 }
352
353                                 __isBeginTransaction = true;
354                         }
355
356                         r = __pDataBase->ExecuteSql(formatQuery, false);
357                         if (IsFailed(r))
358                         {
359                                 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
360                                 return r;
361                         }
362                         count = 0;
363                 }
364         }
365
366         return r;
367 }
368
369 result
370 PresentationModelBase::ExecuteBookmarkQuery(const Tizen::Base::String& formatQuery, int& count, BookmarkData& bookmark)
371 {
372         AppLogDebug("PresentationModelBase::ExecuteBookmarkQuery Query: %ls", formatQuery.GetPointer());
373
374         result r = E_FAILURE;
375         String queryType;
376
377         DateTime dt;
378         String bookmarkId;
379         String bookmarkTitle = bookmark.GetBookmarkTitle();
380         bookmarkTitle.Replace(L"'", L"''");
381         String bookmarkUrl = bookmark.GetUrl();
382         bookmarkUrl.Replace(L"'", L"''");
383
384         PresentationModelBase::GetCurrentDateTime(dt);
385
386         if (__pDataBase == null)
387         {
388                 return E_INIT_FAILED;
389         }
390
391         if (__pDbEnum != null)
392         {
393                 delete __pDbEnum;
394                 __pDbEnum = null;
395         }
396
397         if (__pDbStatement != null)
398         {
399                 delete __pDbStatement;
400                 __pDbStatement = null;
401         }
402
403
404 #if defined(FBK_DB_QUERY_LOG)
405         {
406                 File file;
407                 file.Construct(L"/Home/Query.txt", "a+", true);
408                 file.Write(formatQuery);
409                 file.Write(String("\r\n"));
410         }
411 #endif
412
413         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
414         if (r == E_SUCCESS)
415         {
416                 if (queryType == "SELECT")
417                 {
418                         __pDbEnum = __pDataBase->QueryN(formatQuery);
419                         r = GetLastResult();
420                         if (IsFailed(r))
421                         {
422                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
423                                 return r;
424                         }
425                         count = 0;
426
427                         if (__pDbEnum != null)
428                         {
429                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
430                                 {
431                                         count++;
432                                 }
433                                 __pDbEnum->Reset();
434                         }
435                 }
436                 else
437                 {
438                         if (__isBeginTransaction == false && (queryType == L"INSERT" ))
439                         {
440                                 r = __pDataBase->BeginTransaction();
441                                 if (IsFailed(r))
442                                 {
443                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
444                                         return r;
445                                 }
446
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");
455                                 String dateStr;
456                                 pFormatter->Format(dt, dateStr);
457                                 AppLog("DateStr: %ls", dateStr.GetPointer());
458                                 __pDbStatement->BindString(4,dateStr);
459                                 __pDbStatement->BindString(5,dateStr);
460
461                                 if(bookmark.GetFavIconBuffer() != null)
462                                 {
463                                         __pDbStatement->BindBlob(6,*(bookmark.GetFavIconBuffer()));
464                                 }
465                                 __pDbStatement->BindInt(7,bookmark.GetFavIconWidth());
466                                 __pDbStatement->BindInt(8,bookmark.GetFavIconHeight());
467
468                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
469
470                                 if (IsFailed(GetLastResult()))
471                                 {
472                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
473                                         return r;
474                                 }
475                                 else
476                                 {
477                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
478                                 }
479                         }
480                         else if (__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
481                         {
482                                 r = __pDataBase->ExecuteSql(formatQuery, false);
483                                 if (IsFailed(r))
484                                 {
485                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
486                                         return r;
487                                 }
488                         }
489
490                         count = 0;
491                 }
492         }
493
494         return r;
495 }
496
497 result
498 PresentationModelBase::ExecuteHistoryQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
499 {
500
501         AppLogDebug("Query: %ls", formatQuery.GetPointer());
502
503         result r = E_FAILURE;
504         String queryType;
505         DateTime date;
506         int count;
507
508         String historyTitle = history.GetHistoryTitle();
509         historyTitle.Replace(L"'", L"''");
510
511         String historyUrl = history.GetHistoryUrl();
512         historyUrl.Replace(L"'", L"''");
513
514         GetCurrentDateTime(date);
515
516         if (__pDataBase == null)
517         {
518                 return E_INIT_FAILED;
519         }
520
521         if (__pDbEnum != null)
522         {
523                 delete __pDbEnum;
524                 __pDbEnum = null;
525         }
526
527         if (__pDbStatement != null)
528         {
529                 delete __pDbStatement;
530                 __pDbStatement = null;
531         }
532
533         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
534         if (r == E_SUCCESS)
535         {
536                 if (queryType == "SELECT")
537                 {
538                         __pDbEnum = __pDataBase->QueryN(formatQuery);
539                         r = GetLastResult();
540                         if (IsFailed(r))
541                         {
542                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
543                                 return r;
544                         }
545                         count = 0;
546
547                         if (__pDbEnum != null)
548                         {
549                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
550                                 {
551                                         count++;
552                                 }
553                                 __pDbEnum->Reset();
554                         }
555
556                 }
557                 else
558                 {
559                         if (__isBeginTransaction == false && (queryType == L"INSERT"))
560                         {
561                                 r = __pDataBase->BeginTransaction();
562                                 if (IsFailed(r))
563                                 {
564                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
565                                         return r;
566                                 }
567
568                                 __isBeginTransaction = true;
569                                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
570                                 if(GetLastResult() != E_SUCCESS)
571                                 {
572                                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
573                                         return GetLastResult();
574                                 }
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");
579                                 String dateStr;
580                                 pFormatter->Format(date, dateStr);
581                                 AppLog("DateStr: %ls", dateStr.GetPointer());
582                                 __pDbStatement->BindString(2, dateStr);
583                                 if(history.GetFavIconBuffer() != null)
584                                 {
585                                         __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
586                                 }
587                                 __pDbStatement->BindInt(4,history.GetFavIconWidth());
588                                 __pDbStatement->BindInt(5,history.GetFavIconHeight());
589
590                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
591
592                                 if (IsFailed(GetLastResult()))
593                                 {
594                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
595                                         return r;
596                                 }
597                                 else
598                                 {
599                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
600                                 }
601                         }
602                         else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
603                         {
604                                 r = __pDataBase->ExecuteSql(formatQuery, false);
605                                 if (IsFailed(r))
606                                 {
607                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
608                                         return r;
609                                 }
610                         }
611                         count = 0;
612                 }
613         }
614
615         return r;
616 }
617
618 result
619 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
620 {
621         result r = E_FAILURE;
622
623         if (null == __pDataBase || null == __pDbEnum)
624         {
625                 if (__pDbEnum == null)
626                 {
627                         AppLogException("PresentationModelBase : pdbenum is null");
628                 }
629                 if (__pDataBase == null)
630                 {
631                         AppLogException("PresentationModelBase : __pDataBase is null");
632                 }
633                 return E_OUT_OF_MEMORY;
634         }
635         r = __pDbEnum->MoveNext();
636         if (IsFailed(r))
637         {
638                 nextRowpresent = false;
639                 return r;
640         }
641         nextRowpresent = true;
642         return r;
643 }
644
645 result
646 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
647 {
648         if (null == __pDataBase || null == __pDbEnum)
649         {
650                 return E_DATABASE;
651         }
652         isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
653
654         return E_SUCCESS;
655 }
656
657 result
658 PresentationModelBase::GetColumn(int index, String& stringValue)
659 {
660         result r = E_FAILURE;
661         DbColumnType columnType;
662
663         if (null == __pDataBase || null == __pDbEnum)
664         {
665                 return E_OUT_OF_MEMORY;
666         }
667         // Find column type
668         columnType = __pDbEnum->GetColumnType(index);
669
670         //if column type is null then assign empty string and return
671         if (columnType == DB_COLUMNTYPE_NULL)
672         {
673                 stringValue = "";
674                 return E_SUCCESS;
675         }
676         //Get String value
677         r = __pDbEnum->GetStringAt(index, stringValue);
678         if (IsFailed(r))
679         {
680                 AppLogDebug("GetStringAt: Failed");
681                 return r;
682         }
683         return r;
684 }
685
686 result
687 PresentationModelBase::GetColumn(int index, int& intValue)
688 {
689         result r = E_FAILURE;
690         DbColumnType columnType;
691
692         if (null == __pDataBase || null == __pDbEnum)
693         {
694                 return E_OUT_OF_MEMORY;
695         }
696         // Find column type
697         columnType = __pDbEnum->GetColumnType(index);
698
699         //if column type is null then assign empty string and return
700         if (columnType == DB_COLUMNTYPE_NULL)
701         {
702                 intValue = 0;
703                 return E_SUCCESS;
704         }
705         //Get String value
706         r = __pDbEnum->GetIntAt(index, intValue);
707         TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
708
709         CATCH:
710         return r;
711 }
712
713 result
714 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
715 {
716         result r = E_FAILURE;
717         DbColumnType nType;
718
719         if (null == __pDataBase || null == __pDbEnum)
720         {
721                 AppLogDebug("__pDataBase or __pDbEnum is null");
722                 return E_OUT_OF_MEMORY;
723         }
724         // Find column type
725         nType = __pDbEnum->GetColumnType(index);
726         //if column type is null then assign empty string and return
727         if (nType == DB_COLUMNTYPE_NULL)
728         {
729                 return E_SUCCESS;
730         }
731         //Get String value
732         r = __pDbEnum->GetDateTimeAt(index, dateValue);
733         if (IsFailed(r))
734         {
735                 AppLogDebug("GetDateTimeAt: Failed");
736                 String temp;
737                 int currIndex = -1;
738                 int nextIndex = -1;
739
740                 int year = 0;
741                 int month = 0;
742                 int day = 0;
743                 int hour = 0;
744                 int min = 0;
745                 int sec = 0;
746
747                 int value = 0;
748                 String dbColoumn = "";
749                 __pDbEnum->GetStringAt(index, dbColoumn);
750                 dbColoumn.IndexOf("-", 0, currIndex);
751                 if(currIndex != -1)
752                 {
753                         dbColoumn.SubString(0, currIndex ,temp);
754                         Integer::Parse(temp, 10, year);
755                 }
756
757                 dbColoumn.IndexOf("-", currIndex + 1, nextIndex);
758                 if(currIndex != -1)
759                 {
760                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex - 1 ,temp);
761                         Integer::Parse(temp, 10, month);
762                 }
763                 currIndex = nextIndex;
764
765                 dbColoumn.IndexOf(" ", currIndex + 1, nextIndex);
766                 if(currIndex != -1)
767                 {
768                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
769                         Integer::Parse(temp, 10, day);
770                 }
771                 currIndex = nextIndex;
772
773                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
774                 if(currIndex != -1)
775                 {
776                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
777                         Integer::Parse(temp, 10, hour);
778                 }
779                 currIndex = nextIndex;
780
781                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
782                 if(currIndex != -1)
783                 {
784                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
785                         Integer::Parse(temp, 10, min);
786                 }
787                 currIndex = nextIndex;
788
789                 dbColoumn.SubString(currIndex + 1, temp);
790                 Integer::Parse(temp, 10, sec);
791
792                 dateValue.SetValue(year, month, day, hour, min, sec);
793                 return E_SUCCESS;
794         }
795
796         return r;
797 }
798
799 result
800 PresentationModelBase::GetColumn(int index, double& doubleValue)
801 {
802         result r = E_FAILURE;
803         DbColumnType columnType;
804
805         if (null == __pDataBase || null == __pDbEnum)
806         {
807                 return E_OUT_OF_MEMORY;
808         }
809         // Find column type
810         columnType = __pDbEnum->GetColumnType(index);
811
812         //if column type is null then assign empty string and return
813         if (columnType == DB_COLUMNTYPE_NULL)
814         {
815                 doubleValue = 0.0;
816                 return E_SUCCESS;
817         }
818         //Get String value
819         r = __pDbEnum->GetDoubleAt(index, doubleValue);
820         if (IsFailed(r))
821         {
822                 AppLogDebug("GetDateTimeAt: Failed");
823                 return r;
824         }
825
826         return r;
827 }
828
829 result
830 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
831 {
832         result r = E_FAILURE;
833         DbColumnType columnType;
834
835         if (null == __pDataBase || null == __pDbEnum)
836         {
837                 return E_OUT_OF_MEMORY;
838         }
839         // Find column type
840         columnType = __pDbEnum->GetColumnType(index);
841
842         //if column type is null then assign empty string and return
843         if (columnType == DB_COLUMNTYPE_NULL)
844         {
845                 return E_SUCCESS;
846         }
847         favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
848         //Get String value
849         r = __pDbEnum->GetBlobAt(index, favIconBuffer);
850         AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
851
852
853         if (IsFailed(r))
854         {
855                 AppLogDebug("GetDateTimeAt: Failed");
856                 return r;
857         }
858
859         return r;
860 }
861
862 result
863 PresentationModelBase::RollbackDb()
864 {
865         result r = E_FAILURE;
866
867         if (__pDataBase != null && __isBeginTransaction == true)
868         {
869                 r = __pDataBase->RollbackTransaction();
870                 if (IsFailed(r))
871                 {
872                         return r;
873                 }
874                 __isBeginTransaction = false;
875         }
876         return r;
877 }
878
879 result
880 PresentationModelBase::CommitDb()
881 {
882
883         result r = E_FAILURE;
884
885         if (__pDataBase != null && __isBeginTransaction == true)
886         {
887                 r = __pDataBase->CommitTransaction();
888                 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
889
890                 __isBeginTransaction = false;
891         }
892
893         CATCH:
894         return r;
895 }
896
897 result
898 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
899 {
900         String query;
901         result r = E_FAILURE;
902         int count = -1;
903         bool nextRowpresent = false;
904
905         query.Append(L"SELECT last_insert_rowid() FROM ");
906         query.Append(tableName);
907
908         r = PresentationModelBase::ExecuteQuery(query, count);
909         TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
910
911         r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
912
913         if (nextRowpresent == true)
914         {
915                 r = PresentationModelBase::GetColumn(0, rowId);
916                 if (IsFailed(r))
917                 {
918                         AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
919                         return r;
920                 }
921         }
922         CATCH: return r;
923 }
924
925 bool
926 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
927 {
928         DateTime dtNow;
929         LocaleManager localManager;
930         result r = E_FAILURE;
931         TimeZone tz;
932
933         r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
934         if (IsFailed(r))
935         {
936                 return false;
937         }
938         localManager.Construct();
939         tz = localManager.GetSystemTimeZone();
940         dt = tz.UtcTimeToWallTime(dtNow);
941
942         return true;
943 }
944
945