schema-related information (like schema version, DB syntax, etc.) moved from "config...
[public/netxms.git] / src / server / tools / nxdbmgr / import.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: import.cpp
20**
21**/
22
23#include "nxdbmgr.h"
24#include "sqlite3.h"
25
26
28f5b9a4
VK
27//
28// Tables to import
29//
30
31extern TCHAR *g_tables[];
32
33
34//
35// Callback for import table
36//
37
38static int ImportTableCB(void *arg, int cols, char **data, char **names)
39{
40 String query;
41 int i;
42
43 query.AddFormattedString("INSERT INTO %s (", arg);
44 for(i = 0; i < cols; i++)
45 {
46 query += names[i];
47 query += ",";
48 }
49 query.Shrink();
50 query += ") VALUES (";
51 for(i = 0; i < cols; i++)
52 query.AddFormattedString("'%s',", data[i]);
53 query.Shrink();
54 query += ")";
55
56 return SQLQuery(query) ? 0 : 1;
57}
58
59
60//
61// Import single database table
62//
63
64static BOOL ImportTable(sqlite3 *db, const char *table)
65{
66 char query[256], *errmsg;
67 int rc;
68
69 snprintf(query, 256, "SELECT * FROM %s", table);
70 rc = sqlite3_exec(db, query, ImportTableCB, (void *)table, &errmsg);
71 if (rc != SQLITE_OK)
72 {
73 printf("ERROR: SQL query \"%s\" on import file failed (%s)\n", query, errmsg);
74 sqlite3_free(errmsg);
75 }
76 return rc == SQLITE_OK;
77}
78
79
80//
81// Import idata_xx tables
82//
83
84static BOOL ImportIData(sqlite3 *db)
85{
86 DB_RESULT hResult;
87 int i, count;
88 TCHAR buffer[256];
89
90 hResult = SQLSelect(_T("SELECT id FROM nodes"));
91 if (hResult == NULL)
92 return FALSE;
93
94 count = DBGetNumRows(hResult);
95 for(i = 0; i < count; i++)
96 {
97 _sntprintf(buffer, 256, _T("idata_%d"), DBGetFieldULong(hResult, i, 0));
98 if (!ImportTable(db, buffer))
99 break;
100 }
101
102 DBFreeResult(hResult);
103 return i == count;
104}
105
106
107//
108// Callback for getting schema version
109//
110
111static int GetSchemaVersionCB(void *arg, int cols, char **data, char **names)
112{
113 *((int *)arg) = strtol(data[0], NULL, 10);
114 return 0;
115}
116
117
890a0930
VK
118//
119// Import database
120//
121
122void ImportDatabase(const char *file)
123{
28f5b9a4
VK
124 sqlite3 *db;
125 char *errmsg;
126 int i;
127
128 // Open SQLite database
129 if (sqlite3_open(file, &db) != SQLITE_OK)
130 {
131 printf("ERROR: unable to open output file\n");
890a0930 132 return;
28f5b9a4
VK
133 }
134
135 // Check schema version
136 int version = 0;
137 if (sqlite3_exec(db, "SELECT var_value FROM metadata WHERE var_name='SchemaVersion'", GetSchemaVersionCB, &version, &errmsg) != SQLITE_OK)
138 {
139 printf("ERROR: SQL query failed (%s)\n", errmsg);
140 sqlite3_free(errmsg);
141 goto cleanup;
142 }
143
144 if (version != DB_FORMAT_VERSION)
145 {
146 printf("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);
147 goto cleanup;
148 }
149
150 if (!ClearDatabase())
151 goto cleanup;
152
153 // Import tables
154 for(i = 0; g_tables[i] != NULL; i++)
155 {
156 if (!ImportTable(db, g_tables[i]))
157 goto cleanup;
158 }
159 ImportIData(db);
160
161cleanup:
162 sqlite3_close(db);
890a0930 163}