1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
27 using namespace icinga;
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)
33 std::vector<String> DbType::GetNames(void) const
35 boost::mutex::scoped_lock lock(GetStaticMutex());
39 String DbType::GetTable(void) const
44 long DbType::GetTypeID(void) const
49 String DbType::GetIDColumn(void) const
54 void DbType::RegisterType(const String& name, const DbType::Ptr& type)
56 boost::mutex::scoped_lock lock(GetStaticMutex());
57 type->m_Names.push_back(name);
58 GetTypes()[name] = type;
61 DbType::Ptr DbType::GetByName(const String& name)
63 boost::mutex::scoped_lock lock(GetStaticMutex());
64 DbType::TypeMap::const_iterator it = GetTypes().find(name);
66 if (it == GetTypes().end())
72 DbType::Ptr DbType::GetByID(long tid)
74 boost::mutex::scoped_lock lock(GetStaticMutex());
76 BOOST_FOREACH(const TypeMap::value_type& kv, GetTypes()) {
77 if (kv.second->GetTypeID() == tid)
84 DbObject::Ptr DbType::GetOrCreateObjectByName(const String& name1, const String& name2)
88 ObjectLock olock(this);
90 DbType::ObjectMap::const_iterator it = m_Objects.find(std::make_pair(name1, name2));
92 if (it != m_Objects.end())
95 DbObject::Ptr dbobj = m_ObjectFactory(GetSelf(), name1, name2);
96 m_Objects[std::make_pair(name1, name2)] = dbobj;
101 boost::mutex& DbType::GetStaticMutex(void)
103 static boost::mutex mutex;
108 * Caller must hold static mutex.
110 DbType::TypeMap& DbType::GetTypes(void)
112 static DbType::TypeMap tm;
116 std::set<DbType::Ptr> DbType::GetAllTypes(void)
118 std::set<DbType::Ptr> result;
120 boost::mutex::scoped_lock lock(GetStaticMutex());
121 std::pair<String, DbType::Ptr> kv;
122 BOOST_FOREACH(kv, GetTypes()) {
123 result.insert(kv.second);