Started work on database manager utility
[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;
31
32
33//
34// Static data
35//
36
37static NX_CFG_TEMPLATE m_cfgTemplate[] =
38{
39 { "DBDriver", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDriver },
40 { "DBDrvParams", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDrvParams },
41 { "DBLogin", CT_STRING, 0, 0, MAX_DB_LOGIN, 0, g_szDbLogin },
42 { "DBName", CT_STRING, 0, 0, MAX_DB_NAME, 0, g_szDbName },
43 { "DBPassword", CT_STRING, 0, 0, MAX_DB_PASSWORD, 0, g_szDbPassword },
44 { "DBServer", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbServer },
45 { "LogFailedSQLQueries", CT_IGNORE, 0, 0, 0, 0, NULL },
46 { "LogFile", CT_IGNORE, 0, 0, 0, 0, NULL },
47 { "", CT_END_OF_LIST, 0, 0, 0, 0, NULL }
48};
49
50
51//
52// Startup
53//
54
55int main(int argc, char *argv[])
56{
57 BOOL bStart = TRUE, bForce = FALSE;
58 int ch;
59 TCHAR szConfigFile[MAX_PATH] = DEFAULT_CONFIG_FILE;
60
61 printf("NetXMS Database Manager Version " NETXMS_VERSION_STRING "\n\n");
62
63 // Parse command line
64 opterr = 1;
65 while((ch = getopt(argc, argv, "c:fhv")) != -1)
66 {
67 switch(ch)
68 {
69 case 'h': // Display help and exit
70 _tprintf(_T("Usage: nxdbmgr [<options>] <command>\n"
71 "Valid commands are:\n"
72 " check : Check database for errors\n"
73 " upgrade : Upgrade database to new version\n"
74 "Valid options are:\n"
75 " -c <config> : Use alternate configuration file. Default is" DEFAULT_CONFIG_FILE "\n"
76 " -f : Force repair - do not ask for confirmation.\n"
77 " -h : Display help and exit.\n"
78 " -v : Display version and exit.\n"
79 "\n"));
80 bStart = FALSE;
81 break;
82 case 'v': // Print version and exit
83 bStart = FALSE;
84 break;
85 case 'c':
86 _tcsncpy(szConfigFile, optarg, MAX_PATH);
87 break;
88 case 'f':
89 bForce = TRUE;
90 break;
91 case '?':
92 bStart = FALSE;
93 break;
94 default:
95 break;
96 }
97 }
98
99 if (!bStart)
100 return 1;
101
102 // Check parameter correctness
103 if (argc - optind == 0)
104 {
105 _tprintf(_T("Command missing. Type nxdbmgr -h for command line syntax.\n"));
106 return 1;
107 }
108 if (strcmp(argv[optind], "check") && strcmp(argv[optind], "upgrade"))
109 {
110 _tprintf(_T("Invalid command \"%s\". Type nxdbmgr -h for command line syntax.\n"), argv[optind]);
111 return 1;
112 }
113
114 // Read configuration file
115 if (NxLoadConfig(szConfigFile, _T(""), m_cfgTemplate, TRUE) != NXCFG_ERR_OK)
116 {
117 _tprintf(_T("Error loading configuration file\n"));
118 return 2;
119 }
120
121 // Connect to database
122 if (!DBInit(FALSE, FALSE))
123 {
124 _tprintf(_T("Unable to load and initialize database driver \"%s\"\n"), g_szDbDriver);
125 return 3;
126 }
127
128 g_hCoreDB = DBConnect();
129 if (g_hCoreDB == NULL)
130 {
131 _tprintf(_T("Unable to connect to database %s@%s as %s\n"), g_szDbName,
132 g_szDbServer, g_szDbLogin);
133 DBUnloadDriver();
134 return 4;
135 }
136
137 // Do requested operation
138 if (!strcmp(argv[optind], "check"))
139 CheckDatabase(bForce);
140 else if (!strcmp(argv[optind], "upgrade"))
141 UpgradeDatabase();
142
143 // Shutdown
144 DBDisconnect(g_hCoreDB);
145 DBUnloadDriver();
146 return 0;
147}