21
21
#ifndef NOXML
22
22
#include "axl.h"
23
23
#endif
24
+ #ifndef NOJSON
25
+ #include "ajl.h"
26
+ #endif
24
27
25
28
const char * sqlconf = NULL ;
26
29
const char * sqlhost = NULL ;
@@ -57,6 +60,9 @@ int unsafe = 0;
57
60
#ifndef NOXML
58
61
int xmlout = 0 ;
59
62
#endif
63
+ #ifndef NOJSON
64
+ j_t json = NULL ;
65
+ #endif
60
66
int jsonout = 0 ;
61
67
int csvout = 0 ;
62
68
int abortdeadlock = 0 ;
@@ -136,6 +142,24 @@ dosql (const char *origquery)
136
142
fprintf (stderr , "%d\t%lu\t%s\t" , field [f ].type , field [f ].length , field [f ].name );
137
143
}
138
144
#endif
145
+ #ifndef NOJSON
146
+ if (json )
147
+ {
148
+ while ((row = sql_fetch_row (res )))
149
+ {
150
+ j_t j = j_append_object (json );
151
+ for (f = 0 ; f < fields ; f ++ )
152
+ {
153
+ if (!row [f ])
154
+ j_store_null (j , field [f ].name );
155
+ else if (IS_NUM (field [f ].type ) && field [f ].type != FIELD_TYPE_TIMESTAMP )
156
+ j_store_literal (j , field [f ].name , row [f ]);
157
+ else
158
+ j_store_string (j , field [f ].name , row [f ]);
159
+ }
160
+ }
161
+ }
162
+ #endif
139
163
#ifndef NOXML
140
164
if (xml )
141
165
{
@@ -379,11 +403,15 @@ main (int argc, const char *argv[])
379
403
#ifndef NOXML
380
404
{"XML" , 0 , POPT_ARGFLAG_DOC_HIDDEN | POPT_ARG_NONE , & xmlout , 0 , "Output in XML" , 0 },
381
405
{"xml" , 'X' , POPT_ARG_NONE , & xmlout , 0 , "Output in XML" , 0 },
406
+ #ifdef NOJSON
407
+ {"json" , 0 , POPT_ARG_NONE , & jsonout , 0 , "Output in JSON" , 0 },
408
+ #endif
382
409
#endif
383
410
{"csv" , 0 , POPT_ARG_NONE , & csvout , 0 , "Output in CSV" , 0 },
411
+ #ifndef NOJSON
384
412
{"json" , 0 , POPT_ARG_NONE , & jsonout , 0 , "Output in JSON" , 0 },
413
+ #endif
385
414
#ifndef NOXML
386
-
387
415
{"xml-root" , 0 , POPT_ARGFLAG_SHOW_DEFAULT | POPT_ARG_STRING , & xmlroot , 0 , "Label for root object" , 0 },
388
416
{"xml-info" , 0 , POPT_ARG_STRING , & xmlinfo , 0 , "Label for info object (column type info)" , 0 },
389
417
{
@@ -416,6 +444,7 @@ main (int argc, const char *argv[])
416
444
if (safe && unsafe )
417
445
errx (1 , "Do the safety dance" );
418
446
#ifndef NOXML
447
+ #ifdef NOJSON
419
448
if (jsonout )
420
449
{ // Alternative defaults for json
421
450
if (defxmlroot == xmlroot )
@@ -429,6 +458,7 @@ main (int argc, const char *argv[])
429
458
if (defxmlcol == xmlcol )
430
459
xmlcol = "" ;
431
460
}
461
+ #endif
432
462
#endif
433
463
if (jsarray )
434
464
{ // Alternative defaults for jsarray
@@ -443,12 +473,20 @@ main (int argc, const char *argv[])
443
473
csvcomma = "," ;
444
474
}
445
475
#ifndef NOXML
446
- if (xmlout || jsonout )
476
+ if (xmlout
477
+ #ifdef NOJSON
478
+ || jsonout
479
+ #endif
480
+ )
447
481
{
448
482
xml = xml_tree_new (NULL );
449
483
xml_tree_add_root (xml , xmlroot );
450
484
}
451
485
#endif
486
+ #ifndef NOJSON
487
+ if (jsonout )
488
+ json = j_create ();
489
+ #endif
452
490
453
491
if (!expand && !sqldatabase && poptPeekArg (popt ))
454
492
sqldatabase = poptGetArg (popt );
@@ -509,10 +547,19 @@ main (int argc, const char *argv[])
509
547
}
510
548
if (xmlout )
511
549
xml_write (stdout , xml );
550
+ #ifdef NOJSON
512
551
if (jsonout )
513
552
xml_write_json (stdout , xml );
553
+ #endif
514
554
xml = xml_tree_delete (xml );
515
555
}
556
+ #endif
557
+ #ifndef NOJSON
558
+ if (json )
559
+ {
560
+ j_err (j_write_pretty (json , stdout ));
561
+ j_delete (& json );
562
+ }
516
563
#endif
517
564
if (reportchanges )
518
565
printf ("%llu\n" , changed );
0 commit comments