Consolidate print_col functions into backend.c

This commit is contained in:
Evan Miller
2020-09-02 20:38:15 -04:00
parent 3b20a5aabd
commit aa0ce8fb3e
4 changed files with 57 additions and 106 deletions

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)
{
@@ -267,7 +219,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 +275,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);