/
xmlify.cpp
417 lines (370 loc) · 11.1 KB
/
xmlify.cpp
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
#include <mysql.h>
#include <iostream>
#include "mysqlcb.hpp"
using namespace mysqlcb;
const char table_schema_query[] =
"SELECT COLUMN_NAME,"
" UPPER(DATA_TYPE), "
" CHARACTER_MAXIMUM_LENGTH, "
" COLUMN_TYPE,"
" NULLIF('0',INSTR(EXTRA,'auto_increment')) AS autoinc,"
" NULLIF('0',INSTR(COLUMN_KEY,'PRI')) AS prikey,"
" NULLIF(IS_NULLABLE,'YES') AS nullable"
" FROM COLUMNS"
" WHERE TABLE_SCHEMA=? AND TABLE_NAME=?";
/** Adds attributes to a schema field element for set flags. */
void add_field_attributes_from_flags(unsigned int flags)
{
if (flags & NOT_NULL_FLAG)
std::cout << " not_null=\"true\"";
if (flags & PRI_KEY_FLAG)
std::cout << " primary_key=\"true\"";
if (flags & UNIQUE_KEY_FLAG)
std::cout << " unique_key=\"true\"";
if (flags & MULTIPLE_KEY_FLAG)
std::cout << " multiple_key=\"true\"";
if (flags & BLOB_FLAG)
std::cout << " blob=\"true\"";
if (flags & UNSIGNED_FLAG)
std::cout << " unsigned=\"true\"";
if (flags & ZEROFILL_FLAG)
std::cout << " zero_fill=\"true\"";
if (flags & BINARY_FLAG)
std::cout << " binary=\"true\"";
if (flags & ENUM_FLAG)
std::cout << " enum=\"true\"";
if (flags & AUTO_INCREMENT_FLAG)
std::cout << " auto_increment=\"true\"";
if (flags & TIMESTAMP_FLAG)
std::cout << " timestamp=\"true\"";
if (flags & SET_FLAG)
std::cout << " set=\"true\"";
}
void print_schema(Binder &b)
{
std::cout << "<schema>\n";
const Bind_Data *bd = b.bind_data;
while (valid(bd))
{
std::cout << "<field name=\"" << field_name(bd) << "\" type=\"" << type_name(bd) << "\"";
add_field_attributes_from_flags(bd->field->flags);
std::cout << " />\n";
++bd;
}
std::cout << "</schema>\n";
}
bool is_int_type(const Bind_Data &data_type)
{
size_t len = get_data_len(data_type);
const char *str = static_cast<const char*>(data_type.data);
// Character at position get_data_len may or may not be '\0',
// the existence or lack determines the string length.
// This is important when we're getting the last few chars.
if (str[len-1]=='\0')
--len;
return 0==strncmp("INT", &str[len-3], 3);
}
bool is_enum_type(const Bind_Data &data_type)
{
const char *str = static_cast<const char*>(data_type.data);
return 0==strncmp("ENUM", str, 4);
}
bool is_set_type(const Bind_Data &data_type)
{
const char *str = static_cast<const char*>(data_type.data);
return 0==strncmp("SET", str, 3);
}
bool is_unsigned_type(const Bind_Data &column_type)
{
size_t len = get_data_len(column_type);
if (len > 8)
{
const char *str = static_cast<const char*>(column_type.data);
// Character at position get_data_len may or may not be '\0',
// the existence or lack determines the string length.
// This is important when we're getting the last few chars.
if (str[len-1]=='\0')
--len;
return 0==strncmp("unsigned", &str[len-8], 8);
}
else
return false;
}
/**
* Writes string to stdout, converting &"'<> to XML entities and handling double ' or "
*
* The character that *end points to should be the quoted string terminator or nullptr;
* When the function encounters either ' or ", it will compare it to the char at *end.
* If it matches, duplicates will be supressed. If not matched, adjacent ' or " will
* both print.
*/
void xmlify_sql_string(const char *start, const char *end=nullptr)
{
if (end==nullptr)
end = start + strlen(start);
while(start<end)
{
switch(*start)
{
case '&':
std::cout << "&";
break;
case '<':
std::cout << "<";
break;
case '>':
std::cout << ">";
break;
case '"':
std::cout << """;
break;
case '\'':
std::cout << "'";
break;
default:
std::cout.put(*start);
break;
}
// Skip duplicate if it is string's enclosing character
if (*start==*end && *(start+1)==*end)
++start;
++start;
}
}
void add_value_list(const Bind_Data &column_type)
{
const char *str = static_cast<const char *>(column_type.data);
const char *end = str + get_data_len(column_type);
const char *p = str;
const char *val = nullptr;
// Skip up to just-past the parenthesis. Use the
// post-increment test for '(' to position pointer.
while(p < end && *p++!='(')
;
while (p < end && *p == '\'')
{
val = ++p;
// Breaks out with terminating '
while (++p<end)
{
if (*p=='\'') // terminating '?
{
if (*++p == '\'') // ignore double 's
continue;
else
{
assert(*p==',' || *p==')');
std::cout << " <item val=\"";
xmlify_sql_string(val, p-1); // send last ' as terminator
std::cout << "\" />\n";
val = nullptr;
++p;
break;
}
}
}
}
}
void print_columns_as_fields(const PullPack &pp)
{
Bind_Data *bd = pp.binder.bind_data;
Bind_Data &bName = bd[0];
Bind_Data &bDType = bd[1];
Bind_Data &bCMaxLen = bd[2];
Bind_Data &bCType = bd[3];
Bind_Data &bAutoInc = bd[4];
Bind_Data &bPriKey = bd[5];
Bind_Data &bNullable = bd[6];
while(pp.puller(false))
{
std::cout << "<field name=\"" << bName << "\" type=\"" << bDType;
if (!is_null(bAutoInc))
std::cout << "\" auto_increment=\"true";
if (!is_null(bPriKey))
std::cout << "\" primary_key=\"true";
if (!is_null(bNullable))
std::cout << "\" not_null=\"true";
if (!is_null(bCMaxLen))
std::cout << "\" length=\"" << bCMaxLen;
// This must come just before the end because
// if it's a enum or set type, the values will
// be rendered as child nodes, which means no
// attributes can follow it.
if (is_enum_type(bDType) || is_set_type(bDType))
{
std::cout << "\">\n";
add_value_list(bCType);
std::cout << "</field>\n";
}
else
{
if (is_int_type(bDType) && is_unsigned_type(bCType))
std::cout << "\" unsigned=\"true";
std::cout << "\" />\n";
}
}
}
void print_table_schema(const char *host,
const char *user,
const char *pass,
const char *dbase,
const char *tname)
{
auto f = [&dbase, &tname](MYSQL &mysql)
{
MParam params[3] = { dbase, tname };
std::cout << "<?xml version=\"1.0\" ?>\n<resultset>\n";
std::cout << "<schema name=\"" << tname << "\">\n";
execute_query_pull(mysql, print_columns_as_fields, table_schema_query, params);
std::cout << "</schema>\n";
std::cout << "</resultset>\n";
};
start_mysql(f,host,user,pass,"information_schema");
}
/**
* Uses libmysqlcb functions to open a database, run a query, and output the result as XML.
*
* This function is a demonstration of the library using the **push** strategy. The library
* executes the *push* method by invoking a callback function for each row in the query
* result.
*
* In this example, the callback function is
*/
void run_query(const char *query,
const char *host,
const char *user,
const char *password,
const char *dbase,
bool include_schema)
{
auto xmlify = [&include_schema](Binder &b)
{
if (include_schema)
{
print_schema(b);
include_schema = 0;
}
std::cout << "<row";
const Bind_Data *bd = b.bind_data;
while (valid(bd))
{
if (!is_null(bd))
std::cout << " " << field_name(bd) << "=\"" << bd << "\"";
++bd;
}
std::cout << "/>\n";
};
auto fqp = [&query, &xmlify](Querier_Pack &qp)
{
std::cout << "<?xml version=\"1.0\" ?>\n<resultset>\n";
start_push(qp, xmlify, query);
std::cout << "</resultset>\n";
};
get_querier_pack(fqp, host, user, password, dbase);
}
void show_usage(void)
{
std::cout << "Usage instructions:\n\n"
"xmlify [-u USER] [-pPASSWORD] [-h HOST] [-e SQL_STATEMENT] [-s] [-t tablename] database_name\n\n"
"Options:\n"
"-e sql statement\n"
" SQL statement that should be executed.\n"
"-h [host]\n"
" MYSQL host to which the connection is to be made.\n"
" If this option is omitted, localhost will be used.\n"
"--help\n"
" This usage display.\n"
"-p[password]\n"
" Password for user account. Note there is no space between -p and the password value.\n"
"-s\n"
" Include a schema in the output.\n"
"-t tablename\n"
" Output named table's schema.\n"
"-u [user]\n"
" User account to use for the connection.\n"
"\n"
"The utility will use default values under [client] in ~/.my.cnf for unspecified options.\n"
"\n"
"The options can go before or after the required database name.\n"
"\n"
"Examples:\n"
"\nRun query \"SELECT * FROM Person\" in database \"TheDB\"\nusing explicit MySQL authorization:\n\n"
"xmlify -u root -prootpassword -h localhost -e \"SELECT * FROM Person\" TheDB\n"
"\n\nShow schema of the table \"Person\" from the database \"TheDB\"\nusing default MySQL authorization:\n\n"
"xmlify -t Person TheDB\n";
}
int main(int argc, char **argv)
{
const char *dbase = nullptr;
const char *query = nullptr;
const char *host = "localhost";
const char *user = nullptr;
const char *password = nullptr;
const char *tablename = nullptr;
bool include_schema = 0;
bool display_usage = 0;
if (argc<2)
{
show_usage();
return 1;
}
for (int i=1; i<argc; ++i)
{
char *arg = argv[i];
if (*arg=='-')
{
switch(arg[1])
{
case '-':
arg += 2;
if (0==strcmp(arg,"help"))
{
i = argc;
display_usage = true;
}
case 'e':
query = argv[++i];
break;
case 'h':
host = argv[++i];
break;
case 'p':
password = &arg[2];
break;
case 's':
include_schema = 1;
break;
case 't':
tablename = argv[++i];
break;
case 'u':
user = argv[++i];
break;
default:
i = argc;
display_usage = true;
break;
}
}
else
dbase = arg;
}
if (display_usage)
show_usage();
else if (dbase && tablename)
{
print_table_schema(host, user, password, dbase, tablename);
}
else
{
try
{
run_query(query, host, user, password, dbase, include_schema);
}
catch(std::exception &e)
{
std::cerr << "Error " << e.what() << std::endl;
}
}
return 0;
}