From 8b1db6c08edbdf8129cd5d20f9dd68c2636494c0 Mon Sep 17 00:00:00 2001 From: Ewen McNeill Date: Thu, 7 Apr 2016 08:45:06 +1200 Subject: [PATCH] mdb-export: Add boolean words option (TRUE/FALSE) Adds "-B" (--boolean-words) option to mdb-export, which will reconfigure mdb/data.c to export TRUE/FALSE for boolean values instead of 1/0. The option is needed to support BOOLEAN fields on PostgreSQL, which will not implicitly cast bare 1/0 into a BOOLEAN value. Value literals are the SQL TRUE/FALSE, and _quoted_ words meaning true/false and _quoted_ '1'/'0'. With this flag the SQL TRUE/FALSE values are output, which should work with several SQL databases. PostgreSQL Reference: http://www.postgresql.org/docs/current/static/datatype-boolean.html --- include/mdbtools.h | 1 + src/libmdb/data.c | 24 ++++++++++++++++++++++-- src/util/mdb-export.c | 5 +++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/mdbtools.h b/include/mdbtools.h index 60ed640..f361a36 100644 --- a/include/mdbtools.h +++ b/include/mdbtools.h @@ -496,6 +496,7 @@ extern size_t mdb_ole_read_next(MdbHandle *mdb, MdbColumn *col, void *ole_ptr); extern size_t mdb_ole_read(MdbHandle *mdb, MdbColumn *col, void *ole_ptr, int chunk_size); extern void* mdb_ole_read_full(MdbHandle *mdb, MdbColumn *col, size_t *size); extern void mdb_set_date_fmt(const char *); +extern void mdb_set_boolean_fmt_words(); extern int mdb_read_row(MdbTableDef *table, unsigned int row); /* dump.c */ diff --git a/src/libmdb/data.c b/src/libmdb/data.c index 92e86c5..0920e07 100644 --- a/src/libmdb/data.c +++ b/src/libmdb/data.c @@ -44,6 +44,25 @@ void mdb_set_date_fmt(const char *fmt) strncpy(date_fmt, fmt, 63); } +/* Some databases (eg PostgreSQL) do not understand integer 0/1 values + * as TRUE/FALSE, so provide a means to override the values used to be + * the SQL Standard TRUE/FALSE values. + */ +static char boolean_false_number[] = "0"; +static char boolean_true_number[] = "1"; + +static char boolean_false_word[] = "FALSE"; +static char boolean_true_word[] = "TRUE"; + +static char *boolean_false_value = boolean_false_number; +static char *boolean_true_value = boolean_true_number; + +void mdb_set_boolean_fmt_words() +{ + boolean_false_value = boolean_false_word; + boolean_true_value = boolean_true_word; +} + void mdb_bind_column(MdbTableDef *table, int col_num, void *bind_ptr, int *len_ptr) { MdbColumn *col; @@ -168,10 +187,11 @@ mdb_xfer_bound_bool(MdbHandle *mdb, MdbColumn *col, int value) { col->cur_value_len = value; if (col->bind_ptr) { - strcpy(col->bind_ptr, value ? "0" : "1"); + strcpy(col->bind_ptr, + value ? boolean_false_value : boolean_true_value); } if (col->len_ptr) { - *col->len_ptr = 1; + *col->len_ptr = strlen(col->bind_ptr); } return 1; diff --git a/src/util/mdb-export.c b/src/util/mdb-export.c index a25b7a4..8481144 100644 --- a/src/util/mdb-export.c +++ b/src/util/mdb-export.c @@ -89,6 +89,7 @@ main(int argc, char **argv) char *escape_char = NULL; int header_row = 1; int quote_text = 1; + int boolean_words = 0; char *insert_dialect = NULL; char *date_fmt = NULL; char *namespace = NULL; @@ -108,6 +109,7 @@ main(int argc, char **argv) { "escape", 'X', 0, G_OPTION_ARG_STRING, &escape_char, "Use to escape quoted characters within a field. Default is doubling.", "format"}, { "namespace", 'N', 0, G_OPTION_ARG_STRING, &namespace, "Prefix identifiers with namespace", "namespace"}, { "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}, { NULL }, }; GError *error = NULL; @@ -154,6 +156,9 @@ main(int argc, char **argv) if (date_fmt) mdb_set_date_fmt(date_fmt); + if (boolean_words) + mdb_set_boolean_fmt_words(); + if (str_bin_mode) { if (!strcmp(str_bin_mode, "strip")) bin_mode = MDB_BINEXPORT_STRIP;