2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004-2010 Victor Kirhenshtein
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.
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.
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.
29 extern const TCHAR
*g_tables
[];
32 * Callback for import table
34 static int ImportTableCB(void *arg
, int cols
, char **data
, char **names
)
39 query
.appendFormattedString(_T("INSERT INTO %s ("), arg
);
40 for(i
= 0; i
< cols
; i
++)
42 query
.appendMBString(names
[i
], strlen(names
[i
]), CP_UTF8
);
46 query
+= _T(") VALUES (");
47 for(i
= 0; i
< cols
; i
++)
52 WCHAR
*wcData
= WideStringFromUTF8String(data
[i
]);
53 String prepData
= DBPrepareString(g_hCoreDB
, wcData
);
56 String prepData
= DBPrepareString(g_hCoreDB
, data
[i
]);
58 query
+= (const TCHAR
*)prepData
;
69 return SQLQuery(query
) ? 0 : 1;
73 * Import single database table
75 static BOOL
ImportTable(sqlite3
*db
, const TCHAR
*table
)
77 char query
[256], *errmsg
;
80 _tprintf(_T("Importing table %s\n"), table
);
82 if (DBBegin(g_hCoreDB
))
85 char *mbTable
= MBStringFromWideString(table
);
86 snprintf(query
, 256, "SELECT * FROM %s", mbTable
);
89 snprintf(query
, 256, "SELECT * FROM %s", table
);
91 rc
= sqlite3_exec(db
, query
, ImportTableCB
, (void *)table
, &errmsg
);
98 _tprintf(_T("ERROR: SQL query \"%hs\" on import file failed (%hs)\n"), query
, errmsg
);
100 DBRollback(g_hCoreDB
);
105 _tprintf(_T("ERROR: unable to start transaction in target database\n"));
108 return rc
== SQLITE_OK
;
114 static BOOL
ImportDataTables(sqlite3
*db
)
119 DB_RESULT hResult
= SQLSelect(_T("SELECT id FROM nodes"));
123 // Create and import idata_xx tables for each node in "nodes" table
124 count
= DBGetNumRows(hResult
);
125 for(i
= 0; i
< count
; i
++)
127 DWORD id
= DBGetFieldULong(hResult
, i
, 0);
128 if (!CreateIDataTable(id
))
129 break; // Failed to create idata_xx table
131 _sntprintf(buffer
, 1024, _T("idata_%d"), id
);
132 if (!ImportTable(db
, buffer
))
135 if (!CreateTDataTable(id
))
136 break; // Failed to create tdata tables
138 _sntprintf(buffer
, 1024, _T("tdata_%d"), id
);
139 if (!ImportTable(db
, buffer
))
143 DBFreeResult(hResult
);
148 * Callback for getting schema version
150 static int GetSchemaVersionCB(void *arg
, int cols
, char **data
, char **names
)
152 *((int *)arg
) = strtol(data
[0], NULL
, 10);
159 void ImportDatabase(const char *file
)
164 BOOL success
= FALSE
;
166 // Open SQLite database
167 if (sqlite3_open(file
, &db
) != SQLITE_OK
)
169 _tprintf(_T("ERROR: unable to open output file\nDatabase import failed.\n"));
173 // Check schema version
175 if (sqlite3_exec(db
, "SELECT var_value FROM metadata WHERE var_name='SchemaVersion'", GetSchemaVersionCB
, &version
, &errmsg
) != SQLITE_OK
)
177 _tprintf(_T("ERROR: SQL query failed (%hs)\n"), errmsg
);
178 sqlite3_free(errmsg
);
182 if (version
!= DB_FORMAT_VERSION
)
184 _tprintf(_T("ERROR: Import file was created for database format version %d, but this tool was compiled for database format version %d\n"), version
, DB_FORMAT_VERSION
);
188 if (!ClearDatabase(false))
192 for(i
= 0; g_tables
[i
] != NULL
; i
++)
194 if (!ImportTable(db
, g_tables
[i
]))
197 if (!ImportDataTables(db
))
204 _tprintf(success
? _T("Database import complete.\n") : _T("Database import failed.\n"));