Restore 0.9.0 API backward compatibility of mdb_print_col and MDB_BINEXPORT_*

Other programs (e.g. gmdb2) use mdb_print_col, so restore the old enum
names and values. MDB_EXPORT_ESCAPE_INVISIBLE can be OR'ed into the
last argument to enable C-style escaping of text fields.
This commit is contained in:
Evan Miller
2020-12-30 10:28:49 -05:00
parent d51a37a760
commit 24a7fdab78
3 changed files with 23 additions and 19 deletions

View File

@@ -190,10 +190,12 @@ enum {
/* csv export binary options */ /* csv export binary options */
enum { enum {
MDB_EXPORT_BINARY_STRIP = (1 << 0), MDB_BINEXPORT_STRIP,
MDB_EXPORT_BINARY_RAW = (1 << 1), MDB_BINEXPORT_RAW,
MDB_EXPORT_BINARY_OCTAL = (1 << 2), MDB_BINEXPORT_OCTAL,
MDB_EXPORT_BINARY_HEXADECIMAL = (1 << 3), MDB_BINEXPORT_HEXADECIMAL,
/* Flags that can be OR'ed into the above when calling mdb_print_col */
MDB_EXPORT_ESCAPE_CONTROL_CHARS = (1 << 4) MDB_EXPORT_ESCAPE_CONTROL_CHARS = (1 << 4)
}; };

View File

@@ -1000,6 +1000,7 @@ mdb_print_schema(MdbHandle *mdb, FILE *outfile, char *tabname, char *dbnamespace
} }
} }
#define MDB_BINEXPORT_MASK 0x0F
#define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID) #define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID)
#define is_quote_type(x) (is_binary_type(x) || x==MDB_TEXT || x==MDB_MEMO || x==MDB_DATETIME) #define is_quote_type(x) (is_binary_type(x) || x==MDB_TEXT || x==MDB_MEMO || x==MDB_DATETIME)
//#define DONT_ESCAPE_ESCAPE //#define DONT_ESCAPE_ESCAPE
@@ -1013,7 +1014,7 @@ mdb_print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int b
size_t orig_escape_len = escape_char ? strlen(escape_char) : 0; size_t orig_escape_len = escape_char ? strlen(escape_char) : 0;
int quoting = quote_text && is_quote_type(col_type); int quoting = quote_text && is_quote_type(col_type);
int bin_mode = (flags & 0x0F); int bin_mode = (flags & MDB_BINEXPORT_MASK);
int escape_cr_lf = !!(flags & MDB_EXPORT_ESCAPE_CONTROL_CHARS); int escape_cr_lf = !!(flags & MDB_EXPORT_ESCAPE_CONTROL_CHARS);
/* double the quote char if no escape char passed */ /* double the quote char if no escape char passed */
@@ -1025,7 +1026,7 @@ mdb_print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int b
while (1) { while (1) {
if (is_binary_type(col_type)) { if (is_binary_type(col_type)) {
if (bin_mode == MDB_EXPORT_BINARY_STRIP) if (bin_mode == MDB_BINEXPORT_STRIP)
break; break;
if (!bin_len--) if (!bin_len--)
break; break;
@@ -1033,9 +1034,9 @@ mdb_print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int b
if (!*col_val) if (!*col_val)
break; break;
if (is_binary_type(col_type) && bin_mode == MDB_EXPORT_BINARY_OCTAL) { if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) {
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
} else if (is_binary_type(col_type) && bin_mode == MDB_EXPORT_BINARY_HEXADECIMAL) { } else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
fprintf(outfile, "%02X", *(unsigned char*)col_val++); fprintf(outfile, "%02X", *(unsigned char*)col_val++);
} else if (quoting && quote_len && !strncmp(col_val, quote_char, quote_len)) { } else if (quoting && quote_len && !strncmp(col_val, quote_char, quote_len)) {
fprintf(outfile, "%s%s", escape_char, quote_char); fprintf(outfile, "%s%s", escape_char, quote_char);

View File

@@ -50,6 +50,7 @@ main(int argc, char **argv)
char *str_bin_mode = NULL; char *str_bin_mode = NULL;
char *null_text = NULL; char *null_text = NULL;
int export_flags = 0; int export_flags = 0;
int bin_mode = 0;
char *value; char *value;
size_t length; size_t length;
int ret; int ret;
@@ -122,19 +123,19 @@ main(int argc, char **argv)
if (str_bin_mode) { if (str_bin_mode) {
if (!strcmp(str_bin_mode, "strip")) if (!strcmp(str_bin_mode, "strip"))
export_flags |= MDB_EXPORT_BINARY_STRIP; bin_mode = MDB_BINEXPORT_STRIP;
else if (!strcmp(str_bin_mode, "raw")) else if (!strcmp(str_bin_mode, "raw"))
export_flags |= MDB_EXPORT_BINARY_RAW; bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(str_bin_mode, "octal")) else if (!strcmp(str_bin_mode, "octal"))
export_flags |= MDB_EXPORT_BINARY_OCTAL; bin_mode = MDB_BINEXPORT_OCTAL;
else if (!strcmp(str_bin_mode, "hex")) else if (!strcmp(str_bin_mode, "hex"))
export_flags |= MDB_EXPORT_BINARY_HEXADECIMAL; bin_mode = MDB_BINEXPORT_HEXADECIMAL;
else { else {
fputs("Invalid binary mode\n", stderr); fputs("Invalid binary mode\n", stderr);
exit(1); exit(1);
} }
} else { } else {
export_flags |= MDB_EXPORT_BINARY_RAW; bin_mode = MDB_BINEXPORT_RAW;
} }
if (escape_cr_lf) { if (escape_cr_lf) {
@@ -237,7 +238,7 @@ main(int argc, char **argv)
value = bound_values[i]; value = bound_values[i];
length = bound_lens[i]; length = bound_lens[i];
} }
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, export_flags); mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode | export_flags);
if (col->col_type == MDB_OLE) if (col->col_type == MDB_OLE)
free(value); free(value);
} }
@@ -291,22 +292,22 @@ main(int argc, char **argv)
} }
/* Correctly handle insertion of binary blobs into SQLite using the string literal notation of X'1234ABCD...' */ /* Correctly handle insertion of binary blobs into SQLite using the string literal notation of X'1234ABCD...' */
if (!strcmp(mdb->backend_name, "sqlite") && is_binary_type(col->col_type) if (!strcmp(mdb->backend_name, "sqlite") && is_binary_type(col->col_type)
&& (export_flags & MDB_EXPORT_BINARY_HEXADECIMAL)) { && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
char *quote_char_binary_sqlite = (char *) g_strdup("'"); char *quote_char_binary_sqlite = (char *) g_strdup("'");
fputs("X", outfile); fputs("X", outfile);
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, export_flags); mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, bin_mode | export_flags);
g_free (quote_char_binary_sqlite); g_free (quote_char_binary_sqlite);
/* Correctly handle insertion of binary blobs into PostgreSQL using the notation of decode('1234ABCD...', 'hex') */ /* 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) } else if (!strcmp(mdb->backend_name, "postgres") && is_binary_type(col->col_type)
&& (export_flags & MDB_EXPORT_BINARY_HEXADECIMAL)) { && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
char *quote_char_binary_postgres = (char *) g_strdup("'"); char *quote_char_binary_postgres = (char *) g_strdup("'");
fputs("decode(", outfile); fputs("decode(", outfile);
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, export_flags); mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, bin_mode | export_flags);
fputs(", 'hex')", outfile); fputs(", 'hex')", outfile);
g_free (quote_char_binary_postgres); 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 */ /* No special treatment for other backends or when hexadecimal notation hasn't been selected with the -b hex command line option */
} else { } else {
mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, export_flags); mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode | export_flags);
} }
if (col->col_type == MDB_OLE) if (col->col_type == MDB_OLE)
free(value); free(value);