Repository navigation
Expand file tree
/
Copy pathmake_models.cpp
More file actions
586 lines (505 loc) · 22.4 KB
/
Copy pathmake_models.cpp
File metadata and controls
586 lines (505 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include <map>
#include <string>
#include <vector>
#include <mysql.h>
/////////////////////////////
std::string database = "gcsd000a10a_e"; // better be able to log in as root with no password
/////////////////////////////
std::vector<std::string> only_tables;
std::map<std::string, std::string> pluralize_exceptions;
void populate_only_tables()
{
// if no tables are provided, all tables are done
if (database == "lcbo")
{
only_tables.push_back("stores");
only_tables.push_back("products");
}
if (database == "ttc")
{
only_tables.push_back("routes");
only_tables.push_back("branches");
only_tables.push_back("agency");
only_tables.push_back("stops");
only_tables.push_back("locations");
only_tables.push_back("shapes");
only_tables.push_back("shape_stops");
only_tables.push_back("shape_points");
only_tables.push_back("schedules");
only_tables.push_back("services");
only_tables.push_back("service_exceptions");
only_tables.push_back("vehicle_types");
only_tables.push_back("station_edges");
}
if (database == "gcsd000a10a_e")
{
only_tables.push_back("DBF");
}
}
void populate_pluralize_exceptions()
{
pluralize_exceptions["branch"] = "branches";
pluralize_exceptions["vertex"] = "vertices";
}
/////////////////////////////
std::string location_of_mysql_h = "/usr/local/mysql/include/mysql";
std::string location_of_libmysqlclient = "/usr/local/mysql/lib/mysql";
/////////////////////////////
MYSQL mysql;
MYSQL_ROW row;
MYSQL_RES * res;
char query[500];
int num_rows = 0;
FILE *fp;
struct sField {
std::string name;
std::string type;
bool null;
int size;
};
enum ASSOC_TYPE { NONE, HAS_ONE, HAS_MANY };
struct sAssoc {
std::string field_name;
std::string table_name;
ASSOC_TYPE type;
};
struct sTable {
std::string name;
std::vector<sField> fields;
std::vector<sAssoc> assocs;
};
std::vector<std::string> table_names;
std::map<std::string, sTable> tables;
/*std::string getTableNameFromClassName(std::string val) // translates User => users, Stop => stops
{
std::string cc = val;
cc[0] = tolower(cc[0]);
if (cc.size() > 2 && cc[cc.size()-1] != 's') cc.push_back('s');
for (int i = 0 ; i < (int)cc.size() ; i++)
{
if (cc[i] >= 65 && cc[i] <= 90)
{
cc[i] = tolower(cc[i]);
cc.insert(i, "_");
}
}
return cc;
}*/
std::string getClassNameFromTableName(std::string val) // translates users => User, stops => Stop
{
std::string cc = val;
cc[0] = toupper(cc[0]);
if (cc.size() > 2 && cc[cc.size()-1] == 's' && cc[cc.size()-2] != 's') cc.resize(cc.size()-1);
for (std::map<std::string, std::string>::iterator it = pluralize_exceptions.begin() ; it != pluralize_exceptions.end() ; it++)
{
if (val == it->second)
{
cc = it->first;
cc[0] = toupper(cc[0]);
break;
}
}
for (int i = 0 ; i < (int)cc.size() ; i++)
{
if (cc[i] == '_')
{
cc.erase(i, 1);
cc[i] = toupper(cc[i]);
}
}
return cc;
}
int invalid_table(std::string val)
{
if (only_tables.size() == 0) return 0;
for (std::vector<std::string>::iterator it = only_tables.begin() ; it != only_tables.end() ; it++)
if (*it == val) return 0;
return 1;
}
std::string pluralize(std::string val)
{
if (pluralize_exceptions.find(val) != pluralize_exceptions.end()) return pluralize_exceptions[val];
if (val.size() > 2 && val[val.size()-1] != 's') val.push_back('s');
return val;
}
/*std::string singularize(std::string val)
{
for (std::map<std::string, std::string>::iterator it = pluralize_exceptions.begin() ; it != pluralize_exceptions.end() ; it++)
{
if (val == it->second)
{
cc = it->first;
cc[0] = toupper(cc[0]);
break;
}
}
}*/
int main(int argc, char **argv, char **envp)
{
populate_only_tables();
populate_pluralize_exceptions();
if ((mysql_init(&mysql) == NULL))
return printf("mysql_init error\n");
if (!mysql_real_connect(&mysql, "localhost", "root", "", database.c_str(), 0, NULL, 0))
return printf("mysql_real_connect error (%s)\n", mysql_error(&mysql));
if (database == "")
{
printf("You need to pick a database:\n");
if (mysql_query(&mysql, "SHOW DATABASES")!=0) { printf("mysql_query error (%s)\n", mysql_error(&mysql)); return 0; }
res = mysql_store_result(&mysql);
num_rows = mysql_num_rows(res);
printf("%d databases\n", num_rows);
while ((row = mysql_fetch_row(res)))
printf(" %s \n", row[0]);
mysql_free_result(res);
return 0;
}
if (mysql_query(&mysql, "SHOW TABLES")!=0) { printf("mysql_query error (%s)\n", mysql_error(&mysql)); return 0; }
res = mysql_store_result(&mysql);
num_rows = mysql_num_rows(res);
printf("%d tables in '%s'\n", num_rows, database.c_str());
sprintf(query, "mkdir -p %s_models ; rm %s_models/*", database.c_str(), database.c_str());
printf("%s\n", query);
system(query);
while ((row = mysql_fetch_row(res)))
{
tables[row[0]].name = row[0];
table_names.push_back(row[0]);
}
mysql_free_result(res);
for (std::vector<std::string>::iterator it = table_names.begin() ; it != table_names.end() ; it++)
{
std::string table_name = (*it).c_str();
if (invalid_table(table_name)) continue;
std::string class_name = getClassNameFromTableName(table_name);
sprintf(query, "DESCRIBE %s", table_name.c_str());
if (mysql_query(&mysql, query)!=0) { printf("mysql_query error (%s)\n", mysql_error(&mysql)); return 0; }
res = mysql_store_result(&mysql);
while ((row = mysql_fetch_row(res)))
{
if (strcmp(row[0],"id")==0) continue;
sField field;
field.name = row[0];
field.null = strcmp(row[2],"YES")==0;
if (row[1][0] == 'i')
{
field.type = "int";
field.size = 0;
}
else if (row[1][0] == 'f')
{
field.type = "float";
field.size = 0;
}
else if (row[1][0] == 'v' && strlen(row[1]) > 8)
{
field.type = "char";
field.size = atoi(&row[1][8]);
}
else if (strcmp(row[1],"text")==0)
{
field.type = "char";
field.size = 0; // will find max(length(.)) later, can't do it here
}
else if (strcmp(row[1],"datetime")==0)
{
field.type = "char";
field.size = 20;
}
else
{
field.type = "char";
field.size = 50;
}
tables[(*it).c_str()].fields.push_back(field);
if (field.name.size() > 4 && field.name[field.name.size()-3] == '_' && field.name[field.name.size()-2] == 'i' && field.name[field.name.size()-1] == 'd')
{
std::string join_table_name = field.name;
join_table_name.resize(join_table_name.size() - 3);
join_table_name = pluralize(join_table_name);
if (tables.find(join_table_name) != tables.end())
{
sAssoc assoc;
assoc.field_name = field.name;
assoc.type = HAS_ONE;
assoc.table_name = join_table_name;
tables[(*it).c_str()].assocs.push_back(assoc);
assoc.field_name = field.name;
assoc.type = HAS_MANY;
assoc.table_name = (*it).c_str();
tables[join_table_name].assocs.push_back(assoc);
}
//else
// printf("WARNING %s.%s found but table %s does not exist\n", table_name.c_str(), field.name.c_str(), join_table_name.c_str());
}
}
mysql_free_result(res);
}
for (std::map<std::string, sTable>::iterator it = tables.begin() ; it != tables.end() ; it++)
{
sTable *table = &it->second;
if (invalid_table(table->name)) continue;
for (std::vector<sField>::iterator field = table->fields.begin() ; field != table->fields.end() ; field++)
{
if (field->type != "char" || field->size != 0) continue;
sprintf(query, "SELECT MAX(LENGTH(`%s`))+1 FROM %s", field->name.c_str(), table->name.c_str());
if (mysql_query(&mysql, query)==0)
{
MYSQL_RES *res_temp = mysql_store_result(&mysql);
MYSQL_ROW row_temp = mysql_fetch_row(res_temp);
if (row_temp[0] != NULL)
field->size = atoi(row_temp[0]);
else
{
printf(" table '%s' has no rows, %s undefined length (text field - assuming 100 chars)\n", table->name.c_str(), field->name.c_str());
field->size = 100;
}
mysql_free_result(res_temp);
}
else
{
printf("MySQL error for '%s.%s': %s\n", table->name.c_str(), field->name.c_str(), mysql_error(&mysql));
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
sprintf(query, "%s_models.h", database.c_str());
fp = fopen(query, "w");
fprintf(fp, "\n/* This file was generated by pow, so it's probably best not to expect edits to stick. */\n\n");
for (std::vector<std::string>::iterator it = table_names.begin() ; it != table_names.end() ; it++)
{
std::string table_name = (*it).c_str();
if (invalid_table(table_name)) continue;
fprintf(fp, "#include \"%s_models/%s.h\"\n", database.c_str(), table_name.c_str());
}
fclose(fp);
for (std::vector<std::string>::iterator it = table_names.begin() ; it != table_names.end() ; it++)
{
std::string table_name = (*it).c_str();
if (invalid_table(table_name)) continue;
std::string class_name = getClassNameFromTableName(table_name);
sprintf(query, "%s_models/%s.h", database.c_str(), table_name.c_str());
fp = fopen(query, "w");
fprintf(fp, "\n#ifndef POW_%s_%s_H\n"
"#define POW_%s_%s_H\n\n"
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n\n"
"/* This file was generated by pow, so it's probably best not to expect edits to stick */\n\n"
"struct %s {\n int id;\n", database.c_str(), table_name.c_str(), database.c_str(), table_name.c_str(), class_name.c_str());
for (unsigned int i = 0 ; i < tables[table_name].fields.size() ; i++)
{
sField *f = &tables[table_name].fields[i];
if (f->type == "char" && f->size == 0)
fprintf(fp, " %s %s[50]; /* invalid guessed length */\n", f->type.c_str(), f->name.c_str());
else if (f->type == "char")
fprintf(fp, " %s %s[%d];\n", f->type.c_str(), f->name.c_str(), f->size);
else
fprintf(fp, " %s %s;\n", f->type.c_str(), f->name.c_str());
}
for (unsigned int i = 0 ; i < tables[table_name].assocs.size() ; i++)
{
sAssoc *a = &tables[table_name].assocs[i];
if (a->type == HAS_ONE)
fprintf(fp, " struct %s *%s;\n", getClassNameFromTableName(a->table_name).c_str(), a->table_name.c_str());
else if (a->type == HAS_MANY)
fprintf(fp, " struct %s **%s;\n"
" int num_%s;\n", getClassNameFromTableName(a->table_name).c_str(), a->table_name.c_str(), a->table_name.c_str());
//if (a->type == HAS_ONE)
// fprintf(fp, "struct %s get%s();\n", a->table_name.c_str(), getClassNameFromTableName(a->table_name).c_str());
//else if (a->type == HAS_MANY)
// fprintf(fp, "struct *%s get%s();\n", a->table_name.c_str(), pluralize(getClassNameFromTableName(a->table_name)).c_str());
}
fprintf(fp, "};\n");
fprintf(fp, "\n"
"extern struct %s *%s;\n"
"extern int num_%s;\n\n"
"extern int load_%s();\n"
"extern void %s_json_get_request(struct mg_connection *conn, const struct mg_request_info *ri, void *data);\n\n"
"#ifdef __cplusplus\n"
"}\n"
"#endif\n\n"
"#endif\n", class_name.c_str(), table_name.c_str(), table_name.c_str(), table_name.c_str(), table_name.c_str());
fclose(fp);
///////////////////////////////////////////////////////////////////////////////////////////////
sprintf(query, "%s_models/%s.c", database.c_str(), table_name.c_str());
fp = fopen(query, "w");
fprintf(fp, "\n#include \"%s.h\"\n"
"#include <mysql.h>\n"
"#include <stdlib.h>\n"
"#include <string.h>\n"
"#include <stdio.h>\n"
"#include \"../mongoose.h\"\n\n", table_name.c_str());
/*for (unsigned int i = 0 ; i < tables[table_name].assocs.size() ; i++)
{
sAssoc *a = &tables[table_name].assocs[i];
if (a->type == HAS_ONE)
fprintf(fp, "struct %s;\nstruct %s* get%s(struct %s *self) {\n return NULL;\n};\n",
getClassNameFromTableName(a->table_name).c_str(), getClassNameFromTableName(a->table_name).c_str(), getClassNameFromTableName(a->table_name).c_str(), getClassNameFromTableName(table_name).c_str());
else if (a->type == HAS_MANY)
fprintf(fp, "struct %s;\nstruct %s* get%s(struct %s *self) {\n return NULL;\n};\n",
getClassNameFromTableName(a->table_name).c_str(), getClassNameFromTableName(a->table_name).c_str(), pluralize(getClassNameFromTableName(a->table_name)).c_str(), getClassNameFromTableName(table_name).c_str());
}*/
fprintf(fp, "\n/* This file was generated by pow, so it's probably best not to expect edits to stick */\n\n"
"struct %s *%s = 0;\nint num_%s = 0;\n\n", class_name.c_str(), table_name.c_str(), table_name.c_str());
fprintf(fp, "int load_%s()\n{\n", table_name.c_str());
fprintf(fp, " MYSQL mysql;\n"
" MYSQL_ROW row;\n"
" MYSQL_RES * res;\n"
" char query[500];\n"
" int num_rows = 0;\n"
" \n");
fprintf(fp, " if ((mysql_init(&mysql) == NULL))\n"
" return printf(\"mysql_init error\\n\");\n"
" if (!mysql_real_connect(&mysql, \"localhost\", \"root\", \"\", \"%s\", 0, NULL, 0))\n"
" return printf(\"mysql_real_connect error (%%s)\\n\", mysql_error(&mysql));\n \n", database.c_str());
fprintf(fp, " if (mysql_query(&mysql, \"SELECT MAX(id)+1 FROM %s\")==0)\n"
" {\n"
" res = mysql_store_result(&mysql);\n"
" row = mysql_fetch_row(res);\n"
" if (row[0] != NULL) num_%s = atoi(row[0]);\n"
" mysql_free_result(res);\n"
" }\n"
" else\n"
" printf(\"MySQL error for '%s': %%s\\n\", mysql_error(&mysql));\n\n", table_name.c_str(), table_name.c_str(), table_name.c_str());
fprintf(fp, " if (mysql_query(&mysql, \"SELECT `id`%s ", tables[table_name].fields.size() > 1 ? "," :"");
for (unsigned int i = 0 ; i < tables[table_name].fields.size() ; i++)
{
sField *f = &tables[table_name].fields[i];
fprintf(fp, "`%s`%s", f->name.c_str(), (i==tables[table_name].fields.size()-1)?"":", ");
}
fprintf(fp, " FROM %s\")==0)\n", table_name.c_str());
fprintf(fp, " {\n"
" res = mysql_store_result(&mysql);\n"
" num_rows = mysql_num_rows(res);\n"
" if (num_%s > 100 && num_%s > num_rows*1.5)\n"
" printf(\"Possible problem with the ids on table '%s' (num_rows=%%d, max_id=%%d)\\n\", num_rows, num_%s);\n\n", table_name.c_str(), table_name.c_str(), table_name.c_str(), table_name.c_str());
fprintf(fp, " %s = (struct %s*)realloc(%s, sizeof(%s)*num_%s);\n", table_name.c_str(), class_name.c_str(), table_name.c_str(), class_name.c_str(), table_name.c_str());
fprintf(fp, " memset(%s, 0, sizeof(%s)*num_%s);\n"
" int i = 0;\n"
" while((row = mysql_fetch_row(res)))\n"
" {\n"
" int id = atoi(row[0]);\n"
" %s[id].id = id;\n", table_name.c_str(), class_name.c_str(), table_name.c_str(), table_name.c_str());
for (unsigned int i = 0 ; i < tables[table_name].fields.size() ; i++)
{
sField *f = &tables[table_name].fields[i];
if (f->type == "int")
fprintf(fp, " if (row[%d] != NULL) %s[id].%s = atoi(row[%d]);\n", i+1, table_name.c_str(), f->name.c_str(), i+1);
else if (f->type == "float")
fprintf(fp, " if (row[%d] != NULL) %s[id].%s = atof(row[%d]);\n", i+1, table_name.c_str(), f->name.c_str(), i+1);
else if (f->type == "char")
{
fprintf(fp, " if (row[%d] != NULL) strncpy(%s[id].%s, row[%d], sizeof(%s[id].%s));\n", i+1, table_name.c_str(), f->name.c_str(), i+1, table_name.c_str(), f->name.c_str());
fprintf(fp, " %s[id].%s[sizeof(%s[id].%s)] = '\\0';\n", table_name.c_str(), f->name.c_str(), table_name.c_str(), f->name.c_str());
}
else
fprintf(fp, " /* not sure what to do with %s of type '%s' */\n", f->name.c_str(), f->type.c_str());
}
fprintf(fp, " i++;\n"
" }\n"
" mysql_free_result(res);\n"
" }\n"
" else\n"
" printf(\"MySQL error for '%s': %%s\\n\", mysql_error(&mysql));\n\n"
" printf(\"loaded %%d/%%d %s\\n\", num_rows, num_%s);\n"
" mysql_close(&mysql);\n"
"}\n\n", table_name.c_str(), table_name.c_str(), table_name.c_str());
///////////////////////////////////////////////////
fprintf(fp, "void %s_json_get_request(struct mg_connection *conn, const struct mg_request_info *ri, void *data)\n"
"{\n"
" mg_printf(conn, \"HTTP/1.1 200 OK\\r\\nContent-Type: text/javascript\\r\\nConnection: close\\r\\n\\r\\n\");\n"
" char *id_c = mg_get_var(conn, \"id\");\n"
" if (id_c == NULL) { mg_printf(conn, \"{\\\"error\\\":\\\"You must provide an 'id'\\\"}\"); return; }\n"
" int id = atoi(id_c);\n"
" mg_free(id_c);\n"
" if (id >= num_%s || id <= 0) { mg_printf(conn, \"{\\\"error\\\":\\\"Invalid 'id' provided\\\"}\"); return; }\n\n"
, table_name.c_str(), table_name.c_str());
for (unsigned int i = 0 ; i < tables[table_name].fields.size() ; i++)
{
sField *f = &tables[table_name].fields[i];
if (f->type == "char")
{
fprintf(fp, " char %s_%s[%d];\n", table_name.c_str(), f->name.c_str(), f->size*2+1);
fprintf(fp, " strncpy(%s_%s, %s[id].%s, %d);\n", table_name.c_str(), f->name.c_str(), table_name.c_str(), f->name.c_str(), f->size*2+1);
fprintf(fp, " for (unsigned int i = 0 ; i < sizeof(%s_%s) ; i++)\n", table_name.c_str(), f->name.c_str());
fprintf(fp, " if (%s_%s[i] == '\\\"' || %s_%s[i] == '\\\\')\n", table_name.c_str(), f->name.c_str(), table_name.c_str(), f->name.c_str());
fprintf(fp, " { memmove(&%s_%s[i+1], &%s_%s[i], %d-i); %s_%s[i] = '\\\\'; i++; }\n\n", table_name.c_str(), f->name.c_str(), table_name.c_str(), f->name.c_str(), f->size*2, table_name.c_str(), f->name.c_str());
}
}
fprintf(fp, " mg_printf(conn, \n"
" \"{ \\n\"\n"
" \" \\\"id\\\":%%d,\\n\"\n");
for (unsigned int i = 0 ; i < tables[table_name].fields.size() ; i++)
{
sField *f = &tables[table_name].fields[i];
if (f->type == "int")
fprintf(fp, " \" \\\"%s\\\":%%d%s\\n\"\n", f->name.c_str(), ((i==tables[table_name].fields.size()-1)?"":", "));
else if (f->type == "float")
fprintf(fp, " \" \\\"%s\\\":%%f%s\\n\"\n", f->name.c_str(), ((i==tables[table_name].fields.size()-1)?"":", "));
else if (f->type == "char")
fprintf(fp, " \" \\\"%s\\\":\\\"%%s\\\"%s\\n\"\n", f->name.c_str(), ((i==tables[table_name].fields.size()-1)?"":", "));
}
fprintf(fp, " \"}\",\n");
fprintf(fp, " %s[id].id%s\n", table_name.c_str(), tables[table_name].fields.size() > 1 ? "," : "");
for (unsigned int i = 0 ; i < tables[table_name].fields.size() ; i++)
{
sField *f = &tables[table_name].fields[i];
if (f->type == "char")
fprintf(fp, " %s_%s%s\n", table_name.c_str(), f->name.c_str(), ((i==tables[table_name].fields.size()-1)?"":", "));
else
fprintf(fp, " %s[id].%s%s\n", table_name.c_str(), f->name.c_str(), ((i==tables[table_name].fields.size()-1)?"":", "));
}
fprintf(fp, " );\n");
fprintf(fp, "}\n");
fclose(fp);
}
sprintf(query, "%s_web_test.c", database.c_str());
fp = fopen(query, "w");
fprintf(fp, "\n#include \"%s_models.h\"\n"
"#include <stdio.h>\n"
"#include <stdlib.h>\n"
"#include <time.h>\n"
"#include <unistd.h>\n"
"#include <math.h>\n"
"#include \"mongoose.h\"\n"
"\n"
"/* This file was generated by pow, so it's probably best not to expect edits to stick */\n"
"\n"
"int main(int argc, char **argv)\n"
"{\n"
" clock_t start = clock();\n"
" struct mg_context *ctx = mg_start();\n"
" mg_set_option(ctx, \"dir_list\", \"no\");\n"
" mg_set_option(ctx, \"ports\", \"4444\");\n \n"
, database.c_str());
for (std::vector<std::string>::iterator it = table_names.begin() ; it != table_names.end() ; it++)
{
std::string table_name = (*it).c_str();
if (invalid_table(table_name)) continue;
fprintf(fp, " load_%s();\n", table_name.c_str());
}
fprintf(fp, " \n");
for (std::vector<std::string>::iterator it = table_names.begin() ; it != table_names.end() ; it++)
{
std::string table_name = (*it).c_str();
if (invalid_table(table_name)) continue;
fprintf(fp, " mg_set_uri_callback(ctx, \"/%s\", &%s_json_get_request, NULL);\n", table_name.c_str(), table_name.c_str());
}
fprintf(fp, " \n"
" printf(\"loaded all models in %%f seconds\\n\", (clock()-(float)start)/CLOCKS_PER_SEC);\n"
" printf(\"web server available at http://localhost:4444/\\n\");\n"
" printf(\"-------------------------------------------------------------------------\\n\");\n"
" for (;;) sleep(10000);\n"
" mg_stop(ctx);\n"
"}");
fclose(fp);
sprintf(query, "cd %s_models ; g++ *.c -c -I%s ; cd ..", database.c_str(), location_of_mysql_h.c_str());
printf("%s\n", query);
system(query);
sprintf(query, "g++ -g mongoose.o %s_models/*.o %s_web_test.c -lmysqlclient -L%s -pg -ldl -lpthread -o %s_web_test", database.c_str(), database.c_str(), location_of_libmysqlclient.c_str(), database.c_str());
printf("%s\n", query);
system(query);
printf("\ndone\n");
return 0;
}