28d4f588147c6d7b33443a25628f231c4bca9850
[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
27 using namespace DPL::DB::ORM;
28 using namespace DPL::DB::ORM::autosave;
29 using namespace AutoSaveDB::Interface;
30
31 namespace AutoSaveDB {
32
33 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
34
35 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
36     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
37         LogError(message);                              \
38         ReThrowMsg(AutoSaveDAO::Exception::DatabaseError, \
39                    message);                            \
40     }
41
42 AutoSaveDAO::AutoSaveDAO() :
43     AutoSaveDAOReadOnly()
44 {
45 }
46
47 AutoSaveDAO::~AutoSaveDAO()
48 {
49 }
50
51 void AutoSaveDAO::attachDatabaseRW(void)
52 {
53     m_autoSavedbInterface.AttachToThread(
54         DPL::DB::SqlConnection::Flag::RW);
55 }
56
57 void AutoSaveDAO::detachDatabase(void)
58 {
59     m_autoSavedbInterface.DetachFromThread();
60 }
61
62 void AutoSaveDAO::setAutoSaveIdPasswd(const DPL::String &url,
63                                       const AutoSaveData &saveData)
64 {
65     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
66     {
67         ScopedTransaction transaction(&m_autoSavedbInterface);
68         AutoSaveIdPasswd::Row row;
69
70         row.Set_address(url);
71         row.Set_userId(saveData.userId);
72         row.Set_passwd(saveData.passwd);
73
74         DPL::Optional<AutoSaveData> savedData =
75         getAutoSaveIdPasswd(url);
76
77         if (!savedData.IsNull()) {
78             AUTOSAVE_DB_UPDATE(update, AutoSaveIdPasswd, &m_autoSavedbInterface)
79
80             update->Where(Equals<AutoSaveIdPasswd::address>(url));
81             update->Values(row);
82             update->Execute();
83         } else {
84             AUTOSAVE_DB_INSERT(
85                 insert, AutoSaveIdPasswd, &m_autoSavedbInterface);
86             insert->Values(row);
87             insert->Execute();
88         }
89
90         transaction.Commit();
91     }
92     SQL_CONNECTION_EXCEPTION_HANDLER_END(
93         "Fail to register id, passwd for autosave")
94 }
95
96 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
97 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
98
99 } // namespace AutoSaveDB