Tizen 2.0 Release
[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"Bookmark";
30 const wchar_t* BOOKMARK_FOLDER_TABLE = L"BookmarkFolder";
31 const wchar_t* HISTORY_DATA_TABLE = L"HistoryData";
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 Database* PresentationModelBase::__pDataBase = null;
42 DbEnumerator* PresentationModelBase::__pDbEnum = null;
43 DbStatement* PresentationModelBase::__pDbStatement = null;
44 bool PresentationModelBase::__isBeginTransaction = false;
45
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;
49
50 PresentationModelBase::PresentationModelBase(void)
51 {
52
53 }
54
55 PresentationModelBase::~PresentationModelBase(void)
56 {
57
58 }
59
60 result
61 PresentationModelBase::Initialize(void)
62 {
63         result r = E_FAILURE;
64
65         r = PresentationModelBase::OpenDb();
66
67         return r;
68 }
69
70 result
71 PresentationModelBase::UnInitialize(void)
72 {
73         result r = E_FAILURE;
74
75         r = PresentationModelBase::CloseDb();
76
77         return r;
78 }
79
80 result
81 PresentationModelBase::OpenDb(void)
82 {
83         result r = E_FAILURE;
84
85         if (__pDataBase != null)
86         {
87                 if (__pDbEnum != null)
88                 {
89                         delete __pDbEnum;
90                         __pDbEnum = null;
91                 }
92                 if (__pDbStatement != null)
93                 {
94                         delete __pDbStatement;
95                         __pDbStatement = null;
96                 }
97                 return r;
98         }
99         __pDataBase = new(std::nothrow) Database();
100         if (null == __pDataBase)
101         {
102                 return E_OUT_OF_MEMORY;
103         }
104         String dbName = App::GetInstance()->GetAppRootPath() + DB_FILE_PATH;
105
106         r = __pDataBase->Construct(dbName, true);
107         if (IsFailed(r))
108         {
109                 AppLogDebug("DB construction failed:%s", GetErrorMessage(r));
110                 delete __pDataBase;
111                 __pDataBase = null;
112                 return r;
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         switch (tableType)
160         {
161                 case DB_TYPE_BOOKMARKURLS:
162                 {
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)");
166                         break;
167                 }
168                 case DB_TYPE_BOOKMARKFOLDERS:
169                 {
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)");
173                         break;
174                 }
175
176                 case DB_TYPE_HISTORYDATA:
177                 {
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)");
181                         break;
182                 }
183
184                 case DB_TYPE_FAVICON:
185                 {
186                         query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
187                         break;
188                 }
189
190                 default:
191                 {
192                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
193                         break;
194                 }
195
196         }
197
198         r = PresentationModelBase::ExecuteQuery(query, resultCount);
199         TryCatch(!IsFailed(r),,"PresentationModelBase::CreateDBTables query failed %s",GetErrorMessage(r));
200
201         CATCH: return r;
202 }
203
204 result
205 PresentationModelBase::InitializeDBTables(void)
206 {
207         result r = E_FAILURE;
208
209         for (register int nDbTblIndex = DB_TYPE_INVALID + 1; nDbTblIndex < DB_TYPE_MAX; nDbTblIndex++)
210         {
211                 r = CreateDBTables((BrowserDbTableTypes) nDbTblIndex);
212                 TryCatch(!IsFailed(r),,"PresentationModelBase::InitializeDBTables failed %s",GetErrorMessage(r));
213         }
214
215         CATCH: return r;
216 }
217
218 result
219 PresentationModelBase::DropDBTables(void)
220 {
221         result r = E_FAILURE;
222         String query;
223         int resultCount = -1;
224
225         for (register int tableIndex = DB_TYPE_INVALID + 1; tableIndex < DB_TYPE_MAX; tableIndex++)
226         {
227                 switch (tableIndex)
228                 {
229                 case DB_TYPE_BOOKMARKURLS:
230                 {
231                         query.Clear();
232                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS Bookmark");
233                         break;
234                 }
235                 case DB_TYPE_BOOKMARKFOLDERS:
236                 {
237                         query.Clear();
238                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS BookmarkFolder");
239                         break;
240                 }
241
242                 case DB_TYPE_HISTORYDATA:
243                 {
244                         query.Clear();
245                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS HistoryData");
246                         break;
247                 }
248
249                 case DB_TYPE_FAVICON:
250                 {
251                         query.Clear();
252                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS FaviconData");
253                         break;
254                 }
255
256                 default:
257                 {
258                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableIndex);
259                         break;
260                 }
261
262                 }
263
264                 r = PresentationModelBase::ExecuteQuery(query, resultCount);
265                 TryCatch( !IsFailed(r),,"PresentationModelBase::DropDBTables query failed %s",GetErrorMessage(r));
266         }
267
268         CATCH: return r;
269 }
270
271 result
272 PresentationModelBase::ExecuteQuery(const String& formatQuery, int& count)
273 {
274         AppLogDebug("Query: %ls", formatQuery.GetPointer());
275
276         result r = E_FAILURE;
277         String queryType;
278
279         if (__pDataBase == null)
280         {
281                 return E_INIT_FAILED;
282         }
283
284         if (__pDbEnum != null)
285         {
286                 delete __pDbEnum;
287                 __pDbEnum = null;
288         }
289
290         if (__pDbStatement != null)
291         {
292                 delete __pDbStatement;
293                 __pDbStatement = null;
294         }
295
296
297 #if defined(FBK_DB_QUERY_LOG)
298         {
299                 File file;
300                 file.Construct(L"/Home/Query.txt", "a+", true);
301                 file.Write(formatQuery);
302                 file.Write(String("\r\n"));
303         }
304 #endif
305
306         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
307         if (r == E_SUCCESS)
308         {
309                 if (queryType == "SELECT")
310                 {
311                         __pDbEnum = __pDataBase->QueryN(formatQuery);
312                         r = GetLastResult();
313                         if (IsFailed(r))
314                         {
315                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
316                                 return r;
317                         }
318                         count = 0;
319
320                         if (__pDbEnum != null)
321                         {
322                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
323                                 {
324                                         count++;
325                                 }
326                                 __pDbEnum->Reset();
327                         }
328
329                 }
330                 else
331                 {
332                         if (__isBeginTransaction == false &&
333                                         (queryType == L"INSERT" || queryType == L"UPDATE" || queryType == L"DELETE"))
334                         {
335                                 r = __pDataBase->BeginTransaction();
336                                 if (IsFailed(r))
337                                 {
338                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
339                                         return r;
340                                 }
341
342                                 __isBeginTransaction = true;
343                         }
344
345                         r = __pDataBase->ExecuteSql(formatQuery, false);
346                         if (IsFailed(r))
347                         {
348                                 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
349                                 return r;
350                         }
351                         count = 0;
352                 }
353         }
354
355         return r;
356 }
357
358 result
359 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
360 {
361         result r = E_FAILURE;
362
363         if (null == __pDataBase || null == __pDbEnum)
364         {
365                 if (__pDbEnum == null)
366                 {
367                         AppLogException("PresentationModelBase : pdbenum is null");
368                 }
369                 if (__pDataBase == null)
370                 {
371                         AppLogException("PresentationModelBase : __pDataBase is null");
372                 }
373                 return E_OUT_OF_MEMORY;
374         }
375         r = __pDbEnum->MoveNext();
376         if (IsFailed(r))
377         {
378                 nextRowpresent = false;
379                 return r;
380         }
381         nextRowpresent = true;
382         return r;
383 }
384
385 result
386 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
387 {
388         if (null == __pDataBase || null == __pDbEnum)
389         {
390                 return E_DATABASE;
391         }
392         isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
393
394         return E_SUCCESS;
395 }
396
397 result
398 PresentationModelBase::GetColumn(int index, String& stringValue)
399 {
400         result r = E_FAILURE;
401         DbColumnType columnType;
402
403         if (null == __pDataBase || null == __pDbEnum)
404         {
405                 return E_OUT_OF_MEMORY;
406         }
407         // Find column type
408         columnType = __pDbEnum->GetColumnType(index);
409
410         //if column type is null then assign empty string and return
411         if (columnType == DB_COLUMNTYPE_NULL)
412         {
413                 stringValue = "";
414                 return E_SUCCESS;
415         }
416         //Get String value
417         r = __pDbEnum->GetStringAt(index, stringValue);
418         if (IsFailed(r))
419         {
420                 AppLogDebug("GetStringAt: Failed");
421                 return r;
422         }
423         return r;
424 }
425
426 result
427 PresentationModelBase::GetColumn(int index, int& intValue)
428 {
429         result r = E_FAILURE;
430         DbColumnType columnType;
431
432         if (null == __pDataBase || null == __pDbEnum)
433         {
434                 return E_OUT_OF_MEMORY;
435         }
436         // Find column type
437         columnType = __pDbEnum->GetColumnType(index);
438
439         //if column type is null then assign empty string and return
440         if (columnType == DB_COLUMNTYPE_NULL)
441         {
442                 intValue = 0;
443                 return E_SUCCESS;
444         }
445         //Get String value
446         r = __pDbEnum->GetIntAt(index, intValue);
447         TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
448
449         CATCH:
450         return r;
451 }
452
453 result
454 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
455 {
456         result r = E_FAILURE;
457         DbColumnType nType;
458
459         if (null == __pDataBase || null == __pDbEnum)
460         {
461                 AppLogDebug("__pDataBase or __pDbEnum is null");
462                 return E_OUT_OF_MEMORY;
463         }
464         // Find column type
465         nType = __pDbEnum->GetColumnType(index);
466         //if column type is null then assign empty string and return
467         if (nType == DB_COLUMNTYPE_NULL)
468         {
469                 return E_SUCCESS;
470         }
471         //Get String value
472         r = __pDbEnum->GetDateTimeAt(index, dateValue);
473         if (IsFailed(r))
474         {
475                 AppLogDebug("GetDateTimeAt: Failed");
476                 return r;
477         }
478
479         return r;
480 }
481
482 result
483 PresentationModelBase::GetColumn(int index, double& doubleValue)
484 {
485         result r = E_FAILURE;
486         DbColumnType columnType;
487
488         if (null == __pDataBase || null == __pDbEnum)
489         {
490                 return E_OUT_OF_MEMORY;
491         }
492         // Find column type
493         columnType = __pDbEnum->GetColumnType(index);
494
495         //if column type is null then assign empty string and return
496         if (columnType == DB_COLUMNTYPE_NULL)
497         {
498                 doubleValue = 0.0;
499                 return E_SUCCESS;
500         }
501         //Get String value
502         r = __pDbEnum->GetDoubleAt(index, doubleValue);
503         if (IsFailed(r))
504         {
505                 AppLogDebug("GetDateTimeAt: Failed");
506                 return r;
507         }
508
509         return r;
510 }
511
512 result
513 PresentationModelBase::RollbackDb()
514 {
515         result r = E_FAILURE;
516
517         if (__pDataBase != null && __isBeginTransaction == true)
518         {
519                 r = __pDataBase->RollbackTransaction();
520                 if (IsFailed(r))
521                 {
522                         return r;
523                 }
524                 __isBeginTransaction = false;
525         }
526         return r;
527 }
528
529 result
530 PresentationModelBase::CommitDb()
531 {
532
533         result r = E_FAILURE;
534
535         if (__pDataBase != null && __isBeginTransaction == true)
536         {
537                 r = __pDataBase->CommitTransaction();
538                 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
539
540                 __isBeginTransaction = false;
541         }
542
543         CATCH:
544         return r;
545 }
546
547 result
548 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
549 {
550         String query;
551         result r = E_FAILURE;
552         int count = -1;
553         bool nextRowpresent = false;
554
555         query.Append(L"SELECT last_insert_rowid() FROM ");
556         query.Append(tableName);
557
558         r = PresentationModelBase::ExecuteQuery(query, count);
559         TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
560
561         r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
562
563         if (nextRowpresent == true)
564         {
565                 r = PresentationModelBase::GetColumn(0, rowId);
566                 if (IsFailed(r))
567                 {
568                         AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
569                         return r;
570                 }
571         }
572         CATCH: return r;
573 }
574
575 bool
576 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
577 {
578         DateTime dtNow;
579         LocaleManager localManager;
580         result r = E_FAILURE;
581         TimeZone tz;
582
583         r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
584         if (IsFailed(r))
585         {
586                 return false;
587         }
588         localManager.Construct();
589         tz = localManager.GetSystemTimeZone();
590         dt = tz.UtcTimeToWallTime(dtNow);
591
592         return true;
593 }
594
595