Rename tool to mdb-json and use modern option parsing

This commit is contained in:
Evan Miller 2020-09-02 11:47:50 -04:00
parent a598148afc
commit 0f5bbdf96c
4 changed files with 31 additions and 41 deletions

View File

@ -50,7 +50,8 @@ Provides command line utilities, including:
| ------- | ----------- |
| `mdb-ver` | Prints the version (JET 3 or 4) of an mdb file. |
| `mdb-schema` | Prints DDL for the specified table. |
| `mdb-export` | Export table to CSV format. |
| `mdb-export` | Export table to CSV or SQL formats. |
| `mdb-json` | Export table to JSON format. |
| `mdb-tables` | A simple dump of table names to be used with shell scripts. |
| `mdb-count` | A simple count of number of rows in a table, to be used in shell scripts and ETL pipelines. |
| `mdb-header` | Generates a C header to be used in exporting mdb data to a C prog. |

View File

@ -1,15 +1,7 @@
AUTOMAKE_OPTIONS = subdir-objects
SUBDIRS = bash-completion
bin_PROGRAMS = mdb-export mdb-array mdb-schema mdb-tables mdb-parsecsv mdb-header mdb-sql mdb-ver mdb-prop mdb-count mdb-queries mdb-exportjson
bin_PROGRAMS = mdb-export mdb-array mdb-schema mdb-tables mdb-parsecsv mdb-header mdb-sql mdb-ver mdb-prop mdb-count mdb-queries mdb-json
noinst_PROGRAMS = mdb-import prtable prcat prdata prkkd prdump prole updrow prindex
mdb_export_SOURCES = mdb-export.c
mdb_schema_SOURCES = mdb-schema.c
mdb_tables_SOURCES = mdb-tables.c
mdb_sql_SOURCES = mdb-sql.c
mdb_ver_SOURCES = mdb-ver.c
mdb_import_SOURCES = mdb-import.c
mdb_queries_SOURCES = mdb-queries.c
updrow_SOURCES = updrow.c
LIBS = $(GLIB_LIBS) @LIBS@
DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\"
AM_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) -Wsign-compare

View File

@ -103,7 +103,7 @@ main(int argc, char **argv)
{"row-delimiter", 'R', 0, G_OPTION_ARG_STRING, &row_delimiter, "Specify a row delimiter", "char"},
{"quote", 'q', 0, G_OPTION_ARG_STRING, &quote_char, "Use <char> to wrap text-like fields. Default is double quote.", "char"},
{"backend", 'I', 0, G_OPTION_ARG_STRING, &insert_dialect, "INSERT statements (instead of CSV)", "backend"},
{"date_format", 'D', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date format (see strftime(3) for details)", "format"},
{"date-format", 'D', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date format (see strftime(3) for details)", "format"},
{"escape", 'X', 0, G_OPTION_ARG_STRING, &escape_char, "Use <char> to escape quoted characters within a field. Default is doubling.", "format"},
{"namespace", 'N', 0, G_OPTION_ARG_STRING, &namespace, "Prefix identifiers with namespace", "namespace"},
{"null", '0', 0, G_OPTION_ARG_STRING, &null_text, "Use <char> to represent a NULL value", "char"},

View File

@ -20,10 +20,6 @@
#include "base64.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#undef MDB_BIND_SIZE
#define MDB_BIND_SIZE 200000
@ -38,8 +34,7 @@ static char *row_end = "}\n";
static char *delimiter = ",";
static size_t quote_len = 1; //strlen(quote_char); /* multibyte */
static size_t orig_escape_len = 1; //strlen(escape_char);
static int drop_nonascii;
static int drop_nonascii = 0;
//#define DONT_ESCAPE_ESCAPE
static void
@ -114,40 +109,42 @@ main(int argc, char **argv)
char **bound_values;
int *bound_lens;
FILE *outfile = stdout;
drop_nonascii = 0;
int opt;
char *date_fmt = NULL;
char *value;
size_t length;
while ((opt=getopt(argc, argv, "AD:"))!=-1) {
switch (opt) {
case 'A':
drop_nonascii = 1;
break;
case 'D':
mdb_set_date_fmt(optarg);
break;
default:
break;
}
GOptionEntry entries[] = {
{"date-format", 'D', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date format (see strftime(3) for details)", "format"},
{"no-unprintable", 'U', 0, G_OPTION_ARG_NONE, &drop_nonascii, "Change unprintable characters to spaces (otherwise escaped as \\u00XX)", NULL},
{NULL}
};
GError *error = NULL;
GOptionContext *opt_context;
opt_context = g_option_context_new("<file> <table> - export data from Access file to JSON");
g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/);
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1);
}
/*
** optind is now the position of the first non-option arg,
** see getopt(3)
*/
if (argc-optind < 2) {
fprintf(stderr,"Usage: %s [options] <file> <table>\n",argv[0]);
fprintf(stderr,"where options are:\n");
fprintf(stderr," -D <format> set the date format (see strftime(3) for details)\n");
fprintf(stderr," -A drop non ascii characters in non-binary fields\n");
if (argc != 3) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
if (!(mdb = mdb_open(argv[optind], MDB_NOFLAGS))) {
if (!(mdb = mdb_open(argv[1], MDB_NOFLAGS))) {
exit(1);
}
table = mdb_read_table_by_name(mdb, argv[argc-1], MDB_TABLE);
if (date_fmt)
mdb_set_date_fmt(mdb, date_fmt);
table = mdb_read_table_by_name(mdb, argv[2], MDB_TABLE);
if (!table) {
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[argc-1]);
mdb_close(mdb);