/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/sql_dbcreator.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/bnetd/sql_dbcreator.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: pvpgn_1-7-4-0_MIL, arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17 #include "common/setup_before.h"
18 #ifdef WITH_SQL
19 #include <stdio.h>
20
21 #ifdef STDC_HEADERS
22 # include <stdlib.h>
23 #else
24 # ifdef HAVE_MALLOC_H
25 # include <malloc.h>
26 # endif
27 #endif
28
29 #ifdef HAVE_STRING_H
30 # include <string.h>
31 #else
32 # ifdef HAVE_STRINGS_H
33 # include <strings.h>
34 # endif
35 #endif
36
37 #define SQL_DBCREATOR_INTERNAL_ACCESS
38 #include "sql_dbcreator.h"
39 #undef SQL_DBCREATOR_INTERNAL_ACCESS
40 #include "storage_sql.h"
41
42 #include "common/eventlog.h"
43 #include "common/util.h"
44
45 #include "compat/strdup.h"
46 #include "common/list.h"
47 #include "common/xalloc.h"
48 #include "prefs.h"
49 #include "common/setup_after.h"
50
51 t_elem * curr_table = NULL;
52 t_elem * curr_column = NULL;
53
54 t_db_layout * db_layout;
55
56 t_column * create_column(char * name, char * value)
57 {
58 t_column * column;
59
60 if (!(name))
61 {
62 eventlog(eventlog_level_error,__FUNCTION__,"got NULL column name");
63 return NULL;
64 }
65
66 if (!(value))
67 {
68 eventlog(eventlog_level_error,__FUNCTION__,"got NULL column value");
69 return NULL;
70 }
71
72 column = xmalloc(sizeof(t_column));
73 column->name = xstrdup(name);
74 column->value = xstrdup(value);
75
76 return column;
77 };
78
79 void dispose_column(t_column * column)
80 {
81 if (column)
82 {
83 if (column->name) xfree((void *)column->name);
84 if (column->value) xfree((void *)column->value);
85 xfree((void *)column);
86 }
87 }
88
89 t_table * create_table(char * name)
90 {
91 t_table * table;
92
93 if (!(name))
94 {
95 eventlog(eventlog_level_error,__FUNCTION__,"got NULL table name");
96 return NULL;
97 }
98
99 table = xmalloc(sizeof(t_table));
100 table->name = xstrdup(name);
101
102 table->columns = list_create();
103
104 return table;
105 }
106
107 void dispose_table(t_table * table)
108 {
109 t_elem * curr;
110 t_column * column;
111
112 if (table)
113 {
114 if (table->name) xfree((void *)table->name);
115 // free list
116 if (table->columns)
117 {
118 LIST_TRAVERSE(table->columns,curr)
119 {
120 if (!(column = elem_get_data(curr)))
121 {
122 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
123 continue;
124 }
125 dispose_column(column);
126 list_remove_elem(table->columns,&curr);
127 }
128
129 list_destroy(table->columns);
130
131 }
132
133 xfree((void *)table);
134 }
135 }
136
137 void table_add_column(t_table * table, t_column * column)
138 {
139 if ((table) && (column))
140 {
141 list_append_data(table->columns,column);
142 }
143 }
144
145 t_db_layout * create_db_layout()
146 {
147 t_db_layout * db_layout;
148
149 db_layout = xmalloc(sizeof(t_db_layout));
150
151 db_layout->tables = list_create();
152
153 return db_layout;
154 }
155
156 void dispose_db_layout(t_db_layout * db_layout)
157 {
158 t_elem * curr;
159 t_table * table;
160
161
162 if (db_layout)
163 {
164 if (db_layout->tables)
165 {
166 LIST_TRAVERSE(db_layout->tables,curr)
167 {
168 if (!(table = elem_get_data(curr)))
169 {
170 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
171 continue;
172 }
173 dispose_table(table);
174 list_remove_elem(db_layout->tables,&curr);
175 }
176 list_destroy(db_layout->tables);
177 }
178 xfree((void *)db_layout);
179 }
180
181 }
182
183 void db_layout_add_table(t_db_layout * db_layout, t_table * table)
184 {
185 if ((db_layout) && (table))
186 {
187 list_append_data(db_layout->tables,table);
188 }
189 }
190
191 t_table * db_layout_get_first_table(t_db_layout * db_layout)
192 {
193 t_table * table;
194
195 curr_column = NULL;
196
197 if (!(db_layout))
198 {
199 eventlog(eventlog_level_error,__FUNCTION__,"got NULL db_layout");
200 return NULL;
201 }
202
203 if (!(db_layout->tables))
204 {
205 eventlog(eventlog_level_error,__FUNCTION__,"found NULL db_layout->tables");
206 return NULL;
207 }
208
209 if (!(curr_table = list_get_first(db_layout->tables)))
210 {
211 eventlog(eventlog_level_error,__FUNCTION__,"db_layout has no tables");
212 return NULL;
213 }
214
215 if (!(table = elem_get_data(curr_table)))
216 {
217 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem in list");
218 return NULL;
219 }
220
221 return table;
222 }
223
224 t_table * db_layout_get_next_table(t_db_layout * db_layout)
225 {
226 t_table * table;
227
228 curr_column = NULL;
229
230 if (!(curr_table))
231 {
232 eventlog(eventlog_level_error,__FUNCTION__,"got NULL curr_table");
233 return NULL;
234 }
235
236 if (!(curr_table = elem_get_next(db_layout->tables, curr_table))) return NULL;
237
238 if (!(table = elem_get_data(curr_table)))
239 {
240 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem in list");
241 return NULL;
242 }
243
244 return table;
245 }
246
247 t_column * table_get_first_column(t_table * table)
248 {
249 t_column * column;
250
251 if (!(table))
252 {
253 eventlog(eventlog_level_error,__FUNCTION__,"got NULL table");
254 return NULL;
255 }
256
257 if (!(table->columns))
258 {
259 eventlog(eventlog_level_error,__FUNCTION__,"got NULL table->columns");
260 return NULL;
261 }
262
263 if (!(curr_column = list_get_first(table->columns)))
264 {
265 eventlog(eventlog_level_error,__FUNCTION__,"table has no columns");
266 return NULL;
267 }
268
269 if (!(column = elem_get_data(curr_column)))
270 {
271 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem in list");
272 return NULL;
273 }
274
275 return column;
276 }
277
278 t_column * table_get_next_column(t_table * table)
279 {
280 t_column * column;
281
282 if (!(curr_column))
283 {
284 eventlog(eventlog_level_error,__FUNCTION__,"got NULL curr_column");
285 return NULL;
286 }
287
288 if (!(curr_column = elem_get_next(table->columns, curr_column))) return NULL;
289
290 if (!(column = elem_get_data(curr_column)))
291 {
292 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem in list");
293 return NULL;
294 }
295
296 return column;
297 }
298
299 int load_db_layout(char const * filename)
300 {
301 FILE * fp;
302 int lineno;
303 char * line = NULL;
304 char * tmp = NULL;
305 char * table = NULL;
306 char * column = NULL;
307 char * value = NULL;
308 t_table * _table = NULL;
309 t_column * _column = NULL;
310
311 if (!(filename))
312 {
313 eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
314 return -1;
315 }
316
317 if (!(fp = fopen(filename,"r")))
318 {
319 eventlog(eventlog_level_error,__FUNCTION__,"can't open sql_DB_layout file");
320 return -1;
321 }
322
323 if (!(db_layout = create_db_layout()))
324 {
325 eventlog(eventlog_level_error,__FUNCTION__,"failed to create db_layout");
326 fclose(fp);
327 return -1;
328 }
329
330 for (lineno=1; (line = file_get_line(fp)) ; lineno++)
331 {
332 switch (line[0])
333 {
334 case '[':
335 table = &line[1];
336 if (!(tmp = strchr(table,']')))
337 {
338 eventlog(eventlog_level_error,__FUNCTION__,"missing ']' in line %i",lineno);
339 continue;
340 }
341 tmp[0]='\0';
342 if (_table) db_layout_add_table(db_layout,_table);
343
344 _table = create_table(table);
345
346 break;
347 case '"':
348 if (!(_table))
349 {
350 eventlog(eventlog_level_error,__FUNCTION__,"found a column without previous table in line %i",lineno);
351 continue;
352 }
353 column = &line[1];
354 if (!(tmp = strchr(column,'"')))
355 {
356 eventlog(eventlog_level_error,__FUNCTION__,"missing '\"' at the end of column definitioni in line %i",lineno);
357 continue;
358 }
359 tmp[0]='\0';
360 tmp++;
361 if (!(tmp = strchr(tmp,'"')))
362 {
363 eventlog(eventlog_level_error,__FUNCTION__,"missing default value in line %i",lineno);
364 continue;
365 }
366 value = ++tmp;
367 if (!(tmp = strchr(value,'"')))
368 {
369 eventlog(eventlog_level_error,__FUNCTION__,"missing '\"' at the end of default value in line %i",lineno);
370 continue;
371 }
372 tmp[0]='\0';
373 _column = create_column(column,value);
374 table_add_column(_table,_column);
375 _column = NULL;
376
377 break;
378 case '#':
379 break;
380 default:
381 eventlog(eventlog_level_error,__FUNCTION__,"illegal starting symbol at line %i",lineno);
382 }
383 }
384 if (_table) db_layout_add_table(db_layout,_table);
385
386 file_get_line(NULL); // clear file_get_line buffer
387 fclose(fp);
388 return 0;
389 }
390
391 int sql_dbcreator(t_sql_engine * sql)
392 {
393 t_table * table;
394 t_column * column;
395 char _column[1024];
396 char query[1024];
397
398 load_db_layout(prefs_get_DBlayoutfile());
399
400 eventlog(eventlog_level_info, __FUNCTION__,"Creating missing tables and columns (if any)");
401
402 for (table = db_layout_get_first_table(db_layout);table;table = db_layout_get_next_table(db_layout))
403 {
404 column = table_get_first_column(table);
405 sprintf(query,"CREATE TABLE %s (%s default %s)",table->name,column->name,column->value);
406 //create table if missing
407 if (!(sql->query(query)))
408 {
409 eventlog(eventlog_level_info,__FUNCTION__,"added missing table %s to DB",table->name);
410 eventlog(eventlog_level_info,__FUNCTION__,"added missing column %s to table %s",column->name,table->name);
411 }
412
413 for (;column;column = table_get_next_column(table))
414 {
415 sprintf(query,"ALTER TABLE %s ADD %s",table->name,column->name);
416 // add missing columns
417 if (!(sql->query(query)))
418 {
419 eventlog(eventlog_level_info,__FUNCTION__,"added missing column %s to table %s",column->name,table->name);
420 sscanf(column->name,"%s",_column); //get column name without format infos
421 sprintf(query,"ALTER TABLE %s ALTER %s SET DEFAULT %s",table->name,_column,column->value);
422 sql->query(query);
423 }
424 }
425
426 column = table_get_first_column(table);
427 sscanf(column->name,"%s",_column); //get column name without format infos
428 sprintf(query,"INSERT INTO %s (%s) VALUES (%s)",table->name,_column,column->value);
429 if (!(sql->query(query)))
430 {
431 eventlog(eventlog_level_info,__FUNCTION__,"added missing default account to table %s",table->name);
432 }
433
434 }
435
436 dispose_db_layout(db_layout);
437
438 eventlog(eventlog_level_info,__FUNCTION__,"finished adding missing tables and columns");
439 return 0;
440 }
441
442 #endif /* WITH_SQL */

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1