NABI issues
[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                                 __pDbStatement->BindString(4,dt.ToString());
454                                 __pDbStatement->BindString(5,dt.ToString());
455
456                                 if(bookmark.GetFavIconBuffer() != null)
457                                 {
458                                         __pDbStatement->BindBlob(6,*(bookmark.GetFavIconBuffer()));
459                                 }
460                                 else
461                                 {
462                                         ByteBuffer buff;
463                                         buff.Construct(10);
464                                         __pDbStatement->BindBlob(6,buff);
465                                 }
466                                 __pDbStatement->BindInt(7,bookmark.GetFavIconWidth());
467                                 __pDbStatement->BindInt(8,bookmark.GetFavIconHeight());
468
469                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
470
471                                 if (IsFailed(GetLastResult()))
472                                 {
473                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
474                                         return r;
475                                 }
476                                 else
477                                 {
478                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
479                                 }
480                         }
481                         else if (__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
482                         {
483                                 r = __pDataBase->ExecuteSql(formatQuery, false);
484                                 if (IsFailed(r))
485                                 {
486                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
487                                         return r;
488                                 }
489                         }
490
491                         count = 0;
492                 }
493         }
494
495         return r;
496 }
497
498 result
499 PresentationModelBase::ExecuteHistoryQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
500 {
501
502         AppLogDebug("Query: %ls", formatQuery.GetPointer());
503
504         result r = E_FAILURE;
505         String queryType;
506         DateTime date;
507         int count;
508
509         String historyTitle = history.GetHistoryTitle();
510         historyTitle.Replace(L"'", L"''");
511
512         String historyUrl = history.GetHistoryUrl();
513         historyUrl.Replace(L"'", L"''");
514
515         GetCurrentDateTime(date);
516
517         if (__pDataBase == null)
518         {
519                 return E_INIT_FAILED;
520         }
521
522         if (__pDbEnum != null)
523         {
524                 delete __pDbEnum;
525                 __pDbEnum = null;
526         }
527
528         if (__pDbStatement != null)
529         {
530                 delete __pDbStatement;
531                 __pDbStatement = null;
532         }
533
534         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
535         if (r == E_SUCCESS)
536         {
537                 if (queryType == "SELECT")
538                 {
539                         __pDbEnum = __pDataBase->QueryN(formatQuery);
540                         r = GetLastResult();
541                         if (IsFailed(r))
542                         {
543                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
544                                 return r;
545                         }
546                         count = 0;
547
548                         if (__pDbEnum != null)
549                         {
550                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
551                                 {
552                                         count++;
553                                 }
554                                 __pDbEnum->Reset();
555                         }
556
557                 }
558                 else
559                 {
560                         if (__isBeginTransaction == false && (queryType == L"INSERT"))
561                         {
562                                 r = __pDataBase->BeginTransaction();
563                                 if (IsFailed(r))
564                                 {
565                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
566                                         return r;
567                                 }
568
569                                 __isBeginTransaction = true;
570                                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
571                                 if(GetLastResult() != E_SUCCESS)
572                                 {
573                                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
574                                         return GetLastResult();
575                                 }
576                                 __pDbStatement->BindString(0,historyUrl);
577                                 __pDbStatement->BindString(1,historyTitle);
578                                 __pDbStatement->BindDateTime(2, date);
579                                 if(history.GetFavIconBuffer() != null)
580                                 {
581                                         __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
582                                 }
583                                 else
584                                 {
585                                         ByteBuffer buff;
586                                         buff.Construct(10);
587                                         __pDbStatement->BindBlob(3,buff);
588                                 }
589                                 __pDbStatement->BindInt(4,history.GetFavIconWidth());
590                                 __pDbStatement->BindInt(5,history.GetFavIconHeight());
591
592                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
593
594                                 if (IsFailed(GetLastResult()))
595                                 {
596                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
597                                         return r;
598                                 }
599                                 else
600                                 {
601                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
602                                 }
603                         }
604                         else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
605                         {
606                                 r = __pDataBase->ExecuteSql(formatQuery, false);
607                                 if (IsFailed(r))
608                                 {
609                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
610                                         return r;
611                                 }
612                         }
613                         count = 0;
614                 }
615         }
616
617         return r;
618 }
619
620 result
621 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
622 {
623         result r = E_FAILURE;
624
625         if (null == __pDataBase || null == __pDbEnum)
626         {
627                 if (__pDbEnum == null)
628                 {
629                         AppLogException("PresentationModelBase : pdbenum is null");
630                 }
631                 if (__pDataBase == null)
632                 {
633                         AppLogException("PresentationModelBase : __pDataBase is null");
634                 }
635                 return E_OUT_OF_MEMORY;
636         }
637         r = __pDbEnum->MoveNext();
638         if (IsFailed(r))
639         {
640                 nextRowpresent = false;
641                 return r;
642         }
643         nextRowpresent = true;
644         return r;
645 }
646
647 result
648 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
649 {
650         if (null == __pDataBase || null == __pDbEnum)
651         {
652                 return E_DATABASE;
653         }
654         isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
655
656         return E_SUCCESS;
657 }
658
659 result
660 PresentationModelBase::GetColumn(int index, String& stringValue)
661 {
662         result r = E_FAILURE;
663         DbColumnType columnType;
664
665         if (null == __pDataBase || null == __pDbEnum)
666         {
667                 return E_OUT_OF_MEMORY;
668         }
669         // Find column type
670         columnType = __pDbEnum->GetColumnType(index);
671
672         //if column type is null then assign empty string and return
673         if (columnType == DB_COLUMNTYPE_NULL)
674         {
675                 stringValue = "";
676                 return E_SUCCESS;
677         }
678         //Get String value
679         r = __pDbEnum->GetStringAt(index, stringValue);
680         if (IsFailed(r))
681         {
682                 AppLogDebug("GetStringAt: Failed");
683                 return r;
684         }
685         return r;
686 }
687
688 result
689 PresentationModelBase::GetColumn(int index, int& intValue)
690 {
691         result r = E_FAILURE;
692         DbColumnType columnType;
693
694         if (null == __pDataBase || null == __pDbEnum)
695         {
696                 return E_OUT_OF_MEMORY;
697         }
698         // Find column type
699         columnType = __pDbEnum->GetColumnType(index);
700
701         //if column type is null then assign empty string and return
702         if (columnType == DB_COLUMNTYPE_NULL)
703         {
704                 intValue = 0;
705                 return E_SUCCESS;
706         }
707         //Get String value
708         r = __pDbEnum->GetIntAt(index, intValue);
709         TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
710
711         CATCH:
712         return r;
713 }
714
715 result
716 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
717 {
718         result r = E_FAILURE;
719         DbColumnType nType;
720
721         if (null == __pDataBase || null == __pDbEnum)
722         {
723                 AppLogDebug("__pDataBase or __pDbEnum is null");
724                 return E_OUT_OF_MEMORY;
725         }
726         // Find column type
727         nType = __pDbEnum->GetColumnType(index);
728         //if column type is null then assign empty string and return
729         if (nType == DB_COLUMNTYPE_NULL)
730         {
731                 return E_SUCCESS;
732         }
733         //Get String value
734         r = __pDbEnum->GetDateTimeAt(index, dateValue);
735         if (IsFailed(r))
736         {
737                 AppLogDebug("GetDateTimeAt: Failed");
738                 return r;
739         }
740
741         return r;
742 }
743
744 result
745 PresentationModelBase::GetColumn(int index, double& doubleValue)
746 {
747         result r = E_FAILURE;
748         DbColumnType columnType;
749
750         if (null == __pDataBase || null == __pDbEnum)
751         {
752                 return E_OUT_OF_MEMORY;
753         }
754         // Find column type
755         columnType = __pDbEnum->GetColumnType(index);
756
757         //if column type is null then assign empty string and return
758         if (columnType == DB_COLUMNTYPE_NULL)
759         {
760                 doubleValue = 0.0;
761                 return E_SUCCESS;
762         }
763         //Get String value
764         r = __pDbEnum->GetDoubleAt(index, doubleValue);
765         if (IsFailed(r))
766         {
767                 AppLogDebug("GetDateTimeAt: Failed");
768                 return r;
769         }
770
771         return r;
772 }
773
774 result
775 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
776 {
777         result r = E_FAILURE;
778         DbColumnType columnType;
779
780         if (null == __pDataBase || null == __pDbEnum)
781         {
782                 return E_OUT_OF_MEMORY;
783         }
784         // Find column type
785         columnType = __pDbEnum->GetColumnType(index);
786
787         //if column type is null then assign empty string and return
788         if (columnType == DB_COLUMNTYPE_NULL)
789         {
790                 return E_SUCCESS;
791         }
792         favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
793         //Get String value
794         r = __pDbEnum->GetBlobAt(index, favIconBuffer);
795         AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
796
797
798         if (IsFailed(r))
799         {
800                 AppLogDebug("GetDateTimeAt: Failed");
801                 return r;
802         }
803
804         return r;
805 }
806
807 result
808 PresentationModelBase::RollbackDb()
809 {
810         result r = E_FAILURE;
811
812         if (__pDataBase != null && __isBeginTransaction == true)
813         {
814                 r = __pDataBase->RollbackTransaction();
815                 if (IsFailed(r))
816                 {
817                         return r;
818                 }
819                 __isBeginTransaction = false;
820         }
821         return r;
822 }
823
824 result
825 PresentationModelBase::CommitDb()
826 {
827
828         result r = E_FAILURE;
829
830         if (__pDataBase != null && __isBeginTransaction == true)
831         {
832                 r = __pDataBase->CommitTransaction();
833                 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
834
835                 __isBeginTransaction = false;
836         }
837
838         CATCH:
839         return r;
840 }
841
842 result
843 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
844 {
845         String query;
846         result r = E_FAILURE;
847         int count = -1;
848         bool nextRowpresent = false;
849
850         query.Append(L"SELECT last_insert_rowid() FROM ");
851         query.Append(tableName);
852
853         r = PresentationModelBase::ExecuteQuery(query, count);
854         TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
855
856         r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
857
858         if (nextRowpresent == true)
859         {
860                 r = PresentationModelBase::GetColumn(0, rowId);
861                 if (IsFailed(r))
862                 {
863                         AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
864                         return r;
865                 }
866         }
867         CATCH: return r;
868 }
869
870 bool
871 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
872 {
873         DateTime dtNow;
874         LocaleManager localManager;
875         result r = E_FAILURE;
876         TimeZone tz;
877
878         r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
879         if (IsFailed(r))
880         {
881                 return false;
882         }
883         localManager.Construct();
884         tz = localManager.GetSystemTimeZone();
885         dt = tz.UtcTimeToWallTime(dtNow);
886
887         return true;
888 }
889
890