2000-03-13 02:21:17 +08:00
|
|
|
/* MDB Tools - A library for reading MS Access database file
|
|
|
|
* Copyright (C) 2000 Brian Bruns
|
|
|
|
*
|
2011-08-29 07:53:29 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2000-03-13 02:21:17 +08:00
|
|
|
*
|
2011-08-29 07:53:29 +08:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2000-03-13 02:21:17 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-08-29 07:53:29 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2000-03-13 02:21:17 +08:00
|
|
|
*
|
2011-08-29 07:53:29 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-03-13 02:21:17 +08:00
|
|
|
*/
|
2011-08-29 07:53:29 +08:00
|
|
|
|
2000-03-13 02:21:17 +08:00
|
|
|
#include "mdbtools.h"
|
|
|
|
|
2003-01-29 07:51:06 +08:00
|
|
|
#ifdef DMALLOC
|
|
|
|
#include "dmalloc.h"
|
|
|
|
#endif
|
|
|
|
|
2003-04-30 01:55:09 +08:00
|
|
|
#undef MDB_BIND_SIZE
|
|
|
|
#define MDB_BIND_SIZE 200000
|
|
|
|
|
2012-06-29 08:28:51 +08:00
|
|
|
#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)
|
2004-02-09 11:52:25 +08:00
|
|
|
|
2012-12-03 03:52:21 +08:00
|
|
|
#define BIN_MODE_STRIP 0
|
|
|
|
#define BIN_MODE_RAW 1
|
|
|
|
#define BIN_MODE_OCTAL 2
|
|
|
|
|
2005-09-08 07:27:43 +08:00
|
|
|
static char *escapes(char *s);
|
2004-02-09 11:52:25 +08:00
|
|
|
|
2011-02-22 03:11:41 +08:00
|
|
|
//#define DONT_ESCAPE_ESCAPE
|
|
|
|
static void
|
2012-12-09 20:10:09 +08:00
|
|
|
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)
|
2003-04-30 01:55:09 +08:00
|
|
|
{
|
2011-02-22 03:11:41 +08:00
|
|
|
size_t quote_len = strlen(quote_char); /* multibyte */
|
2003-04-30 01:55:09 +08:00
|
|
|
|
2011-02-22 03:11:41 +08:00
|
|
|
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)) {
|
2012-12-09 20:10:09 +08:00
|
|
|
fputs(quote_char, outfile);
|
2011-02-22 03:11:41 +08:00
|
|
|
while (1) {
|
|
|
|
if (is_binary_type(col_type)) {
|
2012-12-03 03:52:21 +08:00
|
|
|
if (bin_mode == BIN_MODE_STRIP)
|
|
|
|
break;
|
2011-02-22 03:11:41 +08:00
|
|
|
if (!bin_len--)
|
|
|
|
break;
|
|
|
|
} else /* use \0 sentry */
|
|
|
|
if (!*col_val)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (quote_len && !strncmp(col_val, quote_char, quote_len)) {
|
2012-12-09 20:10:09 +08:00
|
|
|
fprintf(outfile, "%s%s", escape_char, quote_char);
|
2011-02-22 03:11:41 +08:00
|
|
|
col_val += quote_len;
|
|
|
|
#ifndef DONT_ESCAPE_ESCAPE
|
|
|
|
} else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len)) {
|
2012-12-09 20:10:09 +08:00
|
|
|
fprintf(outfile, "%s%s", escape_char, escape_char);
|
2011-02-22 03:11:41 +08:00
|
|
|
col_val += orig_escape_len;
|
|
|
|
#endif
|
2012-12-03 22:35:14 +08:00
|
|
|
} else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == BIN_MODE_OCTAL)
|
2012-12-09 20:10:09 +08:00
|
|
|
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
|
2012-12-03 03:52:21 +08:00
|
|
|
else
|
2012-12-09 20:10:09 +08:00
|
|
|
putc(*col_val++, outfile);
|
2003-04-30 01:55:09 +08:00
|
|
|
}
|
2012-12-09 20:10:09 +08:00
|
|
|
fputs(quote_char, outfile);
|
2011-02-17 07:57:40 +08:00
|
|
|
} else
|
2012-12-09 20:10:09 +08:00
|
|
|
fputs(col_val, outfile);
|
2003-04-30 01:55:09 +08:00
|
|
|
}
|
2003-01-21 00:04:24 +08:00
|
|
|
int
|
2000-03-13 02:21:17 +08:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2004-07-09 20:47:04 +08:00
|
|
|
unsigned int j;
|
2004-01-06 08:42:07 +08:00
|
|
|
MdbHandle *mdb;
|
|
|
|
MdbTableDef *table;
|
|
|
|
MdbColumn *col;
|
2005-03-14 05:29:17 +08:00
|
|
|
char **bound_values;
|
|
|
|
int *bound_lens;
|
2004-05-30 15:19:22 +08:00
|
|
|
char *delimiter = NULL;
|
|
|
|
char *row_delimiter = NULL;
|
2005-09-08 07:27:43 +08:00
|
|
|
char *quote_char = NULL;
|
|
|
|
char *escape_char = NULL;
|
2004-01-06 08:42:07 +08:00
|
|
|
char header_row = 1;
|
|
|
|
char quote_text = 1;
|
2010-06-21 04:27:42 +08:00
|
|
|
char *insert_dialect = NULL;
|
2012-01-30 18:41:04 +08:00
|
|
|
char *namespace = NULL;
|
2012-12-03 03:52:21 +08:00
|
|
|
int bin_mode = BIN_MODE_RAW;
|
2004-01-06 08:42:07 +08:00
|
|
|
int opt;
|
2011-02-17 07:57:40 +08:00
|
|
|
char *value;
|
|
|
|
size_t length;
|
2000-03-13 02:21:17 +08:00
|
|
|
|
2012-12-03 03:52:21 +08:00
|
|
|
while ((opt=getopt(argc, argv, "HQq:X:d:D:R:I:N:b:"))!=-1) {
|
2000-10-14 05:33:04 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'H':
|
|
|
|
header_row = 0;
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
quote_text = 0;
|
|
|
|
break;
|
2005-09-08 07:27:43 +08:00
|
|
|
case 'q':
|
|
|
|
quote_char = (char *) g_strdup(optarg);
|
|
|
|
break;
|
2000-10-14 05:33:04 +08:00
|
|
|
case 'd':
|
2005-09-08 07:27:43 +08:00
|
|
|
delimiter = escapes(optarg);
|
2000-10-14 05:33:04 +08:00
|
|
|
break;
|
2004-02-09 11:52:25 +08:00
|
|
|
case 'R':
|
2005-09-08 07:27:43 +08:00
|
|
|
row_delimiter = escapes(optarg);
|
2004-02-09 11:52:25 +08:00
|
|
|
break;
|
|
|
|
case 'I':
|
2010-06-21 04:27:42 +08:00
|
|
|
insert_dialect = (char*) g_strdup(optarg);
|
2004-02-09 11:52:25 +08:00
|
|
|
header_row = 0;
|
|
|
|
break;
|
2004-01-06 08:42:07 +08:00
|
|
|
case 'D':
|
|
|
|
mdb_set_date_fmt(optarg);
|
|
|
|
break;
|
2005-09-08 07:27:43 +08:00
|
|
|
case 'X':
|
|
|
|
escape_char = (char *) g_strdup(optarg);
|
|
|
|
break;
|
2010-08-04 08:09:37 +08:00
|
|
|
case 'N':
|
|
|
|
namespace = (char *) g_strdup(optarg);
|
|
|
|
break;
|
2012-12-03 03:52:21 +08:00
|
|
|
case 'b':
|
|
|
|
if (!strcmp(optarg, "strip"))
|
|
|
|
bin_mode = BIN_MODE_STRIP;
|
|
|
|
else if (!strcmp(optarg, "raw"))
|
|
|
|
bin_mode = BIN_MODE_RAW;
|
|
|
|
else if (!strcmp(optarg, "octal"))
|
|
|
|
bin_mode = BIN_MODE_OCTAL;
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Invalid binary mode\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2000-10-14 05:33:04 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-03-13 02:21:17 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
if (!quote_char) {
|
|
|
|
quote_char = (char *) g_strdup("\"");
|
|
|
|
}
|
2004-05-30 15:19:22 +08:00
|
|
|
if (!delimiter) {
|
|
|
|
delimiter = (char *) g_strdup(",");
|
|
|
|
}
|
|
|
|
if (!row_delimiter) {
|
|
|
|
row_delimiter = (char *) g_strdup("\n");
|
|
|
|
}
|
2000-03-13 02:21:17 +08:00
|
|
|
|
2000-10-14 05:33:04 +08:00
|
|
|
/*
|
|
|
|
** optind is now the position of the first non-option arg,
|
|
|
|
** see getopt(3)
|
|
|
|
*/
|
|
|
|
if (argc-optind < 2) {
|
|
|
|
fprintf(stderr,"Usage: %s [options] <file> <table>\n",argv[0]);
|
|
|
|
fprintf(stderr,"where options are:\n");
|
2012-12-03 03:52:21 +08:00
|
|
|
fprintf(stderr," -H supress header row\n");
|
|
|
|
fprintf(stderr," -Q don't wrap text-like fields in quotes\n");
|
|
|
|
fprintf(stderr," -d <delimiter> specify a column delimiter\n");
|
|
|
|
fprintf(stderr," -R <delimiter> specify a row delimiter\n");
|
|
|
|
fprintf(stderr," -I <backend> INSERT statements (instead of CSV)\n");
|
|
|
|
fprintf(stderr," -D <format> set the date format (see strftime(3) for details)\n");
|
|
|
|
fprintf(stderr," -q <char> Use <char> to wrap text-like fields. Default is \".\n");
|
|
|
|
fprintf(stderr," -X <char> Use <char> to escape quoted characters within a field. Default is doubling.\n");
|
|
|
|
fprintf(stderr," -N <namespace> Prefix identifiers with namespace\n");
|
|
|
|
fprintf(stderr," -b strip|raw|octal Binary export mode.\n");
|
2005-03-14 05:29:17 +08:00
|
|
|
g_free (delimiter);
|
|
|
|
g_free (row_delimiter);
|
2005-09-08 07:27:43 +08:00
|
|
|
g_free (quote_char);
|
|
|
|
if (escape_char) g_free (escape_char);
|
2000-10-14 05:33:04 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2000-04-03 01:08:30 +08:00
|
|
|
|
2004-04-14 04:06:04 +08:00
|
|
|
if (!(mdb = mdb_open(argv[optind], MDB_NOFLAGS))) {
|
2004-07-02 20:29:09 +08:00
|
|
|
g_free (delimiter);
|
|
|
|
g_free (row_delimiter);
|
2005-09-08 07:27:43 +08:00
|
|
|
g_free (quote_char);
|
|
|
|
if (escape_char) g_free (escape_char);
|
2000-10-14 05:33:04 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-07-02 20:29:09 +08:00
|
|
|
|
2010-06-21 04:27:42 +08:00
|
|
|
if (insert_dialect)
|
|
|
|
if (!mdb_set_default_backend(mdb, insert_dialect)) {
|
|
|
|
fprintf(stderr, "Invalid backend type\n");
|
2011-02-17 07:57:40 +08:00
|
|
|
if (escape_char) g_free (escape_char);
|
2010-06-21 04:27:42 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-07-02 20:29:09 +08:00
|
|
|
table = mdb_read_table_by_name(mdb, argv[argc-1], MDB_TABLE);
|
|
|
|
if (!table) {
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[argc-1]);
|
2004-07-02 20:29:09 +08:00
|
|
|
g_free (delimiter);
|
|
|
|
g_free (row_delimiter);
|
2005-09-08 07:27:43 +08:00
|
|
|
g_free (quote_char);
|
|
|
|
if (escape_char) g_free (escape_char);
|
2004-07-02 20:29:09 +08:00
|
|
|
mdb_close(mdb);
|
2005-09-08 07:27:43 +08:00
|
|
|
exit(1);
|
2004-07-02 20:29:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mdb_read_columns(table);
|
|
|
|
mdb_rewind_table(table);
|
2000-10-14 05:33:04 +08:00
|
|
|
|
2005-03-14 05:29:17 +08:00
|
|
|
bound_values = (char **) g_malloc(table->num_cols * sizeof(char *));
|
|
|
|
bound_lens = (int *) g_malloc(table->num_cols * sizeof(int));
|
2004-07-02 20:29:09 +08:00
|
|
|
for (j=0;j<table->num_cols;j++) {
|
|
|
|
bound_values[j] = (char *) g_malloc0(MDB_BIND_SIZE);
|
2004-12-31 09:26:28 +08:00
|
|
|
mdb_bind_column(table, j+1, bound_values[j], &bound_lens[j]);
|
2004-07-02 20:29:09 +08:00
|
|
|
}
|
|
|
|
if (header_row) {
|
2010-06-21 04:27:42 +08:00
|
|
|
for (j=0; j<table->num_cols; j++) {
|
2004-07-02 20:29:09 +08:00
|
|
|
col=g_ptr_array_index(table->columns,j);
|
2010-06-21 04:27:42 +08:00
|
|
|
if (j)
|
2011-03-21 03:26:52 +08:00
|
|
|
fputs(delimiter, stdout);
|
2011-08-29 06:56:03 +08:00
|
|
|
fputs(col->name, stdout);
|
2004-07-02 20:29:09 +08:00
|
|
|
}
|
2011-03-21 03:26:52 +08:00
|
|
|
fputs("\n", stdout);
|
2004-01-11 05:46:14 +08:00
|
|
|
}
|
2000-03-13 02:21:17 +08:00
|
|
|
|
2004-07-02 20:29:09 +08:00
|
|
|
while(mdb_fetch_row(table)) {
|
|
|
|
|
2010-06-21 04:27:42 +08:00
|
|
|
if (insert_dialect) {
|
|
|
|
char *quoted_name;
|
2012-01-30 18:41:04 +08:00
|
|
|
quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[optind + 1]);
|
|
|
|
fprintf(stdout, "INSERT INTO %s (", quoted_name);
|
2010-06-21 04:27:42 +08:00
|
|
|
free(quoted_name);
|
2004-07-02 20:29:09 +08:00
|
|
|
for (j=0;j<table->num_cols;j++) {
|
2011-03-21 03:26:52 +08:00
|
|
|
if (j>0) fputs(", ", stdout);
|
2004-07-02 20:29:09 +08:00
|
|
|
col=g_ptr_array_index(table->columns,j);
|
2011-08-29 06:56:03 +08:00
|
|
|
quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name);
|
2011-03-21 03:26:52 +08:00
|
|
|
fputs(quoted_name, stdout);
|
2010-06-21 04:27:42 +08:00
|
|
|
free(quoted_name);
|
2004-07-02 20:29:09 +08:00
|
|
|
}
|
2011-03-21 03:26:52 +08:00
|
|
|
fputs(") VALUES (", stdout);
|
2004-07-02 20:29:09 +08:00
|
|
|
}
|
2000-03-13 02:21:17 +08:00
|
|
|
|
2004-07-02 20:29:09 +08:00
|
|
|
for (j=0;j<table->num_cols;j++) {
|
2011-02-17 07:57:40 +08:00
|
|
|
if (j>0)
|
|
|
|
fputs(delimiter, stdout);
|
2004-07-02 20:29:09 +08:00
|
|
|
col=g_ptr_array_index(table->columns,j);
|
2010-06-17 11:42:17 +08:00
|
|
|
if (!bound_lens[j]) {
|
2011-02-17 07:57:40 +08:00
|
|
|
if (insert_dialect)
|
|
|
|
fputs("NULL", stdout);
|
2004-07-02 20:29:09 +08:00
|
|
|
} else {
|
2011-02-17 07:57:40 +08:00
|
|
|
if (col->col_type == MDB_OLE) {
|
|
|
|
value = mdb_ole_read_full(mdb, col, &length);
|
|
|
|
} else {
|
|
|
|
value = bound_values[j];
|
|
|
|
length = bound_lens[j];
|
|
|
|
}
|
2012-12-09 20:10:09 +08:00
|
|
|
print_col(stdout, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
|
2011-02-17 07:57:40 +08:00
|
|
|
if (col->col_type == MDB_OLE)
|
|
|
|
free(value);
|
2000-03-13 02:21:17 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-17 07:57:40 +08:00
|
|
|
if (insert_dialect) fputs(");", stdout);
|
|
|
|
fputs(row_delimiter, stdout);
|
2004-07-02 20:29:09 +08:00
|
|
|
}
|
|
|
|
for (j=0;j<table->num_cols;j++) {
|
|
|
|
g_free(bound_values[j]);
|
2000-03-13 02:21:17 +08:00
|
|
|
}
|
2005-03-14 05:29:17 +08:00
|
|
|
g_free(bound_values);
|
|
|
|
g_free(bound_lens);
|
2004-07-02 20:29:09 +08:00
|
|
|
mdb_free_tabledef(table);
|
2000-03-13 02:21:17 +08:00
|
|
|
|
2004-05-30 15:19:22 +08:00
|
|
|
g_free (delimiter);
|
|
|
|
g_free (row_delimiter);
|
2005-09-08 07:27:43 +08:00
|
|
|
g_free (quote_char);
|
|
|
|
if (escape_char) g_free (escape_char);
|
2012-07-22 08:29:04 +08:00
|
|
|
|
2003-01-29 07:51:06 +08:00
|
|
|
mdb_close(mdb);
|
2011-02-17 07:57:40 +08:00
|
|
|
return 0;
|
2000-03-13 02:21:17 +08:00
|
|
|
}
|
|
|
|
|
2005-09-08 07:27:43 +08:00
|
|
|
static char *escapes(char *s)
|
|
|
|
{
|
|
|
|
char *d = (char *) g_strdup(s);
|
|
|
|
char *t = d;
|
|
|
|
unsigned char encode = 0;
|
|
|
|
|
|
|
|
for (;*s; s++) {
|
|
|
|
if (encode) {
|
|
|
|
switch (*s) {
|
|
|
|
case 'n': *t++='\n'; break;
|
|
|
|
case 't': *t++='\t'; break;
|
|
|
|
case 'r': *t++='\r'; break;
|
|
|
|
default: *t++='\\'; *t++=*s; break;
|
|
|
|
}
|
|
|
|
encode=0;
|
|
|
|
} else if (*s=='\\') {
|
|
|
|
encode=1;
|
|
|
|
} else {
|
|
|
|
*t++=*s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*t='\0';
|
|
|
|
return d;
|
|
|
|
}
|