]> granicus.if.org Git - icinga2/blob - lib/db_ido/dbtype.cpp
Fix Zone::IsGlobal()
[icinga2] / lib / db_ido / dbtype.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "db_ido/dbtype.hpp"
21 #include "db_ido/dbconnection.hpp"
22 #include "base/objectlock.hpp"
23 #include "base/debug.hpp"
24 #include <boost/thread/once.hpp>
25 #include <boost/foreach.hpp>
26
27 using namespace icinga;
28
29 DbType::DbType(const String& table, long tid, const String& idcolumn, const DbType::ObjectFactory& factory)
30         : m_Table(table), m_TypeID(tid), m_IDColumn(idcolumn), m_ObjectFactory(factory)
31 { }
32
33 std::vector<String> DbType::GetNames(void) const
34 {
35         boost::mutex::scoped_lock lock(GetStaticMutex());
36         return m_Names;
37 }
38
39 String DbType::GetTable(void) const
40 {
41         return m_Table;
42 }
43
44 long DbType::GetTypeID(void) const
45 {
46         return m_TypeID;
47 }
48
49 String DbType::GetIDColumn(void) const
50 {
51         return m_IDColumn;
52 }
53
54 void DbType::RegisterType(const String& name, const DbType::Ptr& type)
55 {
56         boost::mutex::scoped_lock lock(GetStaticMutex());
57         type->m_Names.push_back(name);
58         GetTypes()[name] = type;
59 }
60
61 DbType::Ptr DbType::GetByName(const String& name)
62 {
63         boost::mutex::scoped_lock lock(GetStaticMutex());
64         DbType::TypeMap::const_iterator it = GetTypes().find(name);
65
66         if (it == GetTypes().end())
67                 return DbType::Ptr();
68
69         return it->second;
70 }
71
72 DbType::Ptr DbType::GetByID(long tid)
73 {
74         boost::mutex::scoped_lock lock(GetStaticMutex());
75
76         BOOST_FOREACH(const TypeMap::value_type& kv, GetTypes()) {
77                 if (kv.second->GetTypeID() == tid)
78                         return kv.second;
79         }
80
81         return DbType::Ptr();
82 }
83
84 DbObject::Ptr DbType::GetOrCreateObjectByName(const String& name1, const String& name2)
85 {
86         ASSERT(!OwnsLock());
87
88         ObjectLock olock(this);
89
90         DbType::ObjectMap::const_iterator it = m_Objects.find(std::make_pair(name1, name2));
91
92         if (it != m_Objects.end())
93                 return it->second;
94
95         DbObject::Ptr dbobj = m_ObjectFactory(GetSelf(), name1, name2);
96         m_Objects[std::make_pair(name1, name2)] = dbobj;
97
98         return dbobj;
99 }
100
101 boost::mutex& DbType::GetStaticMutex(void)
102 {
103         static boost::mutex mutex;
104         return mutex;
105 }
106
107 /**
108  * Caller must hold static mutex.
109  */
110 DbType::TypeMap& DbType::GetTypes(void)
111 {
112         static DbType::TypeMap tm;
113         return tm;
114 }
115
116 std::set<DbType::Ptr> DbType::GetAllTypes(void)
117 {
118         std::set<DbType::Ptr> result;
119
120         boost::mutex::scoped_lock lock(GetStaticMutex());
121         std::pair<String, DbType::Ptr> kv;
122         BOOST_FOREACH(kv, GetTypes()) {
123                 result.insert(kv.second);
124         }
125
126         return result;
127 }