Merge branch 'hexbinaryexportmode' of github.com:davidhicks/mdbtools into export-hex

This commit is contained in:
Evan Miller
2020-09-02 09:53:50 -04:00
4 changed files with 49 additions and 18 deletions

View File

@@ -52,17 +52,21 @@ print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_l
if (!*col_val)
break;
if (quote_len && !strncmp(col_val, quote_char, quote_len)) {
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)) {
} 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)
} else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) {
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
else
} else if (is_binary_hex_col) {
fprintf(outfile, "%02X", *(unsigned char*)col_val++);
} else
putc(*col_val++, outfile);
}
fputs(quote_char, outfile);
@@ -167,6 +171,8 @@ main(int argc, char **argv)
bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(str_bin_mode, "octal"))
bin_mode = MDB_BINEXPORT_OCTAL;
else if (!strcmp(str_bin_mode, "hex"))
bin_mode = MDB_BINEXPORT_HEXADECIMAL;
else {
fputs("Invalid binary mode\n", stderr);
exit(1);
@@ -312,7 +318,23 @@ 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);
/* 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) && 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);
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);
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);
}
if (col->col_type == MDB_OLE)
free(value);
}