UUID functions renamed to avoid conflict with Mac OS X system functions
[public/netxms.git] / src / server / tools / nxdbmgr / mm.cpp
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004-2011 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: mm.cpp
20 ** Purpose: Map Migration
21 **
22 **/
23
24 #include "nxdbmgr.h"
25 #include <netxms_maps.h>
26
27
28 /**
29 * Generate new object ID
30 *
31 * @return new unique object ID
32 */
33 static DWORD GenerateObjectId()
34 {
35 DB_RESULT hResult = SQLSelect(_T("SELECT max(object_id) FROM object_properties"));
36 if (hResult == NULL)
37 return 0;
38
39 DWORD id = DBGetFieldULong(hResult, 0, 0) + 1;
40 if (id < 10)
41 id = 10;
42 DBFreeResult(hResult);
43 return id;
44 }
45
46 /**
47 * Create new network map object
48 *
49 * @param name map name
50 * @param description map description (will be placed into comments)
51 * @return ID of created network map object or 0 in case of failure
52 */
53 static DWORD CreateMapObject(const TCHAR *name, const TCHAR *description)
54 {
55 TCHAR query[4096];
56
57 DWORD id = GenerateObjectId();
58 if (id == 0)
59 return 0;
60
61 _sntprintf(query, 4096, _T("INSERT INTO network_maps (id,map_type,layout,seed,background) VALUES (%d,0,32767,0,'00000000-0000-0000-0000-000000000000')"), (int)id);
62 if (!SQLQuery(query))
63 return FALSE;
64
65 _sntprintf(query, 4096,
66 _T("INSERT INTO object_properties (object_id,guid,name,")
67 _T("status,is_deleted,is_system,inherit_access_rights,")
68 _T("last_modified,status_calc_alg,status_prop_alg,")
69 _T("status_fixed_val,status_shift,status_translation,")
70 _T("status_single_threshold,status_thresholds,location_type,")
71 _T("latitude,longitude,image,comments) VALUES ")
72 _T("(%d,'%s',%s,0,0,0,1,") TIME_T_FMT _T(",0,0,0,0,0,0,'00000000',0,")
73 _T("'0.000000','0.000000','00000000-0000-0000-0000-000000000000',%s)"),
74 (int)id, (const TCHAR *)uuid::generate().toString(),
75 (const TCHAR *)DBPrepareString(g_hCoreDB, name),
76 TIME_T_FCAST(time(NULL)),
77 (const TCHAR *)DBPrepareString(g_hCoreDB, description, 2048));
78 if (!SQLQuery(query))
79 return FALSE;
80
81 _sntprintf(query, 4096, _T("INSERT INTO container_members (container_id,object_id) VALUES (6,%d)"), (int)id);
82 return SQLQuery(query) ? id : 0;
83 }
84
85 /**
86 * Migrate objects
87 */
88 static BOOL MigrateObjects(DWORD mapId, DWORD submapId, DWORD mapObjectId)
89 {
90 TCHAR query[8192];
91
92 _sntprintf(query, 8192, _T("SELECT object_id,x,y FROM submap_object_positions WHERE map_id=%d AND submap_id=%d"), (int)mapId, (int)submapId);
93 DB_RESULT hResult = SQLSelect(query);
94 if (hResult == NULL)
95 return FALSE;
96
97 BOOL success = FALSE;
98
99 int count = DBGetNumRows(hResult);
100 for(int i = 0; i < count; i++)
101 {
102 DWORD id = DBGetFieldULong(hResult, i, 0);
103
104 NetworkMapObject *object = new NetworkMapObject(id, id);
105 object->setPosition(DBGetFieldLong(hResult, i, 1), DBGetFieldLong(hResult, i, 2));
106
107 Config *config = new Config();
108 config->setTopLevelTag(_T("element"));
109 object->updateConfig(config);
110 String data = DBPrepareString(g_hCoreDB, config->createXml());
111 delete config;
112 delete object;
113
114 _sntprintf(query, 8192, _T("INSERT INTO network_map_elements (map_id,element_id,element_type,element_data) VALUES (%d,%d,1,%s)"),
115 (int)mapObjectId, (int)id, (const TCHAR *)data);
116 if (!SQLQuery(query))
117 goto cleanup;
118 }
119
120 success = TRUE;
121
122 cleanup:
123 DBFreeResult(hResult);
124 return success;
125 }
126
127 /**
128 * Migrate links between objects
129 */
130 static BOOL MigrateLinks(DWORD mapId, DWORD submapId, DWORD mapObjectId)
131 {
132 TCHAR query[8192];
133
134 _sntprintf(query, 8192, _T("SELECT object_id1,object_id2,port1,port2,link_type FROM submap_links WHERE map_id=%d AND submap_id=%d"), (int)mapId, (int)submapId);
135 DB_RESULT hResult = SQLSelect(query);
136 if (hResult == NULL)
137 return FALSE;
138
139 BOOL success = FALSE;
140
141 int count = DBGetNumRows(hResult);
142 for(int i = 0; i < count; i++)
143 {
144 DWORD id1 = DBGetFieldULong(hResult, i, 0);
145 DWORD id2 = DBGetFieldULong(hResult, i, 1);
146 int type = (int)DBGetFieldLong(hResult, i, 4);
147
148 TCHAR cname1[256], cname2[256];
149 DBGetField(hResult, i, 2, cname1, 256);
150 DBGetField(hResult, i, 3, cname2, 256);
151 DecodeSQLString(cname1);
152 DecodeSQLString(cname2);
153
154 _sntprintf(query, 8192, _T("INSERT INTO network_map_links (map_id,element1,element2,link_type,link_name,connector_name1,connector_name2) VALUES (%d,%d,%d,%d,'',%s,%s)"),
155 (int)mapObjectId, (int)id1, (int)id2, type,
156 (const TCHAR *)DBPrepareString(g_hCoreDB, cname1),
157 (const TCHAR *)DBPrepareString(g_hCoreDB, cname2));
158 if (!SQLQuery(query))
159 goto cleanup;
160 }
161
162 success = TRUE;
163
164 cleanup:
165 DBFreeResult(hResult);
166 return success;
167 }
168
169 /**
170 * Migrate 1.0.x maps to 1.1.x network map objects
171 */
172 BOOL MigrateMaps()
173 {
174 DB_RESULT hResult = SQLSelect(_T("SELECT map_id,map_name,description,root_object_id FROM maps"));
175 if (hResult == NULL)
176 return FALSE;
177
178 BOOL success = FALSE;
179 int count = DBGetNumRows(hResult);
180 for(int i = 0; i < count; i++)
181 {
182 TCHAR name[256], description[2048];
183
184 DBGetField(hResult, i, 1, name, 256);
185 DecodeSQLString(name);
186 DBGetField(hResult, i, 2, description, 2048);
187 DecodeSQLString(description);
188 DWORD mapObjectId = CreateMapObject(name, description);
189 if (mapObjectId == 0)
190 goto cleanup;
191
192 DWORD mapId = DBGetFieldULong(hResult, i, 0);
193 DWORD submapId = DBGetFieldULong(hResult, i, 3);
194 if (!MigrateObjects(mapId, submapId, mapObjectId))
195 goto cleanup;
196 if (!MigrateLinks(mapId, submapId, mapObjectId))
197 goto cleanup;
198 }
199
200 success = TRUE;
201
202 cleanup:
203 DBFreeResult(hResult);
204 return success;
205 }