mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-06-28 15:39:02 +08:00
Rename tool to mdb-json and use modern option parsing
This commit is contained in:
parent
a598148afc
commit
0f5bbdf96c
@ -50,7 +50,8 @@ Provides command line utilities, including:
|
|||||||
| ------- | ----------- |
|
| ------- | ----------- |
|
||||||
| `mdb-ver` | Prints the version (JET 3 or 4) of an mdb file. |
|
| `mdb-ver` | Prints the version (JET 3 or 4) of an mdb file. |
|
||||||
| `mdb-schema` | Prints DDL for the specified table. |
|
| `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-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-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. |
|
| `mdb-header` | Generates a C header to be used in exporting mdb data to a C prog. |
|
||||||
|
@ -1,15 +1,7 @@
|
|||||||
AUTOMAKE_OPTIONS = subdir-objects
|
AUTOMAKE_OPTIONS = subdir-objects
|
||||||
SUBDIRS = bash-completion
|
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
|
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@
|
LIBS = $(GLIB_LIBS) @LIBS@
|
||||||
DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\"
|
DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\"
|
||||||
AM_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) -Wsign-compare
|
AM_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) -Wsign-compare
|
||||||
|
@ -103,7 +103,7 @@ main(int argc, char **argv)
|
|||||||
{"row-delimiter", 'R', 0, G_OPTION_ARG_STRING, &row_delimiter, "Specify a row delimiter", "char"},
|
{"row-delimiter", 'R', 0, G_OPTION_ARG_STRING, &row_delimiter, "Specify a row delimiter", "char"},
|
||||||
{"quote", 'q', 0, G_OPTION_ARG_STRING, "e_char, "Use <char> to wrap text-like fields. Default is double quote.", "char"},
|
{"quote", 'q', 0, G_OPTION_ARG_STRING, "e_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"},
|
{"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"},
|
{"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"},
|
{"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"},
|
{"null", '0', 0, G_OPTION_ARG_STRING, &null_text, "Use <char> to represent a NULL value", "char"},
|
||||||
|
@ -20,10 +20,6 @@
|
|||||||
|
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
|
|
||||||
#ifdef DMALLOC
|
|
||||||
#include "dmalloc.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#undef MDB_BIND_SIZE
|
#undef MDB_BIND_SIZE
|
||||||
#define MDB_BIND_SIZE 200000
|
#define MDB_BIND_SIZE 200000
|
||||||
|
|
||||||
@ -38,8 +34,7 @@ static char *row_end = "}\n";
|
|||||||
static char *delimiter = ",";
|
static char *delimiter = ",";
|
||||||
static size_t quote_len = 1; //strlen(quote_char); /* multibyte */
|
static size_t quote_len = 1; //strlen(quote_char); /* multibyte */
|
||||||
static size_t orig_escape_len = 1; //strlen(escape_char);
|
static size_t orig_escape_len = 1; //strlen(escape_char);
|
||||||
static int drop_nonascii;
|
static int drop_nonascii = 0;
|
||||||
|
|
||||||
|
|
||||||
//#define DONT_ESCAPE_ESCAPE
|
//#define DONT_ESCAPE_ESCAPE
|
||||||
static void
|
static void
|
||||||
@ -114,40 +109,42 @@ main(int argc, char **argv)
|
|||||||
char **bound_values;
|
char **bound_values;
|
||||||
int *bound_lens;
|
int *bound_lens;
|
||||||
FILE *outfile = stdout;
|
FILE *outfile = stdout;
|
||||||
drop_nonascii = 0;
|
char *date_fmt = NULL;
|
||||||
int opt;
|
|
||||||
char *value;
|
char *value;
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
while ((opt=getopt(argc, argv, "AD:"))!=-1) {
|
GOptionEntry entries[] = {
|
||||||
switch (opt) {
|
{"date-format", 'D', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date format (see strftime(3) for details)", "format"},
|
||||||
case 'A':
|
{"no-unprintable", 'U', 0, G_OPTION_ARG_NONE, &drop_nonascii, "Change unprintable characters to spaces (otherwise escaped as \\u00XX)", NULL},
|
||||||
drop_nonascii = 1;
|
{NULL}
|
||||||
break;
|
};
|
||||||
case 'D':
|
|
||||||
mdb_set_date_fmt(optarg);
|
GError *error = NULL;
|
||||||
break;
|
GOptionContext *opt_context;
|
||||||
default:
|
|
||||||
break;
|
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);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/*
|
if (argc != 3) {
|
||||||
** optind is now the position of the first non-option arg,
|
fputs("Wrong number of arguments.\n\n", stderr);
|
||||||
** see getopt(3)
|
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
|
||||||
*/
|
|
||||||
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");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(mdb = mdb_open(argv[optind], MDB_NOFLAGS))) {
|
if (!(mdb = mdb_open(argv[1], MDB_NOFLAGS))) {
|
||||||
exit(1);
|
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) {
|
if (!table) {
|
||||||
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[argc-1]);
|
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[argc-1]);
|
||||||
mdb_close(mdb);
|
mdb_close(mdb);
|
Loading…
Reference in New Issue
Block a user