fea6309d895b6e774a6100eb156343e9c6b152ec
[framework/web/wrt-commons.git] / modules / custom_handler_dao / include / wrt-commons / custom-handler-dao-ro / CustomHandlerDatabase.h
1 /*
2  * Copyright (c) 2012 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 #ifndef _CUSTOM_HANDLER_DATABASE_H_
18 #define _CUSTOM_HANDLER_DATABASE_H_
19
20 #include <dpl/thread.h>
21 #include <dpl/mutex.h>
22 #include <dpl/db/thread_database_support.h>
23
24 namespace CustomHandlerDB {
25 namespace Interface {
26
27 void attachDatabaseRO();
28 void attachDatabaseRW();
29 void detachDatabase();
30
31 extern DPL::Mutex g_dbQueriesMutex;
32 extern DPL::DB::ThreadDatabaseSupport g_dbInterface;
33
34 } // namespace Interface
35 } // namespace CustomHandlerDB
36
37 #define CUSTOM_HANDLER_DB_INTERNAL(tlsCommand, InternalType)                   \
38     static DPL::ThreadLocalVariable<InternalType> *tlsCommand ## Ptr = NULL;   \
39     {                                                                          \
40         DPL::Mutex::ScopedLock lock(                                           \
41                 &CustomHandlerDB::Interface::g_dbQueriesMutex);                \
42         if (!tlsCommand ## Ptr) {                                              \
43             static DPL::ThreadLocalVariable<InternalType> tmp;                 \
44             tlsCommand ## Ptr = &tmp;                                          \
45         }                                                                      \
46     }                                                                          \
47     DPL::ThreadLocalVariable<InternalType> &tlsCommand = *tlsCommand ## Ptr;   \
48     if (tlsCommand.IsNull())                                                   \
49     {                                                                          \
50         tlsCommand = InternalType(&CustomHandlerDB::Interface::g_dbInterface); \
51     }
52
53 #define CUSTOM_HANDLER_DB_SELECT(name, type) \
54         CUSTOM_HANDLER_DB_INTERNAL(name, type::Select)
55
56 #define CUSTOM_HANDLER_DB_INSERT(name, type) \
57         CUSTOM_HANDLER_DB_INTERNAL(name, type::Insert)
58
59 #define CUSTOM_HANDLER_DB_UPDATE(name, type) \
60         CUSTOM_HANDLER_DB_INTERNAL(name, type::Update)
61
62 #define CUSTOM_HANDLER_DB_DELETE(name, type) \
63         CUSTOM_HANDLER_DB_INTERNAL(name, type::Delete)
64
65 #endif /* _CUSTOM_HANDLER_DATABASE_H_ */
66