Skip to content

Commit e69c73c

Browse files
committed
Cleaner JSON out using AJL
1 parent 7d9218c commit e69c73c

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "stringdecimal"]
22
path = stringdecimal
33
url = https://github.com/revk/stringdecimal.git
4+
[submodule "AJL"]
5+
path = AJL
6+
url = https://github.com/revk/AJL

AJL

Submodule AJL added at 06ca7d5

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ stringdecimal/stringdecimal.o:
2121
git submodule update --recursive
2222
make -C stringdecimal
2323

24+
AJL/ajl.c:
25+
git submodule update --init AJL
26+
27+
AJL/ajl.o: AJL/ajl.c AJL
28+
make -C AJL ajl.o
29+
2430
sqllib.o: sqllib.c sqllib.h Makefile
2531
gcc -g -O -c -o $@ $< -fPIC ${OPTS} -DLIB ${SQLINC} -DMYSQL_VERSION=${SQLVER}
2632

2733
sqllibsd.o: sqllib.c sqllib.h Makefile stringdecimal/stringdecimal.o
2834
gcc -g -O -c -o $@ $< -fPIC ${OPTS} -DLIB ${SQLINC} -DMYSQL_VERSION=${SQLVER} -DSTRINGDECIMAL
2935

30-
sql: sql.c sqllibsd.o sqllib.h sqlexpand.o sqlexpand.h stringdecimal/stringdecimal.o
31-
gcc -g -O -o $@ $< -fPIC ${OPTS} -DNOXML ${SQLINC} ${SQLLIB} sqllibsd.o sqlexpand.o stringdecimal/stringdecimal.o -lcrypto -luuid
36+
sql: sql.c sqllibsd.o sqllib.h sqlexpand.o sqlexpand.h stringdecimal/stringdecimal.o AJL/ajl.o
37+
gcc -g -O -o $@ $< -fPIC ${OPTS} -DNOXML ${SQLINC} ${SQLLIB} sqllibsd.o sqlexpand.o stringdecimal/stringdecimal.o AJL/ajl.o -lcrypto -luuid -IAJL
3238

3339
sqlwrite: sqlwrite.c sqllibsd.o sqllib.h stringdecimal/stringdecimal.o
3440
gcc -g -O -o $@ $< -fPIC ${OPTS} ${SQLINC} ${SQLLIB} sqllibsd.o stringdecimal/stringdecimal.o

sql.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#ifndef NOXML
2222
#include "axl.h"
2323
#endif
24+
#ifndef NOJSON
25+
#include "ajl.h"
26+
#endif
2427

2528
const char *sqlconf = NULL;
2629
const char *sqlhost = NULL;
@@ -57,6 +60,9 @@ int unsafe = 0;
5760
#ifndef NOXML
5861
int xmlout = 0;
5962
#endif
63+
#ifndef NOJSON
64+
j_t json = NULL;
65+
#endif
6066
int jsonout = 0;
6167
int csvout = 0;
6268
int abortdeadlock = 0;
@@ -136,6 +142,24 @@ dosql (const char *origquery)
136142
fprintf (stderr, "%d\t%lu\t%s\t", field[f].type, field[f].length, field[f].name);
137143
}
138144
#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
139163
#ifndef NOXML
140164
if (xml)
141165
{
@@ -379,11 +403,15 @@ main (int argc, const char *argv[])
379403
#ifndef NOXML
380404
{"XML", 0, POPT_ARGFLAG_DOC_HIDDEN | POPT_ARG_NONE, &xmlout, 0, "Output in XML", 0},
381405
{"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
382409
#endif
383410
{"csv", 0, POPT_ARG_NONE, &csvout, 0, "Output in CSV", 0},
411+
#ifndef NOJSON
384412
{"json", 0, POPT_ARG_NONE, &jsonout, 0, "Output in JSON", 0},
413+
#endif
385414
#ifndef NOXML
386-
387415
{"xml-root", 0, POPT_ARGFLAG_SHOW_DEFAULT | POPT_ARG_STRING, &xmlroot, 0, "Label for root object", 0},
388416
{"xml-info", 0, POPT_ARG_STRING, &xmlinfo, 0, "Label for info object (column type info)", 0},
389417
{
@@ -416,6 +444,7 @@ main (int argc, const char *argv[])
416444
if (safe && unsafe)
417445
errx (1, "Do the safety dance");
418446
#ifndef NOXML
447+
#ifdef NOJSON
419448
if (jsonout)
420449
{ // Alternative defaults for json
421450
if (defxmlroot == xmlroot)
@@ -429,6 +458,7 @@ main (int argc, const char *argv[])
429458
if (defxmlcol == xmlcol)
430459
xmlcol = "";
431460
}
461+
#endif
432462
#endif
433463
if (jsarray)
434464
{ // Alternative defaults for jsarray
@@ -443,12 +473,20 @@ main (int argc, const char *argv[])
443473
csvcomma = ",";
444474
}
445475
#ifndef NOXML
446-
if (xmlout || jsonout)
476+
if (xmlout
477+
#ifdef NOJSON
478+
|| jsonout
479+
#endif
480+
)
447481
{
448482
xml = xml_tree_new (NULL);
449483
xml_tree_add_root (xml, xmlroot);
450484
}
451485
#endif
486+
#ifndef NOJSON
487+
if (jsonout)
488+
json = j_create ();
489+
#endif
452490

453491
if (!expand && !sqldatabase && poptPeekArg (popt))
454492
sqldatabase = poptGetArg (popt);
@@ -509,10 +547,19 @@ main (int argc, const char *argv[])
509547
}
510548
if (xmlout)
511549
xml_write (stdout, xml);
550+
#ifdef NOJSON
512551
if (jsonout)
513552
xml_write_json (stdout, xml);
553+
#endif
514554
xml = xml_tree_delete (xml);
515555
}
556+
#endif
557+
#ifndef NOJSON
558+
if (json)
559+
{
560+
j_err (j_write_pretty (json, stdout));
561+
j_delete (&json);
562+
}
516563
#endif
517564
if (reportchanges)
518565
printf ("%llu\n", changed);

0 commit comments

Comments
 (0)