Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / auto_save_dao / dao / auto_save_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file    auto_save_dao.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  * @version 1.0
20  * @brief    This file contains the definition of auto save dao class.
21  */
22
23 #include <wrt-commons/auto-save-dao-rw/auto_save_dao.h>
24 #include <wrt-commons/auto-save-dao/AutoSaveDatabase.h>
25 #include <orm_generator_autosave.h>
26 #include <dpl/foreach.h>
27
28 using namespace DPL::DB::ORM;
29 using namespace DPL::DB::ORM::autosave;
30 using namespace AutoSaveDB::Interface;
31
32 namespace AutoSaveDB {
33
34 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
35
36 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
37     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
38         LogError(message);                              \
39         ReThrowMsg(AutoSaveDAO::Exception::DatabaseError, \
40                    message);                            \
41     }
42
43 AutoSaveDAO::AutoSaveDAO() :
44     AutoSaveDAOReadOnly()
45 {
46 }
47
48 AutoSaveDAO::~AutoSaveDAO()
49 {
50 }
51
52 void AutoSaveDAO::attachDatabaseRW(void)
53 {
54     m_autoSavedbInterface.AttachToThread(
55         DPL::DB::SqlConnection::Flag::RW);
56 }
57
58 void AutoSaveDAO::detachDatabase(void)
59 {
60     m_autoSavedbInterface.DetachFromThread();
61 }
62
63 void AutoSaveDAO::setAutoSaveSubmitFormData(const DPL::String &url,
64                                       const SubmitFormData &submitFormData)
65 {
66     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
67     {
68         ScopedTransaction transaction(&m_autoSavedbInterface);
69
70         SubmitFormData submitData = getAutoSaveSubmitFormData(url);
71         if (!submitData.empty()) {
72             AUTOSAVE_DB_DELETE(del,
73                                AutoSaveSubmitFormElement,
74                                &m_autoSavedbInterface)
75             del->Where(Equals<AutoSaveSubmitFormElement::address>(url));
76             del->Execute();
77         }
78
79         FOREACH(pSubmitFormData, submitFormData) {
80             AutoSaveSubmitFormElement::Row row;
81             row.Set_address(url);
82             row.Set_key(pSubmitFormData->key);
83             row.Set_value(pSubmitFormData->value);
84
85             AUTOSAVE_DB_INSERT(
86                 insert, AutoSaveSubmitFormElement, &m_autoSavedbInterface);
87             insert->Values(row);
88             insert->Execute();
89         }
90
91         transaction.Commit();
92     }
93     SQL_CONNECTION_EXCEPTION_HANDLER_END(
94         "Fail to register id, passwd for autosave")
95 }
96 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
97 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
98
99 } // namespace AutoSaveDB