Merge branch 'dev' of github.com:mdbtools/mdbtools into merge-166

This commit is contained in:
Evan Miller
2020-11-01 07:55:53 -05:00
111 changed files with 750 additions and 10234 deletions

View File

@@ -2,6 +2,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-json
noinst_PROGRAMS = mdb-import prtable prcat prdata prkkd prdump prole updrow prindex
noinst_HEADERS = base64.h
LIBS = $(GLIB_LIBS) @LIBS@
DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\"
AM_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) -Wsign-compare

View File

@@ -1 +1,4 @@
EXTRA_DIST = mdb-export mdb-hexdump mdb-import mdb-parsecsv mdb-prop mdb-schema mdb-sql mdb-tables mdb-ver
if ENABLE_BASH_COMPLETION
bashcompletiondir = $(BASH_COMPLETION_DIR)
dist_bashcompletion_DATA = mdb-count mdb-export mdb-hexdump mdb-import mdb-json mdb-parsecsv mdb-prop mdb-queries mdb-schema mdb-sql mdb-tables mdb-ver
endif

View File

@@ -0,0 +1,21 @@
#-*- mode: shell-script;-*-
_mdb_count()
{
local cur prev words cword
_init_completion || return
if [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname
local tablenames
dbname=$prev
__expand_tilde_by_ref dbname
local IFS=$'\n'
tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
compopt -o filenames
COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else
_filedir '@(mdb|mdw|accdb)'
fi
return 0
} &&
complete -F _mdb_count mdb-count

View File

@@ -1,29 +1,31 @@
#-*- mode: shell-script;-*-
have mdb-export &&
_mdb_export()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
local cur prev words cword split
_init_completion -s || return
if [[ "$prev" == -@(d|-delimiter|R|-row-delimiter|q|-quote|X|-escape|D|-date-format|N|-namespace|h|-help) ]] ; then
if [[ "$prev" == -@(d|-delimiter|R|-row-delimiter|q|-quote|X|-escape|N|-namespace|S|-batch-size|D|-date-format|T|-datetime-format|0|-null|h|-help) ]] ; then
return 0
elif [[ "$prev" == -I ]] ; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) )
elif [[ "$prev" == -@(I|-insert) ]] ; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql sqlite' -- "$cur" ) )
elif [[ "$prev" == -@(b|-bin) ]] ; then
COMPREPLY=( $( compgen -W 'strip raw octal' -- $cur ) )
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-H -d -R -Q -q -X -I -D -N -b -h \
--no-header --no-quote --delimiter --row-delimiter --insert \
--date-format --quote --escape --namespace --bin --help' -- $cur ) )
COMPREPLY=( $( compgen -W 'strip raw octal hex' -- "$cur" ) )
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname
local tablenames
dbname=$prev
__expand_tilde_by_ref dbname
tablenames=$(eval mdb-tables -S -d / "${dbname}" 2>/dev/null)
COMPREPLY=( $( IFS=/ compgen -W "${tablenames}" -- $cur ) )
local IFS=$'\n'
tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
compopt -o filenames
COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else
_filedir '@(mdb|mdw|accdb)'
fi

View File

@@ -1,12 +1,10 @@
#-*- mode: shell-script;-*-
have mdb-hexdump &&
_mdb_hexdump()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
local cur prev words cword
_init_completion || return
if (( COMP_CWORD == 1 )); then
if (( $cword == 1 )); then
_filedir '@(mdb|mdw|accdb)'
fi
return 0

View File

@@ -1,26 +1,27 @@
#-*- mode: shell-script;-*-
have mdb-import &&
_mdb_import()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
local cur prev words cword split
_init_completion -s || return
if [[ "$prev" == -@(d|-delimiter) ]]; then
if [[ "$prev" == -@(H|--header|d|-delimiter) ]]; then
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-H --header \
-d --delimiter \
-h --help' -- $cur ) )
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '--header= --delimiter= --help' -- "$cur" ) )
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
local dbname
local tablenames
dbname=$prev
__expand_tilde_by_ref dbname
tablenames=$(eval mdb-tables -S -d / "${dbname}" 2>/dev/null)
COMPREPLY=( $( IFS=/ compgen -W "${tablenames}" -- $cur ) )
local IFS=$'\n'
tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
compopt -o filenames
COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else
_filedir '@(mdb|mdw|accdb|txt|csv)'
fi

View File

@@ -0,0 +1,30 @@
#-*- mode: shell-script;-*-
_mdb_json()
{
local cur prev words cword split
_init_completion -s || return
if [[ "$prev" == -@(D|-date-format|T|-datetime-format|U|--no-unprintable|h|-help) ]] ; then
return 0
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname
local tablenames
dbname=$prev
__expand_tilde_by_ref dbname
local IFS=$'\n'
tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
compopt -o filenames
COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else
_filedir '@(mdb|mdw|accdb)'
fi
return 0
} &&
complete -F _mdb_json mdb-json

View File

@@ -1,12 +1,10 @@
#-*- mode: shell-script;-*-
have mdb-parsecsv &&
_mdb_parsecsv()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
local cur prev words cword
_init_completion || return
if (( COMP_CWORD == 1 )); then
if (( $cword == 1 )); then
_filedir '@(txt|csv)'
fi
return 0

View File

@@ -1,23 +1,22 @@
#-*- mode: shell-script;-*-
have mdb-prop &&
_mdb_prop()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
local cur prev words cword
_init_completion || return
if (( COMP_CWORD == 1 )); then
if (( $cword == 1 )); then
_filedir '@(mdb|mdw|accdb)'
elif (( COMP_CWORD == 2 )); then
elif (( $cword == 2 )); then
local dbname
local tablenames
dbname=${COMP_WORDS[1]}
dbname=$prev
__expand_tilde_by_ref dbname
tablenames=$(eval mdb-tables -S -d / "${dbname}" 2>/dev/null)
COMPREPLY=( $( IFS=/ compgen -W "${tablenames}" -- $cur ) )
elif (( COMP_CWORD == 3 )); then
COMPREPLY=( $( compgen -W 'Lv LvProp LvModule LvExtra' -- $cur ) )
local IFS=$'\n'
tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
compopt -o filenames
COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
elif (( $cword == 3 )); then
COMPREPLY=( $( compgen -W 'Lv LvProp LvModule LvExtra' -- "$cur" ) )
fi
return 0
} &&

View File

@@ -0,0 +1,25 @@
#-*- mode: shell-script;-*-
_mdb_queries()
{
local cur prev words cword
_init_completion || return
if [[ "$prev" == -@(d) ]]; then
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname
local querynames
dbname=$prev
__expand_tilde_by_ref dbname
local IFS=$'\n'
querynames="$(eval mdb-queries -1 "${dbname}" 2>/dev/null)"
compopt -o filenames
COMPREPLY=( $( compgen -W "${querynames}" -- "$cur" ) )
else
_filedir '@(mdb|mdw|accdb)'
fi
return 0
} &&
complete -F _mdb_queries mdb-queries

View File

@@ -1,27 +1,20 @@
#-*- mode: shell-script;-*-
have mdb-schema &&
_mdb_schema()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
local cur prev words cword split
_init_completion -s || return
if [[ "$prev" == -@(T|-table|N|-namespace) ]] ; then
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-T --table \
-N --namespace \
--drop-table --no-drop-table \
--not-null --no-not-null \
--default-values --no-default-values \
--not-empty --no-not-empty \
--indexes --no-indexes \
--relations --no-relations
-h --help' -- $cur ) )
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) )
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql sqlite' -- "$cur" ) )
else
_filedir '@(mdb|mdw|accdb)'
fi

View File

@@ -1,25 +1,20 @@
#-*- mode: shell-script;-*-
have mdb-sql &&
_mdb_sql()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
local cur prev words cword split
_init_completion -s || return
if [[ "$prev" == -d ]] ; then
if [[ "$prev" == -@(P|-no-pretty-print|H|-no-header|F|-no-footer) ]] ; then
return 0
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == -@(i|-input|o|-output) ]] ; then
_filedir
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-H --no-header \
-F --no-footer \
-p --no-pretty-print \
-d --delimiter \
-i --input \
-o --output \
-h --help' -- $cur ) )
else
_filedir '@(mdb|mdw|accdb)'
fi

View File

@@ -1,25 +1,21 @@
#-*- mode: shell-script;-*-
have mdb-tables &&
_mdb_tables()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
local cur prev words cword split
_init_completion -s || return
if [[ "$prev" == -@(d|-delimiter) ]]; then
return 0
elif [[ "$prev" == -@(t|-type) ]]; then
COMPREPLY=( $( compgen -W 'form table macro systable report query linkedtable module relationship dbprop any all' -- $cur ) )
COMPREPLY=( $( compgen -W 'form table macro systable report query linkedtable module relationship dbprop any all' -- "$cur" ) )
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-S --system\
-1 --single-column \
-d --delimiter \
-t --type \
-T --showtype \
-h --help' -- $cur ) )
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
else
_filedir '@(mdb|mdw|accdb)'
fi

View File

@@ -1,14 +1,12 @@
#-*- mode: shell-script;-*-
have mdb-ver &&
_mdb_ver()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
local cur prev words cword
_init_completion || return
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-M -h --help' -- $cur ) )
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
#COMPREPLY=($(compgen -W '--help --mdbtools' -- "$cur"))
else
_filedir '@(mdb|mdw|accdb)'
fi

View File

@@ -20,58 +20,10 @@
#define EXPORT_BIND_SIZE 200000
#define is_quote_type(x) (x==MDB_TEXT || x==MDB_OLE || x==MDB_MEMO || x==MDB_DATETIME || x==MDB_BINARY || x==MDB_REPID)
#define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID)
static char *escapes(char *s);
//#define DONT_ESCAPE_ESCAPE
static void
print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_len, char *quote_char, char *escape_char, int bin_mode)
/* quote_text: Don't quote if 0.
*/
{
size_t quote_len = strlen(quote_char); /* multibyte */
size_t orig_escape_len = escape_char ? strlen(escape_char) : 0;
/* double the quote char if no escape char passed */
if (!escape_char)
escape_char = quote_char;
if (quote_text && is_quote_type(col_type)) {
fputs(quote_char, outfile);
while (1) {
if (is_binary_type(col_type)) {
if (bin_mode == MDB_BINEXPORT_STRIP)
break;
if (!bin_len--)
break;
} else /* use \0 sentry */
if (!*col_val)
break;
int is_binary_hex_col = is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL;
if (quote_len && !strncmp(col_val, quote_char, quote_len) && !is_binary_hex_col) {
fprintf(outfile, "%s%s", escape_char, quote_char);
col_val += quote_len;
#ifndef DONT_ESCAPE_ESCAPE
} else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len) && !is_binary_hex_col) {
fprintf(outfile, "%s%s", escape_char, escape_char);
col_val += orig_escape_len;
#endif
} else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) {
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
} else if (is_binary_hex_col) {
fprintf(outfile, "%02X", *(unsigned char*)col_val++);
} else
putc(*col_val++, outfile);
}
fputs(quote_char, outfile);
} else
fputs(col_val, outfile);
}
int
main(int argc, char **argv)
{
@@ -91,6 +43,7 @@ main(int argc, char **argv)
int boolean_words = 0;
int batch_size = 1000;
char *insert_dialect = NULL;
char *shortdate_fmt = NULL;
char *date_fmt = NULL;
char *namespace = NULL;
char *str_bin_mode = NULL;
@@ -101,18 +54,19 @@ main(int argc, char **argv)
GOptionEntry entries[] = {
{"no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &header_row, "Suppress header row.", NULL},
{"no-quote", 'Q', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &quote_text, "Don't wrap text-like fields in quotes.", NULL},
{"delimiter", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Specify an alternative column delimiter. Default is comma.", "char"},
{"row-delimiter", 'R', 0, G_OPTION_ARG_STRING, &row_delimiter, "Specify a row delimiter", "char"},
{"no-quote", 'Q', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &quote_text, "Don't wrap text-like fields in quotes.", NULL},
{"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"},
{"escape", 'X', 0, G_OPTION_ARG_STRING, &escape_char, "Use <char> to escape quoted characters within a field. Default is doubling.", "format"},
{"insert", 'I', 0, G_OPTION_ARG_STRING, &insert_dialect, "INSERT statements (instead of CSV)", "backend"},
{"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"},
{"bin", 'b', 0, G_OPTION_ARG_STRING, &str_bin_mode, "Binary export mode", "strip|raw|octal"},
{"boolean-words", 'B', 0, G_OPTION_ARG_NONE, &boolean_words, "Use TRUE/FALSE in Boolean fields (default is 0/1)", NULL},
{"batch-size", 'S', 0, G_OPTION_ARG_INT, &batch_size, "Size of insert batches on supported platforms.", "int"},
{"date-format", 'D', 0, G_OPTION_ARG_STRING, &shortdate_fmt, "Set the date format (see strftime(3) for details)", "format"},
{"datetime-format", 'T', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date/time format (see strftime(3) for details)", "format"},
{"null", '0', 0, G_OPTION_ARG_STRING, &null_text, "Use <char> to represent a NULL value", "char"},
{"bin", 'b', 0, G_OPTION_ARG_STRING, &str_bin_mode, "Binary export mode", "strip|raw|octal|hex"},
{"boolean-words", 'B', 0, G_OPTION_ARG_NONE, &boolean_words, "Use TRUE/FALSE in Boolean fields (default is 0/1)", NULL},
{NULL},
};
GError *error = NULL;
@@ -187,6 +141,9 @@ main(int argc, char **argv)
if (date_fmt)
mdb_set_date_fmt(mdb, date_fmt);
if (shortdate_fmt)
mdb_set_shortdate_fmt(mdb, shortdate_fmt);
if (boolean_words)
mdb_set_boolean_fmt_words(mdb);
@@ -267,7 +224,7 @@ main(int argc, char **argv)
value = bound_values[i];
length = bound_lens[i];
}
print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
if (col->col_type == MDB_OLE)
free(value);
}
@@ -323,18 +280,18 @@ main(int argc, char **argv)
if (!strcmp(mdb->backend_name, "sqlite") && is_binary_type(col->col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
char *quote_char_binary_sqlite = (char *) g_strdup("'");
fputs("X", outfile);
print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, bin_mode);
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, bin_mode);
g_free (quote_char_binary_sqlite);
/* Correctly handle insertion of binary blobs into PostgreSQL using the notation of decode('1234ABCD...', 'hex') */
} else if (!strcmp(mdb->backend_name, "postgres") && is_binary_type(col->col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
char *quote_char_binary_postgres = (char *) g_strdup("'");
fputs("decode(", outfile);
print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, bin_mode);
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, bin_mode);
fputs(", 'hex')", outfile);
g_free (quote_char_binary_postgres);
/* No special treatment for other backends or when hexadecimal notation hasn't been selected with the -b hex command line option */
} else {
print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
}
if (col->col_type == MDB_OLE)
free(value);

View File

@@ -109,11 +109,13 @@ main(int argc, char **argv)
int *bound_lens;
FILE *outfile = stdout;
char *date_fmt = NULL;
char *shortdate_fmt = NULL;
char *value;
size_t length;
GOptionEntry entries[] = {
{"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, &shortdate_fmt, "Set the date format (see strftime(3) for details)", "format"},
{"datetime-format", 'T', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date/time 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}
};
@@ -143,6 +145,9 @@ main(int argc, char **argv)
if (date_fmt)
mdb_set_date_fmt(mdb, date_fmt);
if (shortdate_fmt)
mdb_set_shortdate_fmt(mdb, shortdate_fmt);
mdb_set_bind_size(mdb, EXPORT_BIND_SIZE);
table = mdb_read_table_by_name(mdb, argv[2], MDB_TABLE);

View File

@@ -62,9 +62,11 @@ int main (int argc, char **argv) {
//variables for the generation of sql
char *sql_tables = (char *) malloc(bind_size);
char *sql_predicate = (char *) malloc(bind_size);
char *sql_columns = (char *) malloc(bind_size);
char *sql_where = (char *) malloc(bind_size);
char *sql_sorting = (char *) malloc(bind_size);
int flagint;
/* see getopt(3) for more information on getopt and this will become clear */
while ((opt=getopt(argc, argv, "L1d:"))!=-1) {
@@ -87,7 +89,7 @@ int main (int argc, char **argv) {
fprintf (stderr, "Usage: %s [options] <database filename> <query name>\n",argv[0]);
fprintf (stderr, "where options are:\n");
fprintf (stderr, " -L\t\t\tList queries in the database (default if no query name is passed)\n");
fprintf (stderr, " -1\t\t\tUse newline as the delimiter (used in conjuction with listing)\n");
fprintf (stderr, " -1\t\t\tUse newline as the delimiter (used in conjunction with listing)\n");
fprintf (stderr, " -d <delimiter>\tSpecify delimiter to use\n");
exit (1);
}
@@ -148,8 +150,22 @@ int main (int argc, char **argv) {
while (mdb_fetch_row(table)) {
if(strcmp(query_id,objectid) == 0) {
flagint = atoi(flag);
//we have a row for our query
switch(atoi(attribute)) {
case 3: // predicate
if (flagint & 0x30) {
strcpy(sql_predicate, " TOP ");
strcat(sql_predicate, name1);
if (flagint & 0x20) {
strcat(sql_predicate, " PERCENT");
}
} else if (flagint & 0x8) {
strcpy(sql_predicate, " DISTINCTROW");
} else if (flagint & 0x2) {
strcpy(sql_predicate, " DISTINCT");
}
break;
case 5: // table name
if(strcmp(sql_tables,"") == 0) {
strcpy(sql_tables,name1);
@@ -193,9 +209,9 @@ int main (int argc, char **argv) {
/* print out the sql statement */
if(strcmp(sql_where,"") == 0) {
fprintf(stdout,"SELECT %s FROM %s %s\n",sql_columns,sql_tables,sql_sorting);
fprintf(stdout,"SELECT%s %s FROM %s %s\n",sql_predicate,sql_columns,sql_tables,sql_sorting);
} else {
fprintf(stdout,"SELECT %s FROM %s WHERE %s %s\n",sql_columns,sql_tables,sql_where,sql_sorting);
fprintf(stdout,"SELECT%s %s FROM %s WHERE %s %s\n",sql_predicate,sql_columns,sql_tables,sql_where,sql_sorting);
}
mdb_free_tabledef(table);

View File

@@ -152,40 +152,6 @@ do_set_cmd(MdbSQL *sql, char *s)
}
}
int
read_file(char *s, int line, unsigned int *bufsz, char *mybuf)
{
char *fname;
FILE *in;
char buf[256];
unsigned int cursz = 0;
int lines = 0;
fname = s;
while (*fname && *fname==' ') fname++;
if (! (in = fopen(fname, "r"))) {
fprintf(stderr,"Unable to open file %s\n", fname);
mybuf[0]=0;
return 0;
}
while (fgets(buf, 255, in)) {
cursz += strlen(buf) + 1;
if (cursz > (*bufsz)) {
(*bufsz) *= 2;
mybuf = (char *) realloc(mybuf, *bufsz);
}
strcat(mybuf, buf);
#ifdef HAVE_READLINE_HISTORY
/* don't record blank lines */
if (strlen(buf)) add_history(buf);
#endif
strcat(mybuf, "\n");
lines++;
printf("%d => %s",line+lines, buf);
}
return lines;
}
void
run_query(FILE *out, MdbSQL *sql, char *mybuf, char *delimiter)
{
@@ -330,6 +296,28 @@ dump_results_pp(FILE *out, MdbSQL *sql)
}
}
static char *
find_sql_terminator(char *s)
{
char *sp;
int len = strlen(s);
if (len == 0) {
return NULL;
}
sp = &s[len-1];
while (sp > s && isspace(*sp)) {
sp--;
}
if (*sp == ';') {
return sp;
}
return NULL;
}
int
main(int argc, char **argv)
{
@@ -346,6 +334,7 @@ main(int argc, char **argv)
char *histpath;
#endif
char *delimiter = NULL;
int in_from_colon_r = 0;
GOptionEntry entries[] = {
@@ -419,15 +408,21 @@ main(int argc, char **argv)
if (s) free(s);
if (in) {
s=malloc(256);
if ((!s) || (!fgets(s, 256, in))) {
/* if we have something in the buffer, run it */
if (strlen(mybuf))
run_query((out) ? out : stdout,
sql, mybuf, delimiter);
break;
}
if (s[strlen(s)-1]=='\n')
s=calloc(bufsz, 1);
if (!fgets(s, bufsz, in)) {
// Backwards compatibility with older MDBTools
// Files read from the command line had an
// implicit "go" at the end
if (!in_from_colon_r && strlen(mybuf))
strcpy(s, "go");
else if (in_from_colon_r) {
line = 0;
fclose(in);
in = NULL;
in_from_colon_r = 0;
} else
break;
} else if (s[strlen(s)-1]=='\n')
s[strlen(s)-1]=0;
} else {
sprintf(prompt, "%d => ", line);
@@ -450,19 +445,42 @@ main(int argc, char **argv)
line = 0;
mybuf[0]='\0';
} else if (!strncmp(s,":r",2)) {
line += read_file(&s[2], line, &bufsz, mybuf);
char *fname = &s[2];
if (in) {
fprintf(stderr, "Can not handle nested opens\n");
} else {
while (*fname && isspace(*fname))
fname++;
if (!(in = fopen(fname, "r"))) {
fprintf(stderr,"Unable to open file %s\n", fname);
mybuf[0]=0;
} else {
in_from_colon_r = 1;
}
}
} else {
char *p;
while (strlen(mybuf) + strlen(s) > bufsz) {
bufsz *= 2;
mybuf = (char *) g_realloc(mybuf, bufsz);
}
#ifdef HAVE_READLINE_HISTORY
/* don't record blank lines */
if (strlen(s)) add_history(s);
/* don't record blank lines, or lines read from files
* specified on the command line */
if ((!in || in_from_colon_r) && strlen(s))
add_history(s);
#endif
strcat(mybuf,s);
/* preserve line numbering for the parser */
strcat(mybuf,"\n");
if ((p = find_sql_terminator(mybuf))) {
*p = '\0';
line = 0;
run_query((out) ? out : stdout, sql, mybuf, delimiter);
mybuf[0]='\0';
}
}
}
mdb_sql_exit(sql);