upload tizen1.0 source
[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
29 using namespace DPL::DB::ORM;
30 using namespace DPL::DB::ORM::autosave;
31 using namespace AutoSaveDB::Interface;
32
33
34 namespace AutoSaveDB {
35
36 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
37
38 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
39     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
40         LogError(message);                              \
41         ReThrowMsg(AutoSaveDAOReadOnly::Exception::DatabaseError, \
42                    message);                            \
43     }
44
45 AutoSaveDAOReadOnly::AutoSaveDAOReadOnly()
46 {
47 }
48
49 AutoSaveDAOReadOnly::~AutoSaveDAOReadOnly()
50 {
51 }
52
53 void AutoSaveDAOReadOnly::attachDatabaseRO(void)
54 {
55     m_autoSavedbInterface.AttachToThread(
56         DPL::DB::SqlConnection::Flag::RO);
57 }
58
59 void AutoSaveDAOReadOnly::detachDatabase(void)
60 {
61     m_autoSavedbInterface.DetachFromThread();
62 }
63
64 DPL::Optional<AutoSaveData>
65         AutoSaveDAOReadOnly::getAutoSaveIdPasswd(const DPL::String &url)
66 {
67     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
68     {
69         AUTOSAVE_DB_SELECT(select, AutoSaveIdPasswd, &m_autoSavedbInterface);
70         select->Where(Equals<AutoSaveIdPasswd::address>(url));
71         AutoSaveIdPasswd::Select::RowList rows = select->GetRowList();
72
73         if (rows.empty()) {
74             return DPL::Optional<AutoSaveData>::Null;
75         }
76
77         AutoSaveData saveData;
78         saveData.userId = rows.front().Get_userId();
79         saveData.passwd = rows.front().Get_passwd();
80         return saveData;
81     }
82     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get autosave data string")
83 }
84
85 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
86 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
87
88 } // namespace AutoSaveDB