"Fossies" - the Fresh Open Source Software Archive 
Member "netxms-3.8.166/src/server/tools/nxdbmgr/clear.cpp" (23 Feb 2021, 4011 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "clear.cpp" see the
Fossies "Dox" file reference documentation.
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004-2019 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 /**
26 * Tables to clear
27 */
28 extern const TCHAR *g_tables[];
29
30 /**
31 * Delete idata_xx tables
32 */
33 static bool DeleteDataTables()
34 {
35 DB_RESULT hResult;
36 TCHAR query[256];
37 int i, count;
38
39 hResult = SQLSelect(_T("SELECT id FROM nodes"));
40 if (hResult != NULL)
41 {
42 count = DBGetNumRows(hResult);
43 for(i = 0; i < count; i++)
44 {
45 UINT32 id = DBGetFieldULong(hResult, i, 0);
46 if (IsDataTableExist(_T("idata_%d"), id))
47 {
48 _sntprintf(query, 256, _T("DROP TABLE idata_%d"), id);
49 CHK_EXEC(SQLQuery(query));
50 }
51 if (IsDataTableExist(_T("tdata_%d"), id))
52 {
53 _sntprintf(query, 256, _T("DROP TABLE tdata_%d"), id);
54 CHK_EXEC(SQLQuery(query));
55 }
56 }
57 DBFreeResult(hResult);
58 }
59 else
60 {
61 if (!g_ignoreErrors)
62 return false;
63 }
64
65 return true;
66 }
67
68 /**
69 * Clear table
70 */
71 static bool ClearTable(const TCHAR *table, void *userData)
72 {
73 TCHAR query[256];
74 if ((g_dbSyntax == DB_SYNTAX_ORACLE) || (g_dbSyntax == DB_SYNTAX_PGSQL) || (g_dbSyntax == DB_SYNTAX_TSDB))
75 _sntprintf(query, 256, _T("TRUNCATE TABLE %s"), table);
76 else
77 _sntprintf(query, 256, _T("DELETE FROM %s"), table);
78 return SQLQuery(query);
79 }
80
81 /**
82 * Clear tables
83 */
84 static bool ClearTables()
85 {
86 for(int i = 0; g_tables[i] != NULL; i++)
87 {
88 if ((g_dbSyntax == DB_SYNTAX_TSDB) && (!_tcsicmp(g_tables[i], _T("idata")) || !_tcsicmp(g_tables[i], _T("tdata"))))
89 continue; // idata and tdata are views in TSDB schema
90 if ((g_dbSyntax != DB_SYNTAX_TSDB) && (!_tcsnicmp(g_tables[i], _T("idata_sc_"), 9) || !_tcsnicmp(g_tables[i], _T("tdata_sc_"), 9)))
91 continue; // tables named idata_sc_* and tdata_sc_* exist only in TSDB schema
92 CHK_EXEC(ClearTable(g_tables[i], NULL));
93 }
94 return EnumerateModuleTables(ClearTable, NULL);
95 }
96
97 /**
98 * Warning texts
99 */
100 static const TCHAR *s_warningTextClear = _T("This operation will clear all configuration and collected data from database.\nAre you sure?");
101 static const TCHAR *s_warningTextMigration = _T("This operation will clear all configuration and collected data from destination database before migration.\nAre you sure?");
102 static const TCHAR *s_warningTextMigrationNoData = _T("This operation will clear all configuration from destination database before migration (collected data will be kept).\nAre you sure?");
103
104 /**
105 * Clear database
106 */
107 bool ClearDatabase(bool preMigration)
108 {
109 if (!ValidateDatabase())
110 return false;
111
112 WriteToTerminal(_T("\n\n\x1b[1mWARNING!!!\x1b[0m\n"));
113 if (!GetYesNo(preMigration ? (g_skipDataSchemaMigration ? s_warningTextMigrationNoData : s_warningTextMigration) : s_warningTextClear))
114 return false;
115
116 bool success = false;
117
118 if (DBBegin(g_dbHandle))
119 {
120 if (!g_skipDataSchemaMigration)
121 success = DeleteDataTables();
122 else
123 success = true;
124
125 if (success)
126 success = ClearTables();
127
128 if (success)
129 {
130 success = DBCommit(g_dbHandle);
131 _tprintf(success ? _T("Database successfully cleared\n") : _T("ERROR: cannot commit transaction\n"));
132 }
133 else
134 {
135 DBRollback(g_dbHandle);
136 }
137 }
138 else
139 {
140 _tprintf(_T("ERROR: cannot start transaction\n"));
141 }
142
143 return success;
144 }