Server core can be compiled as shared library under UNIX
[public/netxms.git] / src / server / tools / nxdbmgr / nxdbmgr.cpp
CommitLineData
fc935a60
VK
1/*
2** nxdbmgr - NetXMS database manager
3** Copyright (C) 2004 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 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 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** $module: nxdbmgr.cpp
20**
21**/
22
23#include "nxdbmgr.h"
24
25
26//
27// Global variables
28//
29
30DB_HANDLE g_hCoreDB;
6ad5eb6d 31BOOL g_bIgnoreErrors = FALSE;
fc935a60
VK
32
33
34//
35// Static data
36//
37
38static NX_CFG_TEMPLATE m_cfgTemplate[] =
39{
40 { "DBDriver", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDriver },
41 { "DBDrvParams", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDrvParams },
42 { "DBLogin", CT_STRING, 0, 0, MAX_DB_LOGIN, 0, g_szDbLogin },
43 { "DBName", CT_STRING, 0, 0, MAX_DB_NAME, 0, g_szDbName },
44 { "DBPassword", CT_STRING, 0, 0, MAX_DB_PASSWORD, 0, g_szDbPassword },
45 { "DBServer", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbServer },
46 { "LogFailedSQLQueries", CT_IGNORE, 0, 0, 0, 0, NULL },
47 { "LogFile", CT_IGNORE, 0, 0, 0, 0, NULL },
48 { "", CT_END_OF_LIST, 0, 0, 0, 0, NULL }
49};
50
51
6ad5eb6d
VK
52//
53// Execute SQL query and print error message on screen if query failed
54//
55
56BOOL SQLQuery(TCHAR *pszQuery)
57{
58 BOOL bResult;
59
60 bResult = DBQuery(g_hCoreDB, pszQuery);
61 if (!bResult)
62 {
63#ifdef _WIN32
64 _tprintf(_T("SQL query failed:\n"));
65 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0E);
66 _tprintf(_T("%s\n"), pszQuery);
67 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
68#else
69 _tprintf(_T("SQL query failed:\n%s\n"), pszQuery);
70#endif
71 }
72 return bResult;
73}
74
75
22c38feb
VK
76//
77// Execute SQL batch
78//
79
80BOOL SQLBatch(TCHAR *pszBatch)
81{
82 TCHAR *pszQuery, *ptr;
83
84 pszQuery = pszBatch;
85 while(1)
86 {
87 ptr = _tcschr(pszQuery, _T('\n'));
88 if (ptr != NULL)
89 *ptr = 0;
90 if (!_tcscmp(pszQuery, _T("<END>")))
91 break;
92 if (!DBQuery(g_hCoreDB, pszQuery))
93 {
94#ifdef _WIN32
95 _tprintf(_T("SQL query failed:\n"));
96 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0E);
97 _tprintf(_T("%s\n"), pszQuery);
98 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
99#else
100 _tprintf(_T("SQL query failed:\n%s\n"), pszQuery);
101#endif
102 if (!g_bIgnoreErrors)
103 return FALSE;
104 }
105 ptr++;
106 pszQuery = ptr;
107 }
108 return TRUE;
109}
110
111
fc935a60
VK
112//
113// Startup
114//
115
116int main(int argc, char *argv[])
117{
118 BOOL bStart = TRUE, bForce = FALSE;
119 int ch;
120 TCHAR szConfigFile[MAX_PATH] = DEFAULT_CONFIG_FILE;
121
122 printf("NetXMS Database Manager Version " NETXMS_VERSION_STRING "\n\n");
123
124 // Parse command line
125 opterr = 1;
6ad5eb6d 126 while((ch = getopt(argc, argv, "c:fhvX")) != -1)
fc935a60
VK
127 {
128 switch(ch)
129 {
130 case 'h': // Display help and exit
131 _tprintf(_T("Usage: nxdbmgr [<options>] <command>\n"
132 "Valid commands are:\n"
133 " check : Check database for errors\n"
134 " upgrade : Upgrade database to new version\n"
135 "Valid options are:\n"
37c4d6aa 136 " -c <config> : Use alternate configuration file. Default is " DEFAULT_CONFIG_FILE "\n"
fc935a60
VK
137 " -f : Force repair - do not ask for confirmation.\n"
138 " -h : Display help and exit.\n"
139 " -v : Display version and exit.\n"
6ad5eb6d 140 " -X : Ignore SQL errors when upgrading (USE WITH CARE!!!)\n"
fc935a60
VK
141 "\n"));
142 bStart = FALSE;
143 break;
144 case 'v': // Print version and exit
145 bStart = FALSE;
146 break;
147 case 'c':
148 _tcsncpy(szConfigFile, optarg, MAX_PATH);
149 break;
150 case 'f':
151 bForce = TRUE;
152 break;
6ad5eb6d
VK
153 case 'X':
154 g_bIgnoreErrors = TRUE;
155 break;
fc935a60
VK
156 case '?':
157 bStart = FALSE;
158 break;
159 default:
160 break;
161 }
162 }
163
164 if (!bStart)
165 return 1;
166
167 // Check parameter correctness
168 if (argc - optind == 0)
169 {
170 _tprintf(_T("Command missing. Type nxdbmgr -h for command line syntax.\n"));
171 return 1;
172 }
173 if (strcmp(argv[optind], "check") && strcmp(argv[optind], "upgrade"))
174 {
175 _tprintf(_T("Invalid command \"%s\". Type nxdbmgr -h for command line syntax.\n"), argv[optind]);
176 return 1;
177 }
178
179 // Read configuration file
180 if (NxLoadConfig(szConfigFile, _T(""), m_cfgTemplate, TRUE) != NXCFG_ERR_OK)
181 {
182 _tprintf(_T("Error loading configuration file\n"));
183 return 2;
184 }
185
186 // Connect to database
187 if (!DBInit(FALSE, FALSE))
188 {
189 _tprintf(_T("Unable to load and initialize database driver \"%s\"\n"), g_szDbDriver);
190 return 3;
191 }
192
193 g_hCoreDB = DBConnect();
194 if (g_hCoreDB == NULL)
195 {
196 _tprintf(_T("Unable to connect to database %s@%s as %s\n"), g_szDbName,
197 g_szDbServer, g_szDbLogin);
198 DBUnloadDriver();
199 return 4;
200 }
201
202 // Do requested operation
203 if (!strcmp(argv[optind], "check"))
204 CheckDatabase(bForce);
205 else if (!strcmp(argv[optind], "upgrade"))
206 UpgradeDatabase();
207
208 // Shutdown
209 DBDisconnect(g_hCoreDB);
210 DBUnloadDriver();
211 return 0;
212}