schema-related information (like schema version, DB syntax, etc.) moved from "config...
[public/netxms.git] / src / server / tools / nxdbmgr / clear.cpp
CommitLineData
890a0930
VK
1/*
2** nxdbmgr - NetXMS database manager
3** Copyright (C) 2004-2009 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** File: clear.cpp
20**
21**/
22
23#include "nxdbmgr.h"
24
25
28f5b9a4
VK
26//
27// Tables to clear
28//
29
30extern TCHAR *g_tables[];
31
32
33//
34// Delete idata_xx tables
35//
36
37static BOOL DeleteIData()
38{
39 DB_RESULT hResult;
40 TCHAR query[256];
41 int i, count;
42
43 hResult = SQLSelect(_T("SELECT id FROM nodes"));
44 if (hResult != NULL)
45 {
46 count = DBGetNumRows(hResult);
47 for(i = 0; i < count; i++)
48 {
49 _sntprintf(query, 256, _T("DROP TABLE idata_%d"), DBGetFieldULong(hResult, i, 0));
50 if (!SQLQuery(query))
51 if (!g_bIgnoreErrors)
52 return FALSE;
53 }
54 DBFreeResult(hResult);
55 }
56 else
57 {
58 if (!g_bIgnoreErrors)
59 return FALSE;
60 }
61
62 return TRUE;
63}
64
65
66//
67// Clear tables
68//
69
70static BOOL ClearTables()
71{
72 TCHAR query[256];
73 int i;
74
75 for(i = 0; g_tables[i] != NULL; i++)
76 {
77 _sntprintf(query, 256, _T("DELETE FROM %s"), g_tables[i]);
78 if (!SQLQuery(query))
79 if (!g_bIgnoreErrors)
80 return FALSE;
81 }
82 return TRUE;
83}
84
85
890a0930
VK
86//
87// Clear database
88//
89
90BOOL ClearDatabase()
91{
28f5b9a4
VK
92 if (!ValidateDatabase())
93 return FALSE;
94
95 printf("\n\nWARNING!!!\nThis operation will clear all configuration and collected data from database.\nAre you sure (y/N)?");
890a0930
VK
96 if (!GetYesNo())
97 return FALSE;
98
28f5b9a4
VK
99 BOOL success = FALSE;
100
101 if (DBBegin(g_hCoreDB))
102 {
103 if (DeleteIData() && ClearTables())
104 {
105 success = DBCommit(g_hCoreDB);
106 printf(success ? "Database successfully cleared\n" : "ERROR: cannot commit transaction\n");
107 }
108 else
109 {
110 DBRollback(g_hCoreDB);
111 }
112 }
113 else
114 {
115 printf("ERROR: cannot start transaction\n");
116 }
117
118 return success;
890a0930 119}