cosmetic changes
[public/netxms.git] / src / agent / subagents / dbquery / conn.cpp
CommitLineData
07ca7d19 1/*
4cd1e46b
AK
2** NetXMS - Network Management System
3** Copyright (C) 2003-2013 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
8** (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 Lesser General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: direct.cpp
20**
21**/
22
23#include "dbquery.h"
24
25/**
26 * Database connections
27 */
28static ObjectArray<DBConnection> s_dbConnections(8, 8, true);
29static MUTEX s_dbConnectionsLock = MutexCreate();
30
31/**
32 * DB connection constructor
33 */
34DBConnection::DBConnection()
35{
c7f6930c 36 m_id = NULL;
4cd1e46b
AK
37 m_driver = NULL;
38 m_server = NULL;
39 m_dbName = NULL;
40 m_login = NULL;
41 m_password = NULL;
42 m_hDriver = NULL;
43 m_hdb = NULL;
44}
45
46/**
47 * DB connection destructor
48 */
49DBConnection::~DBConnection()
50{
c7f6930c
VK
51 free(m_id);
52 free(m_driver);
53 free(m_server);
54 free(m_dbName);
55 free(m_login);
56 free(m_password);
4cd1e46b
AK
57 if (m_hdb != NULL)
58 DBDisconnect(m_hdb);
59 if (m_hDriver != NULL)
60 DBUnloadDriver(m_hDriver);
61}
62
63/**
64 * Read attribute from config string
65 */
66static TCHAR *ReadAttribute(const TCHAR *config, const TCHAR *attribute)
67{
68 TCHAR buffer[256];
69 if (!ExtractNamedOptionValue(config, attribute, buffer, 256))
70 return NULL;
71 return _tcsdup(buffer);
72}
73
74/**
75 * Create DB connection from config
76 */
77DBConnection *DBConnection::createFromConfig(const TCHAR *config)
78{
79 DBConnection *conn = new DBConnection();
80 conn->m_id = ReadAttribute(config, _T("id"));
81 conn->m_driver = ReadAttribute(config, _T("driver"));
82 conn->m_server = ReadAttribute(config, _T("server"));
07ca7d19 83 conn->m_dbName = ReadAttribute(config, _T("dbname"));
4cd1e46b 84 conn->m_login = ReadAttribute(config, _T("login"));
07ca7d19 85 conn->m_password = ReadAttribute(config, _T("password"));
04a6de61 86
07ca7d19 87 if(conn->m_password == NULL)
88 conn->m_password = ReadAttribute(config, _T("encryptedPassword"));
89
90 if (conn->m_password != NULL)
91 DecryptPassword(CHECK_NULL_EX(conn->m_login), conn->m_password, conn->m_password, _tcslen(conn->m_password));
4cd1e46b
AK
92
93 if ((conn->m_id == NULL) || (conn->m_driver == NULL))
94 {
95 delete conn;
96 return NULL;
97 }
98
99 conn->m_hDriver = DBLoadDriver(conn->m_driver, _T(""), false, NULL, NULL);
100 if (conn->m_hDriver == NULL)
101 {
102 delete conn;
103 return NULL;
104 }
105
106 conn->connect();
107 return conn;
108}
109
110/**
111 * Connect to database
112 */
113bool DBConnection::connect()
114{
115 if (m_hdb != NULL)
116 DBDisconnect(m_hdb);
117
118 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
119 m_hdb = DBConnect(m_hDriver, m_server, m_dbName, m_login, m_password, NULL, errorText);
120 if (m_hdb != NULL)
121 {
122 AgentWriteLog(NXLOG_INFO, _T("DBQUERY: connected to database %s"), m_id);
123 }
124 else
125 {
126 AgentWriteLog(NXLOG_WARNING, _T("DBQUERY: cannot connect to database %s (%s)"), m_id, errorText);
127 }
128 return m_hdb != NULL;
129}
130
131/**
132 * Add database connection to the list from config
133 */
134bool AddDatabaseFromConfig(const TCHAR *db)
135{
136 DBConnection *conn = DBConnection::createFromConfig(db);
137 if (conn == NULL)
138 return false;
139
140 MutexLock(s_dbConnectionsLock);
141 s_dbConnections.add(conn);
142 MutexUnlock(s_dbConnectionsLock);
143 return true;
144}
145
146/**
147 * Shutdown all DB connections
148 */
149void ShutdownConnections()
150{
151 MutexLock(s_dbConnectionsLock);
152 s_dbConnections.clear();
153 MutexUnlock(s_dbConnectionsLock);
154}
155
156/**
157 * Get connection handle for given database
158 */
159DB_HANDLE GetConnectionHandle(const TCHAR *dbid)
160{
161 DB_HANDLE hdb = NULL;
162 MutexLock(s_dbConnectionsLock);
163 for(int i = 0; i < s_dbConnections.size(); i++)
164 if (!_tcsicmp(dbid, s_dbConnections.get(i)->getId()))
165 {
166 hdb = s_dbConnections.get(i)->getHandle();
ec9c39ea
VK
167 if (hdb == NULL)
168 {
169 // Try to (re)connect to database
170 s_dbConnections.get(i)->connect();
171 hdb = s_dbConnections.get(i)->getHandle();
172 }
4cd1e46b
AK
173 break;
174 }
175 MutexUnlock(s_dbConnectionsLock);
176 return hdb;
177}