Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / auto_save_dao / dao / auto_save_dao_read_only.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  * This file contains the declaration of auto save dao class.
18  *
19  * @file    auto_save_dao_read_only.cpp
20  * @author  Jihoon Chung (jihoon.chung@samsung.com)
21  * @version 1.0
22  * @brief   This file contains the declaration of auto save dao
23  */
24
25 #include <wrt-commons/auto-save-dao-ro/auto_save_dao_read_only.h>
26 #include <wrt-commons/auto-save-dao/AutoSaveDatabase.h>
27 #include <orm_generator_autosave.h>
28 #include <dpl/foreach.h>
29
30 using namespace DPL::DB::ORM;
31 using namespace DPL::DB::ORM::autosave;
32 using namespace AutoSaveDB::Interface;
33
34
35 namespace AutoSaveDB {
36
37 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
38
39 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
40     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
41         LogError(message);                              \
42         ReThrowMsg(AutoSaveDAOReadOnly::Exception::DatabaseError, \
43                    message);                            \
44     }
45
46 AutoSaveDAOReadOnly::AutoSaveDAOReadOnly()
47 {
48 }
49
50 AutoSaveDAOReadOnly::~AutoSaveDAOReadOnly()
51 {
52 }
53
54 void AutoSaveDAOReadOnly::attachDatabaseRO(void)
55 {
56     m_autoSavedbInterface.AttachToThread(
57         DPL::DB::SqlConnection::Flag::RO);
58 }
59
60 void AutoSaveDAOReadOnly::detachDatabase(void)
61 {
62     m_autoSavedbInterface.DetachFromThread();
63 }
64
65 SubmitFormData AutoSaveDAOReadOnly::getAutoSaveSubmitFormData(
66     const DPL::String &url)
67 {
68     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
69     {
70         AUTOSAVE_DB_SELECT(select, AutoSaveSubmitFormElement, &m_autoSavedbInterface);
71         select->Where(Equals<AutoSaveSubmitFormElement::address>(url));
72         AutoSaveSubmitFormElement::Select::RowList rows = select->GetRowList();
73
74         SubmitFormData submitFormData;
75         FOREACH(it, rows) {
76             SubmitFormElement element;
77             element.key = it->Get_key();
78             element.value = it->Get_value();
79             submitFormData.push_back(element);
80         }
81         return submitFormData;
82     }
83     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get autosave data string")
84 }
85
86 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
87 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
88
89 } // namespace AutoSaveDB