Migrate mdb-export changes to gmdb2

Support for octal/stip of binary
Customisation of escape string
Quoting is done according to types rather that empiric analysis of str
conversion.
This commit is contained in:
Nirgal Vourgère
2013-01-13 21:20:00 +01:00
parent 7e8ae9b121
commit 8a545120d7
6 changed files with 366 additions and 230 deletions

View File

@@ -168,6 +168,13 @@ enum {
}; };
#define MDB_SHEXP_DEFAULT (MDB_SHEXP_CST_NOTNULL | MDB_SHEXP_COMMENTS | MDB_SHEXP_INDEXES | MDB_SHEXP_RELATIONS) #define MDB_SHEXP_DEFAULT (MDB_SHEXP_CST_NOTNULL | MDB_SHEXP_COMMENTS | MDB_SHEXP_INDEXES | MDB_SHEXP_RELATIONS)
/* csv export binary options */
enum {
MDB_BINEXPORT_STRIP,
MDB_BINEXPORT_RAW,
MDB_BINEXPORT_OCTAL
};
#define IS_JET4(mdb) (mdb->f->jet_version==MDB_VER_JET4) /* obsolete */ #define IS_JET4(mdb) (mdb->f->jet_version==MDB_VER_JET4) /* obsolete */
#define IS_JET3(mdb) (mdb->f->jet_version==MDB_VER_JET3) #define IS_JET3(mdb) (mdb->f->jet_version==MDB_VER_JET3)

View File

@@ -57,34 +57,6 @@
<property name="y_options"></property> <property name="y_options"></property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;Quotes:&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;Quote Character:&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="top_attach">4</property>
<property name="bottom_attach">5</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child> <child>
<widget class="GtkLabel" id="label6"> <widget class="GtkLabel" id="label6">
<property name="visible">True</property> <property name="visible">True</property>
@@ -140,18 +112,6 @@
<property name="bottom_attach">2</property> <property name="bottom_attach">2</property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkComboBox" id="quote_combo">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child> <child>
<widget class="GtkComboBoxEntry" id="sep_combo"> <widget class="GtkComboBoxEntry" id="sep_combo">
<property name="visible">True</property> <property name="visible">True</property>
@@ -164,18 +124,6 @@
<property name="bottom_attach">3</property> <property name="bottom_attach">3</property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkComboBoxEntry" id="qchar_combo">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">4</property>
<property name="bottom_attach">5</property>
</packing>
</child>
<child> <child>
<widget class="GtkLabel" id="label8"> <widget class="GtkLabel" id="label8">
<property name="visible">True</property> <property name="visible">True</property>
@@ -202,6 +150,58 @@
<property name="bottom_attach">6</property> <property name="bottom_attach">6</property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;Quote Character:&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkComboBoxEntry" id="qchar_combo">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;Escape Character:&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="top_attach">4</property>
<property name="bottom_attach">5</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkComboBoxEntry" id="escchar_combo">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">4</property>
<property name="bottom_attach">5</property>
</packing>
</child>
</widget> </widget>
<packing> <packing>
<property name="position">2</property> <property name="position">2</property>

View File

@@ -81,16 +81,16 @@ void gmdb_table_init_popup (GtkWidget*);
void gmdb_table_set_sensitive(gboolean b); void gmdb_table_set_sensitive(gboolean b);
/* table_export.c */ /* table_export.c */
void gmdb_export_help_cb(GtkWidget *w, gpointer data);
void gmdb_table_export_button_cb(GtkWidget *w, gpointer data);
void gmdb_print_quote(FILE *outfile, int need_quote, char quotechar, char *colsep, char *str);
void gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf); void gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf);
void gmdb_export_get_lineterm(GladeXML *xml, gchar *lineterm, int max_buf); void gmdb_export_get_lineterm(GladeXML *xml, gchar *lineterm, int max_buf);
int gmdb_export_get_quote(GladeXML *xml); void gmdb_export_get_quotechar(GladeXML *xml, gchar *quotechar, int max_buf);
char gmdb_export_get_quotechar(GladeXML *xml); void gmdb_export_get_escapechar(GladeXML *xml, gchar *escapechar, int max_buf);
int gmdb_export_get_binmode(GladeXML *xml); int gmdb_export_get_binmode(GladeXML *xml);
int gmdb_export_get_headers(GladeXML *xml); int gmdb_export_get_headers(GladeXML *xml);
gchar *gmdb_export_get_filepath(GladeXML *xml); gchar *gmdb_export_get_filepath(GladeXML *xml);
void gmdb_export_help_cb(GtkWidget *w, gpointer data);
void gmdb_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);
void gmdb_table_export_button_cb(GtkWidget *w, gpointer data);
extern MdbSQL *sql; extern MdbSQL *sql;

View File

@@ -51,35 +51,40 @@ gchar* gmdb_export_get_filepath (GladeXML*); /* from table_export.c */
static void static void
gmdb_sql_write_rslt_cb(GtkWidget *w, GladeXML *xml) gmdb_sql_write_rslt_cb(GtkWidget *w, GladeXML *xml)
{ {
/* We need to re-run the whole query because some information is not stored
* in the TreeStore, such as column types.
*/
gchar *file_path; gchar *file_path;
GladeXML *sql_xml; GladeXML *sql_xml;
GtkWidget *filesel, *dlg; GtkWidget *filesel, *dlg;
FILE *outfile; FILE *outfile;
int i; int i;
int need_headers = 0; int need_headers = 0;
int need_quote = 0;
gchar delimiter[11]; gchar delimiter[11];
gchar quotechar; gchar quotechar[5];
//TODO int binmode; gchar escape_char[5];
int bin_mode;
gchar lineterm[5]; gchar lineterm[5];
gchar *str;
int rows=0, n_columns;
GtkWidget *treeview;
GtkTreeViewColumn *col;
GList *glist;
GtkTreeModel *store;
GtkTreeIter iter;
GValue value = { 0, };
filesel = glade_xml_get_widget (xml, "export_dialog");
sql_xml = g_object_get_data(G_OBJECT(filesel), "sql_xml");
//printf("sql_xml %p\n",sql_xml);
gmdb_export_get_delimiter(xml, delimiter, 10); guint len;
gmdb_export_get_lineterm(xml, lineterm, 5); gchar *buf;
need_quote = gmdb_export_get_quote(xml); GtkTextIter start, end;
quotechar = gmdb_export_get_quotechar(xml); GtkTextBuffer *txtbuffer;
//TODO binmode = gmdb_export_get_binmode(xml); GtkWidget *textview;
char **bound_values;
int *bound_lens;
MdbSQLColumn *sqlcol;
long row;
char *value;
size_t length;
MdbTableDef *table;
MdbColumn *col = NULL;
gmdb_export_get_delimiter(xml, delimiter, sizeof(delimiter));
gmdb_export_get_lineterm(xml, lineterm, sizeof(lineterm));
gmdb_export_get_quotechar(xml, quotechar, sizeof(quotechar));
gmdb_export_get_escapechar(xml, escape_char, sizeof(escape_char));
bin_mode = gmdb_export_get_binmode(xml);
need_headers = gmdb_export_get_headers(xml); need_headers = gmdb_export_get_headers(xml);
file_path = gmdb_export_get_filepath(xml); file_path = gmdb_export_get_filepath(xml);
@@ -92,53 +97,104 @@ gmdb_sql_write_rslt_cb(GtkWidget *w, GladeXML *xml)
return; return;
} }
treeview = glade_xml_get_widget (sql_xml, "sql_results"); /* Get SQL */
glist = gtk_tree_view_get_columns(GTK_TREE_VIEW(treeview)); filesel = glade_xml_get_widget (xml, "export_dialog");
i = 0; sql_xml = g_object_get_data(G_OBJECT(filesel), "sql_xml");
if (need_headers) { //printf("sql_xml %p\n",sql_xml);
while ((col = g_list_nth_data(glist, i))) { textview = glade_xml_get_widget(sql_xml, "sql_textview");
gchar *title; txtbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
if (i>0) fputs(delimiter, outfile); len = gtk_text_buffer_get_char_count(txtbuffer);
title = g_strdup(gtk_tree_view_column_get_title(col)); gtk_text_buffer_get_iter_at_offset (txtbuffer, &start, 0);
gmdb_print_quote(outfile, need_quote, quotechar, gtk_text_buffer_get_iter_at_offset (txtbuffer, &end, len);
delimiter, title); buf = gtk_text_buffer_get_text(txtbuffer, &start, &end, FALSE);
fputs(title, outfile);
gmdb_print_quote(outfile, need_quote, quotechar,
delimiter, title); /* ok now execute it */
g_free(title); mdb_sql_run_query(sql, buf);
i++; if (mdb_sql_has_error(sql)) {
} GtkWidget* dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)),
fputs(lineterm, outfile); GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
g_list_free(glist); "%s", mdb_sql_last_error(sql));
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
mdb_sql_reset(sql);
fclose(outfile);
gtk_widget_destroy(filesel);
return;
} }
store = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); bound_values = (char **) g_malloc(sql->num_columns * sizeof(char *));
gtk_tree_model_get_iter_first(store, &iter); bound_lens = (int *) g_malloc(sql->num_columns * sizeof(int));
rows=0;
g_value_init (&value, G_TYPE_STRING); for (i=0; i<sql->num_columns; i++) {
do { /* bind columns */
rows++; bound_values[i] = (char *) g_malloc0(MDB_BIND_SIZE);
n_columns = gtk_tree_model_get_n_columns(store); mdb_sql_bind_column(sql, i+1, bound_values[i], &bound_lens[i]);
for (i=0; i < n_columns; i++) {
if (i>0) fputs(delimiter, outfile); /* display column titles */
gtk_tree_model_get_value(store, &iter, i, &value); if (need_headers) {
str = (gchar *) g_value_get_string(&value); if (i>0)
gmdb_print_quote(outfile, need_quote, quotechar, delimiter, str); fputs(delimiter, outfile);
fputs(str, outfile); sqlcol = g_ptr_array_index(sql->columns,i);
gmdb_print_quote(outfile, need_quote, quotechar, delimiter, str); gmdb_print_col(outfile, sqlcol->name, quotechar[0]!='\0', MDB_TEXT, 0, quotechar, escape_char, bin_mode);
g_value_unset(&value); }
}
row = 0;
while (mdb_fetch_row(sql->cur_table)) {
row++;
for (i=0; i<sql->num_columns; i++) {
if (i>0)
fputs(delimiter, outfile);
sqlcol = g_ptr_array_index(sql->columns, i);
/* Find col matching sqlcol */
table = sql->cur_table;
for (i=0; i<table->num_cols; i++) {
col = g_ptr_array_index(table->columns, i);
if (!strcasecmp(sqlcol->name, col->name))
break;
}
/* assert(i!=table->num_cols). Can't happen, already checked. */
/* Don't quote NULLs */
if (bound_lens[i] && sqlcol->bind_type != MDB_OLE) {
if (col->col_type == MDB_OLE) {
value = mdb_ole_read_full(mdb, col, &length);
} else {
value = bound_values[i];
length = bound_lens[i];
}
gmdb_print_col(outfile, value, quotechar[0]!='\0', col->col_type, length, quotechar, escape_char, bin_mode);
if (col->col_type == MDB_OLE)
free(value);
}
} }
fputs(lineterm, outfile); fputs(lineterm, outfile);
} while (gtk_tree_model_iter_next(store, &iter)); }
/* free the memory used to bind */
for (i=0; i<sql->num_columns; i++) {
g_free(bound_values[i]);
}
mdb_sql_reset(sql);
g_free(buf);
fclose(outfile); fclose(outfile);
gtk_widget_destroy(filesel);
dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)), dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)),
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
_("%d rows successfully exported."), rows); _("%ld rows successfully exported."), row);
gtk_dialog_run (GTK_DIALOG (dlg)); gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg); gtk_widget_destroy (dlg);
gtk_widget_destroy(filesel);
} }
static void static void
gmdb_sql_results_cb(GtkWidget *w, GladeXML *xml) gmdb_sql_results_cb(GtkWidget *w, GladeXML *xml)
{ {
@@ -337,6 +393,7 @@ gmdb_sql_select_hist_cb(GtkComboBox *combobox, GladeXML *xml)
static void static void
gmdb_sql_execute_cb(GtkWidget *w, GladeXML *xml) gmdb_sql_execute_cb(GtkWidget *w, GladeXML *xml)
{ {
guint len; guint len;
gchar *buf; gchar *buf;
gchar *bound_data[256]; gchar *bound_data[256];
@@ -364,7 +421,7 @@ gmdb_sql_execute_cb(GtkWidget *w, GladeXML *xml)
textview = glade_xml_get_widget(xml, "sql_textview"); textview = glade_xml_get_widget(xml, "sql_textview");
combobox = glade_xml_get_widget(xml, "sql_combo"); combobox = glade_xml_get_widget(xml, "sql_combo");
txtbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview)); txtbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
len = gtk_text_buffer_get_char_count(txtbuffer); len = gtk_text_buffer_get_char_count(txtbuffer);
gtk_text_buffer_get_iter_at_offset (txtbuffer, &start, 0); gtk_text_buffer_get_iter_at_offset (txtbuffer, &start, 0);
gtk_text_buffer_get_iter_at_offset (txtbuffer, &end, len); gtk_text_buffer_get_iter_at_offset (txtbuffer, &end, len);
buf = gtk_text_buffer_get_text(txtbuffer, &start, &end, FALSE); buf = gtk_text_buffer_get_text(txtbuffer, &start, &end, FALSE);

View File

@@ -42,22 +42,12 @@ MdbCatalogEntry *cat_entry;
#define NEVER "Never" #define NEVER "Never"
#define AUTOMAT "Automatic (where necessary)" #define AUTOMAT "Automatic (where necessary)"
#define NOQUOTE "Don't quote"
#define BIN_STRIP "Strip" #define BIN_STRIP "Strip"
#define BIN_RAW "Raw" #define BIN_RAW "Raw"
#define BIN_OCTAL "Octal" #define BIN_OCTAL "Octal"
void
gmdb_print_quote(FILE *outfile, int need_quote, char quotechar, char *colsep, char *str)
{
if (need_quote==1) {
fprintf(outfile, "%c", quotechar);
} else if (need_quote==-1) {
if (strstr(str,colsep)) {
fprintf(outfile, "%c", quotechar);
}
}
}
void void
gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf) gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf)
{ {
@@ -66,16 +56,22 @@ gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf)
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "sep_combo")); combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "sep_combo"));
str = gtk_combo_box_get_active_text(combobox); str = gtk_combo_box_get_active_text(combobox);
if (!strcmp(str,COMMA)) { strcpy(delimiter, ","); } if (!strcmp(str,COMMA))
else if (!strcmp(str,TAB)) { strcpy(delimiter, "\t"); } strncpy(delimiter, ",", max_buf);
else if (!strcmp(str,SPACE)) { strcpy(delimiter, " "); } else if (!strcmp(str,TAB))
else if (!strcmp(str,COLON)) { strcpy(delimiter, ":"); } strncpy(delimiter, "\t", max_buf);
else if (!strcmp(str,SEMICOLON)) { strcpy(delimiter, ";"); } else if (!strcmp(str,SPACE))
else if (!strcmp(str,PIPE)) { strcpy(delimiter, "|"); } strncpy(delimiter, " ", max_buf);
else { else if (!strcmp(str,COLON))
strncpy(delimiter,str, 10); strncpy(delimiter, ":", max_buf);
delimiter[10]='\0'; else if (!strcmp(str,SEMICOLON))
} strncpy(delimiter, ";", max_buf);
else if (!strcmp(str,PIPE))
strncpy(delimiter, "|", max_buf);
else
strncpy(delimiter, str, max_buf);
if (max_buf)
delimiter[max_buf-1] = '\0';
} }
void void
@@ -86,39 +82,43 @@ gmdb_export_get_lineterm(GladeXML *xml, gchar *lineterm, int max_buf)
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "term_combo")); combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "term_combo"));
str = gtk_combo_box_get_active_text (combobox); str = gtk_combo_box_get_active_text (combobox);
if (!strcmp(str,LF)) { strcpy(lineterm, "\n"); } if (!strcmp(str,LF))
else if (!strcmp(str,CR)) { strcpy(lineterm, "\r"); } strncpy(lineterm, "\n", max_buf);
else if (!strcmp(str,CRLF)) { strcpy(lineterm, "\r\n"); } else if (!strcmp(str,CR))
strncpy(lineterm, "\r", max_buf);
else if (!strcmp(str,CRLF))
strncpy(lineterm, "\r\n", max_buf);
if (max_buf)
lineterm[max_buf-1] = '\0';
} }
int void
gmdb_export_get_quote(GladeXML *xml) gmdb_export_get_quotechar(GladeXML *xml, gchar *quotechar, int max_buf)
{
GtkComboBox *combobox;
int need_quote = 0;
gchar *str;
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "quote_combo"));
str = gtk_combo_box_get_active_text (combobox);
if (!strcmp(str,ALWAYS)) { need_quote = 1; }
else if (!strcmp(str,NEVER)) { need_quote = 0; }
else if (!strcmp(str,AUTOMAT)) { need_quote = -1; }
return need_quote;
}
char
gmdb_export_get_quotechar(GladeXML *xml)
{ {
GtkComboBox *combobox; GtkComboBox *combobox;
gchar *str; gchar *str;
char quotechar;
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "qchar_combo")); combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "qchar_combo"));
str = gtk_combo_box_get_active_text (combobox); str = gtk_combo_box_get_active_text (combobox);
quotechar = str[0]; if (!strcmp(str, NOQUOTE))
quotechar[0] = '\0'; /* Quoting disabled */
else
strncpy(quotechar, str, max_buf);
if (max_buf)
quotechar[max_buf-1] = '\0';
}
return quotechar; void
gmdb_export_get_escapechar(GladeXML *xml, gchar *escapechar, int max_buf)
{
GtkComboBox *combobox;
gchar *str;
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "escchar_combo"));
str = gtk_combo_box_get_active_text (combobox);
strncpy(escapechar, str, max_buf);
if (max_buf)
escapechar[max_buf-1] = '\0';
} }
int int
@@ -130,9 +130,12 @@ gmdb_export_get_binmode(GladeXML *xml)
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "bin_combo")); combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "bin_combo"));
str = gtk_combo_box_get_active_text (combobox); str = gtk_combo_box_get_active_text (combobox);
if (!strcmp(str,BIN_STRIP)) return 1; if (!strcmp(str,BIN_STRIP))
else if (!strcmp(str,BIN_OCTAL)) return 2; return MDB_BINEXPORT_STRIP;
else return 0; else if (!strcmp(str,BIN_OCTAL))
return MDB_BINEXPORT_OCTAL;
else
return MDB_BINEXPORT_RAW;
} }
@@ -167,31 +170,83 @@ gmdb_export_help_cb(GtkWidget *w, gpointer data)
g_error_free (error); g_error_free (error);
} }
} }
/* That function is a duplicate of the one in util/mdb-export.c
* They should be merged and moved in libmdb (backend.c)
*/
#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)
//#define DONT_ESCAPE_ESCAPE
void
gmdb_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)
{
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;
if (quote_len && !strncmp(col_val, quote_char, quote_len)) {
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)) {
fprintf(outfile, "%s%s", escape_char, escape_char);
col_val += orig_escape_len;
#endif
} else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == MDB_BINEXPORT_OCTAL)
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
else
putc(*col_val++, outfile);
}
fputs(quote_char, outfile);
} else
fputs(col_val, outfile);
}
void void
gmdb_table_export_button_cb(GtkWidget *w, gpointer data) gmdb_table_export_button_cb(GtkWidget *w, gpointer data)
{ {
gchar *file_path; gchar *file_path;
FILE *outfile; FILE *outfile;
gchar *bound_data[256]; char **bound_values;
int *bound_lens;
MdbTableDef *table; MdbTableDef *table;
MdbColumn *col; MdbColumn *col;
int i; int i;
int need_headers = 0; int need_headers = 0;
int need_quote = 0;
gchar delimiter[11]; gchar delimiter[11];
gchar quotechar; gchar quotechar[5];
gchar escape_char[5];
gchar lineterm[5]; gchar lineterm[5];
int binmode = 1; int bin_mode = MDB_BINEXPORT_RAW;
int rows=0; int rows=0;
char *value;
size_t length;
GtkWidget *exportwin, *dlg; GtkWidget *exportwin, *dlg;
gmdb_export_get_delimiter(exportwin_xml, delimiter, 10); gmdb_export_get_delimiter(exportwin_xml, delimiter, sizeof(delimiter));
gmdb_export_get_lineterm(exportwin_xml, lineterm, 5); gmdb_export_get_lineterm(exportwin_xml, lineterm, sizeof(lineterm));
need_quote = gmdb_export_get_quote(exportwin_xml); gmdb_export_get_quotechar(exportwin_xml, quotechar, sizeof(quotechar));
quotechar = gmdb_export_get_quotechar(exportwin_xml); gmdb_export_get_escapechar(exportwin_xml, escape_char, sizeof(escape_char));
need_headers = gmdb_export_get_headers(exportwin_xml); need_headers = gmdb_export_get_headers(exportwin_xml);
binmode = gmdb_export_get_binmode(exportwin_xml); bin_mode = gmdb_export_get_binmode(exportwin_xml);
file_path = gmdb_export_get_filepath(exportwin_xml); file_path = gmdb_export_get_filepath(exportwin_xml);
// printf("file path %s\n",file_path); // printf("file path %s\n",file_path);
@@ -209,18 +264,19 @@ int rows=0;
mdb_read_columns(table); mdb_read_columns(table);
mdb_rewind_table(table); mdb_rewind_table(table);
bound_values = (char **) g_malloc(table->num_cols * sizeof(char *));
bound_lens = (int *) g_malloc(table->num_cols * sizeof(int));
for (i=0;i<table->num_cols;i++) { for (i=0;i<table->num_cols;i++) {
/* bind columns */ /* bind columns */
bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE); bound_values[i] = (char *) g_malloc0(MDB_BIND_SIZE);
mdb_bind_column(table, i+1, bound_data[i], NULL); mdb_bind_column(table, i+1, bound_values[i], &bound_lens[i]);
/* display column titles */ /* display column titles */
col=g_ptr_array_index(table->columns,i); if (need_headers) {
if (need_headers) { if (i>0)
if (i>0) fputs(delimiter, outfile); fputs(delimiter, outfile);
gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); col=g_ptr_array_index(table->columns,i);
fputs(col->name, outfile); gmdb_print_col(outfile, col->name, quotechar[0]!='\0', MDB_TEXT, 0, quotechar, escape_char, bin_mode);
gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name);
} }
} }
if (need_headers) fputs(lineterm, outfile); if (need_headers) fputs(lineterm, outfile);
@@ -228,10 +284,21 @@ int rows=0;
/* fetch those rows! */ /* fetch those rows! */
while(mdb_fetch_row(table)) { while(mdb_fetch_row(table)) {
for (i=0;i<table->num_cols;i++) { for (i=0;i<table->num_cols;i++) {
if (i>0) fputs(delimiter, outfile); if (i>0)
gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); fputs(delimiter, outfile);
fputs(bound_data[i], outfile); col=g_ptr_array_index(table->columns,i);
gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); /* Don't quote NULLs */
if (bound_lens[i]) {
if (col->col_type == MDB_OLE) {
value = mdb_ole_read_full(mdb, col, &length);
} else {
value = bound_values[i];
length = bound_lens[i];
}
gmdb_print_col(outfile, value, quotechar[0]!='\0', col->col_type, length, quotechar, escape_char, bin_mode);
if (col->col_type == MDB_OLE)
free(value);
}
} }
fputs(lineterm, outfile); fputs(lineterm, outfile);
rows++; rows++;
@@ -239,8 +306,10 @@ int rows=0;
/* free the memory used to bind */ /* free the memory used to bind */
for (i=0;i<table->num_cols;i++) { for (i=0;i<table->num_cols;i++) {
g_free(bound_data[i]); g_free(bound_values[i]);
} }
g_free(bound_values);
g_free(bound_lens);
fclose(outfile); fclose(outfile);
exportwin = glade_xml_get_widget (exportwin_xml, "export_dialog"); exportwin = glade_xml_get_widget (exportwin_xml, "export_dialog");
@@ -282,16 +351,15 @@ gmdb_table_export_populate_dialog(GladeXML *xml)
gtk_combo_box_append_text(combobox, PIPE); gtk_combo_box_append_text(combobox, PIPE);
gtk_combo_box_set_active(combobox, 0); gtk_combo_box_set_active(combobox, 0);
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "quote_combo"));
gtk_combo_box_append_text(combobox, ALWAYS);
gtk_combo_box_append_text(combobox, NEVER);
gtk_combo_box_append_text(combobox, AUTOMAT);
gtk_combo_box_set_active(combobox, 0);
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "qchar_combo")); combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "qchar_combo"));
gtk_combo_box_append_text(combobox, "\""); gtk_combo_box_append_text(combobox, "\"");
gtk_combo_box_append_text(combobox, "'"); gtk_combo_box_append_text(combobox, "'");
gtk_combo_box_append_text(combobox, "`"); gtk_combo_box_append_text(combobox, "`");
gtk_combo_box_append_text(combobox, NOQUOTE);
gtk_combo_box_set_active(combobox, 0);
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "escchar_combo"));
gtk_combo_box_append_text(combobox, "\\");
gtk_combo_box_set_active(combobox, 0); gtk_combo_box_set_active(combobox, 0);
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "bin_combo")); combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "bin_combo"));

View File

@@ -28,15 +28,13 @@
#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_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) #define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID)
#define BIN_MODE_STRIP 0
#define BIN_MODE_RAW 1
#define BIN_MODE_OCTAL 2
static char *escapes(char *s); static char *escapes(char *s);
//#define DONT_ESCAPE_ESCAPE //#define DONT_ESCAPE_ESCAPE
static void 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) 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 quote_len = strlen(quote_char); /* multibyte */
@@ -50,7 +48,7 @@ print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_l
fputs(quote_char, outfile); fputs(quote_char, outfile);
while (1) { while (1) {
if (is_binary_type(col_type)) { if (is_binary_type(col_type)) {
if (bin_mode == BIN_MODE_STRIP) if (bin_mode == MDB_BINEXPORT_STRIP)
break; break;
if (!bin_len--) if (!bin_len--)
break; break;
@@ -66,7 +64,7 @@ print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_l
fprintf(outfile, "%s%s", escape_char, escape_char); fprintf(outfile, "%s%s", escape_char, escape_char);
col_val += orig_escape_len; col_val += orig_escape_len;
#endif #endif
} else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == BIN_MODE_OCTAL) } else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == MDB_BINEXPORT_OCTAL)
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
else else
putc(*col_val++, outfile); putc(*col_val++, outfile);
@@ -78,12 +76,13 @@ print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_l
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
unsigned int j; unsigned int i;
MdbHandle *mdb; MdbHandle *mdb;
MdbTableDef *table; MdbTableDef *table;
MdbColumn *col; MdbColumn *col;
char **bound_values; char **bound_values;
int *bound_lens; int *bound_lens;
FILE *outfile = stdout;
char *delimiter = NULL; char *delimiter = NULL;
char *row_delimiter = NULL; char *row_delimiter = NULL;
char *quote_char = NULL; char *quote_char = NULL;
@@ -92,7 +91,7 @@ main(int argc, char **argv)
char quote_text = 1; char quote_text = 1;
char *insert_dialect = NULL; char *insert_dialect = NULL;
char *namespace = NULL; char *namespace = NULL;
int bin_mode = BIN_MODE_RAW; int bin_mode = MDB_BINEXPORT_RAW;
int opt; int opt;
char *value; char *value;
size_t length; size_t length;
@@ -129,11 +128,11 @@ main(int argc, char **argv)
break; break;
case 'b': case 'b':
if (!strcmp(optarg, "strip")) if (!strcmp(optarg, "strip"))
bin_mode = BIN_MODE_STRIP; bin_mode = MDB_BINEXPORT_STRIP;
else if (!strcmp(optarg, "raw")) else if (!strcmp(optarg, "raw"))
bin_mode = BIN_MODE_RAW; bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(optarg, "octal")) else if (!strcmp(optarg, "octal"))
bin_mode = BIN_MODE_OCTAL; bin_mode = MDB_BINEXPORT_OCTAL;
else { else {
fprintf(stderr, "Invalid binary mode\n"); fprintf(stderr, "Invalid binary mode\n");
exit(1); exit(1);
@@ -203,23 +202,25 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
/* read table */
mdb_read_columns(table); mdb_read_columns(table);
mdb_rewind_table(table); mdb_rewind_table(table);
bound_values = (char **) g_malloc(table->num_cols * sizeof(char *)); bound_values = (char **) g_malloc(table->num_cols * sizeof(char *));
bound_lens = (int *) g_malloc(table->num_cols * sizeof(int)); bound_lens = (int *) g_malloc(table->num_cols * sizeof(int));
for (j=0;j<table->num_cols;j++) { for (i=0;i<table->num_cols;i++) {
bound_values[j] = (char *) g_malloc0(MDB_BIND_SIZE); /* bind columns */
mdb_bind_column(table, j+1, bound_values[j], &bound_lens[j]); bound_values[i] = (char *) g_malloc0(MDB_BIND_SIZE);
mdb_bind_column(table, i+1, bound_values[i], &bound_lens[i]);
} }
if (header_row) { if (header_row) {
for (j=0; j<table->num_cols; j++) { for (i=0; i<table->num_cols; i++) {
col=g_ptr_array_index(table->columns,j); col=g_ptr_array_index(table->columns,i);
if (j) if (i)
fputs(delimiter, stdout); fputs(delimiter, outfile);
fputs(col->name, stdout); fputs(col->name, outfile);
} }
fputs("\n", stdout); fputs(row_delimiter, outfile);
} }
while(mdb_fetch_row(table)) { while(mdb_fetch_row(table)) {
@@ -227,42 +228,45 @@ main(int argc, char **argv)
if (insert_dialect) { if (insert_dialect) {
char *quoted_name; char *quoted_name;
quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[optind + 1]); quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[optind + 1]);
fprintf(stdout, "INSERT INTO %s (", quoted_name); fprintf(outfile, "INSERT INTO %s (", quoted_name);
free(quoted_name); free(quoted_name);
for (j=0;j<table->num_cols;j++) { for (i=0;i<table->num_cols;i++) {
if (j>0) fputs(", ", stdout); if (i>0) fputs(", ", outfile);
col=g_ptr_array_index(table->columns,j); col=g_ptr_array_index(table->columns,i);
quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name); quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name);
fputs(quoted_name, stdout); fputs(quoted_name, outfile);
free(quoted_name); free(quoted_name);
} }
fputs(") VALUES (", stdout); fputs(") VALUES (", outfile);
} }
for (j=0;j<table->num_cols;j++) { for (i=0;i<table->num_cols;i++) {
if (j>0) if (i>0)
fputs(delimiter, stdout); fputs(delimiter, outfile);
col=g_ptr_array_index(table->columns,j); col=g_ptr_array_index(table->columns,i);
if (!bound_lens[j]) { if (!bound_lens[i]) {
/* Don't quote NULLs */
if (insert_dialect) if (insert_dialect)
fputs("NULL", stdout); fputs("NULL", outfile);
} else { } else {
if (col->col_type == MDB_OLE) { if (col->col_type == MDB_OLE) {
value = mdb_ole_read_full(mdb, col, &length); value = mdb_ole_read_full(mdb, col, &length);
} else { } else {
value = bound_values[j]; value = bound_values[i];
length = bound_lens[j]; length = bound_lens[i];
} }
print_col(stdout, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode); print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
if (col->col_type == MDB_OLE) if (col->col_type == MDB_OLE)
free(value); free(value);
} }
} }
if (insert_dialect) fputs(");", stdout); if (insert_dialect) fputs(");", outfile);
fputs(row_delimiter, stdout); fputs(row_delimiter, outfile);
} }
for (j=0;j<table->num_cols;j++) {
g_free(bound_values[j]); /* free the memory used to bind */
for (i=0;i<table->num_cols;i++) {
g_free(bound_values[i]);
} }
g_free(bound_values); g_free(bound_values);
g_free(bound_lens); g_free(bound_lens);